libjava/
[official-gcc.git] / libjava / classpath / gnu / javax / swing / text / html / parser / models / node.java
blob54469b349959513347b8005f1557bf27f6f7ff61
1 /* node.java --
2 Copyright (C) 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu.javax.swing.text.html.parser.models;
41 import java.io.Serializable;
43 /**
44 * Part of the internal representation of the content model.
45 * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
47 public class node
48 implements Serializable
50 private static final long serialVersionUID = 1;
52 /**
53 * The token to match (can be instance of list).
55 public Object token;
57 /**
58 * True for the node that cannot be visited again.
60 public boolean _closed;
62 /**
63 * The binary operation for this node.
65 public char binary;
67 /**
68 * The unary opeation for this node.
70 public char unary;
72 /**
73 * The number of times the node already was visited.
75 public int visits;
77 /**
78 * The previous node in content model (used for closing nodes).
80 public node previous;
82 /**
83 * Creates a new node.
84 * @param binary_operator The operator, connecting all nodes in the list.
85 * The nodes, connected by the different operators, must be arranged into
86 * the different lists.
87 * @param unary_operator The unary operator for this node or zero if
88 * no such was specified.
89 * @param token The token to match. This can be either a string or
90 * the new instance of the list.
91 * @param a_previous The previous node in the list, null for the first
92 * node. This is used for propagating the closing operation for the
93 * comma delimited list.
95 public node(char binary_operator, char unary_operator, Object a_token)
97 if (a_token != null)
98 if (a_token.getClass().equals(node.class))
99 throw new Error("Creating node in node is redundant and ineffective.");
101 binary = binary_operator;
102 unary = unary_operator;
103 token = a_token;
107 * Checks if this node is in the closed state.
108 * @return True if the node is closed.
110 public boolean isClosed()
112 return _closed;
116 * Check if closing this node means closing the previous node.
118 public boolean closePrevious()
120 return binary == ',';
124 * Return the token object if it could match as a next token in
125 * a token list of null if it could not.
126 * @return
128 public Object findFreeNode()
130 boolean ok;
131 if (isClosed() || silenceAllowed())
132 return null;
134 // Try if the node would stay valid after a one more visit.
135 visits++;
136 ok = valid();
137 visits--;
139 if (ok)
141 if (token instanceof node)
142 return ((node) token).findFreeNode();
143 else
144 return token;
146 else
147 return null;
151 * Check if the current situation is such that the node must be closed
152 * now.
154 public boolean mustClose()
156 switch (unary)
158 case 0 :
159 return true;
161 case '*' :
162 return false;
164 case '+' :
165 return false;
167 case '?' :
168 return visits <= 1;
170 default :
171 throw new Error("Invalid unary operation " + unary + " ( '" +
172 (char) unary + "' )"
178 * Do the match operation with the given token. This sets various
179 * flags.
180 * @param a_token The token to match.
181 * @return true if the the token matches node, false if it does not match
182 * or if the node is closed.
184 public boolean performMatch(Object a_token)
186 if (isClosed())
187 return false;
189 boolean matches = compare(a_token);
190 if (matches)
191 matches();
193 return matches;
197 * Prepares the node for matching against a new list of tokens.
199 public void reset()
201 _closed = false;
202 visits = 0;
206 * Check if the provided token can match this node.
207 * In the case of match, the node state changes, moving
208 * current position after the matched token. However if this method
209 * returns a suggested new token to insert before the provided one,
210 * the state of the list does not change.
211 * @return Boolean.TRUE if the match is found,
212 * Boolean.FALSE if the match is not possible and no token can be
213 * inserted to make the match valid. Otherwise, returns the
214 * token object that can be inserted before the last token in the
215 * list, probably (not for sure) making the match valid.
217 public Object show(Object x)
219 if (compare(x))
220 return performMatch(x) ? Boolean.TRUE : Boolean.FALSE;
222 Object recommended = findFreeNode();
223 return recommended != null ? recommended : Boolean.FALSE;
227 * Check if it would be a valid case if this node is visited zero times.
228 * Nodes with unary operator * or ? need not be matched to make a
229 * model valid.
231 public boolean silenceAllowed()
233 return unary == '?' || unary == '*';
237 * Returns a string representation of the list.
238 * @return String representation, similar to BNF expression.
240 public String toString()
242 StringBuffer b = new StringBuffer();
244 b.append(token);
245 if (unary != 0)
246 b.append((char) unary);
247 else
248 b.append('\'');
250 return b.toString();
254 * Check if the node state is valid.
256 public boolean valid()
258 switch (unary)
260 case 0 :
261 if (binary == '|')
262 return true;
263 else
264 return visits == 1;
266 case '*' :
267 return true;
269 case '+' :
270 return visits > 0;
272 case '?' :
273 return visits <= 1;
275 default :
276 throw new Error("Invalid unary operation " + unary + " ( '" +
277 (char) unary + "' )"
282 public boolean validPreliminary()
284 return visits == 0 || valid();
288 * Closes this node and, if closePrevious() returs true, calls close() for
289 * the previous node.
291 protected void close()
293 _closed = true;
294 if (previous != null && closePrevious())
295 previous.close();
299 * Compare the provided token object with the token object of this node.
301 protected boolean compare(Object a_token)
303 if (token instanceof Object[])
304 throw new Error("Invalid token object, probably the 'list' " +
305 "should be used. "
308 if (token instanceof node[])
309 throw new Error("Do not use 'node' for the array of nodes, use 'list'. ");
311 if (token instanceof node)
313 return ((node) token).performMatch(a_token);
316 boolean rt = false;
318 if (token == a_token)
319 rt = true;
320 if (token.equals(a_token))
321 rt = true;
322 if (token.toString().equalsIgnoreCase(a_token.toString()))
323 rt = true;
325 return rt;
329 * Fire the changes that must happen then the token matches this node.
331 protected void matches()
333 visits++;
334 if (mustClose())
335 close();