KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > designer > DesignerXMLParser


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

4 package org.oddjob.designer;
5
6 import java.io.ByteArrayInputStream JavaDoc;
7
8 import org.apache.log4j.Logger;
9 import org.oddjob.arooa.ArooaException;
10 import org.oddjob.arooa.ArooaContext;
11 import org.oddjob.arooa.ArooaConstants;
12 import org.oddjob.arooa.handlers.MainHandler;
13 import org.oddjob.arooa.handlers.MappedPropertyHandler;
14 import org.oddjob.arooa.xml.XMLDefinitionHelper;
15 import org.oddjob.designer.arooa.DesignComponentHandler;
16 import org.oddjob.designer.arooa.DesignElementHandler;
17 import org.oddjob.designer.arooa.DesignValueHandler;
18 import org.oddjob.designer.factory.DesignFactory;
19 import org.oddjob.designer.model.DesignComponent;
20 import org.xml.sax.InputSource JavaDoc;
21
22 /**
23  * Utility class providing various methods for parsing
24  * Designs and DesignXML.
25  */

26 public class DesignerXMLParser {
27     private static final Logger logger = Logger.getLogger(DesignerXMLParser.class);
28     
29     public static class RootComponent {
30         DesignComponent component;
31         public void addComponent(DesignComponent component) {
32             this.component = component;
33         }
34     }
35     
36     /**
37      * Create a DesignComponent from XML.
38      *
39      * @param xml The xml.
40      * @return A DesignComponent.
41      *
42      * @throws ArooaException If parsing the XML fails.
43      */

44     public static DesignComponent buildComponent(String JavaDoc xml)
45     throws ArooaException {
46         
47         ArooaContext context = new ArooaContext();
48         context.set(ArooaConstants.COMPONENT_FACTORY, DesignFactory.componentFactory());
49         context.set(ArooaConstants.COMPONENT_HANDLER, new DesignComponentHandler());
50         context.set(ArooaConstants.PROPERTY_HANDLER, new DesignValueHandler());
51         context.set(ArooaConstants.MAPPED_HANDLER, new MappedPropertyHandler());
52         context.set(ArooaConstants.ELEMENT_HANDLER, new DesignElementHandler());
53         
54         RootComponent rootComponent = new RootComponent();
55         context.set(ArooaConstants.CURRENTLY_CONFIGURING, rootComponent);
56                 
57         XMLDefinitionHelper ph = new XMLDefinitionHelper(context);
58         
59         ByteArrayInputStream JavaDoc in = new ByteArrayInputStream JavaDoc(xml.getBytes());
60         InputSource JavaDoc inputSource = new InputSource JavaDoc(in);
61         ph.parse(inputSource, new MainHandler(new DesignComponentHandler()));
62         
63         return rootComponent.component;
64     }
65     
66 }
67
Popular Tags