1 /* ContentModel.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)
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 javax
.swing
.text
.html
.parser
;
41 import gnu
.javax
.swing
.text
.html
.parser
.models
.transformer
;
43 import java
.io
.Serializable
;
45 import java
.util
.Vector
;
48 * A representation of the element content. The instances of this class
49 * can be arranged into the linked list, representing a BNF expression.
50 * The content model is constructed as a branched tree structure in the
53 * a = new ContentModel('+', A, null); // a reprensents A+
54 * b = new ContentModel('&', B, a); // b represents B & A+
55 * c = new ContentModel('*', b, null); // c represents ( B & A+) *
56 * d = new ContentModel('|', new ContentModel('*', A, null),
57 * new ContentModel('?', B, null)); // d represents ( A* | B? )
59 * where the valid operations are:
61 * <li><code>E* </code> E occurs zero or more times</li>
62 * <li><code>E+ </code> E occurs one or more times</li>
63 * <li><code>E? </code> E occurs once or not atl all</li>
64 * <li><code>A,B</code> A occurs before B</li>
65 * <li><code>A|B</code> both A and B are permitted in any order.
66 * The '|' alone does not permit the repetetive occurence of A or B
67 * (use <code>(A|B)*</code>.</li>
68 * <li><code>A&B</code> both A and B must occur once (in any order)</li>
70 * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
72 public final class ContentModel
73 implements Serializable
75 /** Use serialVersionUID for interoperability. */
76 private static final long serialVersionUID
= -1130825523866321257L;
79 * The next content model model ( = pointer to the next element of
80 * the linked list) for the binary expression (',','&' or '|'). Null
81 * for the last element in the list.
83 public ContentModel next
;
86 * The document content, containing either Element or the enclosed
87 * content model (that would be in the parentheses in BNF expression).
89 public Object content
;
92 * Specifies the BNF operation between this node and the node,
93 * stored in the field <code>next</code> (or for this node, if it is
99 * Create a content model initializing all fields to default values.
101 public ContentModel()
103 // Nothing to do here.
107 * Create a content model, consisting of the single element.
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);
118 public ContentModel(Element a_content
)
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.
128 public ContentModel(int a_type
, ContentModel a_content
)
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
141 public ContentModel(int a_type
, Object a_content
, ContentModel a_next
)
149 * Adds all list elements to the given vector, ignoring the
150 * operations between the elements. The old vector values are not
152 * @param elements - a vector to add the values to.
154 public void getElements(Vector elements
)
156 ContentModel c
= this;
160 elements
.add(c
.content
);
166 * Checks if the content model matches an empty input stream.
167 * The empty content is created using SGML DTD keyword EMPTY.
168 * The empty model is a model with the content field equal to null.
170 * @return true if the content field is equal to null.
172 public boolean empty()
174 return content
== null;
178 * Get the element, stored in the <code>next.content</code>.
179 * The method is programmed as the part of the standard API, but not
180 * used in this implementation.
181 * @return the value of the field <code>next</code>.
183 public Element
first()
185 return (Element
) next
.content
;
189 * Checks if this object can potentially be the first token in the
190 * ContenModel list. The method is programmed as the part of the
191 * standard API, but not used in this implementation.
193 public boolean first(Object token
)
195 ContentModel c
= this;
196 while (c
.next
!= null)
198 if (c
.content
!= null && c
.content
.toString().equals(token
.toString()) &&
202 // Agree if the operation with the preceeding element
203 // is not the comma operation.
211 * Returns a string representation (an expression) of this content model.
212 * The expression has BNF-like syntax, except the absence of the
213 * unary operator is additionally indicated by " ' ". It is
214 * advisable to check the created models for correctness using this
217 public String
toString()
219 return transformer
.transform(this).toString();