1:
37:
38: package ;
39:
40: import ;
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48:
49: import ;
50: import ;
51: import ;
52:
53: import ;
54: import ;
55: import ;
56:
57:
63: public class PersistenceParser extends DefaultHandler implements Context
64: {
65:
67: private ExceptionListener exceptionListener;
68:
69:
73: private int skipElement;
74:
75:
78: private HashMap handlerCreators = new HashMap();
79:
80:
83: private ElementHandler currentHandler;
84:
85:
88: JavaHandler javaHandler;
89:
90:
91: private List objects = new LinkedList();
92:
93:
94: private XMLDecoder decoder;
95:
96:
105: public PersistenceParser(
106: InputStream inputStream,
107: ExceptionListener exceptionListener,
108: ClassLoader cl,
109: XMLDecoder decoder)
110: {
111:
112: this.exceptionListener = exceptionListener;
113: this.decoder = decoder;
114:
115: DummyHandler dummyHandler = new DummyHandler();
116: currentHandler = dummyHandler;
117: javaHandler = new JavaHandler(dummyHandler, this, cl);
118:
119: SAXParserFactory factory = SAXParserFactory.newInstance();
120:
121: SAXParser parser;
122: try
123: {
124: parser = factory.newSAXParser();
125: }
126: catch (ParserConfigurationException pce)
127: {
128:
129:
130: throw (InternalError) new InternalError(
131: "No SAX Parser available.").initCause(
132: pce);
133: }
134: catch (SAXException saxe)
135: {
136:
137:
138: throw (InternalError) new InternalError(
139: "No SAX Parser available.").initCause(
140: saxe);
141: }
142:
143:
144:
145: handlerCreators.put("java", new JavaHandlerCreator());
146:
147:
148: handlerCreators.put("object", new ObjectHandlerCreator());
149: handlerCreators.put("void", new VoidHandlerCreator());
150:
151: handlerCreators.put("array", new ArrayHandlerCreator());
152:
153:
154: handlerCreators.put("class", new ClassHandlerCreator());
155: handlerCreators.put("null", new NullHandlerCreator());
156:
157: handlerCreators.put("char", new CharHandlerCreator());
158: handlerCreators.put("string", new StringHandlerCreator());
159: handlerCreators.put("boolean", new BooleanHandlerCreator());
160: handlerCreators.put("byte", new ByteHandlerCreator());
161: handlerCreators.put("short", new ShortHandlerCreator());
162: handlerCreators.put("int", new IntHandlerCreator());
163: handlerCreators.put("long", new LongHandlerCreator());
164: handlerCreators.put("float", new FloatHandlerCreator());
165: handlerCreators.put("double", new DoubleHandlerCreator());
166:
167:
168: try
169: {
170: parser.parse(inputStream, this);
171: }
172: catch (SAXException saxe)
173: {
174: exceptionListener.exceptionThrown(
175: new IllegalArgumentException("XML data not well-formed."));
176: }
177: catch (IOException ioe)
178: {
179: exceptionListener.exceptionThrown(ioe);
180: }
181: }
182:
183: public void startElement(
184: String uri,
185: String localName,
186: String qName,
187: Attributes attributes)
188: throws SAXException
189: {
190:
194: if (currentHandler.hasFailed() || skipElement > 0)
195: {
196: exceptionListener.exceptionThrown(
197: new IllegalArgumentException(
198: "Element unusable due to previous error: " + qName));
199:
200: skipElement++;
201:
202: return;
203: }
204:
205:
207: if (!currentHandler.isSubelementAllowed(qName))
208: {
209: exceptionListener.exceptionThrown(
210: new IllegalArgumentException(
211: "Element is not allowed here: " + qName));
212:
213: skipElement++;
214:
215: return;
216: }
217:
218:
221: if (!handlerCreators.containsKey(qName))
222: {
223: exceptionListener.exceptionThrown(
224: new IllegalArgumentException(
225: "Element unusable because tag is unknown: " + qName));
226:
227: skipElement++;
228:
229: return;
230: }
231:
232:
233: AbstractElementHandler handler =
234: ((Creator) handlerCreators.get(qName)).createHandler(
235: currentHandler);
236:
237:
238: currentHandler = handler;
239:
240:
241: currentHandler.start(attributes, exceptionListener);
242: }
243:
244: public void endElement(String uri, String localName, String qName)
245: throws SAXException
246: {
247:
248:
249: if (skipElement > 0)
250: {
251: skipElement--;
252: return;
253: }
254:
255:
256: currentHandler.end(exceptionListener);
257:
258:
259: currentHandler = currentHandler.getParent();
260: }
261:
262:
264: public void characters(char[] ch, int start, int length)
265: throws SAXException
266: {
267:
268: if (skipElement > 0)
269: return;
270:
271: currentHandler.characters(ch, start, length);
272: }
273:
274:
279: interface Creator
280: {
281:
286: AbstractElementHandler createHandler(ElementHandler parent);
287: }
288:
289: class BooleanHandlerCreator implements Creator
290: {
291: public AbstractElementHandler createHandler(ElementHandler parent)
292: {
293: return new BooleanHandler(parent);
294: }
295: }
296:
297: class ByteHandlerCreator implements Creator
298: {
299: public AbstractElementHandler createHandler(ElementHandler parent)
300: {
301: return new ByteHandler(parent);
302: }
303: }
304:
305: class ShortHandlerCreator implements Creator
306: {
307: public AbstractElementHandler createHandler(ElementHandler parent)
308: {
309: return new ShortHandler(parent);
310: }
311: }
312:
313: class IntHandlerCreator implements Creator
314: {
315: public AbstractElementHandler createHandler(ElementHandler parent)
316: {
317: return new IntHandler(parent);
318: }
319: }
320:
321: class LongHandlerCreator implements Creator
322: {
323: public AbstractElementHandler createHandler(ElementHandler parent)
324: {
325: return new LongHandler(parent);
326: }
327: }
328:
329: class FloatHandlerCreator implements Creator
330: {
331: public AbstractElementHandler createHandler(ElementHandler parent)
332: {
333: return new FloatHandler(parent);
334: }
335: }
336:
337: class DoubleHandlerCreator implements Creator
338: {
339: public AbstractElementHandler createHandler(ElementHandler parent)
340: {
341: return new DoubleHandler(parent);
342: }
343: }
344:
345: class CharHandlerCreator implements Creator
346: {
347: public AbstractElementHandler createHandler(ElementHandler parent)
348: {
349: return new CharHandler(parent);
350: }
351: }
352:
353: class StringHandlerCreator implements Creator
354: {
355: public AbstractElementHandler createHandler(ElementHandler parent)
356: {
357: return new StringHandler(parent);
358: }
359: }
360:
361: class JavaHandlerCreator implements Creator
362: {
363: public AbstractElementHandler createHandler(ElementHandler parent)
364: {
365: return javaHandler;
366: }
367: }
368:
369: class ObjectHandlerCreator implements Creator
370: {
371: public AbstractElementHandler createHandler(ElementHandler parent)
372: {
373: return new ObjectHandler(parent);
374: }
375: }
376:
377: class VoidHandlerCreator implements Creator
378: {
379: public AbstractElementHandler createHandler(ElementHandler parent)
380: {
381: return new VoidHandler(parent);
382: }
383: }
384:
385: class ClassHandlerCreator implements Creator
386: {
387: public AbstractElementHandler createHandler(ElementHandler parent)
388: {
389: return new ClassHandler(parent);
390: }
391: }
392:
393: class NullHandlerCreator implements Creator
394: {
395: public AbstractElementHandler createHandler(ElementHandler parent)
396: {
397: return new NullHandler(parent);
398: }
399: }
400:
401: class ArrayHandlerCreator implements Creator
402: {
403: public AbstractElementHandler createHandler(ElementHandler parent)
404: {
405: return new ArrayHandler(parent);
406: }
407: }
408:
409:
410: public void addParameterObject(Object o) throws AssemblyException
411: {
412: objects.add(o);
413: }
414:
415: public void notifyStatement(Context outerContext) throws AssemblyException
416: {
417:
418:
419: }
420:
421: public Object endContext(Context outerContext) throws AssemblyException
422: {
423: return null;
424: }
425:
426: public boolean subContextFailed()
427: {
428:
429: return false;
430: }
431:
432: public void set(int index, Object o) throws AssemblyException
433: {
434:
435: throw new AssemblyException(
436: new IllegalArgumentException("Set method is not allowed in decoder context."));
437: }
438:
439: public Object get(int index) throws AssemblyException
440: {
441:
442: throw new AssemblyException(
443: new IllegalArgumentException("Get method is not allowed in decoder context."));
444: }
445:
446: public Object getResult()
447: {
448:
449:
450: return decoder;
451: }
452:
453: public void setId(String id)
454: {
455: exceptionListener.exceptionThrown(new IllegalArgumentException("id attribute is not allowed for <java> tag."));
456: }
457:
458: public String getId()
459: {
460:
461: return null;
462: }
463:
464: public boolean isStatement()
465: {
466:
467:
468: return true;
469: }
470:
471: public void setStatement(boolean b)
472: {
473:
474: }
475:
476:
480: public Iterator iterator()
481: {
482: return objects.iterator();
483: }
484:
485: }