001 /* ContentModel.java --
002 Copyright (C) 2005 Free Software Foundation, Inc.
003
004 This file is part of GNU Classpath.
005
006 GNU Classpath is free software; you can redistribute it and/or modify
007 it under the terms of the GNU General Public License as published by
008 the Free Software Foundation; either version 2, or (at your option)
009 any later version.
010
011 GNU Classpath is distributed in the hope that it will be useful, but
012 WITHOUT ANY WARRANTY; without even the implied warranty of
013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 General Public License for more details.
015
016 You should have received a copy of the GNU General Public License
017 along with GNU Classpath; see the file COPYING. If not, write to the
018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019 02110-1301 USA.
020
021 Linking this library statically or dynamically with other modules is
022 making a combined work based on this library. Thus, the terms and
023 conditions of the GNU General Public License cover the whole
024 combination.
025
026 As a special exception, the copyright holders of this library give you
027 permission to link this library with independent modules to produce an
028 executable, regardless of the license terms of these independent
029 modules, and to copy and distribute the resulting executable under
030 terms of your choice, provided that you also meet, for each linked
031 independent module, the terms and conditions of the license of that
032 module. An independent module is a module which is not derived from
033 or based on this library. If you modify this library, you may extend
034 this exception to your version of the library, but you are not
035 obligated to do so. If you do not wish to do so, delete this
036 exception statement from your version. */
037
038
039 package javax.swing.text.html.parser;
040
041 import gnu.javax.swing.text.html.parser.models.transformer;
042
043 import java.io.Serializable;
044
045 import java.util.Vector;
046
047 /**
048 * A representation of the element content. The instances of this class
049 * can be arranged into the linked list, representing a BNF expression.
050 * The content model is constructed as a branched tree structure in the
051 * following way:
052 * <pre>
053 * a = new ContentModel('+', A, null); // a reprensents A+
054 * b = new ContentModel('&', B, a); // b represents B & A+
055 * c = new ContentModel('*', b, null); // c represents ( B & A+) *
056 * d = new ContentModel('|', new ContentModel('*', A, null),
057 * new ContentModel('?', B, null)); // d represents ( A* | B? )
058 * </pre>
059 * where the valid operations are:
060 * <ul>
061 * <li><code>E* </code> E occurs zero or more times</li>
062 * <li><code>E+ </code> E occurs one or more times</li>
063 * <li><code>E? </code> E occurs once or not atl all</li>
064 * <li><code>A,B</code> A occurs before B</li>
065 * <li><code>A|B</code> both A and B are permitted in any order.
066 * The '|' alone does not permit the repetetive occurence of A or B
067 * (use <code>(A|B)*</code>.</li>
068 * <li><code>A&B</code> both A and B must occur once (in any order)</li>
069 * </ul>
070 * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
071 */
072 public final class ContentModel
073 implements Serializable
074 {
075 /** Use serialVersionUID for interoperability. */
076 private static final long serialVersionUID = -1130825523866321257L;
077
078 /**
079 * The next content model model ( = pointer to the next element of
080 * the linked list) for the binary expression (',','&' or '|'). Null
081 * for the last element in the list.
082 */
083 public ContentModel next;
084
085 /**
086 * The document content, containing either Element or the enclosed
087 * content model (that would be in the parentheses in BNF expression).
088 */
089 public Object content;
090
091 /**
092 * Specifies the BNF operation between this node and the node,
093 * stored in the field <code>next</code> (or for this node, if it is
094 * an unary operation.
095 */
096 public int type;
097
098 /**
099 * Create a content model initializing all fields to default values.
100 */
101 public ContentModel()
102 {
103 // Nothing to do here.
104 }
105
106 /**
107 * Create a content model, consisting of the single element.
108 * Examples:
109 *<code>
110 * a = new ContentModel('+', A, null); // a reprensents A+
111 * b = new ContentModel('&', B, a); // b represents B & A+
112 * c = new ContentModel('*', b, null); // c represents ( B & A+) *
113 * d = new ContentModel('|', A,
114 * new ContentModel('?',b, null);
115 * // d represents
116 * </code>
117 */
118 public ContentModel(Element a_content)
119 {
120 content = a_content;
121 }
122
123 /**
124 * Create a content model, involving expression of the given type.
125 * @param a_type The expression operation type ('*','?' or '+'
126 * @param a_content The content for that the expression is applied.
127 */
128 public ContentModel(int a_type, ContentModel a_content)
129 {
130 content = a_content;
131 type = a_type;
132 }
133
134 /**
135 * Create a content model, involving binary expression of the given type.
136 * @param a_type The expression operation type ( ',', '|' or '&').
137 * @param a_content The content of the left part of the expression.
138 * @param a_next The content model, representing the right part of the
139 * expression.
140 */
141 public ContentModel(int a_type, Object a_content, ContentModel a_next)
142 {
143 content = a_content;
144 type = a_type;
145 next = a_next;
146 }
147
148 /**
149 * Adds all list elements to the given vector, ignoring the
150 * operations between the elements. The old vector values are not
151 * discarded.
152 * @param elements - a vector to add the values to.
153 */
154 public void getElements(Vector<Element> elements)
155 {
156 ContentModel c = this;
157
158 while (c != null)
159 {
160 // FIXME: correct?
161 if (c.content instanceof Element)
162 elements.add((Element) c.content);
163 c = c.next;
164 }
165 }
166
167 /**
168 * Checks if the content model matches an empty input stream.
169 * The empty content is created using SGML DTD keyword EMPTY.
170 * The empty model is a model with the content field equal to null.
171 *
172 * @return true if the content field is equal to null.
173 */
174 public boolean empty()
175 {
176 return content == null;
177 }
178
179 /**
180 * Get the element, stored in the <code>next.content</code>.
181 * The method is programmed as the part of the standard API, but not
182 * used in this implementation.
183 * @return the value of the field <code>next</code>.
184 */
185 public Element first()
186 {
187 return (Element) next.content;
188 }
189
190 /**
191 * Checks if this object can potentially be the first token in the
192 * ContenModel list. The method is programmed as the part of the
193 * standard API, but not used in this implementation.
194 */
195 public boolean first(Object token)
196 {
197 ContentModel c = this;
198 while (c.next != null)
199 {
200 if (c.content != null && c.content.toString().equals(token.toString()) &&
201 c.type != ','
202 )
203
204 // Agree if the operation with the preceeding element
205 // is not the comma operation.
206 return true;
207 c = c.next;
208 }
209 return false;
210 }
211
212 /**
213 * Returns a string representation (an expression) of this content model.
214 * The expression has BNF-like syntax, except the absence of the
215 * unary operator is additionally indicated by " ' ". It is
216 * advisable to check the created models for correctness using this
217 * method.
218 */
219 public String toString()
220 {
221 return transformer.transform(this).toString();
222 }
223 }