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)
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
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
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
;
44 * Part of the internal representation of the content model.
45 * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
48 implements Serializable
50 private static final long serialVersionUID
= 1;
53 * The token to match (can be instance of list).
58 * True for the node that cannot be visited again.
60 public boolean _closed
;
63 * The binary operation for this node.
68 * The unary opeation for this node.
73 * The number of times the node already was visited.
78 * The previous node in content model (used for closing nodes).
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
)
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
;
107 * Checks if this node is in the closed state.
108 * @return True if the node is closed.
110 public boolean isClosed()
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.
128 public Object
findFreeNode()
131 if (isClosed() || silenceAllowed())
134 // Try if the node would stay valid after a one more visit.
141 if (token
instanceof node
)
142 return ((node
) token
).findFreeNode();
151 * Check if the current situation is such that the node must be closed
154 public boolean mustClose()
171 throw new Error("Invalid unary operation " + unary
+ " ( '" +
178 * Do the match operation with the given token. This sets various
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
)
189 boolean matches
= compare(a_token
);
197 * Prepares the node for matching against a new list of tokens.
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
)
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
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();
246 b
.append((char) unary
);
254 * Check if the node state is valid.
256 public boolean valid()
276 throw new Error("Invalid unary operation " + unary
+ " ( '" +
282 public boolean validPreliminary()
284 return visits
== 0 || valid();
288 * Closes this node and, if closePrevious() returs true, calls close() for
291 protected void close()
294 if (previous
!= null && closePrevious())
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' " +
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
);
318 if (token
== a_token
)
320 if (token
.equals(a_token
))
322 if (token
.toString().equalsIgnoreCase(a_token
.toString()))
329 * Fire the changes that must happen then the token matches this node.
331 protected void matches()