Lets create an xml example from swixml language.
public class SwiXmlParser extends X4OParser { public static final String LANGUAGE = "swixml"; public static final String VERSION_2_NS_URI = "http://swixml.x4o.org/xml/ns/swixml-lang"; public static final String SWING_ENGINE_EL_NAME = "swingEngine"; /** * Protected constructor for language support */ protected SwiXmlParser() { super(LANGUAGE); } public SwiXmlParser(SwingEngine engine) { // Create language by version super(LANGUAGE); // Check engine if (engine==null) { throw new NullPointerException("Can't parse with null SwingEngine."); } // Add engine for callback addELBeanInstance(SWING_ENGINE_EL_NAME,engine); // Set empty ns for v2 documents setProperty(X4OLanguagePropertyKeys.INPUT_EMPTY_NAMESPACE_URI, VERSION_2_NS_URI); } public Component getRootComponent() { return (Component)getDriver().getElementLanguage().getRootElement().getElementObject(); } static public SwingEngine getSwingEngine(ElementLanguage elementLanguage) { ValueExpression ee = elementLanguage.getExpressionFactory().createValueExpression(elementLanguage.getELContext(),"${"+SwiXmlParser.SWING_ENGINE_EL_NAME+"}",Object.class); SwingEngine se = (SwingEngine)ee.getValue(elementLanguage.getELContext()); return se; } }
public class SwiXmlParserSupport2 implements X4OParserSupport { public ElementLanguage loadElementLanguageSupport() throws X4OParserSupportException { SwiXmlParser parser = new SwiXmlParser(); return parser.loadElementLanguageSupport(); } }
First load the language in META-INF/swixml/swixml-modules.xml
<?xml version="1.0" encoding="UTF-8"?> <modules version="1.0" xmlns="http://language.x4o.org/xml/ns/modules" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://language.x4o.org/xml/ns/modules http://language.x4o.org/xml/ns/modules-1.0.xsd" > <language version="1.0"> <eld-resource>swixml-lang.eld</eld-resource> </language> </modules>
<?xml version="1.0" encoding="UTF-8"?> <root:module xmlns:root="http://eld.x4o.org/xml/ns/eld-root" xmlns="http://eld.x4o.org/xml/ns/eld-lang" xmlns:conv="http://eld.x4o.org/xml/ns/eld-conv" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://eld.x4o.org/xml/ns/eld-root http://eld.x4o.org/xml/ns/eld-root-1.0.xsd" providerName="swixml.test.x4o.org" name="Swixml Language" id="swixml-lang" > <bindingHandler id="JComponentBindingHandler" bean.class="org.x4o.xml.test.swixml.bind.JComponentBindingHandler"/> <bindingHandler id="JMenuBarBindingHandler" bean.class="org.x4o.xml.test.swixml.bind.JMenuBarBindingHandler"/> <classBindingHandler id="JDesktopPane-JInternalFrame" parentClass="javax.swing.JDesktopPane" childClass="javax.swing.JInternalFrame" method="add"/> <classBindingHandler id="JFrame-JDesktopPane" parentClass="javax.swing.JFrame" childClass="javax.swing.JDesktopPane" method="setContentPane"/> <classBindingHandler id="JMenuBar-JMenu" parentClass="javax.swing.JMenuBar" childClass="javax.swing.JMenu" method="add"/> <classBindingHandler id="JMenu-JMenuItem" parentClass="javax.swing.JMenu" childClass="javax.swing.JMenuItem" method="add"/> <elementInterface id="Component" interfaceClass="java.awt.Component"> <attribute name="bounds"> <conv:stringSplitConverter classTo="java.awt.Rectangle" split="," splitSize="4" singleToMethod="setRect" useNativeType="true"> <conv:stringSplitConverterStep fromMethod="getX" fromOrder="1" toOrder="1"><conv:doubleConverter/></conv:stringSplitConverterStep> <conv:stringSplitConverterStep fromMethod="getY" fromOrder="2" toOrder="2"><conv:doubleConverter/></conv:stringSplitConverterStep> <conv:stringSplitConverterStep fromMethod="getWidth" fromOrder="3" toOrder="3"><conv:doubleConverter/></conv:stringSplitConverterStep> <conv:stringSplitConverterStep fromMethod="getHeight" fromOrder="4" toOrder="4"><conv:doubleConverter/></conv:stringSplitConverterStep> </conv:stringSplitConverter> </attribute> <attribute name="size"> <conv:stringSplitConverter classTo="java.awt.Dimension" split="," splitSize="2" singleToMethod="setSize" useNativeType="true"> <conv:stringSplitConverterStep fromMethod="getHeight" fromOrder="1" toOrder="1"><conv:integerConverter/></conv:stringSplitConverterStep> <conv:stringSplitConverterStep fromMethod="getWidth" fromOrder="2" toOrder="2"><conv:integerConverter/></conv:stringSplitConverterStep> </conv:stringSplitConverter> </attribute> <attribute name="icon"> <conv:beanConverter bean.class="org.x4o.xml.test.swixml.conv.IconConverter"/> </attribute> <attribute name="background"> <conv:beanConverter bean.class="org.x4o.xml.test.swixml.conv.ColorConverter"/> </attribute> <attribute name="location"> <conv:stringSplitConverter classTo="java.awt.Point" split="," splitSize="2" singleToMethod="setLocation" useNativeType="true"> <conv:stringSplitConverterStep fromMethod="getX" fromOrder="1" toOrder="1"><conv:integerConverter/></conv:stringSplitConverterStep> <conv:stringSplitConverterStep fromMethod="getY" fromOrder="2" toOrder="2"><conv:integerConverter/></conv:stringSplitConverterStep> </conv:stringSplitConverter> </attribute> </elementInterface> <elementInterface id="JComponent" interfaceClass="javax.swing.JComponent"> <attribute name="layout"> <conv:beanConverter bean.class="org.x4o.xml.test.swixml.conv.LayoutConverter"/> </attribute> <attribute name="border"> <conv:beanConverter bean.class="org.x4o.xml.test.swixml.conv.BorderConverter"/> </attribute> </elementInterface> <namespace uri="http://swixml.x4o.org/xml/ns/swixml-root" schemaUri="http://swixml.x4o.org/xml/ns/swixml-root-2.0.xsd" schemaResource="swixml-root-2.0.xsd" schemaPrefix="root" name="Root element" languageRoot="true" > <!-- Single element in language root to create nice tree, for imports in xsd namespace aware generated files. --> <element tag="frame" objectClass="javax.swing.JFrame"/> </namespace> <namespace uri="http://swixml.x4o.org/xml/ns/swixml-lang" schemaUri="http://swixml.x4o.org/xml/ns/swixml-lang-2.0.xsd" schemaResource="swixml-lang-2.0.xsd" schemaPrefix="sx-lang" > <!-- Note frame should not be here(it can but xsd needs root), but else classic xml does not parse without xmlns additions. --> <element tag="frame" objectClass="javax.swing.JFrame"/> <element tag="menubar" objectClass="javax.swing.JMenuBar"/> <element tag="menu" objectClass="javax.swing.JMenu"/> <element tag="menuitem" objectClass="javax.swing.JMenuItem"> <attribute name="accelerator"> <conv:beanConverter bean.class="org.x4o.xml.test.swixml.conv.KeyStrokeConverter"/> <attributeAlias name="Accelerator"/> </attribute> <attribute name="Action" runBeanFill="false"/> <configurator id="menuitem-action" bean.class="org.x4o.xml.test.swixml.SwiXmlActionConfigurator"/> </element> <element tag="separator" /> <element tag="panel" objectClass="javax.swing.JPanel"/> <element tag="splitpane" objectClass="javax.swing.JSplitPane"> <attribute name="orientation"> <conv:beanConverter bean.class="org.x4o.xml.test.swixml.conv.JSplitPaneOrientationConverter"/> </attribute> </element> <element tag="scrollPane" objectClass="javax.swing.JScrollPane"/> <element tag="tree" objectClass="javax.swing.JTree"/> <element tag="button" objectClass="javax.swing.JButton"> <attributeFromBody name="text"/> </element> <element tag="table" objectClass="javax.swing.JTable"/> <element tag="textarea" objectClass="javax.swing.JTextArea"/> <element tag="label" objectClass="javax.swing.JLabel"/> <element tag="textfield" objectClass="javax.swing.JTextField"/> <element tag="desktoppane" objectClass="javax.swing.JDesktopPane"/> <element tag="internalframe" objectClass="javax.swing.JInternalFrame"> <attribute name="title"><attributeAlias name="Title"/></attribute> <attribute name="bounds"><attributeAlias name="Bounds"/></attribute> <attribute name="layout"><attributeAlias name="Layout"/></attribute> <attribute name="visible"><attributeAlias name="Visible"/></attribute> <attribute name="resizable"><attributeAlias name="Resizable"/></attribute> </element> </namespace> </root:module>
public class JMenuBarBindingHandler extends AbstractElementBindingHandler { public Class<?> getBindParentClass() { return JFrame.class; } public Class<?>[] getBindChildClasses() { return new Class[] {JMenuBar.class}; } public void doBind(Object parentObject, Object childObject, Element childElement) throws ElementBindingHandlerException { JMenuBar child = (JMenuBar)childObject; JFrame frame = (JFrame)parentObject; frame.getRootPane().setJMenuBar(child); } }
public class BorderConverter extends AbstractStringObjectConverter { private static final long serialVersionUID = 6729812931433525103L; public Class<?> getObjectClassTo() { return Border.class; } public String convertStringBack(Object obj,Locale locale) throws ObjectConverterException { return ((Border)obj).toString(); } public Object convertStringTo(String str, Locale locale) throws ObjectConverterException { try { if ("LoweredBevelBorder".equals(str)) { return BorderFactory.createLoweredBevelBorder(); } else { return BorderFactory.createEmptyBorder(); } } catch (Exception e) { throw new ObjectConverterException(this,e.getMessage(),e); } } @Override public ObjectConverter clone() throws CloneNotSupportedException { BorderConverter result = new BorderConverter(); result.converters=cloneConverters(); return result; } }
<frame name="mainframe" size="800,600" title="SWIXML-X4O" plaf="com.sun.java.swing.plaf.windows.WindowsLookAndFeel" defaultCloseOperation="3"> <menubar name="menubar"> <menu name="filemenu" text="File"> <menuitem name="mi_new" text="New" icon="icons/new.gif" mnemonic="VK_N" accelerator="control N" Action="newAction"/> <menuitem name="mi_open" text="Open" icon="icons/open.gif" mnemonic="VK_O" Accelerator="control O" ActionCommand="open"/> <menuitem name="mi_save" text="Save" icon="icons/save.gif" mnemonic="VK_S" ActionCommand="save"/> <menu name="propmenu" text="Properties" icon="icons/new.gif" > <menuitem name="mi_prop_edit" text="Edit" icon="icons/new.gif"/> <menuitem name="mi_prop_clear" text="Clear" icon="icons/new.gif"/> </menu> <separator/> <menuitem name="mi_exit" text="Exit" icon="icons/exit.gif" mnemonic="VK_X" Accelerator="control X" ActionCommand="exit" Action="exitAction"/> </menu> <menu text="Help"> <menuitem name="mi_about" text="About" enabled="true" icon="icons/info.gif" Accelerator="alt A" Action="aboutAction" /> </menu> </menubar> <desktoppane> <internalframe Title="Flow Layout (right aligned)" Bounds="10,10,150,150" Layout="FlowLayout(FlowLayout.RIGHT)" Visible="true" Resizable="true"> <button>1</button> <button>2</button> <button>3</button> <button>4</button> </internalframe> <internalframe Title="Grid Layout" Bounds="200,10,170,170" Layout="GridLayout(4,3)" Visible="true" Resizable="true"> <button text="1"/><button text="2"/><button text="3"/> <button text="4"/><button text="5"/><button text="6"/> <button text="7"/><button text="8"/><button text="9"/> <button text="*"/><button text="0"/><button text="#"/> </internalframe> <internalframe Title="Border Layout" Bounds="390,10,150,150" Layout="borderlayout" Visible="true" Resizable="true"> <button constraints="BorderLayout.NORTH" text="1"/> <button constraints="BorderLayout.EAST" text="2"/> <button constraints="BorderLayout.SOUTH" text="3"/> <button constraints="BorderLayout.WEST" text="4"/> </internalframe> <internalframe Title="Tree Window" Bounds="10,170,350,360" Layout="borderlayout" Visible="true" Resizable="true"> <panel layout="borderlayout" constraints="BorderLayout.CENTER"> <splitpane oneTouchExpandable="true" dividerLocation="200"> <splitpane oneTouchExpandable="true" dividerLocation="140" orientation="VERTICAL"> <scrollPane background="blue" > <tree name="tree"/> </scrollPane> <panel layout="borderlayout"> <panel constraints="BorderLayout.NORTH"> <button name="btn_copy" ToolTipText="JPanel" enabled="true" BorderPainted="false" FocusPainted="false" icon="icons/copy.gif" size="24,24"/> <button name="btn_paste" ToolTipText="JButton" enabled="true" BorderPainted="false" FocusPainted="false" icon="icons/paste.gif" size="24,24"/> <button name="btn_cut" ToolTipText="JLabel" enabled="true" icon="icons/cut.gif" BorderPainted="false" FocusPainted="false" size="24,24"/> </panel> <scrollPane constraints="BorderLayout.CENTER"> <table name="table"/> </scrollPane> </panel> </splitpane> <panel name="preview" border="LoweredBevelBorder"> <textarea name="ta" text="Tree Status Log....." background="red"/> </panel> </splitpane> </panel> <panel constraints="BorderLayout.SOUTH"> <label text="Status:"/> <textfield text="OK"/> </panel> </internalframe> </desktoppane> </frame>