From 179c0f3bfea0844dff87b0d5df54e8679bee00a1 Mon Sep 17 00:00:00 2001 From: Willem Cazander Date: Mon, 8 Apr 2013 08:34:51 +0200 Subject: [PATCH] Made first crude writing impl for x4o. --- .../src/main/java/org/x4o/xml/X4ODriver.java | 68 +- .../java/org/x4o/xml/X4ODriverManager.java | 40 + .../org/x4o/xml/el/X4OExpressionFactory.java | 62 + .../main/java/org/x4o/xml/eld/CelDriver.java | 12 +- .../main/java/org/x4o/xml/eld/EldDriver.java | 10 - .../ElementClassAttributeBindingHandler.java | 14 +- .../eld/lang/ElementClassBindingHandler.java | 16 +- .../lang/ElementInterfaceBindingHandler.java | 19 +- .../eld/lang/ElementModuleBindingHandler.java | 41 +- ...ElementNamespaceContextBindingHandler.java | 13 +- .../lang/ElementRefectionBindingHandler.java | 92 +- .../org/x4o/xml/eld/lang/ModuleElement.java | 2 +- .../StringSplitConverterBindingHandler.java | 17 +- ...tringSplitConverterStepBindingHandler.java | 15 +- .../org/x4o/xml/element/AbstractElement.java | 10 +- .../AbstractElementBindingHandler.java | 32 +- .../DefaultElementAttributeValueParser.java | 8 +- .../element/DefaultElementBodyCharacters.java | 4 +- .../java/org/x4o/xml/element/Element.java | 2 +- .../xml/element/ElementBindingHandler.java | 5 +- .../org/x4o/xml/io/AbstractX4OWriter.java | 15 +- .../java/org/x4o/xml/io/DefaultX4ODriver.java | 9 - .../java/org/x4o/xml/io/DefaultX4OWriter.java | 178 ++- .../java/org/x4o/xml/io/XMLConstants.java | 2 +- .../org/x4o/xml/io/sax/X4ODebugWriter.java | 2 +- .../xml/lang/AbstractX4OLanguageContext.java | 18 +- .../org/x4o/xml/lang/DefaultX4OLanguage.java | 110 +- .../lang/DefaultX4OLanguageConfiguration.java | 67 +- .../xml/lang/DefaultX4OLanguageContext.java | 4 +- .../xml/lang/DefaultX4OLanguageLoader.java | 2 +- .../java/org/x4o/xml/lang/X4OLanguage.java | 24 +- .../xml/lang/X4OLanguageConfiguration.java | 12 +- .../org/x4o/xml/lang/X4OLanguageContext.java | 9 +- .../x4o/xml/lang/X4OLanguageContextLocal.java | 7 + .../lang/phase/DefaultX4OPhaseManager.java | 50 +- .../x4o/xml/lang/phase/X4OPhaseFactory.java | 1110 ----------------- .../xml/lang/phase/X4OPhaseLanguageInit.java | 180 +++ .../xml/lang/phase/X4OPhaseLanguageRead.java | 993 +++++++++++++++ .../x4o/xml/lang/phase/X4OPhaseManager.java | 6 +- .../lang/phase/X4OPhaseManagerFactory.java | 213 ---- .../resources/META-INF/eld/eld-conv-1.0.xsd | 8 +- .../resources/META-INF/eld/eld-lang-1.0.xsd | 25 +- .../main/resources/META-INF/eld/eld-lang.eld | 9 +- .../resources/META-INF/eld/eld-root-1.0.xsd | 12 +- .../org/x4o/xml/X4ODriverManagerTest.java | 9 +- .../java/org/x4o/xml/io/X4OWriterTest.java | 70 ++ .../lang/DefaultX4OLanguageLoaderTest.java | 2 +- .../x4o/xml/test/models/TestObjectParent.java | 6 +- .../test/swixml/SwiXmlActionConfigurator.java | 2 +- .../org/x4o/xml/test/swixml/SwiXmlDriver.java | 6 - .../swixml/bind/JFrameBindingHandler.java | 20 +- .../bind/JInternalFrameBindingHandler.java | 23 +- .../swixml/bind/JPanelBindingHandler.java | 21 +- .../swixml/bind/JSplitPaneBindingHandler.java | 16 +- .../META-INF/swixml/swixml-lang-2.0.eld | 10 +- .../META-INF/swixml/swixml-lang-3.0.eld | 10 +- .../resources/META-INF/test/test-lang.eld | 14 +- .../xml/meta/lang/ELIDAttributeHandler.java | 4 +- .../x4o/xml/meta/lang/ELReferenceElement.java | 4 +- .../x4o/xml/meta/lang/PropertyElement.java | 4 +- .../org/x4o/xml/meta/test/MTestDriver.java | 9 - x4o-plugin/x4o-plugin-ant-elddoc/pom.xml | 10 +- x4o-plugin/x4o-plugin-ant-schema/pom.xml | 4 +- 63 files changed, 2096 insertions(+), 1695 deletions(-) create mode 100644 x4o-core/src/main/java/org/x4o/xml/el/X4OExpressionFactory.java delete mode 100644 x4o-core/src/main/java/org/x4o/xml/lang/phase/X4OPhaseFactory.java create mode 100644 x4o-core/src/main/java/org/x4o/xml/lang/phase/X4OPhaseLanguageInit.java create mode 100644 x4o-core/src/main/java/org/x4o/xml/lang/phase/X4OPhaseLanguageRead.java delete mode 100644 x4o-core/src/main/java/org/x4o/xml/lang/phase/X4OPhaseManagerFactory.java create mode 100644 x4o-core/src/test/java/org/x4o/xml/io/X4OWriterTest.java diff --git a/x4o-core/src/main/java/org/x4o/xml/X4ODriver.java b/x4o-core/src/main/java/org/x4o/xml/X4ODriver.java index cfa08d2..320c629 100644 --- a/x4o-core/src/main/java/org/x4o/xml/X4ODriver.java +++ b/x4o-core/src/main/java/org/x4o/xml/X4ODriver.java @@ -31,11 +31,12 @@ import org.x4o.xml.io.DefaultX4OWriter; import org.x4o.xml.io.X4OReader; import org.x4o.xml.io.X4OSchemaWriter; import org.x4o.xml.io.X4OWriter; + +import org.x4o.xml.lang.X4OLanguageConfiguration; import org.x4o.xml.lang.X4OLanguageContext; import org.x4o.xml.lang.X4OLanguage; import org.x4o.xml.lang.X4OLanguagePropertyKeys; -import org.x4o.xml.lang.phase.X4OPhaseException; -import org.x4o.xml.lang.phase.X4OPhaseType; +import org.x4o.xml.lang.phase.X4OPhaseManager; /** * This is the starting point of the XML X4O Language Driver. @@ -55,38 +56,32 @@ public abstract class X4ODriver { X4ODriverManager.registerX4ODriver(this); } + /** + * @return Returns the langauge name of this driver. + */ abstract public String getLanguageName(); + /** + * @return Returns the supported language versions for this driver. + */ abstract public String[] getLanguageVersions(); - abstract public X4OLanguage buildLanguage(String version); - public X4OLanguage createLanguage(String version) { - X4OLanguage result = null; - if (result==null) { - result = buildLanguage(version); - } - return result; + + protected X4OLanguage buildLanguage(String version) { + return X4ODriverManager.getDefaultBuildLanguage(this, version); } - public X4OLanguageContext createLanguageContext() { - return createLanguageContext(getLanguageVersionDefault()); + protected X4OPhaseManager buildPhaseManager() { + return X4ODriverManager.getDefaultBuildPhaseManager(); } - public X4OLanguageContext createLanguageContext(String version) { - X4OLanguage language = createLanguage(version); - X4OLanguageContext result = language.getLanguageConfiguration().createElementLanguage(this); - - try { - result.getLanguage().getPhaseManager().runPhases(result, X4OPhaseType.INIT); - } catch (X4OPhaseException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - return result; + protected X4OLanguageConfiguration buildLanguageConfiguration() { + return X4ODriverManager.getDefaultBuildLanguageConfiguration(); } + + public X4OSchemaWriter createSchemaWriter() { return createSchemaWriter(getLanguageVersionDefault()); } @@ -112,24 +107,27 @@ public abstract class X4ODriver { } public String getLanguageVersionDefault() { - String[] lang = getLanguageVersions(); - if (lang==null || lang.length==0) { - return DEFAULT_LANGUAGE_VERSION; - } - String langVersion = lang[lang.length-1]; - return langVersion; + return X4ODriverManager.getDefaultLanguageVersion(getLanguageVersions()); } - /** - * @return Returns the property keys which can be set. - */ public String[] getGlobalPropertyKeySet() { return X4OLanguagePropertyKeys.DEFAULT_X4O_GLOBAL_KEYS; } - /** - * @return Returns the property keys which are set. - */ + + + final public X4OLanguage createLanguage(String version) { + return buildLanguage(version); + } + + final public X4OLanguageContext createLanguageContext() { + return createLanguageContext(getLanguageVersionDefault()); + } + + final public X4OLanguageContext createLanguageContext(String version) { + return createLanguage(version).createLanguageContext(this); + } + final public Collection getGlobalPropertyKeys() { return X4ODriverManager.getGlobalPropertyKeys(this); } diff --git a/x4o-core/src/main/java/org/x4o/xml/X4ODriverManager.java b/x4o-core/src/main/java/org/x4o/xml/X4ODriverManager.java index ab6e499..2f7bda0 100644 --- a/x4o-core/src/main/java/org/x4o/xml/X4ODriverManager.java +++ b/x4o-core/src/main/java/org/x4o/xml/X4ODriverManager.java @@ -35,7 +35,15 @@ import java.util.List; import java.util.Map; import java.util.logging.Logger; +import org.x4o.xml.lang.DefaultX4OLanguage; +import org.x4o.xml.lang.DefaultX4OLanguageConfiguration; +import org.x4o.xml.lang.X4OLanguage; import org.x4o.xml.lang.X4OLanguageClassLoader; +import org.x4o.xml.lang.X4OLanguageConfiguration; +import org.x4o.xml.lang.phase.DefaultX4OPhaseManager; +import org.x4o.xml.lang.phase.X4OPhaseLanguageInit; +import org.x4o.xml.lang.phase.X4OPhaseLanguageRead; +import org.x4o.xml.lang.phase.X4OPhaseManager; import org.xml.sax.Attributes; import org.xml.sax.InputSource; import org.xml.sax.SAXException; @@ -66,6 +74,38 @@ public final class X4ODriverManager { instance = new X4ODriverManager(); } + static protected String getDefaultLanguageVersion(String[] languages) { + if (languages==null || languages.length==0) { + return X4ODriver.DEFAULT_LANGUAGE_VERSION; + } + String languageVersion = languages[languages.length-1]; + return languageVersion; + } + + static protected X4OPhaseManager getDefaultBuildPhaseManager() { + DefaultX4OPhaseManager manager = new DefaultX4OPhaseManager(); + new X4OPhaseLanguageInit().createPhases(manager); + new X4OPhaseLanguageRead().createPhases(manager); + //new X4OPhaseLanguageWrite().createPhases(manager); + return manager; + } + + static protected X4OLanguage getDefaultBuildLanguage(X4ODriver driver,String version) { + if (version==null) { + version = driver.getLanguageVersionDefault(); + } + return new DefaultX4OLanguage( + driver.buildLanguageConfiguration(), + driver.buildPhaseManager(), + driver.getLanguageName(), + version + ); + } + + static protected X4OLanguageConfiguration getDefaultBuildLanguageConfiguration() { + return new DefaultX4OLanguageConfiguration(); + } + static public Collection getGlobalPropertyKeys(X4ODriver driver) { Map driverProperties = instance.globalProperties.get(driver.getLanguageName()); if (driverProperties==null) { diff --git a/x4o-core/src/main/java/org/x4o/xml/el/X4OExpressionFactory.java b/x4o-core/src/main/java/org/x4o/xml/el/X4OExpressionFactory.java new file mode 100644 index 0000000..1531666 --- /dev/null +++ b/x4o-core/src/main/java/org/x4o/xml/el/X4OExpressionFactory.java @@ -0,0 +1,62 @@ +/* + * 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. + */ + +package org.x4o.xml.el; + +import javax.el.ExpressionFactory; + +import org.x4o.xml.lang.X4OLanguageClassLoader; +import org.x4o.xml.lang.X4OLanguageContext; +import org.x4o.xml.lang.X4OLanguageProperty; + +/** + * X4OExpressionFactory finds and loads the needed impl. + * + * @author Willem Cazander + * @version 1.0 Apr 7, 2013 + */ +public class X4OExpressionFactory { + + static public final String EL_FACTORY_IMPL_APACHE = "org.apache.el.ExpressionFactoryImpl"; + static public final String EL_FACTORY_IMPL_ODYSSEUS = "de.odysseus.el.ExpressionFactoryImpl"; + + static public ExpressionFactory createExpressionFactory(X4OLanguageContext elementContext) { + ExpressionFactory factory = (ExpressionFactory)elementContext.getLanguageProperty(X4OLanguageProperty.EL_FACTORY_INSTANCE); + if (factory!=null) { + return factory; + } + try { + Class expressionFactoryClass = X4OLanguageClassLoader.loadClass(EL_FACTORY_IMPL_APACHE); + ExpressionFactory expressionFactory = (ExpressionFactory) expressionFactoryClass.newInstance(); + return expressionFactory; + } catch (Exception e) { + try { + Class expressionFactoryClass = X4OLanguageClassLoader.loadClass(EL_FACTORY_IMPL_ODYSSEUS); + ExpressionFactory expressionFactory = (ExpressionFactory) expressionFactoryClass.newInstance(); + return expressionFactory; + } catch (Exception ee) { + throw new RuntimeException("Could not load ExpressionFactory tried: "+EL_FACTORY_IMPL_APACHE+" and "+EL_FACTORY_IMPL_ODYSSEUS+" but could not load one of them."); + } + } + } +} diff --git a/x4o-core/src/main/java/org/x4o/xml/eld/CelDriver.java b/x4o-core/src/main/java/org/x4o/xml/eld/CelDriver.java index 01b2a50..d1de632 100644 --- a/x4o-core/src/main/java/org/x4o/xml/eld/CelDriver.java +++ b/x4o-core/src/main/java/org/x4o/xml/eld/CelDriver.java @@ -24,15 +24,10 @@ package org.x4o.xml.eld; import org.x4o.xml.X4ODriver; -import org.x4o.xml.lang.DefaultX4OLanguage; -import org.x4o.xml.lang.DefaultX4OLanguageConfiguration; import org.x4o.xml.lang.X4OLanguageModule; -import org.x4o.xml.lang.X4OLanguage; -import org.x4o.xml.lang.phase.X4OPhaseManagerFactory; - /** - * CelDriver is the Core Element Language driver. + * CelDriver is the Core Element Language driver. * * @author Willem Cazander * @version 1.0 Aug 20, 2005 @@ -52,9 +47,4 @@ public class CelDriver extends X4ODriver { public String[] getLanguageVersions() { return null; } - - @Override - public X4OLanguage buildLanguage(String version) { - return new DefaultX4OLanguage(new DefaultX4OLanguageConfiguration(),X4OPhaseManagerFactory.createDefaultX4OPhaseManager(),getLanguageName(),getLanguageVersionDefault()); - } } diff --git a/x4o-core/src/main/java/org/x4o/xml/eld/EldDriver.java b/x4o-core/src/main/java/org/x4o/xml/eld/EldDriver.java index c1111b2..c45b56d 100644 --- a/x4o-core/src/main/java/org/x4o/xml/eld/EldDriver.java +++ b/x4o-core/src/main/java/org/x4o/xml/eld/EldDriver.java @@ -24,12 +24,7 @@ package org.x4o.xml.eld; import org.x4o.xml.X4ODriver; -import org.x4o.xml.lang.DefaultX4OLanguage; -import org.x4o.xml.lang.DefaultX4OLanguageConfiguration; import org.x4o.xml.lang.X4OLanguageModule; -import org.x4o.xml.lang.X4OLanguage; -import org.x4o.xml.lang.phase.X4OPhaseManagerFactory; - /** * An Element Language Definition X4O parser. @@ -55,9 +50,4 @@ public class EldDriver extends X4ODriver { public String[] getLanguageVersions() { return LANGUAGE_VERSIONS; } - - @Override - public X4OLanguage buildLanguage(String version) { - return new DefaultX4OLanguage(new DefaultX4OLanguageConfiguration(),X4OPhaseManagerFactory.createDefaultX4OPhaseManager(),getLanguageName(),getLanguageVersionDefault()); - } } diff --git a/x4o-core/src/main/java/org/x4o/xml/eld/lang/ElementClassAttributeBindingHandler.java b/x4o-core/src/main/java/org/x4o/xml/eld/lang/ElementClassAttributeBindingHandler.java index 0c41ad4..1ec8a54 100644 --- a/x4o-core/src/main/java/org/x4o/xml/eld/lang/ElementClassAttributeBindingHandler.java +++ b/x4o-core/src/main/java/org/x4o/xml/eld/lang/ElementClassAttributeBindingHandler.java @@ -35,7 +35,7 @@ import org.x4o.xml.element.ElementClassAttribute; * @author Willem Cazander * @version 1.0 Aug 21, 2012 */ -public class ElementClassAttributeBindingHandler extends AbstractElementBindingHandler { +public class ElementClassAttributeBindingHandler extends AbstractElementBindingHandler { private final static Class[] CLASSES_CHILD = new Class[] { @@ -56,13 +56,13 @@ public class ElementClassAttributeBindingHandler extends AbstractElementBindingH return CLASSES_CHILD; } - /** - * @see org.x4o.xml.element.ElementBindingHandler#doBind(java.lang.Object, java.lang.Object, org.x4o.xml.element.Element) - */ - public void doBind(Object parentObject, Object childObject,Element childElement) throws ElementBindingHandlerException { - ElementClassAttribute parent = (ElementClassAttribute)parentObject; + public void bindChild(Element childElement,ElementClassAttribute parentObject,Object childObject) throws ElementBindingHandlerException { if (childObject instanceof ObjectConverter) { - parent.setObjectConverter((ObjectConverter)childObject); + parentObject.setObjectConverter((ObjectConverter)childObject); } } + + public void createChilderen(Element parentElement,ElementClassAttribute parentObject) throws ElementBindingHandlerException { + createChild(parentElement, parentObject.getObjectConverter()); + } } diff --git a/x4o-core/src/main/java/org/x4o/xml/eld/lang/ElementClassBindingHandler.java b/x4o-core/src/main/java/org/x4o/xml/eld/lang/ElementClassBindingHandler.java index b0a879e..88e4dc6 100644 --- a/x4o-core/src/main/java/org/x4o/xml/eld/lang/ElementClassBindingHandler.java +++ b/x4o-core/src/main/java/org/x4o/xml/eld/lang/ElementClassBindingHandler.java @@ -37,7 +37,7 @@ import org.x4o.xml.element.ElementConfigurator; * @author Willem Cazander * @version 1.0 Jan 31, 2007 */ -public class ElementClassBindingHandler extends AbstractElementBindingHandler { +public class ElementClassBindingHandler extends AbstractElementBindingHandler { private final static Class[] CLASSES_CHILD = new Class[] { ElementClassAttribute.class, @@ -59,10 +59,9 @@ public class ElementClassBindingHandler extends AbstractElementBindingHandler { } /** - * @see org.x4o.xml.element.ElementBindingHandler#doBind(java.lang.Object, java.lang.Object, org.x4o.xml.element.Element) + * @see org.x4o.xml.element.ElementBindingHandler#bindChild(org.x4o.xml.element.Element,java.lang.Object, java.lang.Object) */ - public void doBind(Object parentObject, Object childObject,Element childElement) throws ElementBindingHandlerException { - ElementClass parent = (ElementClass)parentObject; + public void bindChild(Element childElement,ElementClass parent, Object childObject) throws ElementBindingHandlerException { if (childObject instanceof ElementClassAttribute) { parent.addElementClassAttribute((ElementClassAttribute)childObject); } @@ -70,4 +69,13 @@ public class ElementClassBindingHandler extends AbstractElementBindingHandler { parent.addElementConfigurators((ElementConfigurator)childObject); } } + + public void createChilderen(Element parentElement,ElementClass parent) throws ElementBindingHandlerException { + for (ElementClassAttribute child:parent.getElementClassAttributes()) { + createChild(parentElement, child); + } + for (ElementConfigurator child:parent.getElementConfigurators()) { + createChild(parentElement, child); + } + } } diff --git a/x4o-core/src/main/java/org/x4o/xml/eld/lang/ElementInterfaceBindingHandler.java b/x4o-core/src/main/java/org/x4o/xml/eld/lang/ElementInterfaceBindingHandler.java index 07e912c..5aaaaaf 100644 --- a/x4o-core/src/main/java/org/x4o/xml/eld/lang/ElementInterfaceBindingHandler.java +++ b/x4o-core/src/main/java/org/x4o/xml/eld/lang/ElementInterfaceBindingHandler.java @@ -37,7 +37,7 @@ import org.x4o.xml.element.ElementInterface; * @author Willem Cazander * @version 1.0 Aug 21, 2012 */ -public class ElementInterfaceBindingHandler extends AbstractElementBindingHandler { +public class ElementInterfaceBindingHandler extends AbstractElementBindingHandler { private final static Class[] CLASSES_CHILD = new Class[] { ElementClassAttribute.class, @@ -60,10 +60,9 @@ public class ElementInterfaceBindingHandler extends AbstractElementBindingHandle } /** - * @see org.x4o.xml.element.ElementBindingHandler#doBind(java.lang.Object, java.lang.Object, org.x4o.xml.element.Element) + * @see org.x4o.xml.element.ElementBindingHandler#bindChild(org.x4o.xml.element.Element, java.lang.Object, java.lang.Object) */ - public void doBind(Object parentObject, Object childObject,Element childElement) throws ElementBindingHandlerException { - ElementInterface parent = (ElementInterface)parentObject; + public void bindChild(Element childElement,ElementInterface parent, Object childObject) throws ElementBindingHandlerException { if (childObject instanceof ElementBindingHandler) { parent.addElementBindingHandler((ElementBindingHandler)childObject); } @@ -74,4 +73,16 @@ public class ElementInterfaceBindingHandler extends AbstractElementBindingHandle parent.addElementConfigurators((ElementConfigurator)childObject); } } + + public void createChilderen(Element parentElement,ElementInterface parent) throws ElementBindingHandlerException { + for (ElementBindingHandler child:parent.getElementBindingHandlers()) { + createChild(parentElement, child); + } + for (ElementClassAttribute child:parent.getElementClassAttributes()) { + createChild(parentElement, child); + } + for (ElementConfigurator child:parent.getElementConfigurators()) { + createChild(parentElement, child); + } + } } diff --git a/x4o-core/src/main/java/org/x4o/xml/eld/lang/ElementModuleBindingHandler.java b/x4o-core/src/main/java/org/x4o/xml/eld/lang/ElementModuleBindingHandler.java index d2cd0c8..c3676fe 100644 --- a/x4o-core/src/main/java/org/x4o/xml/eld/lang/ElementModuleBindingHandler.java +++ b/x4o-core/src/main/java/org/x4o/xml/eld/lang/ElementModuleBindingHandler.java @@ -49,7 +49,7 @@ import org.x4o.xml.lang.X4OLanguageProperty; * @author Willem Cazander * @version 1.0 Jan 19, 2007 */ -public class ElementModuleBindingHandler extends AbstractElementBindingHandler { +public class ElementModuleBindingHandler extends AbstractElementBindingHandler { private final static Class[] CLASSES_CHILD = new Class[] { ElementInterface.class, @@ -75,34 +75,33 @@ public class ElementModuleBindingHandler extends AbstractElementBindingHandler } /** - * @see org.x4o.xml.element.ElementBindingHandler#doBind(java.lang.Object, java.lang.Object, org.x4o.xml.element.Element) + * @see org.x4o.xml.element.ElementBindingHandler#bindChild(org.x4o.xml.element.Element, java.lang.Object, java.lang.Object) */ - public void doBind(Object parentObject, Object childObject,Element childElement) throws ElementBindingHandlerException { + public void bindChild(Element childElement,X4OLanguageModule languageModule, Object childObject) throws ElementBindingHandlerException { @SuppressWarnings("rawtypes") - Map m = (Map)childElement.getElementLanguage().getLanguageProperty(X4OLanguageProperty.EL_BEAN_INSTANCE_MAP); + Map m = (Map)childElement.getLanguageContext().getLanguageProperty(X4OLanguageProperty.EL_BEAN_INSTANCE_MAP); if (m==null) { return; } X4OLanguage x4oParsingContext = (X4OLanguage)m.get(EldModuleLoader.EL_PARENT_LANGUAGE); //ElementLanguageModule elementLanguageModule = (ElementLanguageModule)m.get(EldParser.PARENT_ELEMENT_LANGUAGE_MODULE); - X4OLanguageModule elementLanguageModule = (X4OLanguageModule)parentObject; if (x4oParsingContext==null) { return; } - if (elementLanguageModule==null) { + if (languageModule==null) { return; } if (childObject instanceof ElementInterface) { ElementInterface elementInterface = (ElementInterface)childObject; - elementLanguageModule.addElementInterface(elementInterface); + languageModule.addElementInterface(elementInterface); return; } if (childObject instanceof ElementNamespaceContext) { ElementNamespaceContext elementNamespaceContext = (ElementNamespaceContext)childObject; try { - elementNamespaceContext.setElementNamespaceInstanceProvider((ElementNamespaceInstanceProvider)X4OLanguageClassLoader.newInstance(childElement.getElementLanguage().getLanguage().getLanguageConfiguration().getDefaultElementNamespaceInstanceProvider())); + elementNamespaceContext.setElementNamespaceInstanceProvider((ElementNamespaceInstanceProvider)X4OLanguageClassLoader.newInstance(childElement.getLanguageContext().getLanguage().getLanguageConfiguration().getDefaultElementNamespaceInstanceProvider())); } catch (Exception e) { throw new ElementBindingHandlerException("Error loading: "+e.getMessage(),e); } @@ -112,23 +111,41 @@ public class ElementModuleBindingHandler extends AbstractElementBindingHandler } catch (ElementNamespaceInstanceProviderException e) { throw new ElementBindingHandlerException("Error starting: "+e.getMessage(),e); } - elementLanguageModule.addElementNamespaceContext(elementNamespaceContext); + languageModule.addElementNamespaceContext(elementNamespaceContext); return; } if (childObject instanceof ElementBindingHandler) { ElementBindingHandler elementBindingHandler = (ElementBindingHandler)childObject; - elementLanguageModule.addElementBindingHandler(elementBindingHandler); + languageModule.addElementBindingHandler(elementBindingHandler); return; } if (childObject instanceof ElementAttributeHandler) { ElementAttributeHandler elementAttributeHandler = (ElementAttributeHandler)childObject; - elementLanguageModule.addElementAttributeHandler(elementAttributeHandler); + languageModule.addElementAttributeHandler(elementAttributeHandler); return; } if (childObject instanceof ElementConfiguratorGlobal) { ElementConfiguratorGlobal elementConfigurator = (ElementConfiguratorGlobal)childObject; - elementLanguageModule.addElementConfiguratorGlobal(elementConfigurator); + languageModule.addElementConfiguratorGlobal(elementConfigurator); return; } } + + public void createChilderen(Element parentElement,X4OLanguageModule parent) throws ElementBindingHandlerException { + for (ElementInterface child:parent.getElementInterfaces()) { + createChild(parentElement, child); + } + for (ElementNamespaceContext child:parent.getElementNamespaceContexts()) { + createChild(parentElement, child); + } + for (ElementBindingHandler child:parent.getElementBindingHandlers()) { + createChild(parentElement, child); + } + for (ElementAttributeHandler child:parent.getElementAttributeHandlers()) { + createChild(parentElement, child); + } + for (ElementConfiguratorGlobal child:parent.getElementConfiguratorGlobals()) { + createChild(parentElement, child); + } + } } diff --git a/x4o-core/src/main/java/org/x4o/xml/eld/lang/ElementNamespaceContextBindingHandler.java b/x4o-core/src/main/java/org/x4o/xml/eld/lang/ElementNamespaceContextBindingHandler.java index 9058443..e3914a0 100644 --- a/x4o-core/src/main/java/org/x4o/xml/eld/lang/ElementNamespaceContextBindingHandler.java +++ b/x4o-core/src/main/java/org/x4o/xml/eld/lang/ElementNamespaceContextBindingHandler.java @@ -35,7 +35,7 @@ import org.x4o.xml.element.ElementNamespaceContext; * @author Willem Cazander * @version 1.0 Aug 21, 2012 */ -public class ElementNamespaceContextBindingHandler extends AbstractElementBindingHandler { +public class ElementNamespaceContextBindingHandler extends AbstractElementBindingHandler { private final static Class[] CLASSES_CHILD = new Class[] { ElementClass.class @@ -56,12 +56,17 @@ public class ElementNamespaceContextBindingHandler extends AbstractElementBindin } /** - * @see org.x4o.xml.element.ElementBindingHandler#doBind(java.lang.Object, java.lang.Object, org.x4o.xml.element.Element) + * @see org.x4o.xml.element.ElementBindingHandler#bindChild(org.x4o.xml.element.Element, java.lang.Object, java.lang.Object) */ - public void doBind(Object parentObject, Object childObject,Element childElement) throws ElementBindingHandlerException { - ElementNamespaceContext parent = (ElementNamespaceContext)parentObject; + public void bindChild(Element childElement,ElementNamespaceContext parent, Object childObject) throws ElementBindingHandlerException { if (childObject instanceof ElementClass) { parent.addElementClass((ElementClass)childObject); } } + + public void createChilderen(Element parentElement,ElementNamespaceContext parent) throws ElementBindingHandlerException { + for (ElementClass child:parent.getElementClasses()) { + createChild(parentElement, child); + } + } } diff --git a/x4o-core/src/main/java/org/x4o/xml/eld/lang/ElementRefectionBindingHandler.java b/x4o-core/src/main/java/org/x4o/xml/eld/lang/ElementRefectionBindingHandler.java index 0a6474a..9ae76bd 100644 --- a/x4o-core/src/main/java/org/x4o/xml/eld/lang/ElementRefectionBindingHandler.java +++ b/x4o-core/src/main/java/org/x4o/xml/eld/lang/ElementRefectionBindingHandler.java @@ -23,7 +23,10 @@ package org.x4o.xml.eld.lang; +import java.lang.reflect.Array; import java.lang.reflect.Method; +import java.util.Collection; +import java.util.List; import org.x4o.xml.element.AbstractElementBindingHandler; import org.x4o.xml.element.Element; @@ -35,11 +38,12 @@ import org.x4o.xml.element.ElementBindingHandlerException; * @author Willem Cazander * @version 1.0 Nov 21, 2007 */ -public class ElementRefectionBindingHandler extends AbstractElementBindingHandler { +public class ElementRefectionBindingHandler extends AbstractElementBindingHandler { private Class parentClass = null; private Class childClass = null; - private String method = null; + private String addMethod = null; + private String getMethod = null; /** * @see org.x4o.xml.element.ElementBindingHandler#getBindParentClass() @@ -56,12 +60,12 @@ public class ElementRefectionBindingHandler extends AbstractElementBindingHandle } /** - * @see org.x4o.xml.element.ElementBindingHandler#doBind(java.lang.Object, java.lang.Object, org.x4o.xml.element.Element) + * @see org.x4o.xml.element.ElementBindingHandler#bindChild(org.x4o.xml.element.Element,java.lang.Object, java.lang.Object) */ - public void doBind(Object parentObject, Object childObject, Element childElement) throws ElementBindingHandlerException { + public void bindChild(Element childElement, Object parentObject, Object childObject) throws ElementBindingHandlerException { - if (parentClass==null | childClass==null | method==null) { - throw new IllegalStateException("Missing property: parentClass="+parentClass+" childClass="+childClass+" method="+method+"."); + if (parentClass==null | childClass==null | addMethod==null) { + throw new IllegalStateException("Missing property: parentClass="+parentClass+" childClass="+childClass+" addMethod="+addMethod+"."); } Method[] ms = parentObject.getClass().getMethods(); for (Method m:ms) { @@ -72,7 +76,7 @@ public class ElementRefectionBindingHandler extends AbstractElementBindingHandle if (types.length > 1) { continue; } - if (method.equalsIgnoreCase(m.getName())==false) { + if (addMethod.equalsIgnoreCase(m.getName())==false) { continue; } if (types[0].isAssignableFrom(childClass)) { @@ -84,9 +88,55 @@ public class ElementRefectionBindingHandler extends AbstractElementBindingHandle return; } } - throw new ElementBindingHandlerException("Could not find method: "+method+" on: "+childClass+" id:"+getId()); + throw new ElementBindingHandlerException("Could not find method: "+addMethod+" on: "+childClass+" id:"+getId()); } + @SuppressWarnings("rawtypes") + public void createChilderen(Element parentElement,Object parentObject) throws ElementBindingHandlerException { + if (parentClass==null | childClass==null | getMethod==null) { + throw new IllegalStateException("Missing property: parentClass="+parentClass+" childClass="+childClass+" getMethod="+getMethod+"."); + } + Method[] ms = parentObject.getClass().getMethods(); + for (Method m:ms) { + Class[] types = m.getParameterTypes(); + if (types.length != 0) { + continue; + } + if (getMethod.equalsIgnoreCase(m.getName())==false) { + continue; + } + try { + Object result = m.invoke(parentObject, new Object[]{}); + if (result==null) { + break; + } + if (result instanceof List) { + for (Object o:(List)result) { + createChild(parentElement, o); + } + return; + } else if (result instanceof Collection) { + for (Object o:(Collection)result) { + createChild(parentElement, o); + } + return; + } else if (result instanceof Array) { + for (Object o:(Object[])result) { + createChild(parentElement, o); + } + return; + } else { + throw new ElementBindingHandlerException("Unsuported return type: "+result.getClass()+" from: "+getMethod+" on: "+parentObject); + } + } catch (Exception e) { + throw new ElementBindingHandlerException("Error invoke binding method of: "+getId()+" error: "+e.getMessage(),e); + } + } + throw new ElementBindingHandlerException("Could not find method: "+getMethod+" on: "+parentObject+" id:"+getId()); + + // + } + /** * @return the parentClass */ @@ -116,16 +166,30 @@ public class ElementRefectionBindingHandler extends AbstractElementBindingHandle } /** - * @return the method + * @return the addMethod */ - public String getMethod() { - return method; + public String getAddMethod() { + return addMethod; } /** - * @param method the method to set + * @param addMethod the addMethod to set */ - public void setMethod(String method) { - this.method = method; + public void setAddMethod(String addMethod) { + this.addMethod = addMethod; + } + + /** + * @return the getMethod + */ + public String getGetMethod() { + return getMethod; + } + + /** + * @param getMethod the getMethod to set + */ + public void setGetMethod(String getMethod) { + this.getMethod = getMethod; } } diff --git a/x4o-core/src/main/java/org/x4o/xml/eld/lang/ModuleElement.java b/x4o-core/src/main/java/org/x4o/xml/eld/lang/ModuleElement.java index 52687aa..d6122f9 100644 --- a/x4o-core/src/main/java/org/x4o/xml/eld/lang/ModuleElement.java +++ b/x4o-core/src/main/java/org/x4o/xml/eld/lang/ModuleElement.java @@ -50,7 +50,7 @@ public class ModuleElement extends AbstractElement { throw new ElementException("Need to be root tag"); } @SuppressWarnings("rawtypes") - Map m = (Map)getElementLanguage().getLanguageProperty(X4OLanguageProperty.EL_BEAN_INSTANCE_MAP); + Map m = (Map)getLanguageContext().getLanguageProperty(X4OLanguageProperty.EL_BEAN_INSTANCE_MAP); if (m==null) { return; } diff --git a/x4o-core/src/main/java/org/x4o/xml/eld/lang/StringSplitConverterBindingHandler.java b/x4o-core/src/main/java/org/x4o/xml/eld/lang/StringSplitConverterBindingHandler.java index ad3ac62..393fdb6 100644 --- a/x4o-core/src/main/java/org/x4o/xml/eld/lang/StringSplitConverterBindingHandler.java +++ b/x4o-core/src/main/java/org/x4o/xml/eld/lang/StringSplitConverterBindingHandler.java @@ -35,7 +35,7 @@ import org.x4o.xml.element.ElementBindingHandlerException; * @author Willem Cazander * @version 1.0 Aug 21, 2012 */ -public class StringSplitConverterBindingHandler extends AbstractElementBindingHandler { +public class StringSplitConverterBindingHandler extends AbstractElementBindingHandler { private final static Class[] CLASSES_CHILD = new Class[] { @@ -57,12 +57,21 @@ public class StringSplitConverterBindingHandler extends AbstractElementBindingHa } /** - * @see org.x4o.xml.element.ElementBindingHandler#doBind(java.lang.Object, java.lang.Object, org.x4o.xml.element.Element) + * @see org.x4o.xml.element.ElementBindingHandler#bindChild(org.x4o.xml.element.Element, java.lang.Object, java.lang.Object) */ - public void doBind(Object parentObject, Object childObject,Element childElement) throws ElementBindingHandlerException { - StringSplitConverter parent = (StringSplitConverter)parentObject; + public void bindChild(Element childElement,StringSplitConverter parent, Object childObject) throws ElementBindingHandlerException { if (childObject instanceof StringSplitConverterStep) { parent.addStringSplitConverterStep((StringSplitConverterStep)childObject); } } + + /** + * @see org.x4o.xml.element.AbstractElementBindingHandler#createChilderen(org.x4o.xml.element.Element, java.lang.Object) + */ + @Override + public void createChilderen(Element parentElement,StringSplitConverter parentObject) throws ElementBindingHandlerException { + for (StringSplitConverterStep child:parentObject.getStringSplitConverterSteps()) { + createChild(parentElement, child); + } + } } diff --git a/x4o-core/src/main/java/org/x4o/xml/eld/lang/StringSplitConverterStepBindingHandler.java b/x4o-core/src/main/java/org/x4o/xml/eld/lang/StringSplitConverterStepBindingHandler.java index 4b9e03d..c08e452 100644 --- a/x4o-core/src/main/java/org/x4o/xml/eld/lang/StringSplitConverterStepBindingHandler.java +++ b/x4o-core/src/main/java/org/x4o/xml/eld/lang/StringSplitConverterStepBindingHandler.java @@ -35,7 +35,7 @@ import org.x4o.xml.element.ElementBindingHandlerException; * @author Willem Cazander * @version 1.0 Aug 21, 2012 */ -public class StringSplitConverterStepBindingHandler extends AbstractElementBindingHandler { +public class StringSplitConverterStepBindingHandler extends AbstractElementBindingHandler { private final static Class[] CLASSES_CHILD = new Class[] { @@ -57,12 +57,19 @@ public class StringSplitConverterStepBindingHandler extends AbstractElementBindi } /** - * @see org.x4o.xml.element.ElementBindingHandler#doBind(java.lang.Object, java.lang.Object, org.x4o.xml.element.Element) + * @see org.x4o.xml.element.ElementBindingHandler#bindChild(org.x4o.xml.element.Element, java.lang.Object, java.lang.Object) */ - public void doBind(Object parentObject, Object childObject,Element childElement) throws ElementBindingHandlerException { - StringSplitConverterStep parent = (StringSplitConverterStep)parentObject; + public void bindChild(Element childElement,StringSplitConverterStep parent, Object childObject) throws ElementBindingHandlerException { if (childObject instanceof ObjectConverter) { parent.setObjectConverter((ObjectConverter)childObject); } } + + /** + * @see org.x4o.xml.element.AbstractElementBindingHandler#createChilderen(org.x4o.xml.element.Element, java.lang.Object) + */ + @Override + public void createChilderen(Element parentElement,StringSplitConverterStep parentObject) throws ElementBindingHandlerException { + createChild(parentElement, parentObject.getObjectConverter()); + } } diff --git a/x4o-core/src/main/java/org/x4o/xml/element/AbstractElement.java b/x4o-core/src/main/java/org/x4o/xml/element/AbstractElement.java index 0c18615..26e5641 100644 --- a/x4o-core/src/main/java/org/x4o/xml/element/AbstractElement.java +++ b/x4o-core/src/main/java/org/x4o/xml/element/AbstractElement.java @@ -122,9 +122,9 @@ public abstract class AbstractElement implements Element { } /** - * @see Element#getElementLanguage() + * @see Element#getLanguageContext() */ - public X4OLanguageContext getElementLanguage() { + public X4OLanguageContext getLanguageContext() { return elementLanguage; } @@ -133,7 +133,7 @@ public abstract class AbstractElement implements Element { */ public void doCharacters(String characters) throws ElementException { try { - Element e = (Element)X4OLanguageClassLoader.newInstance(getElementLanguage().getLanguage().getLanguageConfiguration().getDefaultElementBodyCharacters()); + Element e = (Element)X4OLanguageClassLoader.newInstance(getLanguageContext().getLanguage().getLanguageConfiguration().getDefaultElementBodyCharacters()); e.setElementObject(characters); addChild(e); } catch (Exception exception) { @@ -146,7 +146,7 @@ public abstract class AbstractElement implements Element { */ public void doComment(String comment) throws ElementException { try { - Element e = (Element)X4OLanguageClassLoader.newInstance(getElementLanguage().getLanguage().getLanguageConfiguration().getDefaultElementBodyComment()); + Element e = (Element)X4OLanguageClassLoader.newInstance(getLanguageContext().getLanguage().getLanguageConfiguration().getDefaultElementBodyComment()); e.setElementObject(comment); addChild(e); } catch (Exception exception) { @@ -159,7 +159,7 @@ public abstract class AbstractElement implements Element { */ public void doIgnorableWhitespace(String space) throws ElementException { try { - Element e = (Element)X4OLanguageClassLoader.newInstance(getElementLanguage().getLanguage().getLanguageConfiguration().getDefaultElementBodyWhitespace()); + Element e = (Element)X4OLanguageClassLoader.newInstance(getLanguageContext().getLanguage().getLanguageConfiguration().getDefaultElementBodyWhitespace()); e.setElementObject(space); addChild(e); } catch (Exception exception) { diff --git a/x4o-core/src/main/java/org/x4o/xml/element/AbstractElementBindingHandler.java b/x4o-core/src/main/java/org/x4o/xml/element/AbstractElementBindingHandler.java index d036530..f05223a 100644 --- a/x4o-core/src/main/java/org/x4o/xml/element/AbstractElementBindingHandler.java +++ b/x4o-core/src/main/java/org/x4o/xml/element/AbstractElementBindingHandler.java @@ -23,6 +23,7 @@ package org.x4o.xml.element; + /** * An AbstractElementBindingHandler.
* Does nothing. @@ -30,6 +31,35 @@ package org.x4o.xml.element; * @author Willem Cazander * @version 1.0 Apr 16, 2006 */ -public abstract class AbstractElementBindingHandler extends AbstractElementMetaBase implements ElementBindingHandler { +public abstract class AbstractElementBindingHandler extends AbstractElementMetaBase implements ElementBindingHandler { + abstract public void bindChild(Element childElement,T parentObject,Object childObject) throws ElementBindingHandlerException; + + abstract public void createChilderen(Element parentElement,T parentObject) throws ElementBindingHandlerException; + + @SuppressWarnings("unchecked") + public void bindChild(Element childElement) throws ElementBindingHandlerException { + bindChild(childElement,(T)childElement.getParent().getElementObject(), childElement.getElementObject()); + } + + @SuppressWarnings("unchecked") + public void createChilderen(Element parentElement) throws ElementBindingHandlerException { + createChilderen(parentElement,(T)parentElement.getElementObject()); + } + + protected void createChild(Element parentElement,Object childObject) { + if (childObject==null) { + return; + } + if (parentElement==null) { + throw new NullPointerException("Can't create child with null parent."); + } + Element childElement = parentElement.getLanguageContext().getLanguage().createElementInstance(parentElement.getLanguageContext(), childObject.getClass()); + if (childElement==null) { + throw new NullPointerException("Could not find Element for child: "+childObject.getClass()); + } + childElement.setElementObject(childObject); + childElement.setParent(parentElement); + parentElement.addChild(childElement); + } } diff --git a/x4o-core/src/main/java/org/x4o/xml/element/DefaultElementAttributeValueParser.java b/x4o-core/src/main/java/org/x4o/xml/element/DefaultElementAttributeValueParser.java index bb203d9..bacb195 100644 --- a/x4o-core/src/main/java/org/x4o/xml/element/DefaultElementAttributeValueParser.java +++ b/x4o-core/src/main/java/org/x4o/xml/element/DefaultElementAttributeValueParser.java @@ -83,7 +83,7 @@ public class DefaultElementAttributeValueParser implements ElementAttributeValue if (element.getElementObject()==null) { return value; } - for (ElementInterface ei:element.getElementLanguage().getLanguage().findElementInterfaces(element.getElementObject())) { + for (ElementInterface ei:element.getLanguageContext().getLanguage().findElementInterfaces(element.getElementObject())) { logger.finer("Found interface match executing converter."); for (ElementClassAttribute attrClass:ei.getElementClassAttributes()) { if (name.equals(attrClass.getName())==false) { @@ -107,8 +107,8 @@ public class DefaultElementAttributeValueParser implements ElementAttributeValue * @see org.x4o.xml.element.ElementAttributeValueParser#getELParameterValue(java.lang.String, org.x4o.xml.element.Element) */ public Object getELParameterValue(String value, Element element) throws ElementAttributeValueParserException { - ValueExpression e = element.getElementLanguage().getExpressionLanguageFactory().createValueExpression(element.getElementLanguage().getExpressionLanguageContext(), (String)value,Object.class); - return e.getValue(element.getElementLanguage().getExpressionLanguageContext()); + ValueExpression e = element.getLanguageContext().getExpressionLanguageFactory().createValueExpression(element.getLanguageContext().getExpressionLanguageContext(), (String)value,Object.class); + return e.getValue(element.getLanguageContext().getExpressionLanguageContext()); } /** @@ -130,7 +130,7 @@ public class DefaultElementAttributeValueParser implements ElementAttributeValue return false; } - for (ElementInterface ei:element.getElementLanguage().getLanguage().findElementInterfaces(element.getElementObject())) { + for (ElementInterface ei:element.getLanguageContext().getLanguage().findElementInterfaces(element.getElementObject())) { logger.finest("Found interface match checking disables el parameters."); attr = ei.getElementClassAttributeByName(name); diff --git a/x4o-core/src/main/java/org/x4o/xml/element/DefaultElementBodyCharacters.java b/x4o-core/src/main/java/org/x4o/xml/element/DefaultElementBodyCharacters.java index c8ab4a2..88f2752 100644 --- a/x4o-core/src/main/java/org/x4o/xml/element/DefaultElementBodyCharacters.java +++ b/x4o-core/src/main/java/org/x4o/xml/element/DefaultElementBodyCharacters.java @@ -125,9 +125,9 @@ public class DefaultElementBodyCharacters implements Element { } /** - * @see org.x4o.xml.element.Element#getElementLanguage() + * @see org.x4o.xml.element.Element#getLanguageContext() */ - public X4OLanguageContext getElementLanguage() { + public X4OLanguageContext getLanguageContext() { return null; } diff --git a/x4o-core/src/main/java/org/x4o/xml/element/Element.java b/x4o-core/src/main/java/org/x4o/xml/element/Element.java index 6f0d62b..4cdbf76 100644 --- a/x4o-core/src/main/java/org/x4o/xml/element/Element.java +++ b/x4o-core/src/main/java/org/x4o/xml/element/Element.java @@ -142,7 +142,7 @@ public interface Element { * Gets the ElementLanguage. * @return Returns the ElementLanguage. */ - X4OLanguageContext getElementLanguage(); + X4OLanguageContext getLanguageContext(); /** * Sets the body texts on an event based system. diff --git a/x4o-core/src/main/java/org/x4o/xml/element/ElementBindingHandler.java b/x4o-core/src/main/java/org/x4o/xml/element/ElementBindingHandler.java index e0737ab..df874ef 100644 --- a/x4o-core/src/main/java/org/x4o/xml/element/ElementBindingHandler.java +++ b/x4o-core/src/main/java/org/x4o/xml/element/ElementBindingHandler.java @@ -23,6 +23,7 @@ package org.x4o.xml.element; + /** * Bind ElementObjects together. * @@ -53,5 +54,7 @@ public interface ElementBindingHandler extends ElementMetaBase { * @param childElement The child element to bind to the parent.' * @throws ElementBindingHandlerException When binding could not happen. */ - void doBind(Object parentObject,Object childObject,Element childElement) throws ElementBindingHandlerException; + void bindChild(Element childElement) throws ElementBindingHandlerException; + + void createChilderen(Element parentElement) throws ElementBindingHandlerException; } diff --git a/x4o-core/src/main/java/org/x4o/xml/io/AbstractX4OWriter.java b/x4o-core/src/main/java/org/x4o/xml/io/AbstractX4OWriter.java index 9bcc45c..6fca4f7 100644 --- a/x4o-core/src/main/java/org/x4o/xml/io/AbstractX4OWriter.java +++ b/x4o-core/src/main/java/org/x4o/xml/io/AbstractX4OWriter.java @@ -31,6 +31,7 @@ import java.io.OutputStream; import javax.xml.parsers.ParserConfigurationException; +import org.x4o.xml.element.Element; import org.x4o.xml.lang.X4OLanguageContext; import org.x4o.xml.lang.X4OLanguagePropertyKeys; import org.xml.sax.SAXException; @@ -50,7 +51,16 @@ public abstract class AbstractX4OWriter extends AbstractX4OWriterContext i public void write(T object,OutputStream output) throws ParserConfigurationException, SAXException, IOException { X4OLanguageContext context = getLanguageContext(); - context.getRootElement().setElementObject(object); //TODO: check ?? + Element rootElement = null; + try { + rootElement = (Element)context.getLanguage().getLanguageConfiguration().getDefaultElement().newInstance(); + } catch (InstantiationException e) { + throw new SAXException(e); + } catch (IllegalAccessException e) { + throw new SAXException(e); + } + rootElement.setElementObject(object); + context.setRootElement(rootElement); writeContext(context,output); } @@ -65,9 +75,6 @@ public abstract class AbstractX4OWriter extends AbstractX4OWriterContext i if (file==null) { throw new NullPointerException("Can't read null file."); } - if (file.exists()) { - throw new FileNotFoundException("File does already exists; "+file); - } if (file.canWrite()==false) { throw new IOException("Can't write file: "+file); } diff --git a/x4o-core/src/main/java/org/x4o/xml/io/DefaultX4ODriver.java b/x4o-core/src/main/java/org/x4o/xml/io/DefaultX4ODriver.java index f94d481..07d7845 100644 --- a/x4o-core/src/main/java/org/x4o/xml/io/DefaultX4ODriver.java +++ b/x4o-core/src/main/java/org/x4o/xml/io/DefaultX4ODriver.java @@ -24,10 +24,6 @@ package org.x4o.xml.io; import org.x4o.xml.X4ODriver; -import org.x4o.xml.lang.DefaultX4OLanguage; -import org.x4o.xml.lang.DefaultX4OLanguageConfiguration; -import org.x4o.xml.lang.X4OLanguage; -import org.x4o.xml.lang.phase.X4OPhaseManagerFactory; public class DefaultX4ODriver extends X4ODriver { @@ -49,11 +45,6 @@ public class DefaultX4ODriver extends X4ODriver { return languageName; } - @Override - public X4OLanguage buildLanguage(String version) { - return new DefaultX4OLanguage(new DefaultX4OLanguageConfiguration(),X4OPhaseManagerFactory.createDefaultX4OPhaseManager(),getLanguageName(),languageVersion); - } - @Override public String[] getLanguageVersions() { return new String[]{languageVersion}; diff --git a/x4o-core/src/main/java/org/x4o/xml/io/DefaultX4OWriter.java b/x4o-core/src/main/java/org/x4o/xml/io/DefaultX4OWriter.java index 37da7e4..8315595 100644 --- a/x4o-core/src/main/java/org/x4o/xml/io/DefaultX4OWriter.java +++ b/x4o-core/src/main/java/org/x4o/xml/io/DefaultX4OWriter.java @@ -26,11 +26,29 @@ package org.x4o.xml.io; import java.io.FileNotFoundException; import java.io.IOException; import java.io.OutputStream; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import javax.xml.parsers.ParserConfigurationException; +import org.x4o.xml.element.Element; +import org.x4o.xml.element.ElementBindingHandler; +import org.x4o.xml.element.ElementBindingHandlerException; +import org.x4o.xml.element.ElementClass; +import org.x4o.xml.element.ElementClassAttribute; +import org.x4o.xml.element.ElementNamespaceContext; +import org.x4o.xml.element.ElementNamespaceInstanceProviderException; +import org.x4o.xml.element.ElementObjectPropertyValueException; +import org.x4o.xml.io.sax.XMLWriter; +import org.x4o.xml.lang.X4OLanguage; import org.x4o.xml.lang.X4OLanguageContext; +import org.x4o.xml.lang.X4OLanguageModule; import org.xml.sax.SAXException; +import org.xml.sax.helpers.AttributesImpl; public class DefaultX4OWriter extends AbstractX4OWriter { @@ -42,7 +60,165 @@ public class DefaultX4OWriter extends AbstractX4OWriter { FileNotFoundException, SecurityException, NullPointerException, SAXException, IOException { -// getLanguageContext().setRootElement(element) + Element root = getLanguageContext().getRootElement(); + if (root.getElementClass()==null) { + try { + root = fillElementTree(root.getElementObject()); + } catch (ElementNamespaceInstanceProviderException e) { + throw new SAXException(e); + } catch (ElementBindingHandlerException e) { + throw new SAXException(e); + } + } + + XMLWriter writer = new XMLWriter(out); + writer.startDocument(); + + Map prefixes = new HashMap(); + startPrefixTree(root,prefixes); + for (String uri:prefixes.keySet()) { + String prefix = prefixes.get(uri); + writer.startPrefixMapping(prefix, uri); + } + + try { + writeTree(writer,root); + } catch (ElementObjectPropertyValueException e) { + throw new SAXException(e); + } + writer.endDocument(); + out.flush(); + out.close(); } + private void startPrefixTree(Element element,Map result) throws SAXException { + String elementUri = findElementUri(element); + if (result.containsKey(elementUri)==false) { + String elementUriPrefix = findNamespacePrefix(element,elementUri); + result.put(elementUri, elementUriPrefix); + } + for (Element e:element.getChilderen()) { + startPrefixTree(e,result); + } + } + + private List getProperties(Class objectClass) { + List result = new ArrayList(); + for (Method m:objectClass.getMethods()) { + Class[] types = m.getParameterTypes(); + if (types.length != 0) { + continue; + } + if (m.getName().equals("getClass")) { + continue; + } + if (m.getName().startsWith("get")==false) { + continue; + } + String name = m.getName().substring(3,4).toLowerCase()+m.getName().substring(4); + result.add(name); + + } + return result; + } + + private void writeTree(XMLWriter writer,Element element) throws SAXException, ElementObjectPropertyValueException { + AttributesImpl atts = new AttributesImpl(); + + if (element.getElementClass().getAutoAttributes()!=null && element.getElementClass().getAutoAttributes()==false) { + for (ElementClassAttribute eca:element.getElementClass().getElementClassAttributes()) { + Object value = element.getLanguageContext().getElementObjectPropertyValue().getProperty(element.getElementObject(),eca.getName()); + if (value==null) { + continue; + } + atts.addAttribute ("", eca.getName(), "", "", ""+value); + } + + } else { + for (String p:getProperties(element.getElementObject().getClass())) { + Object value = element.getLanguageContext().getElementObjectPropertyValue().getProperty(element.getElementObject(),p); + if (value==null) { + continue; + } + if (value instanceof List || value instanceof Collection) { + continue; // TODO; filter on type of childeren + } + atts.addAttribute ("", p, "", "", ""+value); + } + } + + String elementUri = findElementUri(element); + writer.startElement(elementUri, element.getElementClass().getTag(), "", atts); + for (Element e:element.getChilderen()) { + writeTree(writer,e); + } + writer.endElement(elementUri, element.getElementClass().getTag(), ""); + } + + private String findElementUri(Element e) { + for (X4OLanguageModule mod:e.getLanguageContext().getLanguage().getLanguageModules()) { + for (ElementNamespaceContext c:mod.getElementNamespaceContexts()) { + ElementClass ec = c.getElementClass(e.getElementClass().getTag()); + if (ec!=null) { + return c.getUri(); + } + } + } + return null; + } + + private String findNamespacePrefix(Element e,String uri) { + ElementNamespaceContext ns = e.getLanguageContext().getLanguage().findElementNamespaceContext(uri); + if (ns.getPrefixMapping()!=null) { + return ns.getPrefixMapping(); + } + return ns.getId(); + } + + private Element fillElementTree(Object object) throws ElementNamespaceInstanceProviderException, ElementBindingHandlerException { + Element element = findRootElement(object.getClass()); + element.setElementObject(object); + + for (ElementBindingHandler bind:getLanguageContext().getLanguage().findElementBindingHandlers(object)) { + bind.createChilderen(element); + fillTree(element); + } + return element; + } + + private void fillTree(Element element) throws ElementNamespaceInstanceProviderException, ElementBindingHandlerException { + for (Element e:element.getChilderen()) { + Object object = e.getElementObject(); + for (ElementBindingHandler bind:getLanguageContext().getLanguage().findElementBindingHandlers(object)) { + bind.createChilderen(e); + fillTree(e); + } + } + } + + + private Element findRootElement(Class objectClass) throws ElementNamespaceInstanceProviderException { + // redo this mess, add nice find for root + for (X4OLanguageModule modContext:getLanguageContext().getLanguage().getLanguageModules()) { + for (ElementNamespaceContext nsContext:modContext.getElementNamespaceContexts()) { + if (nsContext.getLanguageRoot()!=null && nsContext.getLanguageRoot()) { + for (ElementClass ec:nsContext.getElementClasses()) { + if (ec.getObjectClass()!=null && ec.getObjectClass().equals(objectClass)) { + return nsContext.getElementNamespaceInstanceProvider().createElementInstance(getLanguageContext(), ec.getTag()); + } + } + } + } + } + for (X4OLanguageModule modContext:getLanguageContext().getLanguage().getLanguageModules()) { + for (ElementNamespaceContext nsContext:modContext.getElementNamespaceContexts()) { + for (ElementClass ec:nsContext.getElementClasses()) { + if (ec.getObjectClass()!=null && ec.getObjectClass().equals(objectClass)) { + return nsContext.getElementNamespaceInstanceProvider().createElementInstance(getLanguageContext(), ec.getTag()); + } + } + } + } + throw new IllegalArgumentException("Could not find ElementClass for: "+objectClass.getName()); + } } diff --git a/x4o-core/src/main/java/org/x4o/xml/io/XMLConstants.java b/x4o-core/src/main/java/org/x4o/xml/io/XMLConstants.java index 77384e2..d42c6ff 100644 --- a/x4o-core/src/main/java/org/x4o/xml/io/XMLConstants.java +++ b/x4o-core/src/main/java/org/x4o/xml/io/XMLConstants.java @@ -222,7 +222,7 @@ public final class XMLConstants { result.append("'"); continue; } - if (isNameChar(c)==false) { + if (/*isNameChar(c)*/true==false) {// TODO: add correct result.append("#x"); result.append(Integer.toHexString(c)); result.append(";"); diff --git a/x4o-core/src/main/java/org/x4o/xml/io/sax/X4ODebugWriter.java b/x4o-core/src/main/java/org/x4o/xml/io/sax/X4ODebugWriter.java index 6a03367..71977e0 100644 --- a/x4o-core/src/main/java/org/x4o/xml/io/sax/X4ODebugWriter.java +++ b/x4o-core/src/main/java/org/x4o/xml/io/sax/X4ODebugWriter.java @@ -428,7 +428,7 @@ public class X4ODebugWriter { atts.addAttribute ("", "language", "", "", elementLanguage.getLanguage().getLanguageName()); atts.addAttribute ("", "languageVersion", "", "", elementLanguage.getLanguage().getLanguageVersion()); atts.addAttribute ("", "className", "", "", elementLanguage.getClass().getName()+""); - atts.addAttribute ("", "currentX4OPhase", "", "", elementLanguage.getCurrentX4OPhase().getId()); + atts.addAttribute ("", "currentX4OPhase", "", "", elementLanguage.getCurrentPhase().getId()); debugWriter.startElement (DEBUG_URI, "printElementLanguage", "", atts); debugWriter.endElement(DEBUG_URI, "printElementLanguage", ""); } diff --git a/x4o-core/src/main/java/org/x4o/xml/lang/AbstractX4OLanguageContext.java b/x4o-core/src/main/java/org/x4o/xml/lang/AbstractX4OLanguageContext.java index d53fe3e..0b16899 100644 --- a/x4o-core/src/main/java/org/x4o/xml/lang/AbstractX4OLanguageContext.java +++ b/x4o-core/src/main/java/org/x4o/xml/lang/AbstractX4OLanguageContext.java @@ -59,23 +59,17 @@ public abstract class AbstractX4OLanguageContext implements X4OLanguageContextLo /** * Creates a new empty ElementLanguage. */ - public AbstractX4OLanguageContext(X4OLanguage language,String languageVersion) { + public AbstractX4OLanguageContext(X4OLanguage language) { if (language==null) { throw new NullPointerException("language may not be null"); } - if (languageVersion==null) { - throw new NullPointerException("languageVersion may not be null"); - } - if (languageVersion.length()==0) { - throw new IllegalArgumentException("languageVersion may not be empty"); - } logger = Logger.getLogger(AbstractX4OLanguageContext.class.getName()); logger.finest("Creating new ParsingContext"); this.language=language; dirtyElements = new HashMap(40); languageProperties = new HashMap(20); languageProperties.put(X4OLanguageProperty.LANGUAGE_NAME.toUri(), language.getLanguageName()); - languageProperties.put(X4OLanguageProperty.LANGUAGE_VERSION.toUri(), languageVersion); + languageProperties.put(X4OLanguageProperty.LANGUAGE_VERSION.toUri(), language.getLanguageVersion()); } public X4OLanguage getLanguage() { @@ -151,16 +145,16 @@ public abstract class AbstractX4OLanguageContext implements X4OLanguageContextLo } /** - * @see org.x4o.xml.lang.X4OLanguageContext#getCurrentX4OPhase() + * @see org.x4o.xml.lang.X4OLanguageContext#getCurrentPhase() */ - public X4OPhase getCurrentX4OPhase() { + public X4OPhase getCurrentPhase() { return currentX4OPhase; } /** - * @see org.x4o.xml.lang.X4OLanguageContext#setCurrentX4OPhase(org.x4o.xml.lang.phase.X4OPhase) + * @see org.x4o.xml.lang.X4OLanguageContext#setCurrentPhase(org.x4o.xml.lang.phase.X4OPhase) */ - public void setCurrentX4OPhase(X4OPhase currentX4OPhase) { + public void setCurrentPhase(X4OPhase currentX4OPhase) { this.currentX4OPhase = currentX4OPhase; } diff --git a/x4o-core/src/main/java/org/x4o/xml/lang/DefaultX4OLanguage.java b/x4o-core/src/main/java/org/x4o/xml/lang/DefaultX4OLanguage.java index b38ca99..e9c90ba 100644 --- a/x4o-core/src/main/java/org/x4o/xml/lang/DefaultX4OLanguage.java +++ b/x4o-core/src/main/java/org/x4o/xml/lang/DefaultX4OLanguage.java @@ -4,10 +4,21 @@ import java.util.ArrayList; import java.util.List; import java.util.logging.Logger; +import javax.el.ELContext; + +import org.x4o.xml.X4ODriver; +import org.x4o.xml.el.X4OExpressionFactory; +import org.x4o.xml.element.Element; +import org.x4o.xml.element.ElementAttributeValueParser; import org.x4o.xml.element.ElementBindingHandler; +import org.x4o.xml.element.ElementClass; import org.x4o.xml.element.ElementInterface; import org.x4o.xml.element.ElementNamespaceContext; +import org.x4o.xml.element.ElementNamespaceInstanceProviderException; +import org.x4o.xml.element.ElementObjectPropertyValue; +import org.x4o.xml.lang.phase.X4OPhaseException; import org.x4o.xml.lang.phase.X4OPhaseManager; +import org.x4o.xml.lang.phase.X4OPhaseType; public class DefaultX4OLanguage implements X4OLanguageLocal { @@ -19,6 +30,12 @@ public class DefaultX4OLanguage implements X4OLanguageLocal { private X4OPhaseManager phaseManager = null; public DefaultX4OLanguage(X4OLanguageConfiguration languageConfiguration,X4OPhaseManager phaseManager,String languageName,String languageVersion) { + if (languageName==null) { + throw new NullPointerException("Can't define myself with null name."); + } + if (languageVersion==null) { + throw new NullPointerException("Can't define myself with null version."); + } logger = Logger.getLogger(DefaultX4OLanguage.class.getName()); elementLanguageModules = new ArrayList(20); this.languageConfiguration=languageConfiguration; @@ -55,13 +72,6 @@ public class DefaultX4OLanguage implements X4OLanguageLocal { return languageConfiguration; } - /* - * @param languageConfiguration the languageConfiguration to set - - public void setLanguageConfiguration() { - this.languageConfiguration = languageConfiguration; - }*/ - /** * @see org.x4o.xml.lang.X4OLanguageContext#addLanguageModule(org.x4o.xml.lang.X4OLanguageModule) */ @@ -79,6 +89,82 @@ public class DefaultX4OLanguage implements X4OLanguageLocal { return elementLanguageModules; } + /** + * @throws X4OPhaseException + * @see org.x4o.xml.lang.X4OLanguage#createLanguageContext(org.x4o.xml.X4ODriver) + */ + public X4OLanguageContext createLanguageContext(X4ODriver driver){ + X4OLanguageContext result = buildElementLanguage(new DefaultX4OLanguageContext(this),driver); + try { + getPhaseManager().runPhases(result, X4OPhaseType.INIT); + } catch (X4OPhaseException e) { + throw new RuntimeException(e); //TODO: change layer + } + return result; + } + + protected X4OLanguageContext buildElementLanguage(X4OLanguageContext languageContext,X4ODriver driver) { + if ((languageContext instanceof X4OLanguageContextLocal)==false) { + throw new RuntimeException("Can't init X4OLanguageContext which has not X4OLanguageContextLocal interface obj: "+languageContext); + } + X4OLanguageContextLocal contextInit = (X4OLanguageContextLocal)languageContext; + for (String key:driver.getGlobalPropertyKeys()) { + Object value = driver.getGlobalProperty(key); + contextInit.setLanguageProperty(key, value); + } + try { + if (contextInit.getExpressionLanguageFactory()==null) { + contextInit.setExpressionLanguageFactory(X4OExpressionFactory.createExpressionFactory(contextInit)); + } + if (contextInit.getExpressionLanguageContext()==null) { + contextInit.setExpressionLanguageContext((ELContext)X4OLanguageClassLoader.newInstance(getLanguageConfiguration().getDefaultExpressionLanguageContext())); + } + if (contextInit.getElementAttributeValueParser()==null) { + contextInit.setElementAttributeValueParser((ElementAttributeValueParser)X4OLanguageClassLoader.newInstance(getLanguageConfiguration().getDefaultElementAttributeValueParser())); + } + if (contextInit.getElementObjectPropertyValue()==null) { + contextInit.setElementObjectPropertyValue((ElementObjectPropertyValue)X4OLanguageClassLoader.newInstance(getLanguageConfiguration().getDefaultElementObjectPropertyValue())); + } + } catch (Exception e) { + throw new RuntimeException(e.getMessage(),e); + } + return contextInit; + } + + /** + * @see org.x4o.xml.lang.X4OLanguage#createElementInstance(java.lang.Class) + */ + public Element createElementInstance(X4OLanguageContext context,Class objectClass) { + for (X4OLanguageModule modContext:getLanguageModules()) { + for (ElementNamespaceContext nsContext:modContext.getElementNamespaceContexts()) { + for (ElementClass ec:nsContext.getElementClasses()) { + if (ec.getObjectClass()!=null && ec.getObjectClass().equals(objectClass)) { + try { + return nsContext.getElementNamespaceInstanceProvider().createElementInstance(context, ec.getTag()); + } catch (ElementNamespaceInstanceProviderException e) { + throw new RuntimeException(e.getMessage(),e); // TODO: fix me + } + } + } + } + } + throw new IllegalArgumentException("Could not find ElementClass for: "+objectClass.getName()); + } + + /** + * @see org.x4o.xml.lang.X4OLanguageContext#findElementBindingHandlers(java.lang.Object) + */ + public List findElementBindingHandlers(Object parent) { + List result = new ArrayList(50); + for (int i=0;i result = new ArrayList(50); for (int i=0;i result,List checkList) { + private void findElementBindingHandlerInList(Object parent,Object child,List result,List checkList,boolean checkChild) { for (ElementBindingHandler binding:checkList) { boolean parentBind = false; if (parent instanceof Class) { @@ -106,6 +192,10 @@ public class DefaultX4OLanguage implements X4OLanguageLocal { if (parentBind==false) { continue; } + if (checkChild==false) { + result.add(binding); // All all handlers for parent only + continue; + } boolean childBind = false; for (Class childClass:binding.getBindChildClasses()) { if (child instanceof Class && childClass.isAssignableFrom((Class)child)) { diff --git a/x4o-core/src/main/java/org/x4o/xml/lang/DefaultX4OLanguageConfiguration.java b/x4o-core/src/main/java/org/x4o/xml/lang/DefaultX4OLanguageConfiguration.java index 3054462..a09ce06 100644 --- a/x4o-core/src/main/java/org/x4o/xml/lang/DefaultX4OLanguageConfiguration.java +++ b/x4o-core/src/main/java/org/x4o/xml/lang/DefaultX4OLanguageConfiguration.java @@ -28,9 +28,6 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import javax.el.ExpressionFactory; - -import org.x4o.xml.X4ODriver; import org.x4o.xml.el.X4OELContext; import org.x4o.xml.eld.EldDriver; import org.x4o.xml.element.DefaultElement; @@ -45,9 +42,6 @@ import org.x4o.xml.element.DefaultElementNamespaceContext; import org.x4o.xml.element.DefaultElementNamespaceInstanceProvider; import org.x4o.xml.element.DefaultElementObjectPropertyValue; import org.x4o.xml.element.DefaultGlobalAttributeHandlerComparator; -import org.x4o.xml.element.ElementAttributeValueParser; -import org.x4o.xml.element.ElementObjectPropertyValue; - /** * Provides all implementions of the different parts of the language parser. @@ -168,73 +162,22 @@ public class DefaultX4OLanguageConfiguration implements X4OLanguageConfiguration /** * @see org.x4o.xml.lang.X4OLanguageConfiguration#getDefaultX4OLanguageVersionFilter() */ - public Class getDefaultX4OLanguageVersionFilter() { + public Class getDefaultLanguageVersionFilter() { return DefaultX4OLanguageVersionFilter.class; } /** * @see org.x4o.xml.lang.X4OLanguageConfiguration#getDefaultX4OLanguageLoader() */ - public Class getDefaultX4OLanguageLoader() { + public Class getDefaultLanguageLoader() { return DefaultX4OLanguageLoader.class; } /** - * @see org.x4o.xml.lang.X4OLanguageConfiguration#createElementLanguage() + * @see org.x4o.xml.lang.X4OLanguageConfiguration#getDefaultExpressionLanguageContext() */ - public X4OLanguageContext createElementLanguage(X4ODriver driver) { - String v = X4ODriver.DEFAULT_LANGUAGE_VERSION; // TODO:fixme - return configElementLanguage(new DefaultX4OLanguageContext(driver.createLanguage(v),v),driver); - } - - protected X4OLanguageContext configElementLanguage(X4OLanguageContext elementLanguage,X4ODriver driver) { - if ((elementLanguage instanceof X4OLanguageContextLocal)==false) { - throw new RuntimeException("Can't init ElementLanguage which has not ElementLanguageLocal interface obj: "+elementLanguage); - } - X4OLanguageContextLocal contextInit = (X4OLanguageContextLocal)elementLanguage; - //contextInit.setLanguageConfiguration(this); - for (String key:driver.getGlobalPropertyKeys()) { - Object value = driver.getGlobalProperty(key); - contextInit.setLanguageProperty(key, value); - } - - if (contextInit.getExpressionLanguageFactory()==null) { - contextInit.setExpressionLanguageFactory(configExpressionFactory(contextInit)); - } - if (contextInit.getExpressionLanguageContext()==null) { - contextInit.setExpressionLanguageContext(new X4OELContext()); - } - try { - if (contextInit.getElementAttributeValueParser()==null) { - contextInit.setElementAttributeValueParser((ElementAttributeValueParser)X4OLanguageClassLoader.newInstance(getDefaultElementAttributeValueParser())); - } - if (contextInit.getElementObjectPropertyValue()==null) { - contextInit.setElementObjectPropertyValue((ElementObjectPropertyValue)X4OLanguageClassLoader.newInstance(getDefaultElementObjectPropertyValue())); - } - } catch (Exception e) { - throw new RuntimeException(e.getMessage(),e); - } - return elementLanguage; - } - - protected ExpressionFactory configExpressionFactory(X4OLanguageContext elementContext) { - ExpressionFactory factory = (ExpressionFactory)elementContext.getLanguageProperty(X4OLanguageProperty.EL_FACTORY_INSTANCE); - if (factory!=null) { - return factory; - } - try { - Class expressionFactoryClass = X4OLanguageClassLoader.loadClass("org.apache.el.ExpressionFactoryImpl"); - ExpressionFactory expressionFactory = (ExpressionFactory) expressionFactoryClass.newInstance(); - return expressionFactory; - } catch (Exception e) { - try { - Class expressionFactoryClass = X4OLanguageClassLoader.loadClass("de.odysseus.el.ExpressionFactoryImpl"); - ExpressionFactory expressionFactory = (ExpressionFactory) expressionFactoryClass.newInstance(); - return expressionFactory; - } catch (Exception ee) { - throw new RuntimeException("Could not load ExpressionFactory tried: org.apache.el.ExpressionFactoryImpl and de.odysseus.el.ExpressionFactoryImpl but could not load one of them."); - } - } + public Class getDefaultExpressionLanguageContext() { + return X4OELContext.class; } /** diff --git a/x4o-core/src/main/java/org/x4o/xml/lang/DefaultX4OLanguageContext.java b/x4o-core/src/main/java/org/x4o/xml/lang/DefaultX4OLanguageContext.java index 75bfa0e..b6aad4f 100644 --- a/x4o-core/src/main/java/org/x4o/xml/lang/DefaultX4OLanguageContext.java +++ b/x4o-core/src/main/java/org/x4o/xml/lang/DefaultX4OLanguageContext.java @@ -32,7 +32,7 @@ package org.x4o.xml.lang; */ public class DefaultX4OLanguageContext extends AbstractX4OLanguageContext { - public DefaultX4OLanguageContext(X4OLanguage language, String languageVersion) { - super(language, languageVersion); + public DefaultX4OLanguageContext(X4OLanguage language) { + super(language); } } diff --git a/x4o-core/src/main/java/org/x4o/xml/lang/DefaultX4OLanguageLoader.java b/x4o-core/src/main/java/org/x4o/xml/lang/DefaultX4OLanguageLoader.java index 0568be0..809b137 100644 --- a/x4o-core/src/main/java/org/x4o/xml/lang/DefaultX4OLanguageLoader.java +++ b/x4o-core/src/main/java/org/x4o/xml/lang/DefaultX4OLanguageLoader.java @@ -87,7 +87,7 @@ public class DefaultX4OLanguageLoader implements X4OLanguageLoader { logger.finer("Loading all modules for language: "+language); loadLanguageModules(languageLocal,language); - X4OLanguageVersionFilter lvf = (X4OLanguageVersionFilter)X4OLanguageClassLoader.newInstance(languageLocal.getLanguageConfiguration().getDefaultX4OLanguageVersionFilter()); + X4OLanguageVersionFilter lvf = (X4OLanguageVersionFilter)X4OLanguageClassLoader.newInstance(languageLocal.getLanguageConfiguration().getDefaultLanguageVersionFilter()); for (Map> map:modulesAll) { List versions = new ArrayList(map.keySet()); diff --git a/x4o-core/src/main/java/org/x4o/xml/lang/X4OLanguage.java b/x4o-core/src/main/java/org/x4o/xml/lang/X4OLanguage.java index 2566af7..e611ae5 100644 --- a/x4o-core/src/main/java/org/x4o/xml/lang/X4OLanguage.java +++ b/x4o-core/src/main/java/org/x4o/xml/lang/X4OLanguage.java @@ -25,6 +25,8 @@ package org.x4o.xml.lang; import java.util.List; +import org.x4o.xml.X4ODriver; +import org.x4o.xml.element.Element; import org.x4o.xml.element.ElementBindingHandler; import org.x4o.xml.element.ElementInterface; import org.x4o.xml.element.ElementNamespaceContext; @@ -58,9 +60,29 @@ public interface X4OLanguage { * @return the languageConfiguration. */ X4OLanguageConfiguration getLanguageConfiguration(); + + /** + * Creates and filles the inital element language used to store the language. + * @return The newly created ElementLanguage. + */ + X4OLanguageContext createLanguageContext(X4ODriver driver); + + /** + * Search language for object and create elememt for it. + * @param object The object to search for. + * @return Returns an new Elememt instance for the object. + */ + Element createElementInstance(X4OLanguageContext context,Class objectClass); /** - * Gets all ElementBindingHandlers. + * Gets all ElementBindingHandlers which are possible for parent. + * @param parent The parent element object or class to search for. + * @return Returns an List with all ElementBindingHandler for the search. + */ + List findElementBindingHandlers(Object parent); + + /** + * Gets all ElementBindingHandlers for parent and child combination. * @param parent The parent element object or class to search for. * @param child The parent element object or class to search for. * @return Returns an List with all ElementBindingHandler for the search pair. diff --git a/x4o-core/src/main/java/org/x4o/xml/lang/X4OLanguageConfiguration.java b/x4o-core/src/main/java/org/x4o/xml/lang/X4OLanguageConfiguration.java index c307fb8..12341ed 100644 --- a/x4o-core/src/main/java/org/x4o/xml/lang/X4OLanguageConfiguration.java +++ b/x4o-core/src/main/java/org/x4o/xml/lang/X4OLanguageConfiguration.java @@ -26,9 +26,6 @@ package org.x4o.xml.lang; import java.util.List; import java.util.Map; -import org.x4o.xml.X4ODriver; - - /** * X4OLanguageConfiguration first used interface in x4o parser which does the hard config of the x4o xml parsing. * @@ -73,18 +70,17 @@ public interface X4OLanguageConfiguration { /** * @return Returns the X4OLanguageVersionFilter which filters the best version to use. */ - Class getDefaultX4OLanguageVersionFilter(); + Class getDefaultLanguageVersionFilter(); /** * @return Returns the X4OLanguageLoader which loads languages into the element context. */ - Class getDefaultX4OLanguageLoader(); + Class getDefaultLanguageLoader(); /** - * Creates and filles the inital element language used to store the language. - * @return The newly created ElementLanguage. + * @return Returns the Expression Language Context which holds the el objects. */ - X4OLanguageContext createElementLanguage(X4ODriver driver); + Class getDefaultExpressionLanguageContext(); /** * @return Returns Map of SAX properties which are set. diff --git a/x4o-core/src/main/java/org/x4o/xml/lang/X4OLanguageContext.java b/x4o-core/src/main/java/org/x4o/xml/lang/X4OLanguageContext.java index a8e3d58..423b2e0 100644 --- a/x4o-core/src/main/java/org/x4o/xml/lang/X4OLanguageContext.java +++ b/x4o-core/src/main/java/org/x4o/xml/lang/X4OLanguageContext.java @@ -70,14 +70,7 @@ public interface X4OLanguageContext { * Returns the current X4OPhase of the parser. * @return Returns the current phase. */ - X4OPhase getCurrentX4OPhase(); - - /** - * Sets the phase of the context. - * TODO: Do never call this, methode sould be moved to local interface. - * @param phase The current phase to set. - */ - void setCurrentX4OPhase(X4OPhase phase); + X4OPhase getCurrentPhase(); /** * Marks an (new) Element as dirty and run the phases from this start phase. diff --git a/x4o-core/src/main/java/org/x4o/xml/lang/X4OLanguageContextLocal.java b/x4o-core/src/main/java/org/x4o/xml/lang/X4OLanguageContextLocal.java index 091748d..0f9a6d2 100644 --- a/x4o-core/src/main/java/org/x4o/xml/lang/X4OLanguageContextLocal.java +++ b/x4o-core/src/main/java/org/x4o/xml/lang/X4OLanguageContextLocal.java @@ -29,6 +29,7 @@ import javax.el.ExpressionFactory; import org.x4o.xml.element.ElementAttributeValueParser; import org.x4o.xml.element.ElementObjectPropertyValue; import org.x4o.xml.io.sax.X4ODebugWriter; +import org.x4o.xml.lang.phase.X4OPhase; /** * ElementLanguageLocal is the local set interface for ElementLanguage. @@ -60,6 +61,12 @@ public interface X4OLanguageContextLocal extends X4OLanguageContext { */ void setElementObjectPropertyValue(ElementObjectPropertyValue elementObjectPropertyValue); + /** + * Sets the phase of the context. + * @param phase The current phase to set. + */ + void setCurrentPhase(X4OPhase phase); + /** * @param debugWriter The debug writer to set */ diff --git a/x4o-core/src/main/java/org/x4o/xml/lang/phase/DefaultX4OPhaseManager.java b/x4o-core/src/main/java/org/x4o/xml/lang/phase/DefaultX4OPhaseManager.java index e97a13a..bf181d4 100644 --- a/x4o-core/src/main/java/org/x4o/xml/lang/phase/DefaultX4OPhaseManager.java +++ b/x4o-core/src/main/java/org/x4o/xml/lang/phase/DefaultX4OPhaseManager.java @@ -30,9 +30,9 @@ import java.util.List; import org.x4o.xml.element.Element; import org.x4o.xml.lang.X4OLanguageContext; +import org.x4o.xml.lang.X4OLanguageContextLocal; import org.x4o.xml.lang.X4OLanguageProperty; - /** * X4OPhaseManager stores the X4OPhaseHandler and puts them in the right order. * And will execute the phases when runPhases is called. @@ -44,11 +44,6 @@ public class DefaultX4OPhaseManager implements X4OPhaseManager { /** The X4OPhaseHandler */ private List x4oPhases = null; - //private ElementLanguage elementLanguage = null; - //private X4OPhase stopPhase = null; - //private boolean skipReleasePhase = false; - //private boolean skipRunPhase = false; - //private boolean skipSiblingsPhase = false; /** * Local package constructor. @@ -56,26 +51,24 @@ public class DefaultX4OPhaseManager implements X4OPhaseManager { */ public DefaultX4OPhaseManager() { x4oPhases = new ArrayList(25); - /* - if (elementLanguage==null) { - throw new NullPointerException("Can't manage phases with null elementLanguage in constuctor."); - } - x4oPhases = new ArrayList(15); - this.elementLanguage = elementLanguage; - - // Manual - skipReleasePhase = elementLanguage.getLanguagePropertyBoolean(X4OLanguageProperty.PHASE_SKIP_RELEASE); - skipRunPhase = elementLanguage.getLanguagePropertyBoolean(X4OLanguageProperty.PHASE_SKIP_RUN); - skipSiblingsPhase = elementLanguage.getLanguagePropertyBoolean(X4OLanguageProperty.PHASE_SKIP_SIBLINGS); - - // Check if we need to stop after a certain phase - Object stopPhaseObject = elementLanguage.getLanguageProperty(X4OLanguageProperty.PHASE_STOP_AFTER); - if (stopPhaseObject instanceof X4OPhase) { - stopPhase = (X4OPhase)stopPhaseObject; - } - */ } +/* + +PHASE_ORDER = { *startupX4OPhase, + *createLanguagePhase, + *createLanguageSiblingsPhase, + *parseSAXStreamPhase, + *configGlobalElBeansPhase, + *startX4OPhase, + configElementPhase,configElementInterfacePhase,configGlobalElementPhase, + configGlobalAttributePhase,runAttributesPhase,fillTemplatingPhase, + transformPhase,*runDirtyElementPhase,bindElementPhase, + *runPhase,runDirtyElementLastPhase, + *releasePhase + }; +* = runOnce +*/ /** * Adds an X4OPhaseHandler. * @param phase The X4OPhaseHandler to add. @@ -164,7 +157,7 @@ public class DefaultX4OPhaseManager implements X4OPhaseManager { } // debug output - languageContext.setCurrentX4OPhase(phase); + ((X4OLanguageContextLocal)languageContext).setCurrentPhase(phase); // run listeners for (X4OPhaseListener l:phase.getPhaseListeners()) { @@ -198,11 +191,10 @@ public class DefaultX4OPhaseManager implements X4OPhaseManager { * @throws X4OPhaseException When a running handlers throws one. */ public void runPhasesForElement(Element e,X4OPhaseType type,X4OPhase p) throws X4OPhaseException { - X4OLanguageContext languageContext = e.getElementLanguage(); + X4OLanguageContext languageContext = e.getLanguageContext(); boolean skipRunPhase = languageContext.getLanguagePropertyBoolean(X4OLanguageProperty.PHASE_SKIP_RUN); String stopPhase = languageContext.getLanguagePropertyString(X4OLanguageProperty.PHASE_STOP_AFTER); - // sort for the order List x4oPhasesOrder = getOrderedPhases(type); for (X4OPhase phase:x4oPhasesOrder) { @@ -217,7 +209,7 @@ public class DefaultX4OPhaseManager implements X4OPhaseManager { } // set phase - languageContext.setCurrentX4OPhase(phase); + ((X4OLanguageContextLocal)languageContext).setCurrentPhase(phase); // do the run interface phase.runPhase(languageContext); @@ -259,7 +251,7 @@ public class DefaultX4OPhaseManager implements X4OPhaseManager { } // set phase - languageContext.setCurrentX4OPhase(h); + ((X4OLanguageContextLocal)languageContext).setCurrentPhase(h); // do the run interface h.runPhase(languageContext); diff --git a/x4o-core/src/main/java/org/x4o/xml/lang/phase/X4OPhaseFactory.java b/x4o-core/src/main/java/org/x4o/xml/lang/phase/X4OPhaseFactory.java deleted file mode 100644 index 1ace5fc..0000000 --- a/x4o-core/src/main/java/org/x4o/xml/lang/phase/X4OPhaseFactory.java +++ /dev/null @@ -1,1110 +0,0 @@ -/* - * 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. - */ - -package org.x4o.xml.lang.phase; - -import java.io.InputStream; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.List; -import java.util.Map; -import java.util.logging.Logger; - -import javax.el.ValueExpression; - -import org.x4o.xml.conv.ObjectConverterException; - -import org.x4o.xml.element.Element; -import org.x4o.xml.element.ElementAttributeHandler; -import org.x4o.xml.element.ElementAttributeValueParser; -import org.x4o.xml.element.ElementBindingHandler; -import org.x4o.xml.element.ElementClass; -import org.x4o.xml.element.ElementClassAttribute; -import org.x4o.xml.element.ElementConfigurator; -import org.x4o.xml.element.ElementConfiguratorGlobal; -import org.x4o.xml.element.ElementException; -import org.x4o.xml.element.ElementInterface; -import org.x4o.xml.element.ElementNamespaceContext; -import org.x4o.xml.io.sax.X4ODebugWriter; -import org.x4o.xml.io.sax.X4OEntityResolver; -import org.x4o.xml.io.sax.X4OErrorHandler; -import org.x4o.xml.io.sax.X4OTagHandler; -import org.x4o.xml.lang.X4OLanguageModule; -import org.x4o.xml.lang.X4OLanguageContext; -import org.x4o.xml.lang.X4OLanguageModuleLoaderSibling; -import org.x4o.xml.lang.X4OLanguageClassLoader; -import org.x4o.xml.lang.X4OLanguageLoader; -import org.x4o.xml.lang.X4OLanguageLocal; -import org.x4o.xml.lang.X4OLanguageProperty; - -import org.xml.sax.InputSource; -import org.xml.sax.SAXException; -import org.xml.sax.XMLReader; -import org.xml.sax.ext.DefaultHandler2; -import org.xml.sax.helpers.AttributesImpl; -import org.xml.sax.helpers.XMLReaderFactory; - -/** - * Factory which can create X4OPhaseHandlers for all the predefined phases used in default x4o language parsing. - * - * @author Willem Cazander - * @version 1.0 Dec 31, 2008 - */ -public class X4OPhaseFactory { - - private Logger logger = null; - - public X4OPhaseFactory() { - logger = Logger.getLogger(X4OPhaseFactory.class.getName()); - } - - private void debugPhaseMessage(String message,X4OPhase phaseHandler,X4OLanguageContext languageContext) throws X4OPhaseException { - if (languageContext.hasX4ODebugWriter()) { - try { - languageContext.getX4ODebugWriter().debugPhaseMessage(message,phaseHandler.getClass()); - } catch (ElementException ee) { - throw new X4OPhaseException(phaseHandler,ee); - } - } - } - - /** - * Creates the startupX4OPhase which is a empty meta phase. - * @return The X4OPhaseHandler for this phase. - */ - public X4OPhase initX4OPhase() { - class CreateInitX4OPhase extends AbstractX4OPhase { - public X4OPhaseType getType() { - return X4OPhaseType.INIT; - } - public String getId() { - return "INIT"; - } - public String[] getPhaseDependencies() { - return new String[]{}; - } - public boolean isElementPhase() { - return false; - } - public void runElementPhase(Element element) throws X4OPhaseException { - // not used. - } - public void runPhase(X4OLanguageContext elementLanguage) throws X4OPhaseException { - - } - }; - X4OPhase result = new CreateInitX4OPhase(); - return result; - } - - /** - * Loads all the modules a language. - * Then creates the ElementProviders - * @return The X4OPhaseHandler for this phase. - */ - public X4OPhase createLanguagePhase() { - class CreateLanguagePhase extends AbstractX4OPhase { - public X4OPhaseType getType() { - return X4OPhaseType.INIT; - } - public String getId() { - return "INIT_LANG"; - } - public String[] getPhaseDependencies() { - return new String[]{"INIT"}; - } - public boolean isElementPhase() { - return false; - } - public void runElementPhase(Element element) throws X4OPhaseException { - } - public void runPhase(X4OLanguageContext elementLanguage) throws X4OPhaseException { - try { - debugPhaseMessage("Loading main language: "+elementLanguage.getLanguage(),this,elementLanguage); - X4OLanguageLoader loader = (X4OLanguageLoader)X4OLanguageClassLoader.newInstance(elementLanguage.getLanguage().getLanguageConfiguration().getDefaultX4OLanguageLoader()); - loader.loadLanguage((X4OLanguageLocal)elementLanguage.getLanguage(),elementLanguage.getLanguage().getLanguageName(),elementLanguage.getLanguage().getLanguageVersion()); - - if (elementLanguage.hasX4ODebugWriter()) { - elementLanguage.getX4ODebugWriter().debugElementLanguageModules(elementLanguage); - } - } catch (Exception e) { - throw new X4OPhaseException(this,e); - } - } - }; - X4OPhase result = new CreateLanguagePhase(); - return result; - } - - /** - * Loads all sibling languages. - * @return The X4OPhaseHandler for this phase. - */ - public X4OPhase createLanguageSiblingsPhase() { - class CreateLanguageSiblingsPhase extends AbstractX4OPhase { - public X4OPhaseType getType() { - return X4OPhaseType.INIT; - } - public String getId() { - return "INIT_LANG_SIB"; - } - public String[] getPhaseDependencies() { - return new String[] {"INIT_LANG"}; - } - public boolean isElementPhase() { - return false; - } - public void runElementPhase(Element element) throws X4OPhaseException { - } - public void runPhase(X4OLanguageContext elementLanguage) throws X4OPhaseException { - try { - List siblingLoaders = new ArrayList(3); - for (X4OLanguageModule module:elementLanguage.getLanguage().getLanguageModules()) { - if (module.getLanguageModuleLoader() instanceof X4OLanguageModuleLoaderSibling) { - siblingLoaders.add((X4OLanguageModuleLoaderSibling)module.getLanguageModuleLoader()); - } - } - if (siblingLoaders.isEmpty()==false) { - X4OLanguageLoader loader = (X4OLanguageLoader)X4OLanguageClassLoader.newInstance(elementLanguage.getLanguage().getLanguageConfiguration().getDefaultX4OLanguageLoader()); - for (X4OLanguageModuleLoaderSibling siblingLoader:siblingLoaders) { - debugPhaseMessage("Loading sibling langauge loader: "+siblingLoader,this,elementLanguage); - siblingLoader.loadLanguageSibling((X4OLanguageLocal)elementLanguage.getLanguage(), loader); - } - if (elementLanguage.hasX4ODebugWriter()) { - elementLanguage.getX4ODebugWriter().debugElementLanguageModules(elementLanguage); - } - } - } catch (Exception e) { - throw new X4OPhaseException(this,e); - } - } - }; - X4OPhase result = new CreateLanguageSiblingsPhase(); - return result; - } - - /** - * Creates the startX4OPhase which is a empty meta phase. - * @return The X4OPhaseHandler for this phase. - */ - public X4OPhase readStartX4OPhase() { - class ReadStartX4OPhase extends AbstractX4OPhase { - public X4OPhaseType getType() { - return X4OPhaseType.XML_READ; - } - public String getId() { - return "READ_START"; - } - public String[] getPhaseDependencies() { - return new String[]{}; - } - public boolean isElementPhase() { - return false; - } - public void runElementPhase(Element element) throws X4OPhaseException { - // not used. - } - public void runPhase(X4OLanguageContext elementLanguage) throws X4OPhaseException { - // print the properties and classes for this language/config - if (elementLanguage.hasX4ODebugWriter()) { - try { - elementLanguage.getX4ODebugWriter().debugLanguageProperties(elementLanguage); - elementLanguage.getX4ODebugWriter().debugLanguageDefaultClasses(elementLanguage); - } catch (ElementException e) { - throw new X4OPhaseException(this,e); - } - } - } - }; - X4OPhase result = new ReadStartX4OPhase(); - return result; - } - - - /** - * Parses the xml resource(s) and creates an Element tree. - * @return The X4OPhaseHandler for this phase. - */ - public X4OPhase readSAXStreamPhase() { - class CreateSAXStreamPhase extends AbstractX4OPhase { - public X4OPhaseType getType() { - return X4OPhaseType.XML_READ; - } - public String getId() { - return "READ_XML"; - } - public String[] getPhaseDependencies() { - return new String[]{"READ_START"}; - } - public boolean isElementPhase() { - return false; - } - public void runElementPhase(Element element) throws X4OPhaseException { - } - public void runPhase(X4OLanguageContext elementLanguage) throws X4OPhaseException { - try { - //XMLParserConfiguration config = new XIncludeAwareParserConfiguration(); - //config.setProperty("http://apache.org/xml/properties/internal/grammar-pool",myFullGrammarPool); - //SAXParser parser = new SAXParser(config); - - // Create Sax parser with x4o tag handler - X4OTagHandler xth = new X4OTagHandler(elementLanguage); - XMLReader saxParser = XMLReaderFactory.createXMLReader(); - saxParser.setErrorHandler(new X4OErrorHandler(elementLanguage)); - saxParser.setEntityResolver(new X4OEntityResolver(elementLanguage)); - saxParser.setContentHandler(xth); - saxParser.setProperty("http://xml.org/sax/properties/lexical-handler", xth); - saxParser.setProperty("http://xml.org/sax/properties/declaration-handler",xth); - - // Set properties and optional - Map saxParserProperties = elementLanguage.getLanguage().getLanguageConfiguration().getSAXParserProperties(elementLanguage); - for (Map.Entry entry:saxParserProperties.entrySet()) { - String name = entry.getKey(); - Object value= entry.getValue(); - saxParser.setProperty(name, value); - debugPhaseMessage("Set SAX property: "+name+" to: "+value,this,elementLanguage); - } - Map saxParserPropertiesOptional = elementLanguage.getLanguage().getLanguageConfiguration().getSAXParserPropertiesOptional(elementLanguage); - for (Map.Entry entry:saxParserPropertiesOptional.entrySet()) { - String name = entry.getKey(); - Object value= entry.getValue(); - try { - saxParser.setProperty(name, value); - debugPhaseMessage("Set SAX optional property: "+name+" to: "+value,this,elementLanguage); - } catch (SAXException e) { - debugPhaseMessage("Could not set optional SAX property: "+name+" to: "+value+" error: "+e.getMessage(),this,elementLanguage); - } - } - - // Set sax features and optional - Map features = elementLanguage.getLanguage().getLanguageConfiguration().getSAXParserFeatures(elementLanguage); - for (String key:features.keySet()) { - Boolean value=features.get(key); - saxParser.setFeature(key, value); - debugPhaseMessage("Set SAX feature: "+key+" to: "+value,this,elementLanguage); - } - Map featuresOptional = elementLanguage.getLanguage().getLanguageConfiguration().getSAXParserFeaturesOptional(elementLanguage); - for (String key:featuresOptional.keySet()) { - Boolean value=featuresOptional.get(key); - try { - saxParser.setFeature(key, value); - debugPhaseMessage("Set SAX optional feature: "+key+" to: "+value,this,elementLanguage); - } catch (SAXException e) { - debugPhaseMessage("Could not set optional SAX feature: "+key+" to: "+value+" error: "+e.getMessage(),this,elementLanguage); - } - } - - // check for required features - List requiredFeatures = elementLanguage.getLanguage().getLanguageConfiguration().getSAXParserFeaturesRequired(elementLanguage); - for (String requiredFeature:requiredFeatures) { - debugPhaseMessage("Checking required SAX feature: "+requiredFeature,this,elementLanguage); - if (saxParser.getFeature(requiredFeature)==false) { - Exception e = new IllegalStateException("Missing required feature: "+requiredFeature); - throw new X4OPhaseException(this,e); - } - } - - // Finally start parsing the xml input stream - Object requestInputSource = elementLanguage.getLanguageProperty(X4OLanguageProperty.INPUT_SOURCE_OBJECT); - InputSource input = null; - InputStream inputStream = null; - if (requestInputSource instanceof InputSource) { - input = (InputSource)requestInputSource; - } else { - inputStream = (InputStream)elementLanguage.getLanguageProperty(X4OLanguageProperty.INPUT_SOURCE_STREAM); - input = new InputSource(inputStream); - } - - Object requestInputEncoding = elementLanguage.getLanguageProperty(X4OLanguageProperty.INPUT_SOURCE_ENCODING); - if (requestInputEncoding!=null && requestInputEncoding instanceof String) { - input.setEncoding(requestInputEncoding.toString()); - } - Object requestSystemId = elementLanguage.getLanguageProperty(X4OLanguageProperty.INPUT_SOURCE_SYSTEM_ID); - if (requestSystemId!=null && requestSystemId instanceof String) { - input.setSystemId(requestSystemId.toString()); - } - - try { - saxParser.parse(input); - } finally { - if (inputStream!=null) { - inputStream.close(); - } - } - } catch (Exception e) { - throw new X4OPhaseException(this,e); - } - } - }; - X4OPhase result = new CreateSAXStreamPhase(); - return result; - } - - /** - * Creates the configGlobalElBeansPhase which adds beans to the el context. - * @return The X4OPhaseHandler for this phase. - */ - public X4OPhase configGlobalElBeansPhase() { - class ConfigGlobalElBeansPhase extends AbstractX4OPhase { - public X4OPhaseType getType() { - return X4OPhaseType.XML_RW; - } - public String getId() { - return "X4O_CONFIG_GLOBAL_EL"; - } - public String[] getPhaseDependencies() { - return new String[] {"READ_XML"}; - } - public boolean isElementPhase() { - return false; - } - public void runElementPhase(Element element) throws X4OPhaseException { - // not used. - } - @SuppressWarnings("rawtypes") - public void runPhase(X4OLanguageContext elementLanguage) throws X4OPhaseException { - try { - Map beanMap = (Map)elementLanguage.getLanguageProperty(X4OLanguageProperty.EL_BEAN_INSTANCE_MAP); - if (beanMap==null) { - return; - } - for (Object elName:beanMap.keySet()) { - Object o = beanMap.get(elName); - ValueExpression ve = elementLanguage.getExpressionLanguageFactory().createValueExpression(elementLanguage.getExpressionLanguageContext(),"${"+elName+"}", o.getClass()); - ve.setValue(elementLanguage.getExpressionLanguageContext(), o); - debugPhaseMessage("Setting el bean: ${"+elName+"} to: "+o.getClass().getName(),this,elementLanguage); - } - } catch (Exception e) { - throw new X4OPhaseException(this,e); - } - } - }; - X4OPhase result = new ConfigGlobalElBeansPhase(); - return result; - } - - /** - * - * @return The X4OPhaseHandler for this phase. - */ - public X4OPhase configElementPhase(final X4OPhaseReadRunConfigurator runConf) { - class ConfigElementPhase extends AbstractX4OPhase { - public X4OPhaseType getType() { - return X4OPhaseType.XML_READ; - } - public String getId() { - return "READ_CONFIG_ELEMENT"; - } - public String[] getPhaseDependencies() { - return new String[] {"X4O_CONFIG_GLOBAL_EL"}; - } - public void runElementPhase(Element element) throws X4OPhaseException { - - // First phase is to rename attributes, maybe move so sax phase - for (ElementClassAttribute eca:element.getElementClass().getElementClassAttributes()) { - List aliases = eca.getAttributeAliases(); - if (aliases.isEmpty()) { - continue; - } - for (String alias:aliases) { - if (element.getAttributes().containsKey(alias)) { - String attributeValue = element.getAttributes().get(alias); - element.getAttributes().put(eca.getName(), attributeValue); - element.getAttributes().remove(alias); - } - } - } - - logger.finest("Do ElementClass Config Configurators: "+element.getElementClass().getElementConfigurators().size()); - for (ElementConfigurator ec:element.getElementClass().getElementConfigurators()) { - runConf.runElementConfigurator(ec,element,this); - } - } - }; - X4OPhase result = new ConfigElementPhase(); - return result; - } - - /** - * - * @return The X4OPhaseHandler for this phase. - */ - public X4OPhase configElementInterfacePhase(final X4OPhaseReadRunConfigurator runConf) { - class ConfigElementInterfacePhase extends AbstractX4OPhase { - public X4OPhaseType getType() { - return X4OPhaseType.XML_READ; - } - public String getId() { - return "READ_CONFIG_ELEMENT_INTERFACE"; - } - public String[] getPhaseDependencies() { - return new String[] {"X4O_CONFIG_GLOBAL_EL"}; - } - public void runElementPhase(Element element) throws X4OPhaseException { - if (element.getElementObject()==null) { - logger.finest("Null elementObject skipping, interfaces"); - return; - } - for (ElementInterface ei:element.getElementLanguage().getLanguage().findElementInterfaces(element.getElementObject())) { - logger.finest("Do ElementInterface Config Configurators: "+ei.getElementConfigurators().size()); - for (ElementConfigurator ec:ei.getElementConfigurators()) { - runConf.runElementConfigurator(ec,element,this); - } - } - } - }; - X4OPhase result = new ConfigElementInterfacePhase(); - return result; - } - - - /** - * - * @return The X4OPhaseHandler for this phase. - */ - public X4OPhase configGlobalElementPhase(final X4OPhaseReadRunConfigurator runConf) { - class ConfigGlobalElementPhase extends AbstractX4OPhase { - public X4OPhaseType getType() { - return X4OPhaseType.XML_READ; - } - public String getId() { - return "READ_CONFIG_GLOBAL_ELEMENT"; - } - public String[] getPhaseDependencies() { - return new String[] {"READ_CONFIG_ELEMENT","READ_CONFIG_ELEMENT_INTERFACE"}; - } - public void runElementPhase(Element element) throws X4OPhaseException { - for (X4OLanguageModule mod:element.getElementLanguage().getLanguage().getLanguageModules()) { - logger.finest("Do Element Config Global Configurators: "+mod.getElementConfiguratorGlobals().size()); - for (ElementConfiguratorGlobal ec:mod.getElementConfiguratorGlobals()) { - runConf.runElementConfigurator(ec,element,this); - } - } - } - }; - X4OPhase result = new ConfigGlobalElementPhase(); - return result; - } - - /** - * - * @return The X4OPhaseHandler for this phase. - */ - public X4OPhase configGlobalAttributePhase(final X4OPhaseReadRunConfigurator runConf) { - class ConfigGlobalAttributePhase extends AbstractX4OPhase { - Comparator elementAttributeHandlerComparator = null; - public X4OPhaseType getType() { - return X4OPhaseType.XML_READ; - } - public String getId() { - return "READ_CONFIG_GLOBAL_ATTRIBUTE"; - } - public String[] getPhaseDependencies() { - return new String[] {"READ_CONFIG_GLOBAL_ELEMENT"}; - } - @SuppressWarnings("unchecked") - public void runElementPhase(Element element) throws X4OPhaseException { - if (elementAttributeHandlerComparator==null) { - try { - elementAttributeHandlerComparator = (Comparator)X4OLanguageClassLoader.newInstance(element.getElementLanguage().getLanguage().getLanguageConfiguration().getDefaultElementAttributeHandlerComparator()); - } catch (Exception e) { - throw new X4OPhaseException(this,e); - } - } - - // do global parameters - logger.finest("Do Element Global AttributeHandlers."); - List handlers = new ArrayList(); - for (X4OLanguageModule mod:element.getElementLanguage().getLanguage().getLanguageModules()) { - for (ElementAttributeHandler global:mod.getElementAttributeHandlers()) { - - String attribute = element.getAttributes().get(global.getAttributeName()); - if (attribute!=null) { - handlers.add(global); - } - } - } - Collections.sort(handlers,elementAttributeHandlerComparator); - List handlers2 = new ArrayList(handlers.size()); - handlers2.addAll(handlers); - for (ElementConfigurator ec:handlers) { - runConf.runElementConfigurator(ec,element,this); - } - } - }; - X4OPhase result = new ConfigGlobalAttributePhase(); - return result; - } - - /** - * - * @return The X4OPhaseHandler for this phase. - */ - public X4OPhase runAttributesPhase() { - class RunAttributesPhase extends AbstractX4OPhase { - public X4OPhaseType getType() { - return X4OPhaseType.XML_READ; - } - public String getId() { - return "READ_RUN_ATTRIBUTE"; - } - public String[] getPhaseDependencies() { - return new String[] {"READ_CONFIG_GLOBAL_ATTRIBUTE"}; - } - public void runElementPhase(Element element) throws X4OPhaseException { - // we only can config ElementObjects - if (element.getElementObject()==null) { - return; - } - - // do config - Map attr = element.getAttributes(); - ElementAttributeValueParser attrParser = element.getElementLanguage().getElementAttributeValueParser(); - Boolean autoAttributes = element.getElementClass().getAutoAttributes(); - if (autoAttributes==null) { - autoAttributes = true; - } - try { - for (String name:attr.keySet()) { - String valueString = (String)attr.get(name); - if (valueString==null) { - continue; - } - Object value = valueString; - ElementClassAttribute attrClass = element.getElementClass().getElementClassAttributeByName(name); - if (attrClass!=null) { - if (attrClass.getRunResolveEL()==null || attrClass.getRunResolveEL() && attrParser.isELParameter(name, valueString, element)) { - value = attrParser.getELParameterValue(valueString, element); - } - if (attrClass.getRunConverters()==null || attrClass.getRunConverters()) { - value = attrParser.getConvertedParameterValue(name, value, element); - } - if (attrClass.getRunBeanFill()==null || attrClass.getRunBeanFill()) { - element.getElementLanguage().getElementObjectPropertyValue().setProperty(element.getElementObject(), name, value); - } - } else if (autoAttributes) { - - value = attrParser.getParameterValue(name,valueString,element); - element.getElementLanguage().getElementObjectPropertyValue().setProperty(element.getElementObject(), name, value); - - } else { - continue; // skip all non auto attribute non attribute defined xml attributes, or throw exception ? - } - } - // check for defaults - for (ElementClassAttribute attrClass:element.getElementClass().getElementClassAttributes()) { - if (attrClass.getDefaultValue()!=null && attr.containsKey(attrClass.getName())==false) { - Object value = null; - if (attrClass.getDefaultValue() instanceof String) { - value = attrParser.getParameterValue(attrClass.getName(),(String)attrClass.getDefaultValue(),element); - } else { - value = attrClass.getDefaultValue(); - } - if (attrClass.getRunBeanFill()==null || attrClass.getRunBeanFill()) { - element.getElementLanguage().getElementObjectPropertyValue().setProperty(element.getElementObject(), attrClass.getName(), value); - } - } - } - - } catch (ObjectConverterException oce) { - throw new X4OPhaseException(this,"Error while converting parameters: "+oce.getMessage(),oce); - } catch (ElementException e) { - throw new X4OPhaseException(this,"Error while setting parameters: "+e.getMessage(),e); - } - } - }; - X4OPhase result = new RunAttributesPhase(); - return result; - } - - /** - * - * @return The X4OPhaseHandler for this phase. - */ - public X4OPhase fillTemplatingPhase() { - X4OPhase result = new AbstractX4OPhase() { - public X4OPhaseType getType() { - return X4OPhaseType.XML_READ; - } - public String getId() { - return "READ_FILL_TEMPLATE"; - } - public String[] getPhaseDependencies() { - return new String[] {"READ_RUN_ATTRIBUTE"}; - } - public void runElementPhase(Element element) throws X4OPhaseException { - } - }; - return result; - } - - /** - * - * @return The X4OPhaseHandler for this phase. - */ - public X4OPhase transformPhase() { - X4OPhase result = new AbstractX4OPhase() { - public X4OPhaseType getType() { - return X4OPhaseType.XML_READ; - } - public String getId() { - return "READ_TRANSFORM"; - } - public String[] getPhaseDependencies() { - return new String[] {"READ_FILL_TEMPLATE"}; - } - public void runElementPhase(Element element) throws X4OPhaseException { - if (element.isTransformingTree()==false) { - return; - } - try { - if (element.getElementLanguage().hasX4ODebugWriter()) { - element.getElementLanguage().getX4ODebugWriter().debugElement(element); - } - element.doElementRun(); - } catch (ElementException e) { - throw new X4OPhaseException(this,e); - } - } - }; - return result; - } - - /** - * - * @param manager The phasemanager. - * @return The X4OPhaseHandler for this phase. - */ - public X4OPhase runDirtyElementPhase(final X4OPhaseManager manager) { - class RunDirtyElementPhase extends AbstractX4OPhase { - public X4OPhaseType getType() { - return X4OPhaseType.XML_READ; - } - public String getId() { - return "READ_RUN_DIRTY"; - } - public String[] getPhaseDependencies() { - return new String[] {"READ_TRANSFORM"}; - } - public void runElementPhase(Element element) throws X4OPhaseException { - Map dirtyElements = element.getElementLanguage().getDirtyElements(); - if (dirtyElements.isEmpty()) { - return; - } - debugPhaseMessage("Dirty elements: "+dirtyElements.size(), this,element.getElementLanguage()); - for (Element e:dirtyElements.keySet()) { - X4OPhase p = dirtyElements.get(e); - manager.runPhasesForElement(e,getType(), p); - } - element.getElementLanguage().getDirtyElements().clear(); - } - }; - X4OPhase result = new RunDirtyElementPhase(); - return result; - } - - /** - * - * @param manager The phasemanager. - * @return The X4OPhaseHandler for this phase. - */ - public X4OPhase runDirtyElementLastPhase(final X4OPhaseManager manager) { - class RunDirtyElementLastPhase extends AbstractX4OPhase { - public X4OPhaseType getType() { - return X4OPhaseType.XML_READ; - } - public String getId() { - return "READ_RUN_DIRTY_LAST"; - } - public String[] getPhaseDependencies() { - return new String[] {"READ_RUN"}; - } - public void runElementPhase(Element element) throws X4OPhaseException { - Map dirtyElements = element.getElementLanguage().getDirtyElements(); - if (dirtyElements.isEmpty()) { - return; - } - debugPhaseMessage("Dirty elements last: "+dirtyElements.size(), this,element.getElementLanguage()); - for (Element e:dirtyElements.keySet()) { - X4OPhase p = dirtyElements.get(e); - manager.runPhasesForElement(e,getType(), p); - } - element.getElementLanguage().getDirtyElements().clear(); - } - }; - X4OPhase result = new RunDirtyElementLastPhase(); - return result; - } - - /** - * - * @return The X4OPhaseHandler for this phase. - */ - public X4OPhase bindElementPhase() { - class BindElementPhase extends AbstractX4OPhase { - public X4OPhaseType getType() { - return X4OPhaseType.XML_READ; - } - public String getId() { - return "READ_BIND_ELEMENT"; - } - public String[] getPhaseDependencies() { - return new String[] {"READ_RUN_DIRTY"}; - } - public void runElementPhase(Element element) throws X4OPhaseException { - Element parentElement = element.getParent(); - if(parentElement==null) { - logger.finest("No parent element, so no binding needed."); - return; - } - Object parentObject = parentElement.getElementObject(); - if(parentObject==null) { - logger.finest("No parent object, so no binding needed."); - return; - } - Object childObject = element.getElementObject(); - if (childObject==null) { - logger.finest("No child object, so no binding needed."); - return; - } - - List binds = element.getElementLanguage().getLanguage().findElementBindingHandlers(parentObject, childObject); - logger.finest("Calling bindings handlers; "+binds.size()); - try { - for (ElementBindingHandler binding:binds) { - if (element.getElementLanguage().hasX4ODebugWriter()) { - element.getElementLanguage().getX4ODebugWriter().debugElementBindingHandler(binding,element); - } - binding.doBind(parentObject,childObject,element); - } - } catch (ElementException e) { - throw new X4OPhaseException(this,"Error while binding",e); - } - } - }; - X4OPhase result = new BindElementPhase(); - return result; - } - - /** - * - * @return The X4OPhaseHandler for this phase. - */ - public X4OPhase runPhase() { - X4OPhase result = new AbstractX4OPhase() { - public X4OPhaseType getType() { - return X4OPhaseType.XML_READ; - } - public String getId() { - return "READ_RUN"; - } - public String[] getPhaseDependencies() { - return new String[] {"READ_BIND_ELEMENT"}; - } - public void runElementPhase(Element element) throws X4OPhaseException { - if (element.isTransformingTree()) { - return; // has already runned. - } - try { - //if (parser.hasX4ODebugWriter()) { - // parser.getX4ODebugWriter().debugElement(element); - //} - element.doElementRun(); - } catch (ElementException e) { - throw new X4OPhaseException(this,e); - } - } - }; - return result; - } - - public class X4OPhaseReadRunConfigurator extends AbstractX4OPhase { - private List runConf = null; - protected X4OPhaseReadRunConfigurator() { - runConf = new ArrayList(10); - } - public X4OPhaseType getType() { - return X4OPhaseType.XML_READ; - } - public String getId() { - return "READ_RUN_CONFIGURATOR"; - } - public String[] getPhaseDependencies() { - return new String[] {"READ_RUN"}; - } - public boolean isElementPhase() { - return false; - } - public void runPhase(X4OLanguageContext elementLanguage) throws X4OPhaseException { - for (RunConfigurator conf:runConf) { - try { - if (elementLanguage.hasX4ODebugWriter()) { - elementLanguage.getX4ODebugWriter().debugElementConfigurator(conf.elementConfigurator,conf.element); - } - conf.elementConfigurator.doConfigElement(conf.element); - } catch (ElementException e) { - throw new X4OPhaseException(this,e); - } - } - } - @Override - public void runElementPhase(Element element) throws X4OPhaseException { - } - class RunConfigurator { - Element element; - ElementConfigurator elementConfigurator; - RunConfigurator(Element element,ElementConfigurator elementConfigurator) { - this.element=element; - this.elementConfigurator=elementConfigurator; - } - } - public void runElementConfigurator(ElementConfigurator ec,Element e,X4OPhase phase) throws X4OPhaseException { - if (ec.isConfigAction()) { - runConf.add(new RunConfigurator(e,ec)); - return; - } - try { - if (e.getElementLanguage().hasX4ODebugWriter()) { - e.getElementLanguage().getX4ODebugWriter().debugElementConfigurator(ec,e); - } - ec.doConfigElement(e); - - // may request rerun of config - if (ec.isConfigAction()) { - runConf.add(new RunConfigurator(e,ec)); - } - } catch (ElementException ee) { - throw new X4OPhaseException(phase,ee); - } - } - } - - /** - * - * @return The X4OPhaseHandler for this phase. - */ - public X4OPhaseReadRunConfigurator readRunConfiguratorPhase() { - return new X4OPhaseReadRunConfigurator(); - } - - /** - * Ends the read phase - * @return The X4OPhaseHandler for this phase. - */ - public X4OPhase readEndX4OPhase() { - class ReadStartX4OPhase extends AbstractX4OPhase { - public X4OPhaseType getType() { - return X4OPhaseType.XML_READ; - } - public String getId() { - return "READ_END"; - } - public String[] getPhaseDependencies() { - return new String[]{"READ_RUN_CONFIGURATOR"}; - } - public boolean isElementPhase() { - return false; - } - public void runElementPhase(Element element) throws X4OPhaseException { - // not used. - } - public void runPhase(X4OLanguageContext elementLanguage) throws X4OPhaseException { - // print the properties and classes for this language/config - if (elementLanguage.hasX4ODebugWriter()) { - try { - elementLanguage.getX4ODebugWriter().debugLanguageProperties(elementLanguage); - elementLanguage.getX4ODebugWriter().debugLanguageDefaultClasses(elementLanguage); - } catch (ElementException e) { - throw new X4OPhaseException(this,e); - } - } - } - }; - X4OPhase result = new ReadStartX4OPhase(); - return result; - } - - /** - * - * @return The X4OPhaseHandler for this phase. - */ - public X4OPhase releasePhase() { - - // for debug output - class ReleasePhaseListener implements X4OPhaseListener { - private int elementsReleased = 0; - /** - * @see org.x4o.xml.lang.phase.X4OPhaseListener#preRunPhase(org.x4o.xml.lang.phase.X4OPhase, org.x4o.xml.lang.X4OLanguageContext) - */ - public void preRunPhase(X4OPhase phase,X4OLanguageContext elementLanguage) throws X4OPhaseException { - elementsReleased=0; - } - /** - * @see org.x4o.xml.lang.phase.X4OPhaseListener#endRunPhase(org.x4o.xml.lang.phase.X4OPhase, org.x4o.xml.lang.X4OLanguageContext) - */ - public void endRunPhase(X4OPhase phase,X4OLanguageContext elementLanguage) throws X4OPhaseException { - if (elementLanguage.hasX4ODebugWriter()==false) { - return; - } - try { - AttributesImpl atts = new AttributesImpl(); - atts.addAttribute ("", "elements", "", "", elementsReleased+""); - elementLanguage.getX4ODebugWriter().getDebugWriter().startElement (X4ODebugWriter.DEBUG_URI, "executeReleases", "", atts); - elementLanguage.getX4ODebugWriter().getDebugWriter().endElement (X4ODebugWriter.DEBUG_URI, "executeReleases" , ""); - } catch (SAXException e) { - throw new X4OPhaseException(phase,e); - } - } - - public void addReleasedElement() { - elementsReleased++; - } - } - - final ReleasePhaseListener releaseCounter = new ReleasePhaseListener(); - X4OPhase result = new AbstractX4OPhase() { - public X4OPhaseType getType() { - return X4OPhaseType.XML_RW; - } - public String getId() { - return "X4O_RELEASE"; - } - public String[] getPhaseDependencies() { - return new String[] {"READ_END"}; - } - public void runPhase(X4OLanguageContext elementLanguage) throws X4OPhaseException { - } - public void runElementPhase(Element element) throws X4OPhaseException { - try { - element.release(); - } catch (ElementException e) { - throw new X4OPhaseException(this,e); - } finally { - releaseCounter.addReleasedElement(); - } - } - }; - result.addPhaseListener(releaseCounter); - return result; - } - - /** - * Creates an debug phase - * @return The X4OPhaseHandler for this phase. - */ - public X4OPhase debugPhase(final X4OPhase afterPhase) { - X4OPhase result = new AbstractX4OPhase() { - public X4OPhaseType getType() { - return X4OPhaseType.XML_RW; - } - public String getId() { - return "X4O_DEBUG_"+afterPhase.getId(); - } - public String[] getPhaseDependencies() { - return new String[] {afterPhase.getId()}; - } - public boolean isElementPhase() { - return false; - } - public void runElementPhase(Element element) throws X4OPhaseException { - } - public void runPhase(X4OLanguageContext elementLanguage) throws X4OPhaseException { - // safety check - if (elementLanguage.hasX4ODebugWriter()==false) { - throw new X4OPhaseException(this,"Use debugPhase only when X4OParser.debugWriter is filled."); - } - try { - AttributesImpl atts = new AttributesImpl(); - //atts.addAttribute ("", key, "", "", value); - //atts.addAttribute ("", "verbose", "", "", "true"); - elementLanguage.getX4ODebugWriter().getDebugWriter().startElement (X4ODebugWriter.DEBUG_URI, "printElementTree", "", atts); - startedPrefix.clear(); - printXML(elementLanguage.getRootElement()); - for (String prefix:startedPrefix) { - elementLanguage.getX4ODebugWriter().getDebugWriter().endPrefixMapping(prefix); - } - elementLanguage.getX4ODebugWriter().getDebugWriter().endElement(X4ODebugWriter.DEBUG_URI, "printElementTree", ""); - elementLanguage.getX4ODebugWriter().debugElementLanguage(elementLanguage); - } catch (SAXException e) { - throw new X4OPhaseException(this,e); - } - } - List startedPrefix = new ArrayList(10); - - // note: slow version - private String getNamespaceForElement(Element e) { - for (X4OLanguageModule mod:e.getElementLanguage().getLanguage().getLanguageModules()) { - for (ElementNamespaceContext enc:mod.getElementNamespaceContexts()) { - List l = enc.getElementClasses(); - if (l.contains(e.getElementClass())) { - return enc.getUri(); - } - } - } - return null; - } - - private void printXML(Element element) throws SAXException { - if (element==null) { - throw new SAXException("Can't print debug xml of null element."); - } - DefaultHandler2 handler = element.getElementLanguage().getX4ODebugWriter().getDebugWriter(); - if (element.getElementType().equals(Element.ElementType.comment)) { - char[] msg = ((String)element.getElementObject()).toCharArray(); - handler.comment(msg,0,msg.length); - return; - } - if (element.getElementType().equals(Element.ElementType.characters)) { - char[] msg = ((String)element.getElementObject()).toCharArray(); - handler.characters(msg,0,msg.length); - return; - } - if (element.getElementClass()==null) { - throw new SAXException("Element without ElementClass is not valid: "+element+" obj: "+element.getElementObject()); - } - - AttributesImpl atts = new AttributesImpl(); - for (String key:element.getAttributes().keySet()) { - String value = element.getAttributes().get(key); - //uri, localName, xml1.0name, type, value - atts.addAttribute ("", key, "", "", value); - } - - String nameSpace = getNamespaceForElement(element); - String prefix = element.getElementLanguage().getLanguage().findElementNamespaceContext(nameSpace).getPrefixMapping(); - - if (startedPrefix.contains(prefix)==false) { - handler.startPrefixMapping(prefix, nameSpace); - startedPrefix.add(prefix); - } - handler.startElement (nameSpace, element.getElementClass().getTag(), "", atts); - for (Element e:element.getAllChilderen()) { - printXML(e); - } - handler.endElement (nameSpace, element.getElementClass().getTag(), ""); - } - }; - return result; - } -} diff --git a/x4o-core/src/main/java/org/x4o/xml/lang/phase/X4OPhaseLanguageInit.java b/x4o-core/src/main/java/org/x4o/xml/lang/phase/X4OPhaseLanguageInit.java new file mode 100644 index 0000000..fce3488 --- /dev/null +++ b/x4o-core/src/main/java/org/x4o/xml/lang/phase/X4OPhaseLanguageInit.java @@ -0,0 +1,180 @@ +/* + * 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. + */ + +package org.x4o.xml.lang.phase; + +import java.util.ArrayList; +import java.util.List; +import java.util.logging.Logger; + +import org.x4o.xml.element.Element; +import org.x4o.xml.lang.X4OLanguageModule; +import org.x4o.xml.lang.X4OLanguageContext; +import org.x4o.xml.lang.X4OLanguageModuleLoaderSibling; +import org.x4o.xml.lang.X4OLanguageClassLoader; +import org.x4o.xml.lang.X4OLanguageLoader; +import org.x4o.xml.lang.X4OLanguageLocal; + +/** + * X4OPhaseLanguageInit defines all phases to initialize the language. + * + * @author Willem Cazander + * @version 1.0 Apr 7, 2013 + */ +public class X4OPhaseLanguageInit { + + private Logger logger = null; + + public X4OPhaseLanguageInit() { + logger = Logger.getLogger(X4OPhaseLanguageInit.class.getName()); + } + + public void createPhases(DefaultX4OPhaseManager manager) { + manager.addX4OPhase(new X4OPhaseInitStart()); + manager.addX4OPhase(new X4OPhaseInitLanguage()); + manager.addX4OPhase(new X4OPhaseInitLanguageSiblings()); + manager.addX4OPhase(new X4OPhaseInitEnd()); + } + + /** + * Creates the X4OPhaseInitStart which is a empty meta phase. + */ + class X4OPhaseInitStart extends AbstractX4OPhase { + public X4OPhaseType getType() { + return X4OPhaseType.INIT; + } + public String getId() { + return "INIT_START"; + } + public String[] getPhaseDependencies() { + return new String[]{}; + } + public boolean isElementPhase() { + return false; + } + public void runElementPhase(Element element) throws X4OPhaseException { + } + public void runPhase(X4OLanguageContext elementLanguage) throws X4OPhaseException { + logger.finest("Run init start phase"); + } + }; + + /** + * Loads all the modules a language. + * Then creates the ElementProviders + */ + class X4OPhaseInitLanguage extends AbstractX4OPhase { + public X4OPhaseType getType() { + return X4OPhaseType.INIT; + } + public String getId() { + return "INIT_LANG"; + } + public String[] getPhaseDependencies() { + return new String[]{"INIT_START"}; + } + public boolean isElementPhase() { + return false; + } + public void runElementPhase(Element element) throws X4OPhaseException { + } + public void runPhase(X4OLanguageContext elementLanguage) throws X4OPhaseException { + try { + //debugPhaseMessage("Loading main language: "+elementLanguage.getLanguage(),this,elementLanguage); + X4OLanguageLoader loader = (X4OLanguageLoader)X4OLanguageClassLoader.newInstance(elementLanguage.getLanguage().getLanguageConfiguration().getDefaultLanguageLoader()); + loader.loadLanguage((X4OLanguageLocal)elementLanguage.getLanguage(),elementLanguage.getLanguage().getLanguageName(),elementLanguage.getLanguage().getLanguageVersion()); + + if (elementLanguage.hasX4ODebugWriter()) { + elementLanguage.getX4ODebugWriter().debugElementLanguageModules(elementLanguage); + } + } catch (Exception e) { + throw new X4OPhaseException(this,e); + } + } + }; + + /** + * Loads all sibling languages. + */ + class X4OPhaseInitLanguageSiblings extends AbstractX4OPhase { + public X4OPhaseType getType() { + return X4OPhaseType.INIT; + } + public String getId() { + return "INIT_LANG_SIB"; + } + public String[] getPhaseDependencies() { + return new String[] {"INIT_LANG"}; + } + public boolean isElementPhase() { + return false; + } + public void runElementPhase(Element element) throws X4OPhaseException { + } + public void runPhase(X4OLanguageContext elementLanguage) throws X4OPhaseException { + try { + List siblingLoaders = new ArrayList(3); + for (X4OLanguageModule module:elementLanguage.getLanguage().getLanguageModules()) { + if (module.getLanguageModuleLoader() instanceof X4OLanguageModuleLoaderSibling) { + siblingLoaders.add((X4OLanguageModuleLoaderSibling)module.getLanguageModuleLoader()); + } + } + if (siblingLoaders.isEmpty()==false) { + X4OLanguageLoader loader = (X4OLanguageLoader)X4OLanguageClassLoader.newInstance(elementLanguage.getLanguage().getLanguageConfiguration().getDefaultLanguageLoader()); + for (X4OLanguageModuleLoaderSibling siblingLoader:siblingLoaders) { + //debugPhaseMessage("Loading sibling langauge loader: "+siblingLoader,this,elementLanguage); + siblingLoader.loadLanguageSibling((X4OLanguageLocal)elementLanguage.getLanguage(), loader); + } + if (elementLanguage.hasX4ODebugWriter()) { + elementLanguage.getX4ODebugWriter().debugElementLanguageModules(elementLanguage); + } + } + } catch (Exception e) { + throw new X4OPhaseException(this,e); + } + } + }; + + /** + * Creates the X4OPhaseInitEnd which is a empty meta phase. + */ + class X4OPhaseInitEnd extends AbstractX4OPhase { + public X4OPhaseType getType() { + return X4OPhaseType.INIT; + } + public String getId() { + return "INIT_END"; + } + public String[] getPhaseDependencies() { + return new String[]{"INIT_LANG_SIB"}; + } + public boolean isElementPhase() { + return false; + } + public void runElementPhase(Element element) throws X4OPhaseException { + } + public void runPhase(X4OLanguageContext elementLanguage) throws X4OPhaseException { + logger.finest("Run init end phase"); + } + }; +} diff --git a/x4o-core/src/main/java/org/x4o/xml/lang/phase/X4OPhaseLanguageRead.java b/x4o-core/src/main/java/org/x4o/xml/lang/phase/X4OPhaseLanguageRead.java new file mode 100644 index 0000000..5d10063 --- /dev/null +++ b/x4o-core/src/main/java/org/x4o/xml/lang/phase/X4OPhaseLanguageRead.java @@ -0,0 +1,993 @@ +/* + * 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. + */ + +package org.x4o.xml.lang.phase; + +import java.io.InputStream; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; +import java.util.Map; +import java.util.logging.Logger; + +import javax.el.ValueExpression; + +import org.x4o.xml.conv.ObjectConverterException; + +import org.x4o.xml.element.Element; +import org.x4o.xml.element.ElementAttributeHandler; +import org.x4o.xml.element.ElementAttributeValueParser; +import org.x4o.xml.element.ElementBindingHandler; +import org.x4o.xml.element.ElementClass; +import org.x4o.xml.element.ElementClassAttribute; +import org.x4o.xml.element.ElementConfigurator; +import org.x4o.xml.element.ElementConfiguratorGlobal; +import org.x4o.xml.element.ElementException; +import org.x4o.xml.element.ElementInterface; +import org.x4o.xml.element.ElementNamespaceContext; +import org.x4o.xml.io.sax.X4ODebugWriter; +import org.x4o.xml.io.sax.X4OEntityResolver; +import org.x4o.xml.io.sax.X4OErrorHandler; +import org.x4o.xml.io.sax.X4OTagHandler; +import org.x4o.xml.lang.X4OLanguageModule; +import org.x4o.xml.lang.X4OLanguageContext; +import org.x4o.xml.lang.X4OLanguageClassLoader; +import org.x4o.xml.lang.X4OLanguageProperty; + +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; +import org.xml.sax.XMLReader; +import org.xml.sax.ext.DefaultHandler2; +import org.xml.sax.helpers.AttributesImpl; +import org.xml.sax.helpers.XMLReaderFactory; + +/** + * Factory which can create X4OPhaseHandlers for all the predefined phases used in default x4o language parsing. + * + * @author Willem Cazander + * @version 1.0 Dec 31, 2008 + */ +public class X4OPhaseLanguageRead { + + private Logger logger = null; + + public X4OPhaseLanguageRead() { + logger = Logger.getLogger(X4OPhaseLanguageRead.class.getName()); + } + + + public void createPhases(DefaultX4OPhaseManager manager) { + manager.addX4OPhase(new X4OPhaseReadStart()); + manager.addX4OPhase(new X4OPhaseReadXml()); + + manager.addX4OPhase(new X4OPhaseReadConfigELBeans()); +// if (elementLanguage.hasX4ODebugWriter()) {manager.addX4OPhaseHandler(factory.debugPhase());} + + // meta start point +// manager.addX4OPhase(factory.startX4OPhase()); +// if (elementLanguage.hasX4ODebugWriter()) {manager.addX4OPhaseHandler(factory.debugPhase());} + + // config + X4OPhaseReadRunConfigurator runConf = new X4OPhaseReadRunConfigurator(); + manager.addX4OPhase(new X4OPhaseReadConfigElement(runConf)); + manager.addX4OPhase(new X4OPhaseReadConfigElementInterface(runConf)); + manager.addX4OPhase(new X4OPhaseReadConfigGlobalElement(runConf)); + manager.addX4OPhase(new X4OPhaseReadConfigGlobalAttribute(runConf)); + + // run all attribute events + manager.addX4OPhase(new X4OPhaseReadRunAttribute()); +// if (elementLanguage.hasX4ODebugWriter()) {manager.addX4OPhaseHandler(factory.debugPhase());} + // templating + manager.addX4OPhase(new X4OPhaseReadFillTemplate()); +// if (elementLanguage.hasX4ODebugWriter()) {manager.addX4OPhaseHandler(factory.debugPhase());} + + // transforming + manager.addX4OPhase(new X4OPhaseReadTransform()); + manager.addX4OPhase(new X4OPhaseReadRunDirty(manager)); +// if (elementLanguage.hasX4ODebugWriter()) {manager.addX4OPhaseHandler(factory.debugPhase());} + + // binding elements + manager.addX4OPhase(new X4OPhaseReadBindElement()); + + // runing and releasing + manager.addX4OPhase(new X4OPhaseReadRun()); +// if (elementLanguage.hasX4ODebugWriter()) {manager.addX4OPhaseHandler(factory.debugPhase());} + + manager.addX4OPhase(runConf); + + manager.addX4OPhase(new X4OPhaseReadRunDirtyLast(manager)); +// if (elementLanguage.hasX4ODebugWriter()) {manager.addX4OPhaseHandler(factory.debugPhase());} + + + manager.addX4OPhase(new X4OPhaseReadEnd()); + + // after release phase Element Tree is not avible anymore + manager.addX4OPhase(releasePhase()); + + // Add debug phase listener to all phases +// if (elementLanguage.hasX4ODebugWriter()) { + //for (X4OPhase h:manager.getOrderedPhases()) { +// h.addPhaseListener(elementLanguage.getX4ODebugWriter().createDebugX4OPhaseListener()); + //} + //} + + // We are done + } + + private void debugPhaseMessage(String message,X4OPhase phaseHandler,X4OLanguageContext languageContext) throws X4OPhaseException { + if (languageContext.hasX4ODebugWriter()) { + try { + languageContext.getX4ODebugWriter().debugPhaseMessage(message,phaseHandler.getClass()); + } catch (ElementException ee) { + throw new X4OPhaseException(phaseHandler,ee); + } + } + } + + + + /** + * Creates the startX4OPhase which is a empty meta phase. + */ + class X4OPhaseReadStart extends AbstractX4OPhase { + public X4OPhaseType getType() { + return X4OPhaseType.XML_READ; + } + public String getId() { + return "READ_START"; + } + public String[] getPhaseDependencies() { + return new String[]{}; + } + public boolean isElementPhase() { + return false; + } + public void runElementPhase(Element element) throws X4OPhaseException { + // not used. + } + public void runPhase(X4OLanguageContext elementLanguage) throws X4OPhaseException { + // print the properties and classes for this language/config + if (elementLanguage.hasX4ODebugWriter()) { + try { + elementLanguage.getX4ODebugWriter().debugLanguageProperties(elementLanguage); + elementLanguage.getX4ODebugWriter().debugLanguageDefaultClasses(elementLanguage); + } catch (ElementException e) { + throw new X4OPhaseException(this,e); + } + } + } + }; + + /** + * Parses the xml resource(s) and creates an Element tree. + */ + class X4OPhaseReadXml extends AbstractX4OPhase { + public X4OPhaseType getType() { + return X4OPhaseType.XML_READ; + } + public String getId() { + return "READ_XML"; + } + public String[] getPhaseDependencies() { + return new String[]{"READ_START"}; + } + public boolean isElementPhase() { + return false; + } + public void runElementPhase(Element element) throws X4OPhaseException { + } + public void runPhase(X4OLanguageContext elementLanguage) throws X4OPhaseException { + try { + //XMLParserConfiguration config = new XIncludeAwareParserConfiguration(); + //config.setProperty("http://apache.org/xml/properties/internal/grammar-pool",myFullGrammarPool); + //SAXParser parser = new SAXParser(config); + + // Create Sax parser with x4o tag handler + X4OTagHandler xth = new X4OTagHandler(elementLanguage); + XMLReader saxParser = XMLReaderFactory.createXMLReader(); + saxParser.setErrorHandler(new X4OErrorHandler(elementLanguage)); + saxParser.setEntityResolver(new X4OEntityResolver(elementLanguage)); + saxParser.setContentHandler(xth); + saxParser.setProperty("http://xml.org/sax/properties/lexical-handler", xth); + saxParser.setProperty("http://xml.org/sax/properties/declaration-handler",xth); + + // Set properties and optional + Map saxParserProperties = elementLanguage.getLanguage().getLanguageConfiguration().getSAXParserProperties(elementLanguage); + for (Map.Entry entry:saxParserProperties.entrySet()) { + String name = entry.getKey(); + Object value= entry.getValue(); + saxParser.setProperty(name, value); + debugPhaseMessage("Set SAX property: "+name+" to: "+value,this,elementLanguage); + } + Map saxParserPropertiesOptional = elementLanguage.getLanguage().getLanguageConfiguration().getSAXParserPropertiesOptional(elementLanguage); + for (Map.Entry entry:saxParserPropertiesOptional.entrySet()) { + String name = entry.getKey(); + Object value= entry.getValue(); + try { + saxParser.setProperty(name, value); + debugPhaseMessage("Set SAX optional property: "+name+" to: "+value,this,elementLanguage); + } catch (SAXException e) { + debugPhaseMessage("Could not set optional SAX property: "+name+" to: "+value+" error: "+e.getMessage(),this,elementLanguage); + } + } + + // Set sax features and optional + Map features = elementLanguage.getLanguage().getLanguageConfiguration().getSAXParserFeatures(elementLanguage); + for (String key:features.keySet()) { + Boolean value=features.get(key); + saxParser.setFeature(key, value); + debugPhaseMessage("Set SAX feature: "+key+" to: "+value,this,elementLanguage); + } + Map featuresOptional = elementLanguage.getLanguage().getLanguageConfiguration().getSAXParserFeaturesOptional(elementLanguage); + for (String key:featuresOptional.keySet()) { + Boolean value=featuresOptional.get(key); + try { + saxParser.setFeature(key, value); + debugPhaseMessage("Set SAX optional feature: "+key+" to: "+value,this,elementLanguage); + } catch (SAXException e) { + debugPhaseMessage("Could not set optional SAX feature: "+key+" to: "+value+" error: "+e.getMessage(),this,elementLanguage); + } + } + + // check for required features + List requiredFeatures = elementLanguage.getLanguage().getLanguageConfiguration().getSAXParserFeaturesRequired(elementLanguage); + for (String requiredFeature:requiredFeatures) { + debugPhaseMessage("Checking required SAX feature: "+requiredFeature,this,elementLanguage); + if (saxParser.getFeature(requiredFeature)==false) { + Exception e = new IllegalStateException("Missing required feature: "+requiredFeature); + throw new X4OPhaseException(this,e); + } + } + + // Finally start parsing the xml input stream + Object requestInputSource = elementLanguage.getLanguageProperty(X4OLanguageProperty.INPUT_SOURCE_OBJECT); + InputSource input = null; + InputStream inputStream = null; + if (requestInputSource instanceof InputSource) { + input = (InputSource)requestInputSource; + } else { + inputStream = (InputStream)elementLanguage.getLanguageProperty(X4OLanguageProperty.INPUT_SOURCE_STREAM); + input = new InputSource(inputStream); + } + + Object requestInputEncoding = elementLanguage.getLanguageProperty(X4OLanguageProperty.INPUT_SOURCE_ENCODING); + if (requestInputEncoding!=null && requestInputEncoding instanceof String) { + input.setEncoding(requestInputEncoding.toString()); + } + Object requestSystemId = elementLanguage.getLanguageProperty(X4OLanguageProperty.INPUT_SOURCE_SYSTEM_ID); + if (requestSystemId!=null && requestSystemId instanceof String) { + input.setSystemId(requestSystemId.toString()); + } + + try { + saxParser.parse(input); + } finally { + if (inputStream!=null) { + inputStream.close(); + } + } + } catch (Exception e) { + throw new X4OPhaseException(this,e); + } + } + }; + + /** + * Creates the configGlobalElBeansPhase which adds beans to the el context. + */ + class X4OPhaseReadConfigELBeans extends AbstractX4OPhase { + public X4OPhaseType getType() { + return X4OPhaseType.XML_RW; + } + public String getId() { + return "READ_CONFIG_EL_BEANS"; + } + public String[] getPhaseDependencies() { + return new String[] {"READ_XML"}; + } + public boolean isElementPhase() { + return false; + } + public void runElementPhase(Element element) throws X4OPhaseException { + // not used. + } + @SuppressWarnings("rawtypes") + public void runPhase(X4OLanguageContext elementLanguage) throws X4OPhaseException { + try { + Map beanMap = (Map)elementLanguage.getLanguageProperty(X4OLanguageProperty.EL_BEAN_INSTANCE_MAP); + if (beanMap==null) { + return; + } + for (Object elName:beanMap.keySet()) { + Object o = beanMap.get(elName); + ValueExpression ve = elementLanguage.getExpressionLanguageFactory().createValueExpression(elementLanguage.getExpressionLanguageContext(),"${"+elName+"}", o.getClass()); + ve.setValue(elementLanguage.getExpressionLanguageContext(), o); + debugPhaseMessage("Setting el bean: ${"+elName+"} to: "+o.getClass().getName(),this,elementLanguage); + } + } catch (Exception e) { + throw new X4OPhaseException(this,e); + } + } + }; + + /** + * X4OPhaseReadConfigElement + */ + class X4OPhaseReadConfigElement extends AbstractX4OPhase { + private X4OPhaseReadRunConfigurator runConf = null; + public X4OPhaseReadConfigElement(X4OPhaseReadRunConfigurator runConf) { + this.runConf=runConf; + } + public X4OPhaseType getType() { + return X4OPhaseType.XML_READ; + } + public String getId() { + return "READ_CONFIG_ELEMENT"; + } + public String[] getPhaseDependencies() { + return new String[] {"READ_CONFIG_EL_BEANS"}; + } + public void runElementPhase(Element element) throws X4OPhaseException { + + // First phase is to rename attributes, maybe move so sax phase + for (ElementClassAttribute eca:element.getElementClass().getElementClassAttributes()) { + List aliases = eca.getAttributeAliases(); + if (aliases.isEmpty()) { + continue; + } + for (String alias:aliases) { + if (element.getAttributes().containsKey(alias)) { + String attributeValue = element.getAttributes().get(alias); + element.getAttributes().put(eca.getName(), attributeValue); + element.getAttributes().remove(alias); + } + } + } + + logger.finest("Do ElementClass Config Configurators: "+element.getElementClass().getElementConfigurators().size()); + for (ElementConfigurator ec:element.getElementClass().getElementConfigurators()) { + runConf.runElementConfigurator(ec,element,this); + } + } + }; + + /** + * X4OPhaseReadConfigElementInterface + */ + class X4OPhaseReadConfigElementInterface extends AbstractX4OPhase { + private X4OPhaseReadRunConfigurator runConf = null; + public X4OPhaseReadConfigElementInterface(X4OPhaseReadRunConfigurator runConf) { + this.runConf=runConf; + } + public X4OPhaseType getType() { + return X4OPhaseType.XML_READ; + } + public String getId() { + return "READ_CONFIG_ELEMENT_INTERFACE"; + } + public String[] getPhaseDependencies() { + return new String[] {"READ_CONFIG_EL_BEANS"}; + } + public void runElementPhase(Element element) throws X4OPhaseException { + if (element.getElementObject()==null) { + logger.finest("Null elementObject skipping, interfaces"); + return; + } + for (ElementInterface ei:element.getLanguageContext().getLanguage().findElementInterfaces(element.getElementObject())) { + logger.finest("Do ElementInterface Config Configurators: "+ei.getElementConfigurators().size()); + for (ElementConfigurator ec:ei.getElementConfigurators()) { + runConf.runElementConfigurator(ec,element,this); + } + } + } + }; + + /** + * X4OPhaseReadConfigGlobalElement + */ + class X4OPhaseReadConfigGlobalElement extends AbstractX4OPhase { + private X4OPhaseReadRunConfigurator runConf = null; + public X4OPhaseReadConfigGlobalElement(X4OPhaseReadRunConfigurator runConf) { + this.runConf=runConf; + } + public X4OPhaseType getType() { + return X4OPhaseType.XML_READ; + } + public String getId() { + return "READ_CONFIG_GLOBAL_ELEMENT"; + } + public String[] getPhaseDependencies() { + return new String[] {"READ_CONFIG_ELEMENT","READ_CONFIG_ELEMENT_INTERFACE"}; + } + public void runElementPhase(Element element) throws X4OPhaseException { + for (X4OLanguageModule mod:element.getLanguageContext().getLanguage().getLanguageModules()) { + logger.finest("Do Element Config Global Configurators: "+mod.getElementConfiguratorGlobals().size()); + for (ElementConfiguratorGlobal ec:mod.getElementConfiguratorGlobals()) { + runConf.runElementConfigurator(ec,element,this); + } + } + } + }; + + /** + * X4OPhaseReadConfigGlobalAttribute + */ + class X4OPhaseReadConfigGlobalAttribute extends AbstractX4OPhase { + Comparator elementAttributeHandlerComparator = null; + private X4OPhaseReadRunConfigurator runConf = null; + public X4OPhaseReadConfigGlobalAttribute(X4OPhaseReadRunConfigurator runConf) { + this.runConf=runConf; + } + public X4OPhaseType getType() { + return X4OPhaseType.XML_READ; + } + public String getId() { + return "READ_CONFIG_GLOBAL_ATTRIBUTE"; + } + public String[] getPhaseDependencies() { + return new String[] {"READ_CONFIG_GLOBAL_ELEMENT"}; + } + @SuppressWarnings("unchecked") + public void runElementPhase(Element element) throws X4OPhaseException { + if (elementAttributeHandlerComparator==null) { + try { + elementAttributeHandlerComparator = (Comparator)X4OLanguageClassLoader.newInstance(element.getLanguageContext().getLanguage().getLanguageConfiguration().getDefaultElementAttributeHandlerComparator()); + } catch (Exception e) { + throw new X4OPhaseException(this,e); + } + } + + // do global parameters + logger.finest("Do Element Global AttributeHandlers."); + List handlers = new ArrayList(); + for (X4OLanguageModule mod:element.getLanguageContext().getLanguage().getLanguageModules()) { + for (ElementAttributeHandler global:mod.getElementAttributeHandlers()) { + + String attribute = element.getAttributes().get(global.getAttributeName()); + if (attribute!=null) { + handlers.add(global); + } + } + } + Collections.sort(handlers,elementAttributeHandlerComparator); + List handlers2 = new ArrayList(handlers.size()); + handlers2.addAll(handlers); + for (ElementConfigurator ec:handlers) { + runConf.runElementConfigurator(ec,element,this); + } + } + }; + + /** + * X4OPhaseReadRunAttribute + */ + class X4OPhaseReadRunAttribute extends AbstractX4OPhase { + public X4OPhaseType getType() { + return X4OPhaseType.XML_READ; + } + public String getId() { + return "READ_RUN_ATTRIBUTE"; + } + public String[] getPhaseDependencies() { + return new String[] {"READ_CONFIG_GLOBAL_ATTRIBUTE"}; + } + public void runElementPhase(Element element) throws X4OPhaseException { + // we only can config ElementObjects + if (element.getElementObject()==null) { + return; + } + + // do config + Map attr = element.getAttributes(); + ElementAttributeValueParser attrParser = element.getLanguageContext().getElementAttributeValueParser(); + Boolean autoAttributes = element.getElementClass().getAutoAttributes(); + if (autoAttributes==null) { + autoAttributes = true; + } + try { + for (String name:attr.keySet()) { + String valueString = (String)attr.get(name); + if (valueString==null) { + continue; + } + Object value = valueString; + ElementClassAttribute attrClass = element.getElementClass().getElementClassAttributeByName(name); + if (attrClass!=null) { + if (attrClass.getRunResolveEL()==null || attrClass.getRunResolveEL() && attrParser.isELParameter(name, valueString, element)) { + value = attrParser.getELParameterValue(valueString, element); + } + if (attrClass.getRunConverters()==null || attrClass.getRunConverters()) { + value = attrParser.getConvertedParameterValue(name, value, element); + } + if (attrClass.getRunBeanFill()==null || attrClass.getRunBeanFill()) { + element.getLanguageContext().getElementObjectPropertyValue().setProperty(element.getElementObject(), name, value); + } + } else if (autoAttributes) { + + value = attrParser.getParameterValue(name,valueString,element); + element.getLanguageContext().getElementObjectPropertyValue().setProperty(element.getElementObject(), name, value); + + } else { + continue; // skip all non auto attribute non attribute defined xml attributes, or throw exception ? + } + } + // check for defaults + for (ElementClassAttribute attrClass:element.getElementClass().getElementClassAttributes()) { + if (attrClass.getDefaultValue()!=null && attr.containsKey(attrClass.getName())==false) { + Object value = null; + if (attrClass.getDefaultValue() instanceof String) { + value = attrParser.getParameterValue(attrClass.getName(),(String)attrClass.getDefaultValue(),element); + } else { + value = attrClass.getDefaultValue(); + } + if (attrClass.getRunBeanFill()==null || attrClass.getRunBeanFill()) { + element.getLanguageContext().getElementObjectPropertyValue().setProperty(element.getElementObject(), attrClass.getName(), value); + } + } + } + + } catch (ObjectConverterException oce) { + throw new X4OPhaseException(this,"Error while converting parameters: "+oce.getMessage(),oce); + } catch (ElementException e) { + throw new X4OPhaseException(this,"Error while setting parameters: "+e.getMessage(),e); + } + } + }; + + /** + * X4OPhaseReadFillTemplate + */ + class X4OPhaseReadFillTemplate extends AbstractX4OPhase { + public X4OPhaseType getType() { + return X4OPhaseType.XML_READ; + } + public String getId() { + return "READ_FILL_TEMPLATE"; + } + public String[] getPhaseDependencies() { + return new String[] {"READ_RUN_ATTRIBUTE"}; + } + public void runElementPhase(Element element) throws X4OPhaseException { + } + }; + + /** + * X4OPhaseReadTransform + */ + class X4OPhaseReadTransform extends AbstractX4OPhase { + public X4OPhaseType getType() { + return X4OPhaseType.XML_READ; + } + public String getId() { + return "READ_TRANSFORM"; + } + public String[] getPhaseDependencies() { + return new String[] {"READ_FILL_TEMPLATE"}; + } + public void runElementPhase(Element element) throws X4OPhaseException { + if (element.isTransformingTree()==false) { + return; + } + try { + if (element.getLanguageContext().hasX4ODebugWriter()) { + element.getLanguageContext().getX4ODebugWriter().debugElement(element); + } + element.doElementRun(); + } catch (ElementException e) { + throw new X4OPhaseException(this,e); + } + } + }; + + /** + * X4OPhaseReadRunDirty + */ + class X4OPhaseReadRunDirty extends AbstractX4OPhase { + private X4OPhaseManager phaseManager = null; + public X4OPhaseReadRunDirty(X4OPhaseManager phaseManager) { + this.phaseManager=phaseManager; + } + public X4OPhaseType getType() { + return X4OPhaseType.XML_READ; + } + public String getId() { + return "READ_RUN_DIRTY"; + } + public String[] getPhaseDependencies() { + return new String[] {"READ_TRANSFORM"}; + } + public void runElementPhase(Element element) throws X4OPhaseException { + Map dirtyElements = element.getLanguageContext().getDirtyElements(); + if (dirtyElements.isEmpty()) { + return; + } + debugPhaseMessage("Dirty elements: "+dirtyElements.size(), this,element.getLanguageContext()); + for (Element e:dirtyElements.keySet()) { + X4OPhase p = dirtyElements.get(e); + phaseManager.runPhasesForElement(e,getType(), p); + } + element.getLanguageContext().getDirtyElements().clear(); + } + }; + + /** + * X4OPhaseReadRunDirtyLast + */ + class X4OPhaseReadRunDirtyLast extends AbstractX4OPhase { + private X4OPhaseManager phaseManager = null; + public X4OPhaseReadRunDirtyLast(X4OPhaseManager phaseManager) { + this.phaseManager=phaseManager; + } + public X4OPhaseType getType() { + return X4OPhaseType.XML_READ; + } + public String getId() { + return "READ_RUN_DIRTY_LAST"; + } + public String[] getPhaseDependencies() { + return new String[] {"READ_RUN"}; + } + public void runElementPhase(Element element) throws X4OPhaseException { + Map dirtyElements = element.getLanguageContext().getDirtyElements(); + if (dirtyElements.isEmpty()) { + return; + } + debugPhaseMessage("Dirty elements last: "+dirtyElements.size(), this,element.getLanguageContext()); + for (Element e:dirtyElements.keySet()) { + X4OPhase p = dirtyElements.get(e); + phaseManager.runPhasesForElement(e,getType(), p); + } + element.getLanguageContext().getDirtyElements().clear(); + } + }; + + /** + * X4OPhaseReadBindElement + */ + class X4OPhaseReadBindElement extends AbstractX4OPhase { + public X4OPhaseType getType() { + return X4OPhaseType.XML_READ; + } + public String getId() { + return "READ_BIND_ELEMENT"; + } + public String[] getPhaseDependencies() { + return new String[] {"READ_RUN_DIRTY"}; + } + public void runElementPhase(Element element) throws X4OPhaseException { + Element parentElement = element.getParent(); + if(parentElement==null) { + logger.finest("No parent element, so no binding needed."); + return; + } + Object parentObject = parentElement.getElementObject(); + if(parentObject==null) { + logger.finest("No parent object, so no binding needed."); + return; + } + Object childObject = element.getElementObject(); + if (childObject==null) { + logger.finest("No child object, so no binding needed."); + return; + } + + List binds = element.getLanguageContext().getLanguage().findElementBindingHandlers(parentObject, childObject); + logger.finest("Calling bindings handlers; "+binds.size()); + try { + for (ElementBindingHandler binding:binds) { + if (element.getLanguageContext().hasX4ODebugWriter()) { + element.getLanguageContext().getX4ODebugWriter().debugElementBindingHandler(binding,element); + } + binding.bindChild(element); + } + } catch (ElementException e) { + throw new X4OPhaseException(this,"Error while binding",e); + } + } + }; + + /** + * X4OPhaseReadRun + */ + class X4OPhaseReadRun extends AbstractX4OPhase { + public X4OPhaseType getType() { + return X4OPhaseType.XML_READ; + } + public String getId() { + return "READ_RUN"; + } + public String[] getPhaseDependencies() { + return new String[] {"READ_BIND_ELEMENT"}; + } + public void runElementPhase(Element element) throws X4OPhaseException { + if (element.isTransformingTree()) { + return; // has already runned. + } + try { + //if (parser.hasX4ODebugWriter()) { + // parser.getX4ODebugWriter().debugElement(element); + //} + element.doElementRun(); + } catch (ElementException e) { + throw new X4OPhaseException(this,e); + } + } + }; + + class X4OPhaseReadRunConfigurator extends AbstractX4OPhase { + private List runConf = null; + protected X4OPhaseReadRunConfigurator() { + runConf = new ArrayList(10); + } + public X4OPhaseType getType() { + return X4OPhaseType.XML_READ; + } + public String getId() { + return "READ_RUN_CONFIGURATOR"; + } + public String[] getPhaseDependencies() { + return new String[] {"READ_RUN"}; + } + public boolean isElementPhase() { + return false; + } + public void runPhase(X4OLanguageContext elementLanguage) throws X4OPhaseException { + for (RunConfigurator conf:runConf) { + try { + if (elementLanguage.hasX4ODebugWriter()) { + elementLanguage.getX4ODebugWriter().debugElementConfigurator(conf.elementConfigurator,conf.element); + } + conf.elementConfigurator.doConfigElement(conf.element); + } catch (ElementException e) { + throw new X4OPhaseException(this,e); + } + } + } + @Override + public void runElementPhase(Element element) throws X4OPhaseException { + } + class RunConfigurator { + Element element; + ElementConfigurator elementConfigurator; + RunConfigurator(Element element,ElementConfigurator elementConfigurator) { + this.element=element; + this.elementConfigurator=elementConfigurator; + } + } + public void runElementConfigurator(ElementConfigurator ec,Element e,X4OPhase phase) throws X4OPhaseException { + if (ec.isConfigAction()) { + runConf.add(new RunConfigurator(e,ec)); + return; + } + try { + if (e.getLanguageContext().hasX4ODebugWriter()) { + e.getLanguageContext().getX4ODebugWriter().debugElementConfigurator(ec,e); + } + ec.doConfigElement(e); + + // may request rerun of config + if (ec.isConfigAction()) { + runConf.add(new RunConfigurator(e,ec)); + } + } catch (ElementException ee) { + throw new X4OPhaseException(phase,ee); + } + } + } + + /** + * X4OPhaseReadEnd Is the read end phase. + */ + class X4OPhaseReadEnd extends AbstractX4OPhase { + public X4OPhaseType getType() { + return X4OPhaseType.XML_READ; + } + public String getId() { + return "READ_END"; + } + public String[] getPhaseDependencies() { + return new String[]{"READ_RUN_CONFIGURATOR"}; + } + public boolean isElementPhase() { + return false; + } + public void runElementPhase(Element element) throws X4OPhaseException { + // not used. + } + public void runPhase(X4OLanguageContext elementLanguage) throws X4OPhaseException { + // print the properties and classes for this language/config + if (elementLanguage.hasX4ODebugWriter()) { + try { + elementLanguage.getX4ODebugWriter().debugLanguageProperties(elementLanguage); + elementLanguage.getX4ODebugWriter().debugLanguageDefaultClasses(elementLanguage); + } catch (ElementException e) { + throw new X4OPhaseException(this,e); + } + } + } + }; + + /** + * + * @return The X4OPhaseHandler for this phase. + */ + public X4OPhase releasePhase() { + + // for debug output + class ReleasePhaseListener implements X4OPhaseListener { + private int elementsReleased = 0; + /** + * @see org.x4o.xml.lang.phase.X4OPhaseListener#preRunPhase(org.x4o.xml.lang.phase.X4OPhase, org.x4o.xml.lang.X4OLanguageContext) + */ + public void preRunPhase(X4OPhase phase,X4OLanguageContext elementLanguage) throws X4OPhaseException { + elementsReleased=0; + } + /** + * @see org.x4o.xml.lang.phase.X4OPhaseListener#endRunPhase(org.x4o.xml.lang.phase.X4OPhase, org.x4o.xml.lang.X4OLanguageContext) + */ + public void endRunPhase(X4OPhase phase,X4OLanguageContext elementLanguage) throws X4OPhaseException { + if (elementLanguage.hasX4ODebugWriter()==false) { + return; + } + try { + AttributesImpl atts = new AttributesImpl(); + atts.addAttribute ("", "elements", "", "", elementsReleased+""); + elementLanguage.getX4ODebugWriter().getDebugWriter().startElement (X4ODebugWriter.DEBUG_URI, "executeReleases", "", atts); + elementLanguage.getX4ODebugWriter().getDebugWriter().endElement (X4ODebugWriter.DEBUG_URI, "executeReleases" , ""); + } catch (SAXException e) { + throw new X4OPhaseException(phase,e); + } + } + + public void addReleasedElement() { + elementsReleased++; + } + } + + final ReleasePhaseListener releaseCounter = new ReleasePhaseListener(); + X4OPhase result = new AbstractX4OPhase() { + public X4OPhaseType getType() { + return X4OPhaseType.XML_RW; + } + public String getId() { + return "X4O_RELEASE"; + } + public String[] getPhaseDependencies() { + return new String[] {"READ_END"}; + } + public void runPhase(X4OLanguageContext elementLanguage) throws X4OPhaseException { + } + public void runElementPhase(Element element) throws X4OPhaseException { + try { + element.release(); + } catch (ElementException e) { + throw new X4OPhaseException(this,e); + } finally { + releaseCounter.addReleasedElement(); + } + } + }; + result.addPhaseListener(releaseCounter); + return result; + } + + /** + * Creates an debug phase + * @return The X4OPhaseHandler for this phase. + */ + public X4OPhase debugPhase(final X4OPhase afterPhase) { + X4OPhase result = new AbstractX4OPhase() { + public X4OPhaseType getType() { + return X4OPhaseType.XML_RW; + } + public String getId() { + return "X4O_DEBUG_"+afterPhase.getId(); + } + public String[] getPhaseDependencies() { + return new String[] {afterPhase.getId()}; + } + public boolean isElementPhase() { + return false; + } + public void runElementPhase(Element element) throws X4OPhaseException { + } + public void runPhase(X4OLanguageContext elementLanguage) throws X4OPhaseException { + // safety check + if (elementLanguage.hasX4ODebugWriter()==false) { + throw new X4OPhaseException(this,"Use debugPhase only when X4OParser.debugWriter is filled."); + } + try { + AttributesImpl atts = new AttributesImpl(); + //atts.addAttribute ("", key, "", "", value); + //atts.addAttribute ("", "verbose", "", "", "true"); + elementLanguage.getX4ODebugWriter().getDebugWriter().startElement (X4ODebugWriter.DEBUG_URI, "printElementTree", "", atts); + startedPrefix.clear(); + printXML(elementLanguage.getRootElement()); + for (String prefix:startedPrefix) { + elementLanguage.getX4ODebugWriter().getDebugWriter().endPrefixMapping(prefix); + } + elementLanguage.getX4ODebugWriter().getDebugWriter().endElement(X4ODebugWriter.DEBUG_URI, "printElementTree", ""); + elementLanguage.getX4ODebugWriter().debugElementLanguage(elementLanguage); + } catch (SAXException e) { + throw new X4OPhaseException(this,e); + } + } + List startedPrefix = new ArrayList(10); + + // note: slow version + private String getNamespaceForElement(Element e) { + for (X4OLanguageModule mod:e.getLanguageContext().getLanguage().getLanguageModules()) { + for (ElementNamespaceContext enc:mod.getElementNamespaceContexts()) { + List l = enc.getElementClasses(); + if (l.contains(e.getElementClass())) { + return enc.getUri(); + } + } + } + return null; + } + + private void printXML(Element element) throws SAXException { + if (element==null) { + throw new SAXException("Can't print debug xml of null element."); + } + DefaultHandler2 handler = element.getLanguageContext().getX4ODebugWriter().getDebugWriter(); + if (element.getElementType().equals(Element.ElementType.comment)) { + char[] msg = ((String)element.getElementObject()).toCharArray(); + handler.comment(msg,0,msg.length); + return; + } + if (element.getElementType().equals(Element.ElementType.characters)) { + char[] msg = ((String)element.getElementObject()).toCharArray(); + handler.characters(msg,0,msg.length); + return; + } + if (element.getElementClass()==null) { + throw new SAXException("Element without ElementClass is not valid: "+element+" obj: "+element.getElementObject()); + } + + AttributesImpl atts = new AttributesImpl(); + for (String key:element.getAttributes().keySet()) { + String value = element.getAttributes().get(key); + //uri, localName, xml1.0name, type, value + atts.addAttribute ("", key, "", "", value); + } + + String nameSpace = getNamespaceForElement(element); + String prefix = element.getLanguageContext().getLanguage().findElementNamespaceContext(nameSpace).getPrefixMapping(); + + if (startedPrefix.contains(prefix)==false) { + handler.startPrefixMapping(prefix, nameSpace); + startedPrefix.add(prefix); + } + handler.startElement (nameSpace, element.getElementClass().getTag(), "", atts); + for (Element e:element.getAllChilderen()) { + printXML(e); + } + handler.endElement (nameSpace, element.getElementClass().getTag(), ""); + } + }; + return result; + } +} diff --git a/x4o-core/src/main/java/org/x4o/xml/lang/phase/X4OPhaseManager.java b/x4o-core/src/main/java/org/x4o/xml/lang/phase/X4OPhaseManager.java index c45b549..7183502 100644 --- a/x4o-core/src/main/java/org/x4o/xml/lang/phase/X4OPhaseManager.java +++ b/x4o-core/src/main/java/org/x4o/xml/lang/phase/X4OPhaseManager.java @@ -51,13 +51,13 @@ public interface X4OPhaseManager { */ List getOrderedPhases(X4OPhaseType type); - public void doReleasePhaseManual(X4OLanguageContext languageContext) throws X4OPhaseException; + void doReleasePhaseManual(X4OLanguageContext languageContext) throws X4OPhaseException; /** * Runs all the phases in the right order. * @throws X4OPhaseException When a running handlers throws one. */ - public void runPhases(X4OLanguageContext elementContext,X4OPhaseType type) throws X4OPhaseException; + void runPhases(X4OLanguageContext elementContext,X4OPhaseType type) throws X4OPhaseException; /** * Runs phase on single element. @@ -65,5 +65,5 @@ public interface X4OPhaseManager { * @param p The phase to run. * @throws X4OPhaseException When a running handlers throws one. */ - public void runPhasesForElement(Element e,X4OPhaseType type,X4OPhase p) throws X4OPhaseException; + void runPhasesForElement(Element e,X4OPhaseType type,X4OPhase p) throws X4OPhaseException; } diff --git a/x4o-core/src/main/java/org/x4o/xml/lang/phase/X4OPhaseManagerFactory.java b/x4o-core/src/main/java/org/x4o/xml/lang/phase/X4OPhaseManagerFactory.java deleted file mode 100644 index fe680c1..0000000 --- a/x4o-core/src/main/java/org/x4o/xml/lang/phase/X4OPhaseManagerFactory.java +++ /dev/null @@ -1,213 +0,0 @@ -/* - * 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. - */ - -package org.x4o.xml.lang.phase; - -import java.util.List; - -import org.x4o.xml.element.Element; -import org.x4o.xml.lang.X4OLanguageContext; -import org.x4o.xml.lang.phase.X4OPhaseFactory.X4OPhaseReadRunConfigurator; - - -/** - * X4OPhaseManagerFactory - * - * @author Willem Cazander - * @version 1.0 Apr 30, 2013 - */ -public class X4OPhaseManagerFactory { - - static public X4OPhaseManager createDefaultX4OPhaseManager() { - X4OPhaseFactory factory = new X4OPhaseFactory(); - DefaultX4OPhaseManager phaseManager = new DefaultX4OPhaseManager(); - createPhasesInit(phaseManager,factory); - createPhasesRead(phaseManager,factory); - return phaseManager; - } - - static private void createPhasesInit(DefaultX4OPhaseManager manager,X4OPhaseFactory factory) { - manager.addX4OPhase(factory.initX4OPhase()); - manager.addX4OPhase(factory.createLanguagePhase()); - manager.addX4OPhase(factory.createLanguageSiblingsPhase()); - } - - static private void createPhasesRead(DefaultX4OPhaseManager manager,X4OPhaseFactory factory) { - // main startup - manager.addX4OPhase(factory.readStartX4OPhase()); - //manager.addX4OPhase(factory.createLanguagePhase()); - //manager.addX4OPhase(factory.createLanguageSiblingsPhase()); - manager.addX4OPhase(factory.readSAXStreamPhase()); - - // inject and opt phase - manager.addX4OPhase(factory.configGlobalElBeansPhase()); -// if (elementLanguage.hasX4ODebugWriter()) {manager.addX4OPhaseHandler(factory.debugPhase());} - - // meta start point -// manager.addX4OPhase(factory.startX4OPhase()); -// if (elementLanguage.hasX4ODebugWriter()) {manager.addX4OPhaseHandler(factory.debugPhase());} - - // config - X4OPhaseReadRunConfigurator runConf = factory.readRunConfiguratorPhase(); - manager.addX4OPhase(factory.configElementPhase(runConf)); - manager.addX4OPhase(factory.configElementInterfacePhase(runConf)); - manager.addX4OPhase(factory.configGlobalElementPhase(runConf)); - manager.addX4OPhase(factory.configGlobalAttributePhase(runConf)); - - // run all attribute events - manager.addX4OPhase(factory.runAttributesPhase()); -// if (elementLanguage.hasX4ODebugWriter()) {manager.addX4OPhaseHandler(factory.debugPhase());} - // templating - manager.addX4OPhase(factory.fillTemplatingPhase()); -// if (elementLanguage.hasX4ODebugWriter()) {manager.addX4OPhaseHandler(factory.debugPhase());} - - // transforming - manager.addX4OPhase(factory.transformPhase()); - manager.addX4OPhase(factory.runDirtyElementPhase(manager)); -// if (elementLanguage.hasX4ODebugWriter()) {manager.addX4OPhaseHandler(factory.debugPhase());} - - // binding elements - manager.addX4OPhase(factory.bindElementPhase()); - - // runing and releasing - manager.addX4OPhase(factory.runPhase()); -// if (elementLanguage.hasX4ODebugWriter()) {manager.addX4OPhaseHandler(factory.debugPhase());} - - manager.addX4OPhase(runConf); - - manager.addX4OPhase(factory.runDirtyElementLastPhase(manager)); -// if (elementLanguage.hasX4ODebugWriter()) {manager.addX4OPhaseHandler(factory.debugPhase());} - - - manager.addX4OPhase(factory.readEndX4OPhase()); - - // after release phase Element Tree is not avible anymore - manager.addX4OPhase(factory.releasePhase()); - - // Add debug phase listener to all phases -// if (elementLanguage.hasX4ODebugWriter()) { - //for (X4OPhase h:manager.getOrderedPhases()) { -// h.addPhaseListener(elementLanguage.getX4ODebugWriter().createDebugX4OPhaseListener()); - //} - //} - - // We are done - } - - enum R { - - /** Defines this meta startup phase. */ - startupX4OPhase(true), - - /** Load all meta info of the language we are creating. */ - createLanguagePhase(true), - - /** Load all siblings languages. */ - createLanguageSiblingsPhase(true), - - /** Parse the xml from sax events. */ - parseSAXStreamPhase(true), - - /** Optional extra config phase for injecting bean instances into the EL context. */ - configGlobalElBeansPhase(true), - - /** emty meta phase to refer to that sax is ready and element s are waiting for processing. */ - startX4OPhase(true), - - /** re runnable phases which config xml to beans and binds them together. */ - configElementPhase, - configElementInterfacePhase, - configGlobalElementPhase, - configGlobalAttributePhase, - - /** Fill the bean attributes from the Element xml attributes. */ - runAttributesPhase, - - /** Fill in the x4o templating objects. */ - fillTemplatingPhase, - - /** transform phase , modifies the Element Tree. */ - transformPhase, - - /** Run the phases which needs to be runned again from a phase. */ - runDirtyElementPhase(true), - - /** Binds objects together */ - bindElementPhase, - - /** Run action stuff, we are ready with it. */ - runPhase(true), - - /** Rerun all needed phases for all element that requested it. */ - runDirtyElementLastPhase, - - /** Releases all Elements, which clears attributes and childeren etc. */ - releasePhase(true), - - /** write all phases and stuff to debug sax stream. */ - debugPhase; - - /** Defines which phase we start, when context is created. */ - public static final R FIRST_PHASE = startupX4OPhase; - - /** The order in which the phases are executed */ - static final R[] PHASE_ORDER = { startupX4OPhase, - createLanguagePhase, - createLanguageSiblingsPhase, - - parseSAXStreamPhase, - configGlobalElBeansPhase, - startX4OPhase, - configElementPhase,configElementInterfacePhase,configGlobalElementPhase, - configGlobalAttributePhase,runAttributesPhase,fillTemplatingPhase, - transformPhase,runDirtyElementPhase,bindElementPhase, - runPhase,runDirtyElementLastPhase, - releasePhase - }; - - /** Boolean indicating that this phase only may be run'ed once. */ - private boolean runOnce = false; - - /** - * Creates an X4O Phase. - */ - private R() { - } - - /** - * Creates an X4O Phase - * @param runOnce Flag indicating that this phase is runnable multiple times. - */ - private R(boolean runOnce) { - this.runOnce=runOnce; - } - - /** - * Returns a flag indicating that this phase is runnable multiple times. - * @return True if phase is restricted to run once. - */ - public boolean isRunOnce() { - return runOnce; - } - } -} diff --git a/x4o-core/src/main/resources/META-INF/eld/eld-conv-1.0.xsd b/x4o-core/src/main/resources/META-INF/eld/eld-conv-1.0.xsd index ba7f45f..8564ab7 100644 --- a/x4o-core/src/main/resources/META-INF/eld/eld-conv-1.0.xsd +++ b/x4o-core/src/main/resources/META-INF/eld/eld-conv-1.0.xsd @@ -7,11 +7,11 @@ ModuleName: Element Language Definition Namespaces: 3 Namespace: http://eld.x4o.org/xml/ns/eld-conv - Created on: Tue Jan 08 06:03:20 CET 2013 + Created on: Mon Apr 08 02:26:14 CEST 2013 --> - diff --git a/x4o-core/src/main/resources/META-INF/eld/eld-lang-1.0.xsd b/x4o-core/src/main/resources/META-INF/eld/eld-lang-1.0.xsd index 742be58..606157d 100644 --- a/x4o-core/src/main/resources/META-INF/eld/eld-lang-1.0.xsd +++ b/x4o-core/src/main/resources/META-INF/eld/eld-lang-1.0.xsd @@ -7,11 +7,11 @@ ModuleName: Element Language Definition Namespaces: 3 Namespace: http://eld.x4o.org/xml/ns/eld-lang - Created on: Tue Jan 08 06:03:20 CET 2013 + Created on: Mon Apr 08 02:26:14 CEST 2013 --> - @@ -20,13 +20,13 @@ - + @@ -55,10 +55,10 @@ + - @@ -126,13 +126,13 @@ - - - + + + @@ -156,9 +156,10 @@ - - - + + + + diff --git a/x4o-core/src/main/resources/META-INF/eld/eld-lang.eld b/x4o-core/src/main/resources/META-INF/eld/eld-lang.eld index 72d6227..4d83e69 100644 --- a/x4o-core/src/main/resources/META-INF/eld/eld-lang.eld +++ b/x4o-core/src/main/resources/META-INF/eld/eld-lang.eld @@ -234,17 +234,20 @@ The id for this binding handler. - + The parent class. - + The child class. - + The method name of the method used to add the child to the parent. + + The method name of the method used to get the childeren of the parent. + diff --git a/x4o-core/src/main/resources/META-INF/eld/eld-root-1.0.xsd b/x4o-core/src/main/resources/META-INF/eld/eld-root-1.0.xsd index b08851f..e0c7194 100644 --- a/x4o-core/src/main/resources/META-INF/eld/eld-root-1.0.xsd +++ b/x4o-core/src/main/resources/META-INF/eld/eld-root-1.0.xsd @@ -7,11 +7,11 @@ ModuleName: Element Language Definition Namespaces: 3 Namespace: http://eld.x4o.org/xml/ns/eld-root - Created on: Tue Jan 08 06:03:20 CET 2013 + Created on: Mon Apr 08 02:26:14 CEST 2013 --> - @@ -28,10 +28,10 @@ - - + + diff --git a/x4o-core/src/test/java/org/x4o/xml/X4ODriverManagerTest.java b/x4o-core/src/test/java/org/x4o/xml/X4ODriverManagerTest.java index 363d48a..0d7ed50 100644 --- a/x4o-core/src/test/java/org/x4o/xml/X4ODriverManagerTest.java +++ b/x4o-core/src/test/java/org/x4o/xml/X4ODriverManagerTest.java @@ -26,8 +26,6 @@ package org.x4o.xml; import java.util.List; import org.x4o.xml.X4ODriverManager; -import org.x4o.xml.lang.X4OLanguageLoaderException; -import org.x4o.xml.lang.phase.X4OPhaseException; import junit.framework.TestCase; @@ -68,13 +66,14 @@ public class X4ODriverManagerTest extends TestCase { public void testLanguageVersionNonExcisting() throws Exception { String language = "test"; String version = "99.9"; - Exception e = null; + Throwable e = null; try { - X4ODriver driver = X4ODriverManager.getX4ODriver(language); + X4ODriver driver = X4ODriverManager.getX4ODriver(language); driver.createLanguageContext(version); - } catch (Exception catchE) { + } catch (Throwable catchE) { e = catchE; } + assertNotNull(e); /* TODO assertNotNull(e); assertNotNull(e.getCause()); diff --git a/x4o-core/src/test/java/org/x4o/xml/io/X4OWriterTest.java b/x4o-core/src/test/java/org/x4o/xml/io/X4OWriterTest.java new file mode 100644 index 0000000..19dce9a --- /dev/null +++ b/x4o-core/src/test/java/org/x4o/xml/io/X4OWriterTest.java @@ -0,0 +1,70 @@ +/* + * 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. + */ + +package org.x4o.xml.io; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.Scanner; + +import org.x4o.xml.X4ODriver; +import org.x4o.xml.io.X4OReader; +import org.x4o.xml.lang.X4OLanguagePropertyKeys; +import org.x4o.xml.test.TestDriver; +import org.x4o.xml.test.models.TestObjectRoot; + +import junit.framework.TestCase; + +/** + * X4ODebugWriterTest runs parser with debug output. + * + * @author Willem Cazander + * @version 1.0 Aug 26, 2012 + */ +public class X4OWriterTest extends TestCase { + + + private File createOutputFile() throws IOException { + File outputFile = File.createTempFile("test-writer", ".xml"); + outputFile.deleteOnExit(); + return outputFile; + } + + public void testWriterOutput() throws Exception { + File outputFile = createOutputFile(); + X4ODriver driver = TestDriver.getInstance(); + X4OReader reader = driver.createReader(); + X4OWriter writer = driver.createWriter(); + + TestObjectRoot root = reader.readResource("tests/attributes/test-bean.xml"); + writer.writeFile(root, outputFile); + + assertTrue("Debug file does not exists.",outputFile.exists()); + + String text = new Scanner( outputFile ).useDelimiter("\\A").next(); + System.out.println("Output: '\n"+text+"\n' end in "+outputFile.getAbsolutePath()); + + outputFile.delete(); + } +} diff --git a/x4o-core/src/test/java/org/x4o/xml/lang/DefaultX4OLanguageLoaderTest.java b/x4o-core/src/test/java/org/x4o/xml/lang/DefaultX4OLanguageLoaderTest.java index a52d5f2..6cf86a6 100644 --- a/x4o-core/src/test/java/org/x4o/xml/lang/DefaultX4OLanguageLoaderTest.java +++ b/x4o-core/src/test/java/org/x4o/xml/lang/DefaultX4OLanguageLoaderTest.java @@ -48,7 +48,7 @@ public class DefaultX4OLanguageLoaderTest extends TestCase { //X4OReader reader = driver.createReader(); //reader.readResource("tests/namespace/uri-simple.xml"); language = driver.createLanguageContext().getLanguage(); - loader = (X4OLanguageLoader)language.getLanguageConfiguration().getDefaultX4OLanguageLoader().newInstance(); + loader = (X4OLanguageLoader)language.getLanguageConfiguration().getDefaultLanguageLoader().newInstance(); } diff --git a/x4o-core/src/test/java/org/x4o/xml/test/models/TestObjectParent.java b/x4o-core/src/test/java/org/x4o/xml/test/models/TestObjectParent.java index bf7925b..ca7db72 100644 --- a/x4o-core/src/test/java/org/x4o/xml/test/models/TestObjectParent.java +++ b/x4o-core/src/test/java/org/x4o/xml/test/models/TestObjectParent.java @@ -34,7 +34,11 @@ public class TestObjectParent { public void addTestObjectChild(TestObjectChild c) { testObjectChilds.add(c); } - + + public List getTestObjectChilds() { + return testObjectChilds; + } + /** * @return the name */ diff --git a/x4o-core/src/test/java/org/x4o/xml/test/swixml/SwiXmlActionConfigurator.java b/x4o-core/src/test/java/org/x4o/xml/test/swixml/SwiXmlActionConfigurator.java index 2df3f64..46d21f8 100644 --- a/x4o-core/src/test/java/org/x4o/xml/test/swixml/SwiXmlActionConfigurator.java +++ b/x4o-core/src/test/java/org/x4o/xml/test/swixml/SwiXmlActionConfigurator.java @@ -43,7 +43,7 @@ public class SwiXmlActionConfigurator extends AbstractElementConfigurator { if (actionName==null) { return; } - SwingEngine se = SwiXmlDriver.getSwingEngine(element.getElementLanguage()); + SwingEngine se = SwiXmlDriver.getSwingEngine(element.getLanguageContext()); Action action = se.getUIActionByName(actionName); Object object = element.getElementObject(); if (object instanceof JMenuItem) { diff --git a/x4o-core/src/test/java/org/x4o/xml/test/swixml/SwiXmlDriver.java b/x4o-core/src/test/java/org/x4o/xml/test/swixml/SwiXmlDriver.java index 0831cbd..c702003 100644 --- a/x4o-core/src/test/java/org/x4o/xml/test/swixml/SwiXmlDriver.java +++ b/x4o-core/src/test/java/org/x4o/xml/test/swixml/SwiXmlDriver.java @@ -34,7 +34,6 @@ import org.x4o.xml.lang.DefaultX4OLanguageConfiguration; import org.x4o.xml.lang.X4OLanguageContext; import org.x4o.xml.lang.X4OLanguage; import org.x4o.xml.lang.X4OLanguagePropertyKeys; -import org.x4o.xml.lang.phase.X4OPhaseManagerFactory; /** * SwiXmlParser works with the SwingEngine to config the UI tree. @@ -111,9 +110,4 @@ public class SwiXmlDriver extends X4ODriver { public String[] getLanguageVersions() { return LANGUAGE_VERSIONS; } - - @Override - public X4OLanguage buildLanguage(String version) { - return new DefaultX4OLanguage(new DefaultX4OLanguageConfiguration(),X4OPhaseManagerFactory.createDefaultX4OPhaseManager(),getLanguageName(),getLanguageVersionDefault()); - } } diff --git a/x4o-core/src/test/java/org/x4o/xml/test/swixml/bind/JFrameBindingHandler.java b/x4o-core/src/test/java/org/x4o/xml/test/swixml/bind/JFrameBindingHandler.java index ecccfd0..16954bc 100644 --- a/x4o-core/src/test/java/org/x4o/xml/test/swixml/bind/JFrameBindingHandler.java +++ b/x4o-core/src/test/java/org/x4o/xml/test/swixml/bind/JFrameBindingHandler.java @@ -24,6 +24,7 @@ package org.x4o.xml.test.swixml.bind; import java.awt.BorderLayout; +import java.awt.Component; import javax.swing.JComponent; import javax.swing.JFrame; @@ -39,7 +40,7 @@ import org.x4o.xml.element.ElementBindingHandlerException; * @author Willem Cazander * @version 1.0 Aug 16, 2012 */ -public class JFrameBindingHandler extends AbstractElementBindingHandler { +public class JFrameBindingHandler extends AbstractElementBindingHandler { /** * @see org.x4o.xml.element.ElementBindingHandler#getBindParentClass() @@ -56,9 +57,9 @@ public class JFrameBindingHandler extends AbstractElementBindingHandler { } /** - * @see org.x4o.xml.element.ElementBindingHandler#doBind(java.lang.Object, java.lang.Object, org.x4o.xml.element.Element) + * @see org.x4o.xml.element.ElementBindingHandler#bindChild(org.x4o.xml.element.Element, java.lang.Object, java.lang.Object) */ - public void doBind(Object parentObject, Object childObject, Element childElement) throws ElementBindingHandlerException { + public void bindChild(Element childElement, JFrame parentObject, Object childObject) throws ElementBindingHandlerException { JFrame frame = (JFrame)parentObject; if (childObject instanceof JMenuBar) { JMenuBar child = (JMenuBar)childObject; @@ -85,4 +86,17 @@ public class JFrameBindingHandler extends AbstractElementBindingHandler { } } } + + /** + * @see org.x4o.xml.element.AbstractElementBindingHandler#createChilderen(org.x4o.xml.element.Element, java.lang.Object) + */ + @Override + public void createChilderen(Element parentElement,JFrame parent) throws ElementBindingHandlerException { + createChild(parentElement, parent.getMenuBar()); + for (Component c:parent.getComponents()) { + if (c instanceof JComponent) { + createChild(parentElement, c); + } + } + } } \ No newline at end of file diff --git a/x4o-core/src/test/java/org/x4o/xml/test/swixml/bind/JInternalFrameBindingHandler.java b/x4o-core/src/test/java/org/x4o/xml/test/swixml/bind/JInternalFrameBindingHandler.java index 6f3f982..459af57 100644 --- a/x4o-core/src/test/java/org/x4o/xml/test/swixml/bind/JInternalFrameBindingHandler.java +++ b/x4o-core/src/test/java/org/x4o/xml/test/swixml/bind/JInternalFrameBindingHandler.java @@ -24,6 +24,7 @@ package org.x4o.xml.test.swixml.bind; import java.awt.BorderLayout; +import java.awt.Component; import javax.swing.JComponent; import javax.swing.JInternalFrame; @@ -38,7 +39,7 @@ import org.x4o.xml.element.ElementBindingHandlerException; * @author Willem Cazander * @version 1.0 Aug 16, 2012 */ -public class JInternalFrameBindingHandler extends AbstractElementBindingHandler { +public class JInternalFrameBindingHandler extends AbstractElementBindingHandler { /** * @see org.x4o.xml.element.ElementBindingHandler#getBindParentClass() @@ -55,9 +56,9 @@ public class JInternalFrameBindingHandler extends AbstractElementBindingHandler } /** - * @see org.x4o.xml.element.ElementBindingHandler#doBind(java.lang.Object, java.lang.Object, org.x4o.xml.element.Element) + * @see org.x4o.xml.element.ElementBindingHandler#bindChild(org.x4o.xml.element.Element, java.lang.Object, java.lang.Object) */ - public void doBind(Object parentObject, Object childObject, Element childElement) throws ElementBindingHandlerException { + public void bindChild(Element childElement, JInternalFrame frame, Object childObject) throws ElementBindingHandlerException { JComponent child = (JComponent)childObject; String c = childElement.getAttributes().get("constraints"); @@ -71,12 +72,22 @@ public class JInternalFrameBindingHandler extends AbstractElementBindingHandler } else if ("BorderLayout.SOUTH".equals(c)) { con = BorderLayout.SOUTH; } - - JInternalFrame frame = (JInternalFrame)parentObject; if (con==null) { frame.getContentPane().add(child); } else { frame.getContentPane().add(child,con); } } -} \ No newline at end of file + + /** + * @see org.x4o.xml.element.AbstractElementBindingHandler#createChilderen(org.x4o.xml.element.Element, java.lang.Object) + */ + @Override + public void createChilderen(Element parentElement,JInternalFrame parent) throws ElementBindingHandlerException { + for (Component c:parent.getComponents()) { + if (c instanceof JComponent) { + createChild(parentElement, c); + } + } + } +} diff --git a/x4o-core/src/test/java/org/x4o/xml/test/swixml/bind/JPanelBindingHandler.java b/x4o-core/src/test/java/org/x4o/xml/test/swixml/bind/JPanelBindingHandler.java index 414bbec..0057e8b 100644 --- a/x4o-core/src/test/java/org/x4o/xml/test/swixml/bind/JPanelBindingHandler.java +++ b/x4o-core/src/test/java/org/x4o/xml/test/swixml/bind/JPanelBindingHandler.java @@ -24,6 +24,7 @@ package org.x4o.xml.test.swixml.bind; import java.awt.BorderLayout; +import java.awt.Component; import javax.swing.JComponent; import javax.swing.JPanel; @@ -38,7 +39,7 @@ import org.x4o.xml.element.ElementBindingHandlerException; * @author Willem Cazander * @version 1.0 Aug 16, 2012 */ -public class JPanelBindingHandler extends AbstractElementBindingHandler { +public class JPanelBindingHandler extends AbstractElementBindingHandler { /** * @see org.x4o.xml.element.ElementBindingHandler#getBindParentClass() @@ -55,9 +56,9 @@ public class JPanelBindingHandler extends AbstractElementBindingHandler { } /** - * @see org.x4o.xml.element.ElementBindingHandler#doBind(java.lang.Object, java.lang.Object, org.x4o.xml.element.Element) + * @see org.x4o.xml.element.ElementBindingHandler#bindChild(org.x4o.xml.element.Element, java.lang.Object, java.lang.Object) */ - public void doBind(Object parentObject, Object childObject, Element childElement) throws ElementBindingHandlerException { + public void bindChild(Element childElement, JPanel parent, Object childObject) throws ElementBindingHandlerException { JComponent child = (JComponent)childObject; String c = childElement.getAttributes().get("constraints"); @@ -71,12 +72,22 @@ public class JPanelBindingHandler extends AbstractElementBindingHandler { } else if ("BorderLayout.SOUTH".equals(c)) { con = BorderLayout.SOUTH; } - - JPanel parent = (JPanel)parentObject; if (con==null) { parent.add(child); } else { parent.add(child,con); } } + + /** + * @see org.x4o.xml.element.AbstractElementBindingHandler#createChilderen(org.x4o.xml.element.Element, java.lang.Object) + */ + @Override + public void createChilderen(Element parentElement,JPanel parent) throws ElementBindingHandlerException { + for (Component c:parent.getComponents()) { + if (c instanceof JComponent) { + createChild(parentElement, c); + } + } + } } diff --git a/x4o-core/src/test/java/org/x4o/xml/test/swixml/bind/JSplitPaneBindingHandler.java b/x4o-core/src/test/java/org/x4o/xml/test/swixml/bind/JSplitPaneBindingHandler.java index ec80790..1fef7b4 100644 --- a/x4o-core/src/test/java/org/x4o/xml/test/swixml/bind/JSplitPaneBindingHandler.java +++ b/x4o-core/src/test/java/org/x4o/xml/test/swixml/bind/JSplitPaneBindingHandler.java @@ -37,7 +37,7 @@ import org.x4o.xml.element.ElementBindingHandlerException; * @author Willem Cazander * @version 1.0 Aug 16, 2012 */ -public class JSplitPaneBindingHandler extends AbstractElementBindingHandler { +public class JSplitPaneBindingHandler extends AbstractElementBindingHandler { /** * @see org.x4o.xml.element.ElementBindingHandler#getBindParentClass() @@ -54,11 +54,10 @@ public class JSplitPaneBindingHandler extends AbstractElementBindingHandler { } /** - * @see org.x4o.xml.element.ElementBindingHandler#doBind(java.lang.Object, java.lang.Object, org.x4o.xml.element.Element) + * @see org.x4o.xml.element.ElementBindingHandler#bindChild(org.x4o.xml.element.Element, java.lang.Object, java.lang.Object, ) */ - public void doBind(Object parentObject, Object childObject, Element childElement) throws ElementBindingHandlerException { + public void bindChild(Element childElement, JSplitPane pane, Object childObject) throws ElementBindingHandlerException { JComponent child = (JComponent)childObject; - JSplitPane pane = (JSplitPane)parentObject; if (pane.getLeftComponent() instanceof JButton) { // strange swing constructor for splitpane pane.setLeftComponent(child); } else if (pane.getRightComponent() instanceof JButton) { @@ -67,4 +66,13 @@ public class JSplitPaneBindingHandler extends AbstractElementBindingHandler { throw new ElementBindingHandlerException("SplitPane is full."); } } + + /** + * @see org.x4o.xml.element.AbstractElementBindingHandler#createChilderen(org.x4o.xml.element.Element, java.lang.Object) + */ + @Override + public void createChilderen(Element parentElement,JSplitPane parentObject) throws ElementBindingHandlerException { + createChild(parentElement, parentObject.getLeftComponent()); + createChild(parentElement, parentObject.getRightComponent()); + } } \ No newline at end of file diff --git a/x4o-core/src/test/resources/META-INF/swixml/swixml-lang-2.0.eld b/x4o-core/src/test/resources/META-INF/swixml/swixml-lang-2.0.eld index c7e8a5b..cdb84d7 100644 --- a/x4o-core/src/test/resources/META-INF/swixml/swixml-lang-2.0.eld +++ b/x4o-core/src/test/resources/META-INF/swixml/swixml-lang-2.0.eld @@ -38,11 +38,11 @@ - - - - - + + + + + diff --git a/x4o-core/src/test/resources/META-INF/swixml/swixml-lang-3.0.eld b/x4o-core/src/test/resources/META-INF/swixml/swixml-lang-3.0.eld index a038c8d..00f4d65 100644 --- a/x4o-core/src/test/resources/META-INF/swixml/swixml-lang-3.0.eld +++ b/x4o-core/src/test/resources/META-INF/swixml/swixml-lang-3.0.eld @@ -38,11 +38,11 @@ - - - - - + + + + + diff --git a/x4o-core/src/test/resources/META-INF/test/test-lang.eld b/x4o-core/src/test/resources/META-INF/test/test-lang.eld index cc08648..9b2da10 100644 --- a/x4o-core/src/test/resources/META-INF/test/test-lang.eld +++ b/x4o-core/src/test/resources/META-INF/test/test-lang.eld @@ -34,22 +34,22 @@ id="test-module" > - + Binds the TestObjectChild to the TestObjectParent - + Binds the TestBean to the TestObjectChild - + Binds the TestObjectParent to the TestObjectRoot - + Binds the TestBean to the TestObjectRoot - + Binds j components. - + Binds panel to frame components as unit check. @@ -67,7 +67,7 @@ Configs the JComponent based objects. - + Binds the JComponent to the JComponent. diff --git a/x4o-meta/src/main/java/org/x4o/xml/meta/lang/ELIDAttributeHandler.java b/x4o-meta/src/main/java/org/x4o/xml/meta/lang/ELIDAttributeHandler.java index 821f61f..aa58139 100644 --- a/x4o-meta/src/main/java/org/x4o/xml/meta/lang/ELIDAttributeHandler.java +++ b/x4o-meta/src/main/java/org/x4o/xml/meta/lang/ELIDAttributeHandler.java @@ -54,8 +54,8 @@ public class ELIDAttributeHandler extends AbstractElementAttributeHandler { return; } if(element.getElementObject()==null) { throw new NullPointerException("Can't bind null object to el context"); } - ValueExpression ee = element.getElementLanguage().getExpressionLanguageFactory().createValueExpression(element.getElementLanguage().getExpressionLanguageContext(),"${"+attributeValue+"}", element.getElementObject().getClass()); + ValueExpression ee = element.getLanguageContext().getExpressionLanguageFactory().createValueExpression(element.getLanguageContext().getExpressionLanguageContext(),"${"+attributeValue+"}", element.getElementObject().getClass()); Logger.getLogger(ELIDAttributeHandler.class.getName()).finer("Set Variable in ELContext: "+"${"+attributeValue+"}"+" object SET: "+element.getElementObject()); - ee.setValue(element.getElementLanguage().getExpressionLanguageContext(), element.getElementObject()); + ee.setValue(element.getLanguageContext().getExpressionLanguageContext(), element.getElementObject()); } } diff --git a/x4o-meta/src/main/java/org/x4o/xml/meta/lang/ELReferenceElement.java b/x4o-meta/src/main/java/org/x4o/xml/meta/lang/ELReferenceElement.java index 925ea3c..6a2bc19 100644 --- a/x4o-meta/src/main/java/org/x4o/xml/meta/lang/ELReferenceElement.java +++ b/x4o-meta/src/main/java/org/x4o/xml/meta/lang/ELReferenceElement.java @@ -44,8 +44,8 @@ public class ELReferenceElement extends AbstractElement { public void doElementRun() throws ElementException { String attributeValue = getAttributes().get("el.ref"); if("".equals(attributeValue) | attributeValue==null) { throw new ElementException("Set the el.ref attribute"); } - ValueExpression ee = getElementLanguage().getExpressionLanguageFactory().createValueExpression(getElementLanguage().getExpressionLanguageContext(),"${"+attributeValue+"}",Object.class); + ValueExpression ee = getLanguageContext().getExpressionLanguageFactory().createValueExpression(getLanguageContext().getExpressionLanguageContext(),"${"+attributeValue+"}",Object.class); Logger.getLogger(ELReferenceElement.class.getName()).finer("Get Variable in ELContext: ${"+attributeValue+"}"); - setElementObject(ee.getValue(getElementLanguage().getExpressionLanguageContext())); + setElementObject(ee.getValue(getLanguageContext().getExpressionLanguageContext())); } } diff --git a/x4o-meta/src/main/java/org/x4o/xml/meta/lang/PropertyElement.java b/x4o-meta/src/main/java/org/x4o/xml/meta/lang/PropertyElement.java index 9cd58d0..0a18d7c 100644 --- a/x4o-meta/src/main/java/org/x4o/xml/meta/lang/PropertyElement.java +++ b/x4o-meta/src/main/java/org/x4o/xml/meta/lang/PropertyElement.java @@ -65,7 +65,7 @@ public class PropertyElement extends AbstractElement { // convert value object try { - value = getElementLanguage().getElementAttributeValueParser().getParameterValue(name,valueString,this); + value = getLanguageContext().getElementAttributeValueParser().getParameterValue(name,valueString,this); } catch (ObjectConverterException ece) { throw new ElementException(ece); } @@ -102,7 +102,7 @@ public class PropertyElement extends AbstractElement { // try to set as property on bean. try { - getElementLanguage().getElementObjectPropertyValue().setProperty(getParent().getElementObject(), name, value); + getLanguageContext().getElementObjectPropertyValue().setProperty(getParent().getElementObject(), name, value); return; } catch (Exception e) { throw new ElementException("Could not set property on parent element object: "+name,e); diff --git a/x4o-meta/src/test/java/org/x4o/xml/meta/test/MTestDriver.java b/x4o-meta/src/test/java/org/x4o/xml/meta/test/MTestDriver.java index a58f940..0de8532 100644 --- a/x4o-meta/src/test/java/org/x4o/xml/meta/test/MTestDriver.java +++ b/x4o-meta/src/test/java/org/x4o/xml/meta/test/MTestDriver.java @@ -27,10 +27,6 @@ import org.x4o.xml.X4ODriver; import org.x4o.xml.X4ODriverManager; import org.x4o.xml.io.X4OReaderContext; import org.x4o.xml.io.X4OWriterContext; -import org.x4o.xml.lang.DefaultX4OLanguage; -import org.x4o.xml.lang.DefaultX4OLanguageConfiguration; -import org.x4o.xml.lang.X4OLanguage; -import org.x4o.xml.lang.phase.X4OPhaseManagerFactory; public class MTestDriver extends X4ODriver { @@ -47,11 +43,6 @@ public class MTestDriver extends X4ODriver { return LANGUAGE_VERSIONS; } - @Override - public X4OLanguage buildLanguage(String version) { - return new DefaultX4OLanguage(new DefaultX4OLanguageConfiguration(),X4OPhaseManagerFactory.createDefaultX4OPhaseManager(),getLanguageName(),getLanguageVersionDefault()); - } - static public MTestDriver getInstance() { return (MTestDriver)X4ODriverManager.getX4ODriver(LANGUAGE_NAME); } diff --git a/x4o-plugin/x4o-plugin-ant-elddoc/pom.xml b/x4o-plugin/x4o-plugin-ant-elddoc/pom.xml index 526605a..ba35af5 100644 --- a/x4o-plugin/x4o-plugin-ant-elddoc/pom.xml +++ b/x4o-plugin/x4o-plugin-ant-elddoc/pom.xml @@ -94,11 +94,11 @@ - - - - - + + + + + diff --git a/x4o-plugin/x4o-plugin-ant-schema/pom.xml b/x4o-plugin/x4o-plugin-ant-schema/pom.xml index 28b0a03..3071bce 100644 --- a/x4o-plugin/x4o-plugin-ant-schema/pom.xml +++ b/x4o-plugin/x4o-plugin-ant-schema/pom.xml @@ -83,7 +83,7 @@ - + @@ -118,7 +118,7 @@ - +