281 lines
11 KiB
Text
281 lines
11 KiB
Text
|
|
~~
|
||
|
|
~~ Copyright (c) 2004-2012, Willem Cazander
|
||
|
|
~~ All rights reserved.
|
||
|
|
~~
|
||
|
|
~~ Redistribution and use in source and binary forms, with or without modification, are permitted provided
|
||
|
|
~~ that the following conditions are met:
|
||
|
|
~~
|
||
|
|
~~ * Redistributions of source code must retain the above copyright notice, this list of conditions and the
|
||
|
|
~~ following disclaimer.
|
||
|
|
~~ * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
|
||
|
|
~~ the following disclaimer in the documentation and/or other materials provided with the distribution.
|
||
|
|
~~
|
||
|
|
~~ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
|
||
|
|
~~ EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||
|
|
~~ MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
||
|
|
~~ THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||
|
|
~~ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
|
||
|
|
~~ OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||
|
|
~~ HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
|
||
|
|
~~ TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||
|
|
~~ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||
|
|
~~
|
||
|
|
|
||
|
|
Sample SwiXml
|
||
|
|
|
||
|
|
Lets create an xml example from swixml language.
|
||
|
|
|
||
|
|
* Create parser
|
||
|
|
|
||
|
|
+--
|
||
|
|
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;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
+--
|
||
|
|
|
||
|
|
* Create parser support
|
||
|
|
|
||
|
|
+--
|
||
|
|
public class SwiXmlParserSupport2 implements X4OParserSupport {
|
||
|
|
|
||
|
|
public ElementLanguage loadElementLanguageSupport() throws X4OParserSupportException {
|
||
|
|
SwiXmlParser parser = new SwiXmlParser();
|
||
|
|
return parser.loadElementLanguageSupport();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
+--
|
||
|
|
|
||
|
|
|
||
|
|
* Define modules
|
||
|
|
|
||
|
|
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>
|
||
|
|
+--
|
||
|
|
|
||
|
|
* Define the element language
|
||
|
|
|
||
|
|
+--
|
||
|
|
<?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>
|
||
|
|
+--
|
||
|
|
|
||
|
|
* JMenuBarBindingHandler
|
||
|
|
|
||
|
|
+--
|
||
|
|
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);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
+--
|
||
|
|
|
||
|
|
* BorderConverter
|
||
|
|
|
||
|
|
+--
|
||
|
|
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;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
+--
|
||
|
|
|
||
|
|
* Source
|
||
|
|
|
||
|
|
This example is found in the x4o-core source package in the test path.
|
||
|
|
|