001 /*
002 * Copyright 1998-2004 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package javax.swing.text.html.parser;
027
028 import java.util.Vector;
029 import java.util.Enumeration;
030 import java.io.*;
031
032 /**
033 * A representation of a content model. A content model is
034 * basically a restricted BNF expression. It is restricted in
035 * the sense that it must be deterministic. This means that you
036 * don't have to represent it as a finite state automata.<p>
037 * See Annex H on page 556 of the SGML handbook for more information.
038 *
039 * @author Arthur van Hoff
040 * @version 1.18,05/05/07
041 *
042 */
043 public final class ContentModel implements Serializable {
044 /**
045 * Type. Either '*', '?', '+', ',', '|', '&'.
046 */
047 public int type;
048
049 /**
050 * The content. Either an Element or a ContentModel.
051 */
052 public Object content;
053
054 /**
055 * The next content model (in a ',', '|' or '&' expression).
056 */
057 public ContentModel next;
058
059 public ContentModel() {
060 }
061
062 /**
063 * Create a content model for an element.
064 */
065 public ContentModel(Element content) {
066 this (0, content, null);
067 }
068
069 /**
070 * Create a content model of a particular type.
071 */
072 public ContentModel(int type, ContentModel content) {
073 this (type, content, null);
074 }
075
076 /**
077 * Create a content model of a particular type.
078 */
079 public ContentModel(int type, Object content, ContentModel next) {
080 this .type = type;
081 this .content = content;
082 this .next = next;
083 }
084
085 /**
086 * Return true if the content model could
087 * match an empty input stream.
088 */
089 public boolean empty() {
090 switch (type) {
091 case '*':
092 case '?':
093 return true;
094
095 case '+':
096 case '|':
097 for (ContentModel m = (ContentModel) content; m != null; m = m.next) {
098 if (m.empty()) {
099 return true;
100 }
101 }
102 return false;
103
104 case ',':
105 case '&':
106 for (ContentModel m = (ContentModel) content; m != null; m = m.next) {
107 if (!m.empty()) {
108 return false;
109 }
110 }
111 return true;
112
113 default:
114 return false;
115 }
116 }
117
118 /**
119 * Update elemVec with the list of elements that are
120 * part of the this contentModel.
121 */
122 public void getElements(Vector<Element> elemVec) {
123 switch (type) {
124 case '*':
125 case '?':
126 case '+':
127 ((ContentModel) content).getElements(elemVec);
128 break;
129 case ',':
130 case '|':
131 case '&':
132 for (ContentModel m = (ContentModel) content; m != null; m = m.next) {
133 m.getElements(elemVec);
134 }
135 break;
136 default:
137 elemVec.addElement((Element) content);
138 }
139 }
140
141 private boolean valSet[];
142 private boolean val[];
143
144 // A cache used by first(). This cache was found to speed parsing
145 // by about 10% (based on measurements of the 4-12 code base after
146 // buffering was fixed).
147
148 /**
149 * Return true if the token could potentially be the
150 * first token in the input stream.
151 */
152 public boolean first(Object token) {
153 switch (type) {
154 case '*':
155 case '?':
156 case '+':
157 return ((ContentModel) content).first(token);
158
159 case ',':
160 for (ContentModel m = (ContentModel) content; m != null; m = m.next) {
161 if (m.first(token)) {
162 return true;
163 }
164 if (!m.empty()) {
165 return false;
166 }
167 }
168 return false;
169
170 case '|':
171 case '&': {
172 Element e = (Element) token;
173 if (valSet == null) {
174 valSet = new boolean[Element.maxIndex + 1];
175 val = new boolean[Element.maxIndex + 1];
176 // All Element instances are created before this ever executes
177 }
178 if (valSet[e.index]) {
179 return val[e.index];
180 }
181 for (ContentModel m = (ContentModel) content; m != null; m = m.next) {
182 if (m.first(token)) {
183 val[e.index] = true;
184 break;
185 }
186 }
187 valSet[e.index] = true;
188 return val[e.index];
189 }
190
191 default:
192 return (content == token);
193 // PENDING: refer to comment in ContentModelState
194 /*
195 if (content == token) {
196 return true;
197 }
198 Element e = (Element)content;
199 if (e.omitStart() && e.content != null) {
200 return e.content.first(token);
201 }
202 return false;
203 */
204 }
205 }
206
207 /**
208 * Return the element that must be next.
209 */
210 public Element first() {
211 switch (type) {
212 case '&':
213 case '|':
214 case '*':
215 case '?':
216 return null;
217
218 case '+':
219 case ',':
220 return ((ContentModel) content).first();
221
222 default:
223 return (Element) content;
224 }
225 }
226
227 /**
228 * Convert to a string.
229 */
230 public String toString() {
231 switch (type) {
232 case '*':
233 return content + "*";
234 case '?':
235 return content + "?";
236 case '+':
237 return content + "+";
238
239 case ',':
240 case '|':
241 case '&':
242 char data[] = { ' ', (char) type, ' ' };
243 String str = "";
244 for (ContentModel m = (ContentModel) content; m != null; m = m.next) {
245 str = str + m;
246 if (m.next != null) {
247 str += new String(data);
248 }
249 }
250 return "(" + str + ")";
251
252 default:
253 return content.toString();
254 }
255 }
256 }
|