~~ ~~ Copyright (c) 2004-2013, 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 +-- swixml-lang.eld +-- * Define the element language +-- +-- * 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; } } +-- * XML to parse +--