001: /* Generated By:JavaCC: Do not edit this line. DTDInitialParser.java */
002: package org.exolab.castor.xml.dtd.parser;
003:
004: import java.util.Hashtable;
005:
006: import org.exolab.castor.xml.dtd.DTDException;
007:
008: /**
009: * Initial XML DTD parser. Searches for <b>parameter entity</b> declarations
010: * and substitutes <b>parameter entity references</b> by
011: * corresponding <b>replacement text</b>.
012: * @author <a href="mailto:totok@intalio.com">Alexander Totok</a>
013: * @version $Revision: 5962 $ $Date: 2006-04-14 04:14:43 -0600 (Fri, 14 Apr 2006) $
014: */
015: public class DTDInitialParser implements DTDInitialParserConstants {
016:
017: /**
018: * Main method that starts parsing process.
019: * @return result of the parsing process - document with parameter entity
020: * references expaned.
021: */
022: final public String Input() throws ParseException, DTDException {
023: Hashtable parameterEntities = new Hashtable();
024: String result = "";
025: Token token;
026: String peReference;
027: String key;
028: String value;
029: label_1: while (true) {
030: switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
031: case TEXT_UNIT:
032: case BEGIN_PE_DECL:
033: case PE_REFERENCE_D:
034: break;
035: default:
036: jj_la1[0] = jj_gen;
037: break label_1;
038: }
039: switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
040: case TEXT_UNIT:
041: token = jj_consume_token(TEXT_UNIT);
042: result += token.image;
043: break;
044: case PE_REFERENCE_D:
045: token = jj_consume_token(PE_REFERENCE_D);
046: peReference = token.image;
047: key = peReference
048: .substring(1, peReference.length() - 1);
049: value = (String) parameterEntities.get(key);
050: if (value == null) {
051: String err = "Initial Parser: parameter entity named \""
052: + key;
053: err += "\" has not been defined, while is tried to be referenced...";
054: {
055: if (true)
056: throw new DTDException(err);
057: }
058: } else {
059: result += value;
060: }
061: break;
062: case BEGIN_PE_DECL:
063: PEdecl(parameterEntities);
064: break;
065: default:
066: jj_la1[1] = jj_gen;
067: jj_consume_token(-1);
068: throw new ParseException();
069: }
070: }
071: jj_consume_token(0);
072: {
073: if (true)
074: return result;
075: }
076: throw new Error("Missing return statement in function");
077: }
078:
079: /**
080: * Parsing procedure corresponding to the <b>Parameter Entity Declaration</b>
081: * nonterminal.
082: */
083: final public void PEdecl(Hashtable parameterEntities)
084: throws ParseException, DTDException {
085: Token token;
086: String name;
087: String replacementText;
088: jj_consume_token(BEGIN_PE_DECL);
089: token = jj_consume_token(NAME_OF_PE);
090: name = token.image;
091: if (parameterEntities.containsKey(name)) {
092: String err = "Initial Parser: parameter entity named \""
093: + name;
094: err += "\" has been already defined, while is tried ";
095: err += "to be defined again...";
096: {
097: if (true)
098: throw new DTDException(err);
099: }
100: }
101: jj_consume_token(SPACE);
102: replacementText = EntityValue(parameterEntities);
103: parameterEntities.put(name, replacementText);
104: switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
105: case SPACE:
106: jj_consume_token(SPACE);
107: break;
108: default:
109: jj_la1[2] = jj_gen;
110: }
111: jj_consume_token(END_PE_DECL);
112: }
113:
114: /**
115: * Parsing procedure corresponding to the <b>Entity Value</b> nonterminal.
116: */
117: final public String EntityValue(Hashtable parameterEntities)
118: throws ParseException, DTDException {
119: Token token;
120: String peReference;
121: String key;
122: String value;
123: String replacementText = "";
124: switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
125: case START_DQ:
126: jj_consume_token(START_DQ);
127: label_2: while (true) {
128: switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
129: case ENTITY_VALUE_TEXT_DQ:
130: case PE_REFERENCE_DQ:
131: break;
132: default:
133: jj_la1[3] = jj_gen;
134: break label_2;
135: }
136: switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
137: case ENTITY_VALUE_TEXT_DQ:
138: token = jj_consume_token(ENTITY_VALUE_TEXT_DQ);
139: replacementText += token.image;
140: break;
141: case PE_REFERENCE_DQ:
142: token = jj_consume_token(PE_REFERENCE_DQ);
143: peReference = token.image;
144: key = peReference.substring(1,
145: peReference.length() - 1);
146: value = (String) parameterEntities.get(key);
147: if (value == null) {
148: String err = "Initial Parser: parameter entity named \""
149: + key;
150: err += "\" has not been defined, while is tried to be referenced...";
151: {
152: if (true)
153: throw new DTDException(err);
154: }
155: } else {
156: replacementText += value;
157: }
158: break;
159: default:
160: jj_la1[4] = jj_gen;
161: jj_consume_token(-1);
162: throw new ParseException();
163: }
164: }
165: jj_consume_token(END_DQ);
166: break;
167: case START_SQ:
168: jj_consume_token(START_SQ);
169: label_3: while (true) {
170: switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
171: case ENTITY_VALUE_TEXT_SQ:
172: case PE_REFERENCE_SQ:
173: break;
174: default:
175: jj_la1[5] = jj_gen;
176: break label_3;
177: }
178: switch ((jj_ntk == -1) ? jj_ntk() : jj_ntk) {
179: case ENTITY_VALUE_TEXT_SQ:
180: token = jj_consume_token(ENTITY_VALUE_TEXT_SQ);
181: replacementText += token.image;
182: break;
183: case PE_REFERENCE_SQ:
184: token = jj_consume_token(PE_REFERENCE_SQ);
185: peReference = token.image;
186: key = peReference.substring(1,
187: peReference.length() - 1);
188: value = (String) parameterEntities.get(key);
189: if (value == null) {
190: String err = "Initial Parser: parameter entity named \""
191: + key;
192: err += "\" has not been defined, while is tried to be referenced...";
193: {
194: if (true)
195: throw new DTDException(err);
196: }
197: } else {
198: replacementText += value;
199: }
200: break;
201: default:
202: jj_la1[6] = jj_gen;
203: jj_consume_token(-1);
204: throw new ParseException();
205: }
206: }
207: jj_consume_token(END_SQ);
208: break;
209: default:
210: jj_la1[7] = jj_gen;
211: jj_consume_token(-1);
212: throw new ParseException();
213: }
214: {
215: if (true)
216: return replacementText;
217: }
218: throw new Error("Missing return statement in function");
219: }
220:
221: public DTDInitialParserTokenManager token_source;
222: public Token token, jj_nt;
223: private int jj_ntk;
224: private int jj_gen;
225: final private int[] jj_la1 = new int[8];
226: final private int[] jj_la1_0 = { 0x700, 0x700, 0x1000, 0x30000,
227: 0x30000, 0x180000, 0x180000, 0x6000, };
228:
229: public DTDInitialParser(CharStream stream) {
230: token_source = new DTDInitialParserTokenManager(stream);
231: token = new Token();
232: jj_ntk = -1;
233: jj_gen = 0;
234: for (int i = 0; i < 8; i++)
235: jj_la1[i] = -1;
236: }
237:
238: public void ReInit(CharStream stream) {
239: token_source.ReInit(stream);
240: token = new Token();
241: jj_ntk = -1;
242: jj_gen = 0;
243: for (int i = 0; i < 8; i++)
244: jj_la1[i] = -1;
245: }
246:
247: public DTDInitialParser(DTDInitialParserTokenManager tm) {
248: token_source = tm;
249: token = new Token();
250: jj_ntk = -1;
251: jj_gen = 0;
252: for (int i = 0; i < 8; i++)
253: jj_la1[i] = -1;
254: }
255:
256: public void ReInit(DTDInitialParserTokenManager tm) {
257: token_source = tm;
258: token = new Token();
259: jj_ntk = -1;
260: jj_gen = 0;
261: for (int i = 0; i < 8; i++)
262: jj_la1[i] = -1;
263: }
264:
265: final private Token jj_consume_token(int kind)
266: throws ParseException {
267: Token oldToken;
268: if ((oldToken = token).next != null)
269: token = token.next;
270: else
271: token = token.next = token_source.getNextToken();
272: jj_ntk = -1;
273: if (token.kind == kind) {
274: jj_gen++;
275: return token;
276: }
277: token = oldToken;
278: jj_kind = kind;
279: throw generateParseException();
280: }
281:
282: final public Token getNextToken() {
283: if (token.next != null)
284: token = token.next;
285: else
286: token = token.next = token_source.getNextToken();
287: jj_ntk = -1;
288: jj_gen++;
289: return token;
290: }
291:
292: final public Token getToken(int index) {
293: Token t = token;
294: for (int i = 0; i < index; i++) {
295: if (t.next != null)
296: t = t.next;
297: else
298: t = t.next = token_source.getNextToken();
299: }
300: return t;
301: }
302:
303: final private int jj_ntk() {
304: if ((jj_nt = token.next) == null) {
305: return (jj_ntk = (token.next = token_source.getNextToken()).kind);
306: }
307: return (jj_ntk = jj_nt.kind);
308: }
309:
310: private java.util.Vector jj_expentries = new java.util.Vector();
311: private int[] jj_expentry;
312: private int jj_kind = -1;
313:
314: final public ParseException generateParseException() {
315: jj_expentries.removeAllElements();
316: boolean[] la1tokens = new boolean[22];
317: for (int i = 0; i < 22; i++) {
318: la1tokens[i] = false;
319: }
320: if (jj_kind >= 0) {
321: la1tokens[jj_kind] = true;
322: jj_kind = -1;
323: }
324: for (int i = 0; i < 8; i++) {
325: if (jj_la1[i] == jj_gen) {
326: for (int j = 0; j < 32; j++) {
327: if ((jj_la1_0[i] & (1 << j)) != 0) {
328: la1tokens[j] = true;
329: }
330: }
331: }
332: }
333: for (int i = 0; i < 22; i++) {
334: if (la1tokens[i]) {
335: jj_expentry = new int[1];
336: jj_expentry[0] = i;
337: jj_expentries.addElement(jj_expentry);
338: }
339: }
340: int[][] exptokseq = new int[jj_expentries.size()][];
341: for (int i = 0; i < jj_expentries.size(); i++) {
342: exptokseq[i] = (int[]) jj_expentries.elementAt(i);
343: }
344: return new ParseException(token, exptokseq, tokenImage);
345: }
346:
347: final public void enable_tracing() {
348: }
349:
350: final public void disable_tracing() {
351: }
352:
353: }
|