1 4 package org.oddjob.designer; 5 6 import java.io.ByteArrayInputStream ; 7 import java.io.ByteArrayOutputStream ; 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 ; 21 import org.xml.sax.SAXParseException ; 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 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 (new ByteArrayInputStream (xml.getBytes())), 46 new MainHandler(elementHandler)); 47 48 return surrogate.getCreated(); 49 } 50 51 public static String xmlForDE(DesignElement de) 52 throws SAXParseException { 53 ByteArrayOutputStream out = new ByteArrayOutputStream (); 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 |