001: /*
002: * $Header: /cvsroot/webman-cms/source/webman/com/teamkonzept/publishing/markups/TKDefinedMarkupParser.java,v 1.7 2001/11/26 15:14:26 sebastian Exp $
003: *
004: */
005: package com.teamkonzept.publishing.markups;
006:
007: import com.teamkonzept.lib.*;
008:
009: public class TKDefinedMarkupParser extends TKMarkupParser {
010:
011: // Bitte nicht ˆffentlich machen !
012: protected TKMarkupDefinition currentDefinition;
013: protected TKMarkupParamCall currentParamDefinition;
014: protected String closeMarkup;
015:
016: public TKDefinedMarkupParser(TKVector diagnostics) {
017:
018: super (diagnostics, new TKMarkupTree());
019:
020: this .currentDefinition = null;
021: this .currentParamDefinition = null;
022: this .closeMarkup = null;
023: }
024:
025: public void initMarkupParsing() {
026:
027: currentDefinition = null;
028: currentParamDefinition = null;
029: }
030:
031: public int parseValue(String text, int pos, StringBuffer value)
032: throws TKMarkupParserException {
033:
034: if (currentParamDefinition == null)
035: throw new TKMarkupParserException(
036: "Aktueller Parameter nicht definiert", pos);
037:
038: return currentParamDefinition.param.typeClass.parseValue(text,
039: pos, value);
040: }
041:
042: public int forceValue(String text, int pos, StringBuffer value)
043: throws TKMarkupParserException {
044:
045: if (currentParamDefinition == null)
046: throw new TKMarkupParserException(
047: "Aktueller Parameter nicht definiert", pos);
048:
049: int oldPos = pos;
050:
051: pos = parseValue(text, pos, value);
052: if (pos == oldPos)
053: throw new TKMarkupParserException("Syntax: "
054: + currentParamDefinition.param.typeClass.name
055: + " erwartet", pos);
056:
057: return pos;
058: }
059:
060: public void cleanup(TKMarkupItemList itemList, int pos) {
061:
062: if (closeLevel(itemList)) {
063:
064: addDiagnostic((pos < 0 ? "" : "Position " + pos + ": ")
065: + "Syntax-Fehler in /" + compoundName()
066: + ": Markupanfang nicht gefunden");
067:
068: } else
069: super .cleanup(itemList, pos);
070:
071: cleanupCloseLevel();
072: }
073:
074: public TKMarkupParam createParam(String name, String value) {
075:
076: try {
077: if (currentDefinition == null)
078: throw new Exception("Aktuelles Markup f¸r Parameter "
079: + name + " nicht definiert");
080:
081: TKMarkupParamCall definition = currentDefinition
082: .lookupParam(name);
083:
084: if (definition == null)
085: throw new Exception("Parameter " + name
086: + " nicht definiert");
087:
088: if (value == null)
089: value = definition.param.typeClass.typeDefault();
090: return new TKDefinedMarkupParam(name, value, definition);
091:
092: } catch (Exception ex) {
093:
094: addDiagnostic(TKMarkupParserException.demandMsg(ex));
095: return super .createParam(name, value);
096: }
097: }
098:
099: public TKMarkup createMarkup(String name, TKHashtable params,
100: int pos) {
101:
102: try {
103: return new TKDefinedMarkup(name, params, pos);
104: } catch (Exception ex) {
105:
106: addDiagnostic(TKMarkupParserException.demandMsg(ex));
107: return super .createMarkup(name, params, pos);
108: }
109: }
110:
111: public TKMarkup createMarkup(String name, TKHashtable params,
112: int pos, boolean backsl) {
113:
114: try {
115: return new TKDefinedMarkup(name, params, pos, backsl);
116: } catch (Exception ex) {
117:
118: addDiagnostic(TKMarkupParserException.demandMsg(ex));
119: return super .createMarkup(name, params, pos);
120: }
121: }
122:
123: public void registerMarkup(String markupName) {
124:
125: super .registerMarkup(markupName);
126:
127: currentDefinition = TKMarkupDefinition.lookup(markupName);
128: currentParamDefinition = null;
129: }
130:
131: public void registerParam(String paramName) {
132:
133: super .registerParam(paramName);
134:
135: try {
136: if (currentDefinition == null)
137: throw new Exception("Aktuelles Markup f¸r Parameter "
138: + paramName + " nicht definiert");
139:
140: currentParamDefinition = currentDefinition
141: .lookupParam(paramName);
142:
143: if (currentParamDefinition == null)
144: throw new Exception("Parameter " + paramName
145: + " nicht definiert");
146:
147: } catch (Exception ex) {
148:
149: addDiagnostic(TKMarkupParserException.demandMsg(ex));
150: }
151: }
152:
153: public void registerCompound(String markupName) {
154:
155: registerMarkup(markupName);
156:
157: if ((currentDefinition != null) && currentDefinition.isAtom)
158: addDiagnostic("Attribut-Fehler in " + markupName
159: + ": ist terminal");
160:
161: closeMarkup = markupName;
162: }
163:
164: public String compoundName() {
165:
166: return closeMarkup == null ? "" : closeMarkup;
167: }
168:
169: public boolean closeLevel(TKMarkupItemList itemList) {
170:
171: checkCompound(itemList);
172: return super .closeLevel(itemList) || (closeMarkup != null);
173: }
174:
175: public void cleanupCloseLevel() {
176:
177: super .cleanupCloseLevel();
178: closeMarkup = null;
179: }
180:
181: public void checkCompound(TKMarkupItemList itemList) {
182:
183: if (closeMarkup == null)
184: return;
185:
186: TKMarkup markup = null;
187: int i;
188:
189: for (i = itemList.size() - 1; i >= 0; i--) {
190:
191: Object item = itemList.get(i);
192: if (item instanceof TKDefinedMarkup)
193: continue;
194: else if (item instanceof TKMarkup) {
195:
196: markup = (TKMarkup) item;
197: if (closeMarkup.equalsIgnoreCase(markup.name))
198: break;
199: }
200: }
201:
202: if (i < 0)
203: return;
204:
205: TKMarkupTree subTree = null;
206:
207: if (i < itemList.size() - 1) {
208:
209: subTree = new TKMarkupTree();
210:
211: for (int j = i + 1; j < itemList.size(); j++)
212: subTree.addElement(itemList.get(j));
213: }
214:
215: itemList.removeRange(i, itemList.size());
216: itemList.addElement(new TKMarkupNode(markup, subTree));
217: closeMarkup = null;
218: }
219:
220: public int tieMarkup(String text, int pos, TKMarkup markup,
221: TKMarkupItemList itemList) {
222:
223: if (!(markup instanceof TKDefinedMarkup)) {
224:
225: itemList.addElement(markup);
226:
227: if ((closeMarkup != null)
228: && closeMarkup.equalsIgnoreCase(markup.name))
229: closeMarkup = null;
230: else
231: checkCompound(itemList);
232: return pos;
233: }
234:
235: TKDefinedMarkup definedMarkup = (TKDefinedMarkup) markup;
236:
237: if (definedMarkup.isAtom) {
238:
239: itemList.addElement(definedMarkup);
240: return pos;
241: }
242:
243: TKMarkupTree subTree = new TKMarkupTree();
244:
245: // rekursiver Abstieg
246: pos = parse(text, pos, subTree);
247:
248: if (itemList instanceof TKMarkupTree)
249: itemList
250: .addElement(new TKMarkupNode(definedMarkup, subTree));
251: else
252: itemList.concat(subTree);
253:
254: if ((closeMarkup != null)
255: && closeMarkup.equalsIgnoreCase(markup.name))
256: closeMarkup = null;
257: else
258: checkCompound(itemList);
259:
260: return pos;
261: }
262:
263: public int parseAliens(String text, int pos)
264: throws TKMarkupParserException {
265:
266: StringBuffer name = new StringBuffer();
267:
268: int startPos = pos;
269: pos = parsePattern(text, pos, null, "/");
270:
271: if (pos > startPos) {
272:
273: try {
274: pos = forceIdentifier(text, pos, name);
275: pos = parseWhitespace(text, pos, null);
276: pos = forcePattern(text, pos, null, ">");
277:
278: String markupName = new String(name).toUpperCase();
279:
280: registerCompound(markupName);
281:
282: return pos;
283:
284: } catch (TKMarkupParserException ex) {
285:
286: ex.throwAgain(pos);
287:
288: } catch (Exception ex) {
289:
290: TKMarkupParserException.throwAgain(ex, pos);
291: }
292: }
293:
294: return pos;
295: }
296: //{{DECLARE_CONTROLS
297: //}}
298: }
|