KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > designer > arooa > DesignParser


1 /*
2  * (c) Rob Gordon 2005.
3  */

4 package org.oddjob.designer.arooa;
5
6 import java.io.ByteArrayInputStream JavaDoc;
7
8 import org.oddjob.arooa.ArooaHandler;
9 import org.oddjob.arooa.ArooaContext;
10 import org.oddjob.arooa.xml.XMLDefinitionHelper;
11 import org.xml.sax.Attributes JavaDoc;
12 import org.xml.sax.InputSource JavaDoc;
13 import org.xml.sax.SAXParseException JavaDoc;
14
15 /**
16  * Parses a design into a sequence of events which are
17  * passed to a handler for further processing.
18  *
19  */

20 public class DesignParser {
21     
22     private final ArooaContext context;
23     
24     /**
25      * Constructor.
26      *
27      * @param context The context which will be passed to
28      * the handler.
29      */

30     public DesignParser(ArooaContext context) {
31         this.context = context;
32     }
33     
34     /**
35      * Parse a design.
36      *
37      * @param tag The top level tag, e.g. oddjob.
38      * @param object The top level element.
39      * @param handler The handler to use.
40      *
41      * @throws SAXParseException Could be thrown by the handler.
42      */

43     public void parse(String JavaDoc tag,
44             Object JavaDoc object, ArooaHandler handler)
45     throws SAXParseException JavaDoc {
46         parse(tag, object, handler, context);
47     }
48
49     void parse(String JavaDoc tag,
50             Object JavaDoc object, ArooaHandler handler, ArooaContext context)
51     throws SAXParseException JavaDoc {
52         DesignIH dih = DesignIH.getHelper(object.getClass());
53         String JavaDoc unknown = dih.unknown(object);
54         // if the object provides the design as XML, parse the XML.
55
if (unknown != null) {
56             XMLDefinitionHelper xdef = new XMLDefinitionHelper(new ArooaContext());
57             InputSource JavaDoc in = new InputSource JavaDoc(new ByteArrayInputStream JavaDoc(unknown.getBytes()));
58             xdef.parse(in, handler);
59         }
60         else {
61             Attributes JavaDoc attrs = dih.attributes(object);
62             ArooaHandler nextHandler = handler.onStartChild("",
63                     tag, tag, attrs, context);
64             context = new ArooaContext(context);
65             nextHandler.onStartElement("", tag, tag, attrs, context);
66             String JavaDoc text = dih.text(object);
67             if (text != null) {
68                 nextHandler.characters(text.toCharArray(), 0, text.length(), context);
69             }
70     
71             ElementWrapper[] elements = dih.elements(object);
72             for (int i = 0 ; i < elements.length; ++i ) {
73                 parse(elements[i].getName(), elements[i].getElement(), nextHandler, context);
74             }
75             nextHandler.onEndElement("", tag, context);
76             context = context.getParent();
77             handler.onEndChild("", tag, tag, context);
78         }
79     }
80     
81 }
82
Popular Tags