KickJava   Java API By Example, From Geeks To Geeks.

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


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

4 package org.oddjob.designer;
5
6 import java.io.ByteArrayInputStream JavaDoc;
7 import java.io.ByteArrayOutputStream JavaDoc;
8
9 import org.oddjob.arooa.ArooaConstants;
10 import org.oddjob.arooa.ArooaContext;
11 import org.oddjob.arooa.ArooaHandler;
12 import org.oddjob.arooa.handlers.MainHandler;
13 import org.oddjob.arooa.handlers.XmlHandler;
14 import org.oddjob.arooa.xml.XMLDefinitionHelper;
15 import org.oddjob.designer.arooa.DesignElementHandler;
16 import org.oddjob.designer.arooa.DesignParser;
17 import org.oddjob.designer.arooa.DesignValueHandler;
18 import org.oddjob.designer.model.DesignAdult;
19 import org.oddjob.designer.model.DesignElement;
20 import org.xml.sax.InputSource JavaDoc;
21 import org.xml.sax.SAXParseException JavaDoc;
22
23 public class DesignerHelper {
24
25     public static class Surrogate extends DesignAdult {
26         DesignElement getCreated() {
27             return children(0);
28         }
29     }
30     
31     public static DesignElement createDeForXML(String JavaDoc xml) {
32         
33         Surrogate surrogate = new Surrogate();
34         
35         ArooaContext context = new ArooaContext();
36         
37         ArooaHandler elementHandler = new DesignElementHandler();
38         
39         context.set(ArooaConstants.ELEMENT_HANDLER, elementHandler);
40         context.set(ArooaConstants.PROPERTY_HANDLER, new DesignValueHandler());
41         context.set(ArooaConstants.CURRENTLY_CONFIGURING, surrogate);
42         
43         XMLDefinitionHelper ph = new XMLDefinitionHelper(context);
44         
45         ph.parse(new InputSource JavaDoc(new ByteArrayInputStream JavaDoc(xml.getBytes())),
46                 new MainHandler(elementHandler));
47
48         return surrogate.getCreated();
49     }
50     
51     public static String JavaDoc xmlForDE(DesignElement de)
52     throws SAXParseException JavaDoc {
53         ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
54         ArooaHandler xmlh = new XmlHandler(out);
55         
56         ArooaContext context = new ArooaContext();
57
58         DesignParser dp = new DesignParser(context);
59         dp.parse("files", de, new MainHandler(xmlh));
60         
61         return out.toString();
62     }
63 }
64
Popular Tags