From ed1732a62566f867dd3087a65b6e5dba99472068 Mon Sep 17 00:00:00 2001 From: Willem Cazander Date: Sun, 28 Apr 2013 18:51:23 +0200 Subject: [PATCH] Moved writer to phases. Refactored language properties. Refactored io connection exceptions. --- .../src/main/java/org/x4o/xml/X4ODriver.java | 53 ++- .../java/org/x4o/xml/X4ODriverManager.java | 26 +- .../org/x4o/xml/el/X4OExpressionFactory.java | 29 +- .../java/org/x4o/xml/eld/EldModuleLoader.java | 12 +- .../x4o/xml/eld/xsd/EldXsdXmlGenerator.java | 30 +- .../org/x4o/xml/io/AbstractX4OReader.java | 72 ++-- .../x4o/xml/io/AbstractX4OReaderContext.java | 54 ++- .../org/x4o/xml/io/AbstractX4OWriter.java | 35 +- .../x4o/xml/io/AbstractX4OWriterContext.java | 13 +- .../java/org/x4o/xml/io/DefaultX4OReader.java | 83 +++-- .../x4o/xml/io/DefaultX4OSchemaWriter.java | 6 +- .../java/org/x4o/xml/io/DefaultX4OWriter.java | 167 +-------- .../x4o/xml/io/X4OConnectionException.java | 68 ++++ .../main/java/org/x4o/xml/io/X4OReader.java | 63 ++-- .../java/org/x4o/xml/io/X4OReaderContext.java | 63 ++-- .../main/java/org/x4o/xml/io/X4OWriter.java | 9 +- .../java/org/x4o/xml/io/X4OWriterContext.java | 9 +- .../org/x4o/xml/io/sax/X4OEntityResolver.java | 6 +- .../org/x4o/xml/io/sax/X4OErrorHandler.java | 2 +- .../org/x4o/xml/io/sax/X4OTagHandler.java | 2 +- .../java/org/x4o/xml/io/sax/XMLWriter.java | 12 + .../AbstractX4OLanguageConfiguration.java | 324 ++++++++++++++++++ .../org/x4o/xml/lang/DefaultX4OLanguage.java | 14 +- .../lang/DefaultX4OLanguageConfiguration.java | 179 +++------- .../xml/lang/X4OLanguageConfiguration.java | 7 +- .../lang/X4OLanguageConfigurationLocal.java | 59 ++++ .../org/x4o/xml/lang/X4OLanguageProperty.java | 181 ++++++---- .../x4o/xml/lang/X4OLanguagePropertyKeys.java | 67 ++-- .../lang/phase/DefaultX4OPhaseManager.java | 8 +- .../xml/lang/phase/X4OPhaseLanguageRead.java | 8 +- .../xml/lang/phase/X4OPhaseLanguageWrite.java | 188 +++++++++- .../org/x4o/xml/core/NamespaceUriTest.java | 2 +- .../org/x4o/xml/core/X4ODebugWriterTest.java | 2 +- .../x4o/xml/core/X4OEntityResolverTest.java | 4 +- .../org/x4o/xml/eld/EldValidatingTest.java | 2 +- .../java/org/x4o/xml/io/X4OWriterTest.java | 9 +- .../java/org/x4o/xml/test/SwingTests.java | 2 +- 37 files changed, 1131 insertions(+), 739 deletions(-) create mode 100644 x4o-driver/src/main/java/org/x4o/xml/io/X4OConnectionException.java create mode 100644 x4o-driver/src/main/java/org/x4o/xml/lang/AbstractX4OLanguageConfiguration.java create mode 100644 x4o-driver/src/main/java/org/x4o/xml/lang/X4OLanguageConfigurationLocal.java diff --git a/x4o-driver/src/main/java/org/x4o/xml/X4ODriver.java b/x4o-driver/src/main/java/org/x4o/xml/X4ODriver.java index afc4d69..1d757ea 100644 --- a/x4o-driver/src/main/java/org/x4o/xml/X4ODriver.java +++ b/x4o-driver/src/main/java/org/x4o/xml/X4ODriver.java @@ -23,8 +23,6 @@ package org.x4o.xml; -import java.util.Collection; - import org.x4o.xml.io.DefaultX4OReader; import org.x4o.xml.io.DefaultX4OSchemaWriter; import org.x4o.xml.io.DefaultX4OWriter; @@ -37,7 +35,6 @@ import org.x4o.xml.io.X4OWriterContext; 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.X4OPhaseManager; /** @@ -69,6 +66,8 @@ public abstract class X4ODriver { + // =============== build methods to override + protected X4OLanguage buildLanguage(String version) { return X4ODriverManager.getDefaultBuildLanguage(this, version); } @@ -83,6 +82,8 @@ public abstract class X4ODriver { + // =============== SchemaWriter + public X4OSchemaWriter createSchemaWriter() { return createSchemaWriter(getLanguageVersionDefault()); } @@ -93,6 +94,8 @@ public abstract class X4ODriver { + // =============== Reader + public X4OReader createReader() { return createReaderContext(); } @@ -101,14 +104,6 @@ public abstract class X4ODriver { return createReaderContext(version); } - public X4OWriter createWriter() { - return createWriterContext(); - } - - public X4OWriter createWriter(String version) { - return createWriterContext(version); - } - public X4OReaderContext createReaderContext() { return createReaderContext(getLanguageVersionDefault()); } @@ -117,6 +112,18 @@ public abstract class X4ODriver { return new DefaultX4OReader(createLanguageContext(version)); } + + + // =============== Writer + + public X4OWriter createWriter() { + return createWriterContext(); + } + + public X4OWriter createWriter(String version) { + return createWriterContext(version); + } + public X4OWriterContext createWriterContext() { return createWriterContext(getLanguageVersionDefault()); } @@ -127,15 +134,11 @@ public abstract class X4ODriver { - public String getLanguageVersionDefault() { + // =============== Language + + final public String getLanguageVersionDefault() { return X4ODriverManager.getDefaultLanguageVersion(getLanguageVersions()); - } - - public String[] getGlobalPropertyKeySet() { - return X4OLanguagePropertyKeys.DEFAULT_X4O_GLOBAL_KEYS; - } - - + } final public X4OLanguage createLanguage(String version) { return buildLanguage(version); @@ -148,16 +151,4 @@ public abstract class X4ODriver { final public X4OLanguageContext createLanguageContext(String version) { return createLanguage(version).createLanguageContext(this); } - - final public Collection getGlobalPropertyKeys() { - return X4ODriverManager.getGlobalPropertyKeys(this); - } - - final public Object getGlobalProperty(String key) { - return X4ODriverManager.getGlobalProperty(this, key); - } - - final public void setGlobalProperty(String key,Object value) { - X4ODriverManager.setGlobalProperty(this, key, value); - } } diff --git a/x4o-driver/src/main/java/org/x4o/xml/X4ODriverManager.java b/x4o-driver/src/main/java/org/x4o/xml/X4ODriverManager.java index 4bfbe93..9f27ca4 100644 --- a/x4o-driver/src/main/java/org/x4o/xml/X4ODriverManager.java +++ b/x4o-driver/src/main/java/org/x4o/xml/X4ODriverManager.java @@ -27,7 +27,6 @@ import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.util.ArrayList; -import java.util.Collection; import java.util.Collections; import java.util.Enumeration; import java.util.HashMap; @@ -67,14 +66,12 @@ public final class X4ODriverManager { private Map classdrivers = null; private Map defaultDrivers = null; private Map> drivers = null; - private Map> globalProperties = null; private X4ODriverManager() { logger = Logger.getLogger(X4ODriverManager.class.getName()); classdrivers = new HashMap(10); defaultDrivers = new HashMap(10); drivers = new HashMap>(10); - globalProperties = new HashMap>(20); } static { @@ -110,25 +107,13 @@ public final class X4ODriverManager { } static protected X4OLanguageConfiguration getDefaultBuildLanguageConfiguration() { - return new DefaultX4OLanguageConfiguration(); - } - - static public Collection getGlobalPropertyKeys(X4ODriver driver) { - Map driverProperties = instance.globalProperties.get(driver.getLanguageName()); - if (driverProperties==null) { - return Collections.emptySet(); - } - return driverProperties.keySet(); - } - - static public Object getGlobalProperty(X4ODriver driver,String key) { - Map driverProperties = instance.globalProperties.get(driver.getLanguageName()); - if (driverProperties==null) { - return null; - } - return driverProperties.get(key); + DefaultX4OLanguageConfiguration config = new DefaultX4OLanguageConfiguration(); + config.fillDefaults(); + X4OLanguageConfiguration result = config.createProxy(); + return result; } + /* static public void setGlobalProperty(X4ODriver driver,String key,Object value) { Map driverProperties = instance.globalProperties.get(driver.getLanguageName()); if (driverProperties==null) { @@ -145,6 +130,7 @@ public final class X4ODriverManager { } throw new IllegalArgumentException("Property with key: "+key+" is protected by key limit."); } + */ static public void registerX4ODriver(X4ODriver driver) { if (driver==null) { diff --git a/x4o-driver/src/main/java/org/x4o/xml/el/X4OExpressionFactory.java b/x4o-driver/src/main/java/org/x4o/xml/el/X4OExpressionFactory.java index 1531666..92902c7 100644 --- a/x4o-driver/src/main/java/org/x4o/xml/el/X4OExpressionFactory.java +++ b/x4o-driver/src/main/java/org/x4o/xml/el/X4OExpressionFactory.java @@ -23,6 +23,7 @@ package org.x4o.xml.el; +import javax.el.ELContext; import javax.el.ExpressionFactory; import org.x4o.xml.lang.X4OLanguageClassLoader; @@ -40,23 +41,35 @@ 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; + static public ExpressionFactory createExpressionFactory(X4OLanguageContext languageContext) { + ExpressionFactory result = (ExpressionFactory)languageContext.getLanguageProperty(X4OLanguageProperty.EL_FACTORY_INSTANCE); + if (result!=null) { + return result; } try { Class expressionFactoryClass = X4OLanguageClassLoader.loadClass(EL_FACTORY_IMPL_APACHE); - ExpressionFactory expressionFactory = (ExpressionFactory) expressionFactoryClass.newInstance(); - return expressionFactory; + result = (ExpressionFactory) expressionFactoryClass.newInstance(); } catch (Exception e) { try { Class expressionFactoryClass = X4OLanguageClassLoader.loadClass(EL_FACTORY_IMPL_ODYSSEUS); - ExpressionFactory expressionFactory = (ExpressionFactory) expressionFactoryClass.newInstance(); - return expressionFactory; + result = (ExpressionFactory) expressionFactoryClass.newInstance(); } 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."); } } + return result; + } + + static public ELContext createELContext(X4OLanguageContext languageContext) { + ELContext result = (ELContext)languageContext.getLanguageProperty(X4OLanguageProperty.EL_CONTEXT_INSTANCE); + if (result!=null) { + return result; + } + try { + result = (ELContext)X4OLanguageClassLoader.newInstance(languageContext.getLanguage().getLanguageConfiguration().getDefaultExpressionLanguageContext()); + } catch (Exception e) { + throw new RuntimeException("Could not create instance of ELContext: "+e.getMessage(),e); + } + return result; } } diff --git a/x4o-driver/src/main/java/org/x4o/xml/eld/EldModuleLoader.java b/x4o-driver/src/main/java/org/x4o/xml/eld/EldModuleLoader.java index 7ff6864..eac69e2 100644 --- a/x4o-driver/src/main/java/org/x4o/xml/eld/EldModuleLoader.java +++ b/x4o-driver/src/main/java/org/x4o/xml/eld/EldModuleLoader.java @@ -23,15 +23,13 @@ package org.x4o.xml.eld; -import java.io.FileNotFoundException; import java.io.IOException; import java.util.logging.Logger; -import javax.xml.parsers.ParserConfigurationException; - import org.x4o.xml.X4ODriver; import org.x4o.xml.X4ODriverManager; import org.x4o.xml.io.DefaultX4OReader; +import org.x4o.xml.io.X4OConnectionException; import org.x4o.xml.io.X4OReader; import org.x4o.xml.lang.X4OLanguageModule; import org.x4o.xml.lang.X4OLanguageContext; @@ -106,13 +104,7 @@ public class EldModuleLoader implements X4OLanguageModuleLoader { // } reader.readResource(eldResource); - } catch (FileNotFoundException e) { - throw new X4OLanguageModuleLoaderException(this,e.getMessage()+" while parsing: "+eldResource,e); - } catch (SecurityException e) { - throw new X4OLanguageModuleLoaderException(this,e.getMessage()+" while parsing: "+eldResource,e); - } catch (NullPointerException e) { - throw new X4OLanguageModuleLoaderException(this,e.getMessage()+" while parsing: "+eldResource,e); - } catch (ParserConfigurationException e) { + } catch (X4OConnectionException e) { throw new X4OLanguageModuleLoaderException(this,e.getMessage()+" while parsing: "+eldResource,e); } catch (SAXException e) { throw new X4OLanguageModuleLoaderException(this,e.getMessage()+" while parsing: "+eldResource,e); diff --git a/x4o-driver/src/main/java/org/x4o/xml/eld/xsd/EldXsdXmlGenerator.java b/x4o-driver/src/main/java/org/x4o/xml/eld/xsd/EldXsdXmlGenerator.java index 9418064..6104f8d 100644 --- a/x4o-driver/src/main/java/org/x4o/xml/eld/xsd/EldXsdXmlGenerator.java +++ b/x4o-driver/src/main/java/org/x4o/xml/eld/xsd/EldXsdXmlGenerator.java @@ -29,9 +29,12 @@ import java.io.FileOutputStream; import org.x4o.xml.element.ElementClass; import org.x4o.xml.element.ElementException; import org.x4o.xml.element.ElementNamespaceContext; +import org.x4o.xml.io.XMLConstants; import org.x4o.xml.io.sax.XMLWriter; +import org.x4o.xml.lang.X4OLanguageContext; import org.x4o.xml.lang.X4OLanguageModule; import org.x4o.xml.lang.X4OLanguage; +import org.x4o.xml.lang.X4OLanguageProperty; import org.xml.sax.SAXException; import org.xml.sax.ext.DefaultHandler2; @@ -44,10 +47,11 @@ import org.xml.sax.ext.DefaultHandler2; public class EldXsdXmlGenerator { private X4OLanguage language = null; + private X4OLanguageContext languageContext = null; - public EldXsdXmlGenerator(X4OLanguage language) { - this.language=language; - + public EldXsdXmlGenerator(X4OLanguageContext languageContext) { + this.languageContext=languageContext; + this.language=languageContext.getLanguage(); } private void checkNamespace(ElementNamespaceContext ns) { @@ -59,8 +63,20 @@ public class EldXsdXmlGenerator { } } - public void writeSchema(File basePath,String namespace) throws ElementException { + public void writeSchema(String namespace) throws ElementException { + File basePath = (File)languageContext.getLanguageProperty(X4OLanguageProperty.SCHEMA_WRITER_OUTPUT_PATH); + String encoding = languageContext.getLanguagePropertyString(X4OLanguageProperty.SCHEMA_WRITER_OUTPUT_ENCODING); + String charNew = languageContext.getLanguagePropertyString(X4OLanguageProperty.SCHEMA_WRITER_OUTPUT_CHAR_NEWLINE); + String charTab = languageContext.getLanguagePropertyString(X4OLanguageProperty.SCHEMA_WRITER_OUTPUT_CHAR_TAB); + if (basePath==null) { + throw new ElementException("Can't write schema to null output path."); + } + if (encoding==null) { encoding = XMLConstants.XML_DEFAULT_ENCODING; } + if (charNew==null) { charNew = XMLConstants.CHAR_NEWLINE; } + if (charTab==null) { charTab = XMLConstants.CHAR_TAB; } try { + + if (namespace!=null) { ElementNamespaceContext ns = language.findElementNamespaceContext(namespace); if (ns==null) { @@ -69,7 +85,7 @@ public class EldXsdXmlGenerator { checkNamespace(ns); FileOutputStream fio = new FileOutputStream(new File(basePath.getAbsolutePath()+File.separatorChar+ns.getSchemaResource())); try { - XMLWriter out = new XMLWriter(fio); + XMLWriter out = new XMLWriter(fio,encoding,charNew,charTab); generateSchema(ns.getUri(), out); } finally { fio.close(); @@ -81,7 +97,7 @@ public class EldXsdXmlGenerator { checkNamespace(ns); FileOutputStream fio = new FileOutputStream(new File(basePath.getAbsolutePath()+File.separatorChar+ns.getSchemaResource())); try { - XMLWriter out = new XMLWriter(fio); + XMLWriter out = new XMLWriter(fio,encoding,charNew,charTab); generateSchema(ns.getUri(), out); } finally { fio.close(); @@ -90,7 +106,7 @@ public class EldXsdXmlGenerator { } } catch (Exception e) { - throw new ElementException(e); // todo rm + throw new ElementException(e); // TODO: rm } } diff --git a/x4o-driver/src/main/java/org/x4o/xml/io/AbstractX4OReader.java b/x4o-driver/src/main/java/org/x4o/xml/io/AbstractX4OReader.java index 2f87542..369051e 100644 --- a/x4o-driver/src/main/java/org/x4o/xml/io/AbstractX4OReader.java +++ b/x4o-driver/src/main/java/org/x4o/xml/io/AbstractX4OReader.java @@ -23,21 +23,18 @@ package org.x4o.xml.io; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.InputStream; -import java.net.URL; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; import org.x4o.xml.lang.X4OLanguageContext; import org.x4o.xml.lang.X4OLanguagePropertyKeys; -import org.xml.sax.SAXException; -import javax.xml.parsers.ParserConfigurationException; +import org.xml.sax.SAXException; /** - * This is a base class for building an SAX XML parser. - * It adds methode's for parsing different input types of - * xml data which gets wrapped into an InputStream for parsing. + * AbstractX4OReader wraps method to contexted reader. * * @author Willem Cazander * @version 1.0 Aug 11, 2005 @@ -56,78 +53,69 @@ abstract public class AbstractX4OReader extends AbstractX4OReaderContext i return X4OLanguagePropertyKeys.DEFAULT_X4O_READER_KEYS; } - public T read(InputStream input, String systemId, URL basePath) throws ParserConfigurationException, SAXException, IOException { + public T read(InputStream input, String systemId, URL basePath) throws X4OConnectionException,SAXException,IOException { return (T)readContext(input, systemId, basePath).getRootElement().getElementObject(); } /** * Reads the file fileName and reads it as an InputStream. * @param fileName The file name to read. - * @throws readrConfigurationException - * @throws FileNotFoundException - * @throws SecurityException - * @throws NullPointerException - * @throws SAXException - * @throws IOException + * @throws FileNotFoundException Is thrown is file is not found. + * @throws X4OConnectionException Is thrown after x4o exception. + * @throws SAXException Is thrown after sax xml exception. + * @throws IOException Is thrown after io exception. * @see org.x4o.xml.io.X4OReaderContext#readContext(java.io.InputStream,java.lang.String,java.net.URL) */ - public T readFile(String fileName) throws ParserConfigurationException,FileNotFoundException,SecurityException,NullPointerException,SAXException,IOException { + public T readFile(String fileName) throws X4OConnectionException,SAXException,IOException,FileNotFoundException { return (T)readFileContext(fileName).getRootElement().getElementObject(); } /** * Reads the file and reads it as an InputStream. * @param file The file to read. - * @throws readrConfigurationException - * @throws FileNotFoundException - * @throws SecurityException - * @throws NullPointerException - * @throws SAXException - * @throws IOException + * @throws FileNotFoundException Is thrown is file is not found. + * @throws X4OConnectionException Is thrown after x4o exception. + * @throws SAXException Is thrown after sax xml exception. + * @throws IOException Is thrown after io exception. * @see org.x4o.xml.io.X4OReaderContext#readContext(java.io.InputStream,java.lang.String,java.net.URL) */ - public T readFile(File file) throws ParserConfigurationException,FileNotFoundException,SecurityException,NullPointerException,SAXException,IOException { + public T readFile(File file) throws X4OConnectionException,SAXException,IOException,FileNotFoundException { return (T)readFileContext(file).getRootElement().getElementObject(); } /** * reads an resource locaction. * @param resourceName The resource to readr. - * @throws readrConfigurationException - * @throws FileNotFoundException - * @throws SecurityException - * @throws NullPointerException - * @throws SAXException - * @throws IOException + * @throws X4OConnectionException Is thrown after x4o exception. + * @throws SAXException Is thrown after sax xml exception. + * @throws IOException Is thrown after io exception. * @see org.x4o.xml.io.X4OReaderContext#readContext(java.io.InputStream,java.lang.String,java.net.URL) */ - public T readResource(String resourceName) throws ParserConfigurationException,FileNotFoundException,SecurityException,NullPointerException,SAXException,IOException { + public T readResource(String resourceName) throws X4OConnectionException,SAXException,IOException { return (T)readResourceContext(resourceName).getRootElement().getElementObject(); } /** * Converts a String to a InputStream to is can me readd by SAX. * @param xmlString The xml as String to read. - * @throws readrConfigurationException - * @throws SAXException - * @throws IOException - * @throws NullPointerException + * @throws X4OConnectionException Is thrown after x4o exception. + * @throws SAXException Is thrown after sax xml exception. + * @throws IOException Is thrown after io exception. * @see org.x4o.xml.io.X4OReaderContext#readContext(java.io.InputStream,java.lang.String,java.net.URL) */ - public T readString(String xmlString) throws ParserConfigurationException,SAXException,IOException,NullPointerException { + public T readString(String xmlString) throws X4OConnectionException,SAXException,IOException { return (T)readStringContext(xmlString).getRootElement().getElementObject(); } /** * Fetched the data direct from remote url to a InputStream to is can me readd by SAX. * @param url The url to read. - * @throws readrConfigurationException - * @throws SAXException - * @throws IOException - * @throws NullPointerException + * @throws X4OConnectionException Is thrown after x4o exception. + * @throws SAXException Is thrown after sax xml exception. + * @throws IOException Is thrown after io exception. * @see org.x4o.xml.io.X4OReaderContext#readContext(java.io.InputStream,java.lang.String,java.net.URL) */ - public T readUrl(URL url) throws ParserConfigurationException,SAXException,IOException,NullPointerException { + public T readUrl(URL url) throws X4OConnectionException,SAXException,IOException { return (T)readUrlContext(url).getRootElement().getElementObject(); } } diff --git a/x4o-driver/src/main/java/org/x4o/xml/io/AbstractX4OReaderContext.java b/x4o-driver/src/main/java/org/x4o/xml/io/AbstractX4OReaderContext.java index 76a928f..9069805 100644 --- a/x4o-driver/src/main/java/org/x4o/xml/io/AbstractX4OReaderContext.java +++ b/x4o-driver/src/main/java/org/x4o/xml/io/AbstractX4OReaderContext.java @@ -33,7 +33,6 @@ import java.net.URL; import org.x4o.xml.lang.X4OLanguageContext; import org.xml.sax.SAXException; -import javax.xml.parsers.ParserConfigurationException; /** * AbstractX4OContextReader @@ -51,15 +50,13 @@ abstract public class AbstractX4OReaderContext extends AbstractX4OConnection /** * Reads the file fileName and reads it as an InputStream. * @param fileName The file name to read. - * @throws readrConfigurationException - * @throws FileNotFoundException - * @throws SecurityException - * @throws NullPointerException - * @throws SAXException - * @throws IOException + * @throws FileNotFoundException Is thrown is file is not found. + * @throws X4OConnectionException Is thrown after x4o exception. + * @throws SAXException Is thrown after sax xml exception. + * @throws IOException Is thrown after io exception. * @see org.x4o.xml.io.X4OReaderContext#readContext(java.io.InputStream,java.lang.String,java.net.URL) */ - public X4OLanguageContext readFileContext(String fileName) throws ParserConfigurationException,FileNotFoundException,SecurityException,NullPointerException,SAXException,IOException { + public X4OLanguageContext readFileContext(String fileName) throws X4OConnectionException,SAXException,IOException,FileNotFoundException { if (fileName==null) { throw new NullPointerException("Can't convert null fileName to file object."); } @@ -69,15 +66,13 @@ abstract public class AbstractX4OReaderContext extends AbstractX4OConnection /** * Reads the file and reads it as an InputStream. * @param file The file to read. - * @throws readrConfigurationException - * @throws FileNotFoundException - * @throws SecurityException - * @throws NullPointerException - * @throws SAXException - * @throws IOException + * @throws FileNotFoundException Is thrown is file is not found. + * @throws X4OConnectionException Is thrown after x4o exception. + * @throws SAXException Is thrown after sax xml exception. + * @throws IOException Is thrown after io exception. * @see org.x4o.xml.io.X4OReaderContext#readContext(java.io.InputStream,java.lang.String,java.net.URL) */ - public X4OLanguageContext readFileContext(File file) throws ParserConfigurationException,FileNotFoundException,SecurityException,NullPointerException,SAXException,IOException { + public X4OLanguageContext readFileContext(File file) throws X4OConnectionException,SAXException,IOException,FileNotFoundException { if (file==null) { throw new NullPointerException("Can't read null file."); } @@ -101,15 +96,12 @@ abstract public class AbstractX4OReaderContext extends AbstractX4OConnection /** * reads an resource locaction. * @param resourceName The resource to readr. - * @throws readrConfigurationException - * @throws FileNotFoundException - * @throws SecurityException - * @throws NullPointerException - * @throws SAXException - * @throws IOException + * @throws X4OConnectionException Is thrown after x4o exception. + * @throws SAXException Is thrown after sax xml exception. + * @throws IOException Is thrown after io exception. * @see org.x4o.xml.io.X4OReaderContext#readContext(java.io.InputStream,java.lang.String,java.net.URL) */ - public X4OLanguageContext readResourceContext(String resourceName) throws ParserConfigurationException,FileNotFoundException,SecurityException,NullPointerException,SAXException,IOException { + public X4OLanguageContext readResourceContext(String resourceName) throws X4OConnectionException,SAXException,IOException { if (resourceName==null) { throw new NullPointerException("Can't read null resourceName from classpath."); } @@ -138,13 +130,12 @@ abstract public class AbstractX4OReaderContext extends AbstractX4OConnection /** * Converts a String to a InputStream to is can me readd by SAX. * @param xmlString The xml as String to read. - * @throws readrConfigurationException - * @throws SAXException - * @throws IOException - * @throws NullPointerException + * @throws X4OConnectionException Is thrown after x4o exception. + * @throws SAXException Is thrown after sax xml exception. + * @throws IOException Is thrown after io exception. * @see org.x4o.xml.io.X4OReaderContext#readContext(java.io.InputStream,java.lang.String,java.net.URL) */ - public X4OLanguageContext readStringContext(String xmlString) throws ParserConfigurationException,SAXException,IOException,NullPointerException { + public X4OLanguageContext readStringContext(String xmlString) throws X4OConnectionException,SAXException,IOException { if (xmlString==null) { throw new NullPointerException("Can't read null xml string."); } @@ -155,13 +146,12 @@ abstract public class AbstractX4OReaderContext extends AbstractX4OConnection /** * Fetched the data direct from remote url to a InputStream to is can me readd by SAX. * @param url The url to read. - * @throws readrConfigurationException - * @throws SAXException - * @throws IOException - * @throws NullPointerException + * @throws X4OConnectionException Is thrown after x4o exception. + * @throws SAXException Is thrown after sax xml exception. + * @throws IOException Is thrown after io exception. * @see org.x4o.xml.io.X4OReaderContext#readContext(java.io.InputStream,java.lang.String,java.net.URL) */ - public X4OLanguageContext readUrlContext(URL url) throws ParserConfigurationException,SAXException,IOException,NullPointerException { + public X4OLanguageContext readUrlContext(URL url) throws X4OConnectionException,SAXException,IOException { if (url==null) { throw new NullPointerException("Can't read null url."); } diff --git a/x4o-driver/src/main/java/org/x4o/xml/io/AbstractX4OWriter.java b/x4o-driver/src/main/java/org/x4o/xml/io/AbstractX4OWriter.java index bb276e9..c6fb21c 100644 --- a/x4o-driver/src/main/java/org/x4o/xml/io/AbstractX4OWriter.java +++ b/x4o-driver/src/main/java/org/x4o/xml/io/AbstractX4OWriter.java @@ -25,12 +25,9 @@ package org.x4o.xml.io; import java.io.File; import java.io.FileNotFoundException; -import java.io.FileOutputStream; import java.io.IOException; 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; @@ -55,7 +52,7 @@ public abstract class AbstractX4OWriter extends AbstractX4OWriterContext i return X4OLanguagePropertyKeys.DEFAULT_X4O_WRITER_KEYS; } - public void write(T object,OutputStream output) throws ParserConfigurationException, SAXException, IOException { + private X4OLanguageContext toObjectContext(T object) throws SAXException { X4OLanguageContext context = getLanguageContext(); Element rootElement = null; try { @@ -67,30 +64,18 @@ public abstract class AbstractX4OWriter extends AbstractX4OWriterContext i } rootElement.setElementObject(object); context.setRootElement(rootElement); - writeContext(context,output); + return context; } - public void writeFile(T object,String fileName) throws ParserConfigurationException,FileNotFoundException,SecurityException,NullPointerException,SAXException,IOException { - if (fileName==null) { - throw new NullPointerException("Can't convert null fileName to file object."); - } - writeFile(object,new File(fileName)); + public void write(T object,OutputStream output) throws X4OConnectionException,SAXException,IOException { + writeContext(toObjectContext(object), output); } - public void writeFile(T object,File file) throws ParserConfigurationException,FileNotFoundException,SecurityException,NullPointerException,SAXException,IOException { - if (file==null) { - throw new NullPointerException("Can't read null file."); - } - if (file.canWrite()==false) { - throw new IOException("Can't write file: "+file); - } - OutputStream outputStream = new FileOutputStream(file); - try { - write(object,outputStream); - } finally { - if(outputStream!=null) { - outputStream.close(); - } - } + public void writeFile(T object,String fileName) throws X4OConnectionException,SAXException,IOException,FileNotFoundException { + writeFileContext(toObjectContext(object), fileName); + } + + public void writeFile(T object,File file) throws X4OConnectionException,SAXException,IOException,FileNotFoundException { + writeFileContext(toObjectContext(object), file); } } diff --git a/x4o-driver/src/main/java/org/x4o/xml/io/AbstractX4OWriterContext.java b/x4o-driver/src/main/java/org/x4o/xml/io/AbstractX4OWriterContext.java index 16e8e94..c21803e 100644 --- a/x4o-driver/src/main/java/org/x4o/xml/io/AbstractX4OWriterContext.java +++ b/x4o-driver/src/main/java/org/x4o/xml/io/AbstractX4OWriterContext.java @@ -24,13 +24,10 @@ package org.x4o.xml.io; import java.io.File; -import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; -import javax.xml.parsers.ParserConfigurationException; - import org.x4o.xml.lang.X4OLanguageContext; import org.xml.sax.SAXException; @@ -46,23 +43,17 @@ public abstract class AbstractX4OWriterContext extends AbstractX4OConnection super(elementLanguage); } - public void writeFileContext(X4OLanguageContext context,String fileName) throws ParserConfigurationException,FileNotFoundException,SecurityException,NullPointerException,SAXException,IOException { + public void writeFileContext(X4OLanguageContext context,String fileName) throws X4OConnectionException,SAXException,IOException { if (fileName==null) { throw new NullPointerException("Can't convert null fileName to file object."); } writeFileContext(context,new File(fileName)); } - public void writeFileContext(X4OLanguageContext context,File file) throws ParserConfigurationException,FileNotFoundException,SecurityException,NullPointerException,SAXException,IOException { + public void writeFileContext(X4OLanguageContext context,File file) throws X4OConnectionException,SAXException,IOException { 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); - } OutputStream outputStream = new FileOutputStream(file); try { writeContext(context,outputStream); diff --git a/x4o-driver/src/main/java/org/x4o/xml/io/DefaultX4OReader.java b/x4o-driver/src/main/java/org/x4o/xml/io/DefaultX4OReader.java index 6908c6a..82a067e 100644 --- a/x4o-driver/src/main/java/org/x4o/xml/io/DefaultX4OReader.java +++ b/x4o-driver/src/main/java/org/x4o/xml/io/DefaultX4OReader.java @@ -64,10 +64,10 @@ public class DefaultX4OReader extends AbstractX4OReader { logger = Logger.getLogger(DefaultX4OReader.class.getName()); } - public X4OLanguageContext readContext(InputStream input, String systemId, URL basePath) throws ParserConfigurationException, SAXException, IOException { - setProperty(X4OLanguagePropertyKeys.INPUT_SOURCE_STREAM, input); - setProperty(X4OLanguagePropertyKeys.INPUT_SOURCE_SYSTEM_ID, systemId); - setProperty(X4OLanguagePropertyKeys.INPUT_SOURCE_BASE_PATH, basePath); + public X4OLanguageContext readContext(InputStream input, String systemId, URL basePath) throws X4OConnectionException, SAXException, IOException { + setProperty(X4OLanguagePropertyKeys.READER_INPUT_STREAM, input); + setProperty(X4OLanguagePropertyKeys.READER_INPUT_SYSTEM_ID, systemId); + setProperty(X4OLanguagePropertyKeys.READER_INPUT_BASE_PATH, basePath); read(); return getLanguageContext(); } @@ -95,17 +95,17 @@ public class DefaultX4OReader extends AbstractX4OReader { /** * Parses the input stream as a X4O document. */ - protected void read() throws ParserConfigurationException,SAXException,IOException { - X4OLanguageContext elementLanguage = getLanguageContext(); - if (elementLanguage.getLanguage()==null) { - throw new ParserConfigurationException("parserConfig is broken getLanguage() returns null."); + protected void read() throws X4OConnectionException,SAXException,IOException { + X4OLanguageContext languageContext = getLanguageContext(); + if (languageContext.getLanguage()==null) { + throw new X4OConnectionException("languageContext is broken getLanguage() returns null."); } // init debugWriter if enabled boolean startedDebugWriter = false; - Object debugOutputHandler = elementLanguage.getLanguageProperty(X4OLanguageProperty.DEBUG_OUTPUT_HANDLER); - Object debugOutputStream = elementLanguage.getLanguageProperty(X4OLanguageProperty.DEBUG_OUTPUT_STREAM); - if (elementLanguage.getX4ODebugWriter()==null) { + Object debugOutputHandler = languageContext.getLanguageProperty(X4OLanguageProperty.DEBUG_OUTPUT_HANDLER); + Object debugOutputStream = languageContext.getLanguageProperty(X4OLanguageProperty.DEBUG_OUTPUT_STREAM); + if (languageContext.getX4ODebugWriter()==null) { DefaultHandler2 xmlDebugWriter = null; if (debugOutputHandler instanceof DefaultHandler2) { xmlDebugWriter = (DefaultHandler2)debugOutputHandler; @@ -116,18 +116,18 @@ public class DefaultX4OReader extends AbstractX4OReader { xmlDebugWriter.startDocument(); xmlDebugWriter.startPrefixMapping("debug", X4ODebugWriter.DEBUG_URI); X4ODebugWriter debugWriter = new X4ODebugWriter(xmlDebugWriter); - X4OLanguageContextLocal local = (X4OLanguageContextLocal)elementLanguage; + X4OLanguageContextLocal local = (X4OLanguageContextLocal)languageContext; local.setX4ODebugWriter(debugWriter); startedDebugWriter = true; } } // debug language - if (elementLanguage.hasX4ODebugWriter()) { + if (languageContext.hasX4ODebugWriter()) { AttributesImpl atts = new AttributesImpl(); - atts.addAttribute ("", "language", "", "", elementLanguage.getLanguage().getLanguageName()); + atts.addAttribute ("", "language", "", "", languageContext.getLanguage().getLanguageName()); atts.addAttribute ("", "currentTimeMillis", "", "", System.currentTimeMillis()+""); - elementLanguage.getX4ODebugWriter().getDebugWriter().startElement(X4ODebugWriter.DEBUG_URI, "X4ODriver", "", atts); + languageContext.getX4ODebugWriter().getDebugWriter().startElement(X4ODebugWriter.DEBUG_URI, "X4ODriver", "", atts); } // start parsing language @@ -136,33 +136,32 @@ public class DefaultX4OReader extends AbstractX4OReader { } catch (Exception e) { // also debug exceptions - if (elementLanguage.hasX4ODebugWriter()) { + if (languageContext.hasX4ODebugWriter()) { try { - AttributesImpl atts = new AttributesImpl(); - atts.addAttribute ("", "message", "", "", e.getMessage()); - if (e instanceof X4OPhaseException) { - atts.addAttribute ("", "phase", "", "", ((X4OPhaseException)e).getX4OPhaseHandler().getId()); - } - elementLanguage.getX4ODebugWriter().getDebugWriter().startElement(X4ODebugWriter.DEBUG_URI, "exceptionStackTrace", "", atts); - StringWriter writer = new StringWriter(); - PrintWriter printer = new PrintWriter(writer); - printer.append('\n'); - if (e.getCause()==null) { - e.printStackTrace(printer); - } else { - e.getCause().printStackTrace(printer); - } - char[] stack = writer.getBuffer().toString().toCharArray(); - elementLanguage.getX4ODebugWriter().getDebugWriter().characters(stack, 0, stack.length); - elementLanguage.getX4ODebugWriter().getDebugWriter().endElement(X4ODebugWriter.DEBUG_URI, "exceptionStackTrace", ""); + AttributesImpl atts = new AttributesImpl(); + atts.addAttribute ("", "message", "", "", e.getMessage()); + if (e instanceof X4OPhaseException) { + atts.addAttribute ("", "phase", "", "", ((X4OPhaseException)e).getX4OPhaseHandler().getId()); + } + languageContext.getX4ODebugWriter().getDebugWriter().startElement(X4ODebugWriter.DEBUG_URI, "exceptionStackTrace", "", atts); + StringWriter writer = new StringWriter(); + PrintWriter printer = new PrintWriter(writer); + printer.append('\n'); + if (e.getCause()==null) { + e.printStackTrace(printer); + } else { + e.getCause().printStackTrace(printer); + } + char[] stack = writer.getBuffer().toString().toCharArray(); + languageContext.getX4ODebugWriter().getDebugWriter().characters(stack, 0, stack.length); + languageContext.getX4ODebugWriter().getDebugWriter().endElement(X4ODebugWriter.DEBUG_URI, "exceptionStackTrace", ""); } catch (Exception ee) { - ee.printStackTrace(); } } // unwrap exception if (e.getCause() instanceof ParserConfigurationException) { - throw (ParserConfigurationException)e.getCause(); + throw new X4OConnectionException((ParserConfigurationException)e.getCause()); } if (e.getCause() instanceof SAXException) { throw (SAXException)e.getCause(); @@ -176,16 +175,12 @@ public class DefaultX4OReader extends AbstractX4OReader { throw new SAXException((Exception)e.getCause()); } } finally { - // close all our resources. - //if (inputStream!=null) { - // inputStream.close(); - //} - if (elementLanguage.hasX4ODebugWriter()) { - elementLanguage.getX4ODebugWriter().getDebugWriter().endElement(X4ODebugWriter.DEBUG_URI, "X4ODriver", ""); + if (languageContext.hasX4ODebugWriter()) { + languageContext.getX4ODebugWriter().getDebugWriter().endElement(X4ODebugWriter.DEBUG_URI, "X4ODriver", ""); } - if (startedDebugWriter && elementLanguage.hasX4ODebugWriter()) { - elementLanguage.getX4ODebugWriter().getDebugWriter().endPrefixMapping("debug"); - elementLanguage.getX4ODebugWriter().getDebugWriter().endDocument(); + if (startedDebugWriter && languageContext.hasX4ODebugWriter()) { + languageContext.getX4ODebugWriter().getDebugWriter().endPrefixMapping("debug"); + languageContext.getX4ODebugWriter().getDebugWriter().endDocument(); if (debugOutputStream instanceof OutputStream) { OutputStream outputStream = (OutputStream)debugOutputStream; outputStream.flush(); diff --git a/x4o-driver/src/main/java/org/x4o/xml/io/DefaultX4OSchemaWriter.java b/x4o-driver/src/main/java/org/x4o/xml/io/DefaultX4OSchemaWriter.java index c1777fa..d522528 100644 --- a/x4o-driver/src/main/java/org/x4o/xml/io/DefaultX4OSchemaWriter.java +++ b/x4o-driver/src/main/java/org/x4o/xml/io/DefaultX4OSchemaWriter.java @@ -60,8 +60,8 @@ public class DefaultX4OSchemaWriter extends AbstractX4OConnection implements X4O * @see org.x4o.xml.io.X4OSchemaWriter#writeSchema(java.io.File, java.lang.String) */ public void writeSchema(File basePath, String namespace) throws ElementException { - // Start xsd generator - EldXsdXmlGenerator xsd = new EldXsdXmlGenerator(getLanguageContext().getLanguage()); - xsd.writeSchema(basePath, namespace); + setProperty(X4OLanguagePropertyKeys.SCHEMA_WRITER_OUTPUT_PATH, basePath); + EldXsdXmlGenerator xsd = new EldXsdXmlGenerator(getLanguageContext()); + xsd.writeSchema(namespace); // Start xsd generator } } diff --git a/x4o-driver/src/main/java/org/x4o/xml/io/DefaultX4OWriter.java b/x4o-driver/src/main/java/org/x4o/xml/io/DefaultX4OWriter.java index 10a0e0d..b5763af 100644 --- a/x4o-driver/src/main/java/org/x4o/xml/io/DefaultX4OWriter.java +++ b/x4o-driver/src/main/java/org/x4o/xml/io/DefaultX4OWriter.java @@ -23,31 +23,14 @@ 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.ElementClass; -import org.x4o.xml.element.ElementClassAttribute; -import org.x4o.xml.element.ElementInterface; -import org.x4o.xml.element.ElementNamespaceContext; -import org.x4o.xml.element.ElementObjectPropertyValueException; -import org.x4o.xml.io.sax.XMLWriter; import org.x4o.xml.lang.X4OLanguageContext; -import org.x4o.xml.lang.X4OLanguageModule; +import org.x4o.xml.lang.X4OLanguagePropertyKeys; import org.x4o.xml.lang.phase.X4OPhaseException; import org.x4o.xml.lang.phase.X4OPhaseType; import org.xml.sax.SAXException; -import org.xml.sax.helpers.AttributesImpl; /** * DefaultX4OWriter can write the xml language. @@ -56,146 +39,24 @@ import org.xml.sax.helpers.AttributesImpl; * @version 1.0 Apr 6, 2013 */ public class DefaultX4OWriter extends AbstractX4OWriter { - + + /** + * Default constructor. + * @param languageContext The language context for this writer. + */ public DefaultX4OWriter(X4OLanguageContext languageContext) { super(languageContext); } - - public void writeContext(X4OLanguageContext languageContext,OutputStream out) throws ParserConfigurationException, - FileNotFoundException, SecurityException, NullPointerException, - SAXException, IOException { - + + /** + * @see org.x4o.xml.io.X4OWriterContext#writeContext(org.x4o.xml.lang.X4OLanguageContext, java.io.OutputStream) + */ + public void writeContext(X4OLanguageContext languageContext,OutputStream output) throws X4OConnectionException,SAXException,IOException { + setProperty(X4OLanguagePropertyKeys.WRITER_OUTPUT_STREAM, output); try { languageContext.getLanguage().getPhaseManager().runPhases(languageContext, X4OPhaseType.XML_WRITE); - } catch (X4OPhaseException e1) { - throw new SAXException(e1); - } - Element root = languageContext.getRootElement(); - - 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); + } catch (X4OPhaseException e) { + throw new X4OConnectionException(e); } } - - 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()) { - if (eca.getRunBeanValue()!=null && eca.getRunBeanValue()==false) { - continue; - } - - Object value = element.getLanguageContext().getElementObjectPropertyValue().getProperty(element.getElementObject(),eca.getId()); - if (value==null) { - continue; - } - atts.addAttribute ("", eca.getId(), "", "", ""+value); - } - - } else { - for (String p:getProperties(element.getElementObject().getClass())) { - - ElementClassAttribute eca = element.getElementClass().getElementClassAttributeByName(p); - if (eca!=null && eca.getRunBeanValue()!=null && eca.getRunBeanValue()) { - continue; - } - boolean writeValue = true; - for (ElementInterface ei:element.getLanguageContext().getLanguage().findElementInterfaces(element.getElementObject().getClass())) { - eca = ei.getElementClassAttributeByName(p); - if (eca!=null && eca.getRunBeanValue()!=null && eca.getRunBeanValue()==false) { - writeValue = false; - break; - } - } - if (writeValue==false) { - continue; - } - - // TODO: check attr see reader - 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().getId(), "", atts); - for (Element e:element.getChilderen()) { - writeTree(writer,e); - } - writer.endElement(elementUri, element.getElementClass().getId(), ""); - } - - private String findElementUri(Element e) { - for (X4OLanguageModule mod:getLanguageContext().getLanguage().getLanguageModules()) { - for (ElementNamespaceContext c:mod.getElementNamespaceContexts()) { - ElementClass ec = c.getElementClass(e.getElementClass().getId()); - if (ec!=null) { - return c.getUri(); - } - } - } - return null; - } - - private String findNamespacePrefix(Element e,String uri) { - ElementNamespaceContext ns = getLanguageContext().getLanguage().findElementNamespaceContext(uri); - if (ns.getPrefixMapping()!=null) { - return ns.getPrefixMapping(); - } - return ns.getId(); - } - } diff --git a/x4o-driver/src/main/java/org/x4o/xml/io/X4OConnectionException.java b/x4o-driver/src/main/java/org/x4o/xml/io/X4OConnectionException.java new file mode 100644 index 0000000..96a61e1 --- /dev/null +++ b/x4o-driver/src/main/java/org/x4o/xml/io/X4OConnectionException.java @@ -0,0 +1,68 @@ +/* + * 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; + +/** + * X4OConnectionException is top level exception for io connections. + * + * @author Willem Cazander + * @version 1.0 28 Apr, 2013 + */ +public class X4OConnectionException extends Exception { + + /** The serial version uid */ + static final long serialVersionUID = 10L; + + /** + * Constructs an X4OConnectionException without a detail message. + */ + public X4OConnectionException() { + super(); + } + + /** + * Constructs an X4OConnectionException with a detail message. + * @param message The message of this Exception + */ + public X4OConnectionException(String message) { + super(message); + } + + /** + * Creates an X4OConnectionException from a parent exception. + * @param e The error exception. + */ + public X4OConnectionException(Exception e) { + super(e); + } + + /** + * Constructs an X4OConnectionException with a detail message. + * @param message The message of this Exception + * @param e The error exception. + */ + public X4OConnectionException(String message,Exception e) { + super(message,e); + } +} diff --git a/x4o-driver/src/main/java/org/x4o/xml/io/X4OReader.java b/x4o-driver/src/main/java/org/x4o/xml/io/X4OReader.java index 585d73d..f85ad58 100644 --- a/x4o-driver/src/main/java/org/x4o/xml/io/X4OReader.java +++ b/x4o-driver/src/main/java/org/x4o/xml/io/X4OReader.java @@ -29,8 +29,6 @@ import java.io.IOException; import java.io.InputStream; import java.net.URL; -import javax.xml.parsers.ParserConfigurationException; - import org.xml.sax.SAXException; /** @@ -46,70 +44,61 @@ public interface X4OReader extends X4OConnection { /** * Method to parse the xml data. * @param input The inputStream to parse. - * @throws ParserConfigurationException - * @throws SAXException - * @throws IOException + * @throws X4OConnectionException Is thrown after x4o exception. + * @throws SAXException Is thrown after sax xml exception. + * @throws IOException Is thrown after io exception. */ - T read(InputStream input,String systemId,URL basePath) throws ParserConfigurationException,SAXException,IOException; + T read(InputStream input,String systemId,URL basePath) throws X4OConnectionException,SAXException,IOException; /** * Reads the file fileName and parses it as an InputStream. * @param fileName The file name to parse. - * @throws ParserConfigurationException - * @throws FileNotFoundException - * @throws SecurityException - * @throws NullPointerException - * @throws SAXException - * @throws IOException + * @throws FileNotFoundException Is thrown is file is not found. + * @throws X4OConnectionException Is thrown after x4o exception. + * @throws SAXException Is thrown after sax xml exception. + * @throws IOException Is thrown after io exception. * @see org.x4o.xml.io.X4OReaderContext#readContext(java.io.InputStream,java.lang.String,java.net.URL) */ - T readFile(String fileName) throws ParserConfigurationException,FileNotFoundException,SecurityException,NullPointerException,SAXException,IOException; + T readFile(String fileName) throws X4OConnectionException,SAXException,IOException,FileNotFoundException; /** * Reads the file and parses it as an InputStream. * @param file The file to parse. - * @throws ParserConfigurationException - * @throws FileNotFoundException - * @throws SecurityException - * @throws NullPointerException - * @throws SAXException - * @throws IOException + * @throws FileNotFoundException Is thrown is file is not found. + * @throws X4OConnectionException Is thrown after x4o exception. + * @throws SAXException Is thrown after sax xml exception. + * @throws IOException Is thrown after io exception. * @see org.x4o.xml.io.X4OReaderContext#readContext(java.io.InputStream,java.lang.String,java.net.URL) */ - T readFile(File file) throws ParserConfigurationException,FileNotFoundException,SecurityException,NullPointerException,SAXException,IOException; + T readFile(File file) throws X4OConnectionException,SAXException,IOException,FileNotFoundException; /** * Parses an resource locaction. * @param resourceName The resource to parser. - * @throws ParserConfigurationException - * @throws FileNotFoundException - * @throws SecurityException - * @throws NullPointerException - * @throws SAXException - * @throws IOException + * @throws X4OConnectionException Is thrown after x4o exception. + * @throws SAXException Is thrown after sax xml exception. + * @throws IOException Is thrown after io exception. * @see org.x4o.xml.io.X4OReaderContext#readContext(java.io.InputStream,java.lang.String,java.net.URL) */ - T readResource(String resourceName) throws ParserConfigurationException,FileNotFoundException,SecurityException,NullPointerException,SAXException,IOException; + T readResource(String resourceName) throws X4OConnectionException,SAXException,IOException; /** * Converts a String to a InputStream to is can me parsed by SAX. * @param xmlString The xml as String to parse. - * @throws ParserConfigurationException - * @throws SAXException - * @throws IOException - * @throws NullPointerException + * @throws X4OConnectionException Is thrown after x4o exception. + * @throws SAXException Is thrown after sax xml exception. + * @throws IOException Is thrown after io exception. * @see org.x4o.xml.io.X4OReaderContext#readContext(java.io.InputStream,java.lang.String,java.net.URL) */ - T readString(String xmlString) throws ParserConfigurationException,SAXException,IOException,NullPointerException; + T readString(String xmlString) throws X4OConnectionException,SAXException,IOException; /** * Fetched the data direct from remote url to a InputStream to is can me parsed by SAX. * @param url The url to parse. - * @throws ParserConfigurationException - * @throws SAXException - * @throws IOException - * @throws NullPointerException + * @throws X4OConnectionException Is thrown after x4o exception. + * @throws SAXException Is thrown after sax xml exception. + * @throws IOException Is thrown after io exception. * @see org.x4o.xml.io.X4OReaderContext#readContext(java.io.InputStream,java.lang.String,java.net.URL) */ - T readUrl(URL url) throws ParserConfigurationException,SAXException,IOException,NullPointerException; + T readUrl(URL url) throws X4OConnectionException,SAXException,IOException; } diff --git a/x4o-driver/src/main/java/org/x4o/xml/io/X4OReaderContext.java b/x4o-driver/src/main/java/org/x4o/xml/io/X4OReaderContext.java index 04e6fb1..abb807f 100644 --- a/x4o-driver/src/main/java/org/x4o/xml/io/X4OReaderContext.java +++ b/x4o-driver/src/main/java/org/x4o/xml/io/X4OReaderContext.java @@ -29,8 +29,6 @@ import java.io.IOException; import java.io.InputStream; import java.net.URL; -import javax.xml.parsers.ParserConfigurationException; - import org.x4o.xml.lang.X4OLanguageContext; import org.x4o.xml.lang.phase.X4OPhaseException; import org.xml.sax.SAXException; @@ -48,70 +46,61 @@ public interface X4OReaderContext extends X4OReader { /** * Method to parse the xml data. * @param input The inputStream to parse. - * @throws ParserConfigurationException - * @throws SAXException - * @throws IOException + * @throws X4OConnectionException Is thrown after x4o exception. + * @throws SAXException Is thrown after sax xml exception. + * @throws IOException Is thrown after io exception. */ - X4OLanguageContext readContext(InputStream input,String systemId,URL basePath) throws ParserConfigurationException,SAXException,IOException; + X4OLanguageContext readContext(InputStream input,String systemId,URL basePath) throws X4OConnectionException,SAXException,IOException; /** * Reads the file fileName and parses it as an InputStream. * @param fileName The file name to parse. - * @throws ParserConfigurationException - * @throws FileNotFoundException - * @throws SecurityException - * @throws NullPointerException - * @throws SAXException - * @throws IOException + * @throws FileNotFoundException Is thrown is file is not found. + * @throws X4OConnectionException Is thrown after x4o exception. + * @throws SAXException Is thrown after sax xml exception. + * @throws IOException Is thrown after io exception. * @see org.x4o.xml.io.X4OReaderContext#readContext(java.io.InputStream,java.lang.String,java.net.URL) */ - X4OLanguageContext readFileContext(String fileName) throws ParserConfigurationException,FileNotFoundException,SecurityException,NullPointerException,SAXException,IOException; + X4OLanguageContext readFileContext(String fileName) throws X4OConnectionException,SAXException,IOException,FileNotFoundException; /** * Reads the file and parses it as an InputStream. * @param file The file to parse. - * @throws ParserConfigurationException - * @throws FileNotFoundException - * @throws SecurityException - * @throws NullPointerException - * @throws SAXException - * @throws IOException + * @throws FileNotFoundException Is thrown is file is not found. + * @throws X4OConnectionException Is thrown after x4o exception. + * @throws SAXException Is thrown after sax xml exception. + * @throws IOException Is thrown after io exception. * @see org.x4o.xml.io.X4OReaderContext#readContext(java.io.InputStream,java.lang.String,java.net.URL) */ - X4OLanguageContext readFileContext(File file) throws ParserConfigurationException,FileNotFoundException,SecurityException,NullPointerException,SAXException,IOException; + X4OLanguageContext readFileContext(File file) throws X4OConnectionException,SAXException,IOException,FileNotFoundException; /** * Parses an resource locaction. * @param resourceName The resource to parser. - * @throws ParserConfigurationException - * @throws FileNotFoundException - * @throws SecurityException - * @throws NullPointerException - * @throws SAXException - * @throws IOException + * @throws X4OConnectionException Is thrown after x4o exception. + * @throws SAXException Is thrown after sax xml exception. + * @throws IOException Is thrown after io exception. * @see org.x4o.xml.io.X4OReaderContext#readContext(java.io.InputStream,java.lang.String,java.net.URL) */ - X4OLanguageContext readResourceContext(String resourceName) throws ParserConfigurationException,FileNotFoundException,SecurityException,NullPointerException,SAXException,IOException; + X4OLanguageContext readResourceContext(String resourceName) throws X4OConnectionException,SAXException,IOException; /** * Converts a String to a InputStream to is can me parsed by SAX. * @param xmlString The xml as String to parse. - * @throws ParserConfigurationException - * @throws SAXException - * @throws IOException - * @throws NullPointerException + * @throws X4OConnectionException Is thrown after x4o exception. + * @throws SAXException Is thrown after sax xml exception. + * @throws IOException Is thrown after io exception. * @see org.x4o.xml.io.X4OReaderContext#readContext(java.io.InputStream,java.lang.String,java.net.URL) */ - X4OLanguageContext readStringContext(String xmlString) throws ParserConfigurationException,SAXException,IOException,NullPointerException; + X4OLanguageContext readStringContext(String xmlString) throws X4OConnectionException,SAXException,IOException; /** * Fetched the data direct from remote url to a InputStream to is can me parsed by SAX. * @param url The url to parse. - * @throws ParserConfigurationException - * @throws SAXException - * @throws IOException - * @throws NullPointerException + * @throws X4OConnectionException Is thrown after x4o exception. + * @throws SAXException Is thrown after sax xml exception. + * @throws IOException Is thrown after io exception. * @see org.x4o.xml.io.X4OReaderContext#readContext(java.io.InputStream,java.lang.String,java.net.URL) */ - X4OLanguageContext readUrlContext(URL url) throws ParserConfigurationException,SAXException,IOException,NullPointerException; + X4OLanguageContext readUrlContext(URL url) throws X4OConnectionException,SAXException,IOException; } diff --git a/x4o-driver/src/main/java/org/x4o/xml/io/X4OWriter.java b/x4o-driver/src/main/java/org/x4o/xml/io/X4OWriter.java index f04ab4a..a4b3bb4 100644 --- a/x4o-driver/src/main/java/org/x4o/xml/io/X4OWriter.java +++ b/x4o-driver/src/main/java/org/x4o/xml/io/X4OWriter.java @@ -24,12 +24,9 @@ package org.x4o.xml.io; import java.io.File; -import java.io.FileNotFoundException; import java.io.IOException; import java.io.OutputStream; -import javax.xml.parsers.ParserConfigurationException; - import org.xml.sax.SAXException; /** @@ -40,9 +37,9 @@ import org.xml.sax.SAXException; */ public interface X4OWriter extends X4OConnection { - void write(T object,OutputStream out) throws ParserConfigurationException,FileNotFoundException,SecurityException,NullPointerException,SAXException,IOException; + void write(T object,OutputStream out) throws X4OConnectionException,SAXException,IOException; - void writeFile(T object,String fileName) throws ParserConfigurationException,FileNotFoundException,SecurityException,NullPointerException,SAXException,IOException; + void writeFile(T object,String fileName) throws X4OConnectionException,SAXException,IOException; - void writeFile(T object,File file) throws ParserConfigurationException,FileNotFoundException,SecurityException,NullPointerException,SAXException,IOException; + void writeFile(T object,File file) throws X4OConnectionException,SAXException,IOException; } diff --git a/x4o-driver/src/main/java/org/x4o/xml/io/X4OWriterContext.java b/x4o-driver/src/main/java/org/x4o/xml/io/X4OWriterContext.java index 7b12816..21410cb 100644 --- a/x4o-driver/src/main/java/org/x4o/xml/io/X4OWriterContext.java +++ b/x4o-driver/src/main/java/org/x4o/xml/io/X4OWriterContext.java @@ -24,12 +24,9 @@ package org.x4o.xml.io; import java.io.File; -import java.io.FileNotFoundException; import java.io.IOException; import java.io.OutputStream; -import javax.xml.parsers.ParserConfigurationException; - import org.x4o.xml.lang.X4OLanguageContext; import org.xml.sax.SAXException; @@ -41,9 +38,9 @@ import org.xml.sax.SAXException; */ public interface X4OWriterContext extends X4OWriter { - void writeContext(X4OLanguageContext context,OutputStream out) throws ParserConfigurationException,FileNotFoundException,SecurityException,NullPointerException,SAXException,IOException; + void writeContext(X4OLanguageContext context,OutputStream out) throws X4OConnectionException,SAXException,IOException; - void writeFileContext(X4OLanguageContext context,String fileName) throws ParserConfigurationException,FileNotFoundException,SecurityException,NullPointerException,SAXException,IOException; + void writeFileContext(X4OLanguageContext context,String fileName) throws X4OConnectionException,SAXException,IOException; - void writeFileContext(X4OLanguageContext context,File file) throws ParserConfigurationException,FileNotFoundException,SecurityException,NullPointerException,SAXException,IOException; + void writeFileContext(X4OLanguageContext context,File file) throws X4OConnectionException,SAXException,IOException; } diff --git a/x4o-driver/src/main/java/org/x4o/xml/io/sax/X4OEntityResolver.java b/x4o-driver/src/main/java/org/x4o/xml/io/sax/X4OEntityResolver.java index c19dc86..255974f 100644 --- a/x4o-driver/src/main/java/org/x4o/xml/io/sax/X4OEntityResolver.java +++ b/x4o-driver/src/main/java/org/x4o/xml/io/sax/X4OEntityResolver.java @@ -70,7 +70,7 @@ public class X4OEntityResolver implements EntityResolver { } this.logger=Logger.getLogger(X4OEntityResolver.class.getName()); this.elementContext=elementContext; - this.basePath=(URL)elementContext.getLanguageProperty(X4OLanguageProperty.INPUT_SOURCE_BASE_PATH); + this.basePath=(URL)elementContext.getLanguageProperty(X4OLanguageProperty.READER_INPUT_BASE_PATH); this.schemaResources=new HashMap(20); this.schemaPathResources=new HashMap(20); for (X4OLanguageModule mod:elementContext.getLanguage().getLanguageModules()) { @@ -110,7 +110,7 @@ public class X4OEntityResolver implements EntityResolver { logger.finer("Fetch sysId: "+systemId+" pubId: "+publicId); // Check if other resolver has resource - EntityResolver resolver = (EntityResolver)elementContext.getLanguageProperty(X4OLanguageProperty.CONFIG_ENTITY_RESOLVER); + EntityResolver resolver = (EntityResolver)elementContext.getLanguageProperty(X4OLanguageProperty.READER_ENTITY_RESOLVER); if (resolver!=null) { InputSource result = resolver.resolveEntity(publicId, systemId); if (result!=null) { @@ -120,7 +120,7 @@ public class X4OEntityResolver implements EntityResolver { // Check if we have it on user defined schema base path if (schemaPathResources.containsKey(systemId)) { - File schemaBasePath = (File)elementContext.getLanguageProperty(X4OLanguageProperty.VALIDATION_SCHEMA_PATH); + File schemaBasePath = (File)elementContext.getLanguageProperty(X4OLanguageProperty.READER_VALIDATION_SCHEMA_PATH); if (schemaBasePath!=null && schemaBasePath.exists()) { String schemeResource = schemaResources.get(systemId); File schemaFile = new File(schemaBasePath.getAbsolutePath()+File.separatorChar+schemeResource); diff --git a/x4o-driver/src/main/java/org/x4o/xml/io/sax/X4OErrorHandler.java b/x4o-driver/src/main/java/org/x4o/xml/io/sax/X4OErrorHandler.java index 147ced8..279346e 100644 --- a/x4o-driver/src/main/java/org/x4o/xml/io/sax/X4OErrorHandler.java +++ b/x4o-driver/src/main/java/org/x4o/xml/io/sax/X4OErrorHandler.java @@ -50,7 +50,7 @@ public class X4OErrorHandler implements ErrorHandler { throw new NullPointerException("Can't debug and proxy errors with null languageContext."); } this.languageContext=languageContext; - this.errorHandler=(ErrorHandler)languageContext.getLanguageProperty(X4OLanguageProperty.CONFIG_ERROR_HANDLER); + this.errorHandler=(ErrorHandler)languageContext.getLanguageProperty(X4OLanguageProperty.READER_ERROR_HANDLER); } /** diff --git a/x4o-driver/src/main/java/org/x4o/xml/io/sax/X4OTagHandler.java b/x4o-driver/src/main/java/org/x4o/xml/io/sax/X4OTagHandler.java index 9cd20d1..0e2358b 100644 --- a/x4o-driver/src/main/java/org/x4o/xml/io/sax/X4OTagHandler.java +++ b/x4o-driver/src/main/java/org/x4o/xml/io/sax/X4OTagHandler.java @@ -124,7 +124,7 @@ public class X4OTagHandler extends DefaultHandler2 { ElementNamespaceContext enc = elementLanguage.getLanguage().findElementNamespaceContext(namespaceUri); if (enc==null) { if ("".equals(namespaceUri)) { - String configEmptryUri = (String)elementLanguage.getLanguageProperty(X4OLanguageProperty.INPUT_EMPTY_NAMESPACE_URI); + String configEmptryUri = (String)elementLanguage.getLanguageProperty(X4OLanguageProperty.READER_EMPTY_NAMESPACE_URI); if (configEmptryUri!=null) { namespaceUri = configEmptryUri; enc = elementLanguage.getLanguage().findElementNamespaceContext(namespaceUri); diff --git a/x4o-driver/src/main/java/org/x4o/xml/io/sax/XMLWriter.java b/x4o-driver/src/main/java/org/x4o/xml/io/sax/XMLWriter.java index 68b106f..66761d8 100644 --- a/x4o-driver/src/main/java/org/x4o/xml/io/sax/XMLWriter.java +++ b/x4o-driver/src/main/java/org/x4o/xml/io/sax/XMLWriter.java @@ -117,6 +117,18 @@ public class XMLWriter extends DefaultHandler2 { public XMLWriter(OutputStream out) throws UnsupportedEncodingException { this(new OutputStreamWriter(out, XMLConstants.XML_DEFAULT_ENCODING),XMLConstants.XML_DEFAULT_ENCODING); } + + /** + * Creates XmlWriter which prints to the OutputStream interface. + * @param out The OutputStream to write to. + * @param encoding The OutputStream encoding. + * @param charNewLine The newline char. + * @param charTab The tab char. + * @throws UnsupportedEncodingException Is thrown when UTF-8 can't we printed. + */ + public XMLWriter(OutputStream out,String encoding,String charNewLine,String charTab) throws UnsupportedEncodingException { + this(new OutputStreamWriter(out, encoding),encoding,charNewLine,charTab); + } /** * @see org.xml.sax.ContentHandler#startDocument() diff --git a/x4o-driver/src/main/java/org/x4o/xml/lang/AbstractX4OLanguageConfiguration.java b/x4o-driver/src/main/java/org/x4o/xml/lang/AbstractX4OLanguageConfiguration.java new file mode 100644 index 0000000..5a5e295 --- /dev/null +++ b/x4o-driver/src/main/java/org/x4o/xml/lang/AbstractX4OLanguageConfiguration.java @@ -0,0 +1,324 @@ +package org.x4o.xml.lang; + +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +public abstract class AbstractX4OLanguageConfiguration implements X4OLanguageConfigurationLocal { + + private String languageResourcePathPrefix = null; + private String languageResourceModulesFileName = null; + + private Class defaultElementNamespaceContext = null; + private Class defaultElementInterface = null; + private Class defaultElement = null; + private Class defaultElementClass = null; + private Class defaultElementClassAttribute = null; + + private Class defaultElementLanguageModule = null; + private Class defaultElementBodyComment = null; + private Class defaultElementBodyCharacters = null; + private Class defaultElementBodyWhitespace = null; + private Class defaultElementNamespaceInstanceProvider = null; + private Class defaultElementAttributeValueParser = null; + private Class defaultElementObjectPropertyValue = null; + private Class defaultElementAttributeHandlerComparator = null; + + private Class defaultLanguageVersionFilter = null; + private Class defaultLanguageLoader = null; + private Class defaultExpressionLanguageContext = null; + + private Map globalProperties = null; + + /** + * Default constructor. + */ + public AbstractX4OLanguageConfiguration() { + globalProperties = new HashMap(10); + } + + /** + * @return the languageResourcePathPrefix + */ + public String getLanguageResourcePathPrefix() { + return languageResourcePathPrefix; + } + + /** + * @param languageResourcePathPrefix the languageResourcePathPrefix to set + */ + public void setLanguageResourcePathPrefix(String languageResourcePathPrefix) { + this.languageResourcePathPrefix = languageResourcePathPrefix; + } + + /** + * @return the languageResourceModulesFileName + */ + public String getLanguageResourceModulesFileName() { + return languageResourceModulesFileName; + } + + /** + * @param languageResourceModulesFileName the languageResourceModulesFileName to set + */ + public void setLanguageResourceModulesFileName( + String languageResourceModulesFileName) { + this.languageResourceModulesFileName = languageResourceModulesFileName; + } + + /** + * @return the defaultElementNamespaceContext + */ + public Class getDefaultElementNamespaceContext() { + return defaultElementNamespaceContext; + } + + /** + * @param defaultElementNamespaceContext the defaultElementNamespaceContext to set + */ + public void setDefaultElementNamespaceContext( + Class defaultElementNamespaceContext) { + this.defaultElementNamespaceContext = defaultElementNamespaceContext; + } + + /** + * @return the defaultElementInterface + */ + public Class getDefaultElementInterface() { + return defaultElementInterface; + } + + /** + * @param defaultElementInterface the defaultElementInterface to set + */ + public void setDefaultElementInterface(Class defaultElementInterface) { + this.defaultElementInterface = defaultElementInterface; + } + + /** + * @return the defaultElement + */ + public Class getDefaultElement() { + return defaultElement; + } + + /** + * @param defaultElement the defaultElement to set + */ + public void setDefaultElement(Class defaultElement) { + this.defaultElement = defaultElement; + } + + /** + * @return the defaultElementClass + */ + public Class getDefaultElementClass() { + return defaultElementClass; + } + + /** + * @param defaultElementClass the defaultElementClass to set + */ + public void setDefaultElementClass(Class defaultElementClass) { + this.defaultElementClass = defaultElementClass; + } + + /** + * @return the defaultElementClassAttribute + */ + public Class getDefaultElementClassAttribute() { + return defaultElementClassAttribute; + } + + /** + * @param defaultElementClassAttribute the defaultElementClassAttribute to set + */ + public void setDefaultElementClassAttribute( + Class defaultElementClassAttribute) { + this.defaultElementClassAttribute = defaultElementClassAttribute; + } + + /** + * @return the defaultElementLanguageModule + */ + public Class getDefaultElementLanguageModule() { + return defaultElementLanguageModule; + } + + /** + * @param defaultElementLanguageModule the defaultElementLanguageModule to set + */ + public void setDefaultElementLanguageModule( + Class defaultElementLanguageModule) { + this.defaultElementLanguageModule = defaultElementLanguageModule; + } + + /** + * @return the defaultElementBodyComment + */ + public Class getDefaultElementBodyComment() { + return defaultElementBodyComment; + } + + /** + * @param defaultElementBodyComment the defaultElementBodyComment to set + */ + public void setDefaultElementBodyComment(Class defaultElementBodyComment) { + this.defaultElementBodyComment = defaultElementBodyComment; + } + + /** + * @return the defaultElementBodyCharacters + */ + public Class getDefaultElementBodyCharacters() { + return defaultElementBodyCharacters; + } + + /** + * @param defaultElementBodyCharacters the defaultElementBodyCharacters to set + */ + public void setDefaultElementBodyCharacters( + Class defaultElementBodyCharacters) { + this.defaultElementBodyCharacters = defaultElementBodyCharacters; + } + + /** + * @return the defaultElementBodyWhitespace + */ + public Class getDefaultElementBodyWhitespace() { + return defaultElementBodyWhitespace; + } + + /** + * @param defaultElementBodyWhitespace the defaultElementBodyWhitespace to set + */ + public void setDefaultElementBodyWhitespace( + Class defaultElementBodyWhitespace) { + this.defaultElementBodyWhitespace = defaultElementBodyWhitespace; + } + + /** + * @return the defaultElementNamespaceInstanceProvider + */ + public Class getDefaultElementNamespaceInstanceProvider() { + return defaultElementNamespaceInstanceProvider; + } + + /** + * @param defaultElementNamespaceInstanceProvider the defaultElementNamespaceInstanceProvider to set + */ + public void setDefaultElementNamespaceInstanceProvider( + Class defaultElementNamespaceInstanceProvider) { + this.defaultElementNamespaceInstanceProvider = defaultElementNamespaceInstanceProvider; + } + + /** + * @return the defaultElementAttributeValueParser + */ + public Class getDefaultElementAttributeValueParser() { + return defaultElementAttributeValueParser; + } + + /** + * @param defaultElementAttributeValueParser the defaultElementAttributeValueParser to set + */ + public void setDefaultElementAttributeValueParser( + Class defaultElementAttributeValueParser) { + this.defaultElementAttributeValueParser = defaultElementAttributeValueParser; + } + + /** + * @return the defaultElementObjectPropertyValue + */ + public Class getDefaultElementObjectPropertyValue() { + return defaultElementObjectPropertyValue; + } + + /** + * @param defaultElementObjectPropertyValue the defaultElementObjectPropertyValue to set + */ + public void setDefaultElementObjectPropertyValue( + Class defaultElementObjectPropertyValue) { + this.defaultElementObjectPropertyValue = defaultElementObjectPropertyValue; + } + + /** + * @return the defaultElementAttributeHandlerComparator + */ + public Class getDefaultElementAttributeHandlerComparator() { + return defaultElementAttributeHandlerComparator; + } + + /** + * @param defaultElementAttributeHandlerComparator the defaultElementAttributeHandlerComparator to set + */ + public void setDefaultElementAttributeHandlerComparator( + Class defaultElementAttributeHandlerComparator) { + this.defaultElementAttributeHandlerComparator = defaultElementAttributeHandlerComparator; + } + + /** + * @return the defaultLanguageVersionFilter + */ + public Class getDefaultLanguageVersionFilter() { + return defaultLanguageVersionFilter; + } + + /** + * @param defaultLanguageVersionFilter the defaultLanguageVersionFilter to set + */ + public void setDefaultLanguageVersionFilter( + Class defaultLanguageVersionFilter) { + this.defaultLanguageVersionFilter = defaultLanguageVersionFilter; + } + + /** + * @return the defaultLanguageLoader + */ + public Class getDefaultLanguageLoader() { + return defaultLanguageLoader; + } + + /** + * @param defaultLanguageLoader the defaultLanguageLoader to set + */ + public void setDefaultLanguageLoader(Class defaultLanguageLoader) { + this.defaultLanguageLoader = defaultLanguageLoader; + } + + /** + * @return the defaultExpressionLanguageContext + */ + public Class getDefaultExpressionLanguageContext() { + return defaultExpressionLanguageContext; + } + + /** + * @param defaultExpressionLanguageContext the defaultExpressionLanguageContext to set + */ + public void setDefaultExpressionLanguageContext( + Class defaultExpressionLanguageContext) { + this.defaultExpressionLanguageContext = defaultExpressionLanguageContext; + } + + /** + * @see org.x4o.xml.lang.X4OLanguageConfiguration#getGlobalPropertyKeys() + */ + public Collection getGlobalPropertyKeys() { + return globalProperties.keySet(); + } + + /** + * @see org.x4o.xml.lang.X4OLanguageConfiguration#getGlobalProperty(java.lang.String) + */ + public Object getGlobalProperty(String key) { + return globalProperties.get(key); + } + + /** + * @see org.x4o.xml.lang.X4OLanguageConfigurationLocal#setGlobalProperty(java.lang.String, java.lang.Object) + */ + public void setGlobalProperty(String key, Object value) { + globalProperties.put(key,value); + } +} diff --git a/x4o-driver/src/main/java/org/x4o/xml/lang/DefaultX4OLanguage.java b/x4o-driver/src/main/java/org/x4o/xml/lang/DefaultX4OLanguage.java index 540e4c8..8c1c0cf 100644 --- a/x4o-driver/src/main/java/org/x4o/xml/lang/DefaultX4OLanguage.java +++ b/x4o-driver/src/main/java/org/x4o/xml/lang/DefaultX4OLanguage.java @@ -4,8 +4,6 @@ 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; @@ -20,6 +18,12 @@ import org.x4o.xml.lang.phase.X4OPhaseException; import org.x4o.xml.lang.phase.X4OPhaseManager; import org.x4o.xml.lang.phase.X4OPhaseType; +/** + * DefaultX4OLanguage holds all information about the x4o xml language. + * + * @author Willem Cazander + * @version 1.0 30 apr 2013 + */ public class DefaultX4OLanguage implements X4OLanguageLocal { private Logger logger = null; @@ -108,8 +112,8 @@ public class DefaultX4OLanguage implements X4OLanguageLocal { 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); + for (String key:languageContext.getLanguage().getLanguageConfiguration().getGlobalPropertyKeys()) { + Object value = languageContext.getLanguage().getLanguageConfiguration().getGlobalProperty(key); contextInit.setLanguageProperty(key, value); } try { @@ -117,7 +121,7 @@ public class DefaultX4OLanguage implements X4OLanguageLocal { contextInit.setExpressionLanguageFactory(X4OExpressionFactory.createExpressionFactory(contextInit)); } if (contextInit.getExpressionLanguageContext()==null) { - contextInit.setExpressionLanguageContext((ELContext)X4OLanguageClassLoader.newInstance(getLanguageConfiguration().getDefaultExpressionLanguageContext())); + contextInit.setExpressionLanguageContext(X4OExpressionFactory.createELContext(contextInit)); } if (contextInit.getElementAttributeValueParser()==null) { contextInit.setElementAttributeValueParser((ElementAttributeValueParser)X4OLanguageClassLoader.newInstance(getLanguageConfiguration().getDefaultElementAttributeValueParser())); diff --git a/x4o-driver/src/main/java/org/x4o/xml/lang/DefaultX4OLanguageConfiguration.java b/x4o-driver/src/main/java/org/x4o/xml/lang/DefaultX4OLanguageConfiguration.java index 11160e9..0cb043b 100644 --- a/x4o-driver/src/main/java/org/x4o/xml/lang/DefaultX4OLanguageConfiguration.java +++ b/x4o-driver/src/main/java/org/x4o/xml/lang/DefaultX4OLanguageConfiguration.java @@ -23,6 +23,9 @@ package org.x4o.xml.lang; +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; +import java.lang.reflect.Proxy; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -49,135 +52,57 @@ import org.x4o.xml.element.DefaultGlobalAttributeHandlerComparator; * @author Willem Cazander * @version 1.0 27 Oct 2009 */ -public class DefaultX4OLanguageConfiguration implements X4OLanguageConfiguration { +public class DefaultX4OLanguageConfiguration extends AbstractX4OLanguageConfiguration { + /** + * Default constructor. + */ public DefaultX4OLanguageConfiguration() { } - - /** - * @see org.x4o.xml.lang.X4OLanguageConfiguration#getLanguageResourcePathPrefix() - */ - public String getLanguageResourcePathPrefix() { - return X4OLanguageConfiguration.DEFAULT_LANG_PATH_PREFIX; + + public void fillDefaults() { + if (getLanguageResourcePathPrefix()==null) { setLanguageResourcePathPrefix( X4OLanguageConfiguration.DEFAULT_LANG_PATH_PREFIX); } + if (getLanguageResourceModulesFileName()==null) { setLanguageResourceModulesFileName( X4OLanguageConfiguration.DEFAULT_LANG_MODULES_FILE);} + if (getDefaultElementNamespaceContext()==null) { setDefaultElementNamespaceContext( DefaultElementNamespaceContext.class); } + if (getDefaultElementInterface()==null) { setDefaultElementInterface( DefaultElementInterface.class); } + if (getDefaultElement()==null) { setDefaultElement( DefaultElement.class); } + if (getDefaultElementClass()==null) { setDefaultElementClass( DefaultElementClass.class); } + if (getDefaultElementClassAttribute()==null) { setDefaultElementClassAttribute( DefaultElementClassAttribute.class); } + if (getDefaultElementLanguageModule()==null) { setDefaultElementLanguageModule( DefaultX4OLanguageModule.class); } + if (getDefaultElementBodyComment()==null) { setDefaultElementBodyComment( DefaultElementBodyComment.class); } + if (getDefaultElementBodyCharacters()==null) { setDefaultElementBodyCharacters( DefaultElementBodyCharacters.class); } + if (getDefaultElementBodyWhitespace()==null) { setDefaultElementBodyWhitespace( DefaultElementBodyWhitespace.class); } + if (getDefaultElementNamespaceInstanceProvider()==null) { setDefaultElementNamespaceInstanceProvider( DefaultElementNamespaceInstanceProvider.class); } + if (getDefaultElementAttributeValueParser()==null) { setDefaultElementAttributeValueParser( DefaultElementAttributeValueParser.class); } + if (getDefaultElementObjectPropertyValue()==null) { setDefaultElementObjectPropertyValue( DefaultElementObjectPropertyValue.class); } + if (getDefaultElementAttributeHandlerComparator()==null) { setDefaultElementAttributeHandlerComparator( DefaultGlobalAttributeHandlerComparator.class); } + if (getDefaultLanguageVersionFilter()==null) { setDefaultLanguageVersionFilter( DefaultX4OLanguageVersionFilter.class); } + if (getDefaultLanguageLoader()==null) { setDefaultLanguageLoader( DefaultX4OLanguageLoader.class); } + if (getDefaultExpressionLanguageContext()==null) { setDefaultExpressionLanguageContext( X4OELContext.class); } } /** - * @see org.x4o.xml.lang.X4OLanguageConfiguration#getLanguageResourceModulesFileName() + * @see org.x4o.xml.lang.X4OLanguageConfigurationLocal#createProxy() */ - public String getLanguageResourceModulesFileName() { - return X4OLanguageConfiguration.DEFAULT_LANG_MODULES_FILE; - } - - /** - * @see org.x4o.xml.lang.X4OLanguageConfiguration#getDefaultElementNamespaceContext() - */ - public Class getDefaultElementNamespaceContext() { - return DefaultElementNamespaceContext.class; - } - - /** - * @see org.x4o.xml.lang.X4OLanguageConfiguration#getDefaultElementInterface() - */ - public Class getDefaultElementInterface() { - return DefaultElementInterface.class; - } - - /** - * @see org.x4o.xml.lang.X4OLanguageConfiguration#getDefaultElement() - */ - public Class getDefaultElement() { - return DefaultElement.class; - } - - /** - * @see org.x4o.xml.lang.X4OLanguageConfiguration#getDefaultElementClass() - */ - public Class getDefaultElementClass() { - return DefaultElementClass.class; - } - - /** - * @see org.x4o.xml.lang.X4OLanguageConfiguration#getDefaultElementClassAttribute() - */ - public Class getDefaultElementClassAttribute() { - return DefaultElementClassAttribute.class; - } - - /** - * @see org.x4o.xml.lang.X4OLanguageConfiguration#getDefaultElementLanguageModule() - */ - public Class getDefaultElementLanguageModule() { - return DefaultX4OLanguageModule.class; - } - - /** - * @see org.x4o.xml.lang.X4OLanguageConfiguration#getDefaultElementBodyComment() - */ - public Class getDefaultElementBodyComment() { - return DefaultElementBodyComment.class; - } - - /** - * @see org.x4o.xml.lang.X4OLanguageConfiguration#getDefaultElementBodyCharacters() - */ - public Class getDefaultElementBodyCharacters() { - return DefaultElementBodyCharacters.class; - } - - /** - * @see org.x4o.xml.lang.X4OLanguageConfiguration#getDefaultElementBodyWhitespace() - */ - public Class getDefaultElementBodyWhitespace() { - return DefaultElementBodyWhitespace.class; - } - - /** - * @see org.x4o.xml.lang.X4OLanguageConfiguration#getDefaultElementNamespaceInstanceProvider() - */ - public Class getDefaultElementNamespaceInstanceProvider() { - return DefaultElementNamespaceInstanceProvider.class; - } - - /** - * @see org.x4o.xml.lang.X4OLanguageConfiguration#getDefaultElementAttributeValueParser() - */ - public Class getDefaultElementAttributeValueParser() { - return DefaultElementAttributeValueParser.class; - } - - /** - * @see org.x4o.xml.lang.X4OLanguageConfiguration#getDefaultElementObjectPropertyValue() - */ - public Class getDefaultElementObjectPropertyValue() { - return DefaultElementObjectPropertyValue.class; - } - - /** - * @see org.x4o.xml.lang.X4OLanguageConfiguration#getDefaultElementAttributeHandlerComparator() - */ - public Class getDefaultElementAttributeHandlerComparator() { - return DefaultGlobalAttributeHandlerComparator.class; - } - - /** - * @see org.x4o.xml.lang.X4OLanguageConfiguration#getDefaultLanguageVersionFilter() - */ - public Class getDefaultLanguageVersionFilter() { - return DefaultX4OLanguageVersionFilter.class; - } - - /** - * @see org.x4o.xml.lang.X4OLanguageConfiguration#getDefaultLanguageLoader() - */ - public Class getDefaultLanguageLoader() { - return DefaultX4OLanguageLoader.class; - } - - /** - * @see org.x4o.xml.lang.X4OLanguageConfiguration#getDefaultExpressionLanguageContext() - */ - public Class getDefaultExpressionLanguageContext() { - return X4OELContext.class; + public X4OLanguageConfiguration createProxy() { + Object proxy = Proxy.newProxyInstance(X4OLanguageClassLoader.getClassLoader(), new Class[]{X4OLanguageConfiguration.class}, new InvocationHandler() { + public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { + X4OLanguageConfigurationLocal local = DefaultX4OLanguageConfiguration.this; + int argsLength = 0; + if (args!=null) { + argsLength = args.length; + } + Class[] invokeArgs = new Class[argsLength]; + for (int i=0;i getSAXParserPropertiesOptional(X4OLanguageContext elementContext) { Map saxParserProperties = new HashMap(1); - saxParserProperties.put("http://apache.org/xml/properties/input-buffer-size",elementContext.getLanguagePropertyInteger(X4OLanguageProperty.INPUT_BUFFER_SIZE)); // Increase buffer to 8KB + saxParserProperties.put("http://apache.org/xml/properties/input-buffer-size",elementContext.getLanguagePropertyInteger(X4OLanguageProperty.READER_BUFFER_SIZE)); // Increase buffer to 8KB return saxParserProperties; } @@ -223,11 +148,11 @@ public class DefaultX4OLanguageConfiguration implements X4OLanguageConfiguration boolean validation = false; boolean validationXsd = false; if (EldDriver.LANGUAGE_NAME.equals(elementContext.getLanguage().getLanguageName())) { - validation = elementContext.getLanguagePropertyBoolean(X4OLanguageProperty.VALIDATION_ELD); - validationXsd = elementContext.getLanguagePropertyBoolean(X4OLanguageProperty.VALIDATION_ELD_XSD); + validation = false; //TODO: elementContext.getLanguagePropertyBoolean(X4OLanguageProperty.VALIDATION_ELD); + validationXsd = false; //elementContext.getLanguagePropertyBoolean(X4OLanguageProperty.VALIDATION_ELD_XSD); } else { - validation = elementContext.getLanguagePropertyBoolean(X4OLanguageProperty.VALIDATION_INPUT); - validationXsd = elementContext.getLanguagePropertyBoolean(X4OLanguageProperty.VALIDATION_INPUT_XSD); + validation = elementContext.getLanguagePropertyBoolean(X4OLanguageProperty.READER_VALIDATION_INPUT); + validationXsd = elementContext.getLanguagePropertyBoolean(X4OLanguageProperty.READER_VALIDATION_INPUT_XSD); } if (validation) { saxParserFeatures.put("http://xml.org/sax/features/validation", true); // Validate the document and report validity errors. diff --git a/x4o-driver/src/main/java/org/x4o/xml/lang/X4OLanguageConfiguration.java b/x4o-driver/src/main/java/org/x4o/xml/lang/X4OLanguageConfiguration.java index 12341ed..dc6fd5e 100644 --- a/x4o-driver/src/main/java/org/x4o/xml/lang/X4OLanguageConfiguration.java +++ b/x4o-driver/src/main/java/org/x4o/xml/lang/X4OLanguageConfiguration.java @@ -23,11 +23,12 @@ package org.x4o.xml.lang; +import java.util.Collection; import java.util.List; import java.util.Map; /** - * X4OLanguageConfiguration first used interface in x4o parser which does the hard config of the x4o xml parsing. + * X4OLanguageConfiguration is base configuration of language used iin x4o parser. * * @author Willem Cazander * @version 1.0 27 Oct 2009 @@ -82,6 +83,10 @@ public interface X4OLanguageConfiguration { */ Class getDefaultExpressionLanguageContext(); + Collection getGlobalPropertyKeys(); + Object getGlobalProperty(String key); + void setGlobalProperty(String key,Object value); + /** * @return Returns Map of SAX properties which are set. */ diff --git a/x4o-driver/src/main/java/org/x4o/xml/lang/X4OLanguageConfigurationLocal.java b/x4o-driver/src/main/java/org/x4o/xml/lang/X4OLanguageConfigurationLocal.java new file mode 100644 index 0000000..a347aa2 --- /dev/null +++ b/x4o-driver/src/main/java/org/x4o/xml/lang/X4OLanguageConfigurationLocal.java @@ -0,0 +1,59 @@ +/* + * 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; + +/** + * X4OLanguageConfigurationLocal is for safe dynamic configuration. + * + * @author Willem Cazander + * @version 1.0 28 Apr 2013 + */ +public interface X4OLanguageConfigurationLocal extends X4OLanguageConfiguration { + + X4OLanguageConfiguration createProxy(); + + void setLanguageResourcePathPrefix(String value); + void setLanguageResourceModulesFileName(String value); + + void setDefaultElementNamespaceContext(Class value); + void setDefaultElementInterface(Class value); + void setDefaultElement(Class value); + void setDefaultElementClass(Class value); + void setDefaultElementClassAttribute(Class value); + + void setDefaultElementLanguageModule(Class value); + void setDefaultElementBodyComment(Class value); + void setDefaultElementBodyCharacters(Class value); + void setDefaultElementBodyWhitespace(Class value); + void setDefaultElementNamespaceInstanceProvider(Class value); + void setDefaultElementAttributeValueParser(Class value); + void setDefaultElementObjectPropertyValue(Class value); + void setDefaultElementAttributeHandlerComparator(Class value); + + void setDefaultLanguageVersionFilter(Class value); + void setDefaultLanguageLoader(Class value); + void setDefaultExpressionLanguageContext(Class value); + + void setGlobalProperty(String key,Object value); +} diff --git a/x4o-driver/src/main/java/org/x4o/xml/lang/X4OLanguageProperty.java b/x4o-driver/src/main/java/org/x4o/xml/lang/X4OLanguageProperty.java index 2a932d5..50b6b8e 100644 --- a/x4o-driver/src/main/java/org/x4o/xml/lang/X4OLanguageProperty.java +++ b/x4o-driver/src/main/java/org/x4o/xml/lang/X4OLanguageProperty.java @@ -31,6 +31,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; +import javax.el.ELContext; import javax.el.ExpressionFactory; import org.xml.sax.EntityResolver; @@ -46,132 +47,175 @@ import org.xml.sax.ext.DefaultHandler2; */ public enum X4OLanguageProperty { - /* TODO: refactor all keys ? - private final static String ENCODING = "http://writer.x4o.org/xml/properties/encoding"; - private final static String CHAR_NEWLINE = "http://writer.x4o.org/xml/properties/char/newline"; - private final static String CHAR_TAB = "http://writer.x4o.org/xml/properties/char/tab"; - private final static String URI_PREFX = "http://writer.x4o.org/xml/properties/char/"; - */ - /** Read-Only property returning the language we are working with. */ LANGUAGE_NAME(IO.GLOBAL,"language/name"), /** Read-Only property returning the version of the language. */ LANGUAGE_VERSION(IO.GLOBAL,"language/version"), - - /** When set to OutputStream xml debug is written to it. note: when output-handler is set this property is ignored. */ - DEBUG_OUTPUT_STREAM(IO.GLOBAL,"debug/output-stream",OutputStream.class), - - /** When set to DefaultHandler2 xml debug events are fired to the object. */ - DEBUG_OUTPUT_HANDLER(IO.GLOBAL,"debug/output-handler",DefaultHandler2.class), - - /** When set to true print also phases for parsing eld files. */ - DEBUG_OUTPUT_ELD_PARSER(IO.GLOBAL,"debug/output-eld-parser",Boolean.class,false), - - /** When set to true print full element tree per phase. */ - //DEBUG_OUTPUT_TREE_PER_PHASE("debug/output-tree-per-phase",Boolean.class), - - - /** SAX parser input buffer size: 512-16k defaults to 8k. */ - INPUT_BUFFER_SIZE(IO.READER,"input/buffer-size",Integer.class,4096*2), - - /** When set it allows parsing of non-namespace aware documents. */ - INPUT_EMPTY_NAMESPACE_URI(IO.READER,"input/empty-namespace-uri"), /** The input stream to parse, note is skipped when object is set. */ - INPUT_SOURCE_STREAM(IO.READER,"input/source/stream",InputStream.class), + READER_INPUT_STREAM(IO.READER,"reader/input/stream",InputStream.class), /** When set it overrides automatic encoding detection of sax parser. */ - INPUT_SOURCE_ENCODING(IO.READER,"input/source/encoding"), + READER_INPUT_ENCODING(IO.READER,"reader/input/encoding",String.class), /** When set use this InputSource instance for parsing. */ - INPUT_SOURCE_OBJECT(IO.READER,"input/source/object",InputSource.class), + READER_INPUT_SOURCE(IO.READER,"reader/input/source",InputSource.class), /** Sets the system-id to the input source. */ - INPUT_SOURCE_SYSTEM_ID(IO.READER,"input/source/system-id",String.class), + READER_INPUT_SYSTEM_ID(IO.READER,"reader/input/system-id",String.class), /** Sets the base path to resolve external input sources. */ - INPUT_SOURCE_BASE_PATH(IO.READER,"input/source/base-path",URL.class), + READER_INPUT_BASE_PATH(IO.READER,"reader/input/base-path",URL.class), + + + + /** SAX parser input buffer size: 512-16k defaults to 8k. */ + READER_BUFFER_SIZE(IO.READER,"reader/buffer-size",Integer.class,4096*2), + + /** When set it allows parsing of non-namespace aware documents. */ + READER_EMPTY_NAMESPACE_URI(IO.READER,"reader/empty-namespace-uri"), /** Set for custom handling of validation errors. */ - CONFIG_ERROR_HANDLER(IO.GLOBAL,"config/error-handler",ErrorHandler.class), + READER_ERROR_HANDLER(IO.READER,"reader/error-handler",ErrorHandler.class), /** Resolve more entities from local sources. */ - CONFIG_ENTITY_RESOLVER(IO.GLOBAL,"config/entity-resolver",EntityResolver.class), + READER_ENTITY_RESOLVER(IO.READER,"reader/entity-resolver",EntityResolver.class), + + + + /** When set to true then input xml is validated. */ + READER_VALIDATION_SCHEMA_AUTO_WRITE(IO.READER,"reader/validation/schema-auto-write",Boolean.class,true), + + /** When set this path is searched for xsd schema files in the language sub directory. */ + READER_VALIDATION_SCHEMA_PATH(IO.READER,"reader/validation/schema-path",File.class), + + /** When set to true then input xml is validated. */ + READER_VALIDATION_INPUT(IO.READER,"reader/validation/input",Boolean.class,false), + + /** When set to true then input xsd xml grammer is validated. */ + READER_VALIDATION_INPUT_XSD(IO.READER,"reader/validation/input/xsd",Boolean.class,false), + + + + + + /** The writer output stream to write to. */ + WRITER_OUTPUT_STREAM(IO.WRITER,"writer/output/stream",OutputStream.class), + + /** The writer output encoding. */ + WRITER_OUTPUT_ENCODING(IO.WRITER,"writer/output/encoding",String.class), + + /** The writer output newline. */ + WRITER_OUTPUT_CHAR_NEWLINE(IO.WRITER,"writer/output/char/newline",String.class), + + /** The writer output tab char. */ + WRITER_OUTPUT_CHAR_TAB(IO.WRITER,"writer/output/char/tab",String.class), + + /** When writing print schema uri. */ + WRITER_SCHEMA_URI_PRINT(IO.WRITER,"writer/schema/uri-print",Boolean.class,true), + + /** Override eld root schema uri. */ + WRITER_SCHEMA_URI_ROOT(IO.WRITER,"writer/schema/uri-root",String.class), + + + + + + /** The schema writer output path to write to. */ + SCHEMA_WRITER_OUTPUT_PATH(IO.SCHEMA_WRITER,"schema-writer/output/path",File.class), + + /** The schema writer output encoding. */ + SCHEMA_WRITER_OUTPUT_ENCODING(IO.SCHEMA_WRITER,"schema-writer/output/encoding",String.class), + + /** The schema writer output newline. */ + SCHEMA_WRITER_OUTPUT_CHAR_NEWLINE(IO.SCHEMA_WRITER,"schema-writer/output/char/newline",String.class), + + /** The schema writer output tab char. */ + SCHEMA_WRITER_OUTPUT_CHAR_TAB(IO.SCHEMA_WRITER,"schema-writer/output/char/tab",String.class), + + + + + + /** When set to OutputStream xml debug is written to it. note: when output-handler is set this property is ignored. */ + DEBUG_OUTPUT_STREAM(IO.READER_WRITER,"debug/output-stream",OutputStream.class), + + /** When set to DefaultHandler2 xml debug events are fired to the object. */ + DEBUG_OUTPUT_HANDLER(IO.READER_WRITER,"debug/output-handler",DefaultHandler2.class), + + /* When set to true print also phases for parsing eld files. */ + //DEBUG_OUTPUT_ELD_PARSER(IO.READER_WRITER,"debug/output-eld-parser",Boolean.class,false), + + /* When set to true print full element tree per phase. */ + //DEBUG_OUTPUT_TREE_PER_PHASE("debug/output-tree-per-phase",Boolean.class), + + - //CONFIG_CACHE_HANDLER("config/cache-handler"), - //CONFIG_MODULE_PROVIDER("config/module-provider"), - //CONFIG_LOCK_PROPERTIES("config/lock-properties"), /** The beans in this map are set into the EL context for reference. */ - EL_BEAN_INSTANCE_MAP(IO.READER,"el/bean-instance-map",Map.class), - //EL_CONTEXT_INSTANCE("el/context-instance"), + EL_BEAN_INSTANCE_MAP(IO.READER_WRITER,"el/bean-instance-map",Map.class), + + /** When set use this instance for the ELContext. */ + EL_CONTEXT_INSTANCE(IO.READER_WRITER,"el/context-instance",ELContext.class), + + /** When set use this instance as the ExpressionFactory. */ + EL_FACTORY_INSTANCE(IO.READER_WRITER,"el/factory-instance",ExpressionFactory.class), + + - /** When set this instance is used in ElementLanguage. */ - EL_FACTORY_INSTANCE(IO.READER,"el/factory-instance",ExpressionFactory.class), - //EL_INSTANCE_FACTORY("phase/skip-elb-init",Boolean.class), /** When set to an X4OPhase then parsing stops after completing the set phase. */ - PHASE_STOP_AFTER(IO.READER,"phase/stop-after",String.class), + PHASE_STOP_AFTER(IO.READER_WRITER,"phase/stop-after",String.class), /** When set to true skip the release phase. */ - PHASE_SKIP_RELEASE(IO.READER,"phase/skip-release",Boolean.class,false), + PHASE_SKIP_RELEASE(IO.READER_WRITER,"phase/skip-release",Boolean.class,false), /** When set to true skip the run phase. */ - PHASE_SKIP_RUN(IO.READER,"phase/skip-run",Boolean.class,false), - - /** When set to true skip the load siblings language phase. */ - PHASE_SKIP_SIBLINGS(IO.READER,"phase/skip-siblings",Boolean.class,false), + PHASE_SKIP_RUN(IO.READER_WRITER,"phase/skip-run",Boolean.class,false); - /** When set this path is searched for xsd schema files in the language sub directory. */ - VALIDATION_SCHEMA_PATH(IO.READER,"validation/schema-path",File.class), - /** When set to true then input xml is validated. */ - VALIDATION_INPUT(IO.READER,"validation/input",Boolean.class,false), - /** When set to true then input xsd xml grammer is validated. */ - VALIDATION_INPUT_XSD(IO.READER,"validation/input/xsd",Boolean.class,false), - /** When set to true then eld xml is validated. */ - VALIDATION_ELD(IO.READER,"validation/eld",Boolean.class,false), + // (temp) removed because init is now in driver manager - /** When set to true than eld xsd xml grammer is also validated. */ - VALIDATION_ELD_XSD(IO.GLOBAL, "validation/eld/xsd",Boolean.class,false); + /* When set to true skip the load siblings language phase. */ + //PHASE_SKIP_SIBLINGS(IO.READER,"phase/skip-siblings",Boolean.class,false); + + /* When set to true then eld xml is validated. */ + //VALIDATION_ELD(IO.INIT,"validation/eld",Boolean.class,false), + + /* When set to true than eld xsd xml grammer is also validated. */ + //VALIDATION_ELD_XSD(IO.INIT, "validation/eld/xsd",Boolean.class,false); - public final static X4OLanguageProperty[] DEFAULT_X4O_GLOBAL_KEYS; public final static X4OLanguageProperty[] DEFAULT_X4O_READER_KEYS; public final static X4OLanguageProperty[] DEFAULT_X4O_WRITER_KEYS; public final static X4OLanguageProperty[] DEFAULT_X4O_SCHEMA_WRITER_KEYS; static { - List globalResultKeys = new ArrayList(); List readerResultKeys = new ArrayList(); List writerResultKeys = new ArrayList(); List schemaWriterResultKeys = new ArrayList(); X4OLanguageProperty[] keys = X4OLanguageProperty.values(); for (int i=0;i prefixes = new HashMap(); + startPrefixTree(root,prefixes); + for (String uri:prefixes.keySet()) { + String prefix = prefixes.get(uri); + writer.startPrefixMapping(prefix, uri); + } + try { + writeTree(writer,root,true); + } catch (ElementObjectPropertyValueException e) { + throw new SAXException(e); + } + writer.endDocument(); + out.flush(); + } catch (Exception e) { throw new X4OPhaseException(this,e); + } finally { + if (out!=null) { + try { + out.close(); + } catch (IOException e) { + } + } } } + + 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,boolean isRoot) throws SAXException, ElementObjectPropertyValueException { + AttributesImpl atts = new AttributesImpl(); + + if (isRoot && schemaUriPrint) { + String rootUri = findElementUri(element); + writer.startPrefixMapping("xsi", XMLConstants.XML_SCHEMA_INSTANCE_NS_URI); + atts.addAttribute ("xsi", "schemaLocation", "", "", rootUri+" "+schemaUriRoot); + } + if (element.getElementClass().getAutoAttributes()!=null && element.getElementClass().getAutoAttributes()==false) { + for (ElementClassAttribute eca:element.getElementClass().getElementClassAttributes()) { + if (eca.getRunBeanValue()!=null && eca.getRunBeanValue()==false) { + continue; + } + + Object value = element.getLanguageContext().getElementObjectPropertyValue().getProperty(element.getElementObject(),eca.getId()); + if (value==null) { + continue; + } + atts.addAttribute ("", eca.getId(), "", "", ""+value); + } + + } else { + for (String p:getProperties(element.getElementObject().getClass())) { + + ElementClassAttribute eca = element.getElementClass().getElementClassAttributeByName(p); + if (eca!=null && eca.getRunBeanValue()!=null && eca.getRunBeanValue()) { + continue; + } + boolean writeValue = true; + for (ElementInterface ei:element.getLanguageContext().getLanguage().findElementInterfaces(element.getElementObject().getClass())) { + eca = ei.getElementClassAttributeByName(p); + if (eca!=null && eca.getRunBeanValue()!=null && eca.getRunBeanValue()==false) { + writeValue = false; + break; + } + } + if (writeValue==false) { + continue; + } + + // TODO: check attr see reader + 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().getId(), "", atts); + for (Element e:element.getChilderen()) { + writeTree(writer,e,false); + } + writer.endElement(elementUri, element.getElementClass().getId(), ""); + } + + private String findElementUri(Element e) { + for (X4OLanguageModule mod:e.getLanguageContext().getLanguage().getLanguageModules()) { + for (ElementNamespaceContext c:mod.getElementNamespaceContexts()) { + ElementClass ec = c.getElementClass(e.getElementClass().getId()); + 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(); + } }; /** @@ -201,7 +365,7 @@ public class X4OPhaseLanguageWrite { return "WRITE_END"; } public String[] getPhaseDependencies() { - return new String[]{"WRITE_FILL_TREE"}; + return new String[]{"WRITE_XML"}; } public boolean isElementPhase() { return false; diff --git a/x4o-driver/src/test/java/org/x4o/xml/core/NamespaceUriTest.java b/x4o-driver/src/test/java/org/x4o/xml/core/NamespaceUriTest.java index 266e2ee..93bd48a 100644 --- a/x4o-driver/src/test/java/org/x4o/xml/core/NamespaceUriTest.java +++ b/x4o-driver/src/test/java/org/x4o/xml/core/NamespaceUriTest.java @@ -57,7 +57,7 @@ public class NamespaceUriTest extends TestCase { TestDriver driver = TestDriver.getInstance(); X4OReaderContext reader = driver.createReaderContext(); reader.setProperty(X4OLanguagePropertyKeys.PHASE_SKIP_RELEASE, true); - reader.setProperty(X4OLanguagePropertyKeys.INPUT_EMPTY_NAMESPACE_URI, "http://test.x4o.org/xml/ns/test-lang"); + reader.setProperty(X4OLanguagePropertyKeys.READER_EMPTY_NAMESPACE_URI, "http://test.x4o.org/xml/ns/test-lang"); try { context = reader.readResourceContext("tests/namespace/uri-empty.xml"); assertEquals(true,context.getRootElement().getChilderen().size()==1); diff --git a/x4o-driver/src/test/java/org/x4o/xml/core/X4ODebugWriterTest.java b/x4o-driver/src/test/java/org/x4o/xml/core/X4ODebugWriterTest.java index 201eaba..e9876d5 100644 --- a/x4o-driver/src/test/java/org/x4o/xml/core/X4ODebugWriterTest.java +++ b/x4o-driver/src/test/java/org/x4o/xml/core/X4ODebugWriterTest.java @@ -66,7 +66,7 @@ public class X4ODebugWriterTest extends TestCase { X4ODriver driver = TestDriver.getInstance(); X4OReader reader = driver.createReader(); reader.setProperty(X4OLanguagePropertyKeys.DEBUG_OUTPUT_STREAM, new FileOutputStream(debugFile)); - reader.setProperty(X4OLanguagePropertyKeys.DEBUG_OUTPUT_ELD_PARSER, true); + //reader.setProperty(X4OLanguagePropertyKeys.DEBUG_OUTPUT_ELD_PARSER, true); reader.readResource("tests/attributes/test-bean.xml"); assertTrue("Debug file does not exists.",debugFile.exists()); diff --git a/x4o-driver/src/test/java/org/x4o/xml/core/X4OEntityResolverTest.java b/x4o-driver/src/test/java/org/x4o/xml/core/X4OEntityResolverTest.java index 46586ab..c55e2e5 100644 --- a/x4o-driver/src/test/java/org/x4o/xml/core/X4OEntityResolverTest.java +++ b/x4o-driver/src/test/java/org/x4o/xml/core/X4OEntityResolverTest.java @@ -82,7 +82,7 @@ public class X4OEntityResolverTest extends TestCase { public void testResolveProperty() throws Exception { X4ODriver driver = new TestDriver(); X4OLanguageContext language = driver.createLanguageContext(); - language.setLanguageProperty(X4OLanguageProperty.CONFIG_ENTITY_RESOLVER, new TestEntityResolver()); + language.setLanguageProperty(X4OLanguageProperty.READER_ENTITY_RESOLVER, new TestEntityResolver()); X4OEntityResolver resolver = new X4OEntityResolver(language); Exception e = null; InputSource input = null; @@ -98,7 +98,7 @@ public class X4OEntityResolverTest extends TestCase { public void testResolvePropertyNull() throws Exception { X4ODriver driver = new TestDriver(); X4OLanguageContext language = driver.createLanguageContext(); - language.setLanguageProperty(X4OLanguageProperty.CONFIG_ENTITY_RESOLVER, new TestEntityResolver()); + language.setLanguageProperty(X4OLanguageProperty.READER_ENTITY_RESOLVER, new TestEntityResolver()); X4OEntityResolver resolver = new X4OEntityResolver(language); Exception e = null; try { diff --git a/x4o-driver/src/test/java/org/x4o/xml/eld/EldValidatingTest.java b/x4o-driver/src/test/java/org/x4o/xml/eld/EldValidatingTest.java index ba4d316..9479806 100644 --- a/x4o-driver/src/test/java/org/x4o/xml/eld/EldValidatingTest.java +++ b/x4o-driver/src/test/java/org/x4o/xml/eld/EldValidatingTest.java @@ -44,7 +44,7 @@ public class EldValidatingTest extends TestCase { X4ODriver driver = TestDriver.getInstance(); X4OReader reader = driver.createReader(); reader.setProperty(X4OLanguagePropertyKeys.PHASE_SKIP_RELEASE, true); - reader.setProperty(X4OLanguagePropertyKeys.VALIDATION_INPUT, true); + reader.setProperty(X4OLanguagePropertyKeys.READER_VALIDATION_INPUT, true); //parser.setProperty(X4OLanguagePropertyKeys.VALIDATION_SCHEMA_PATH, "/tmp"); try { // TODO: reader.readResource("META-INF/eld/eld-lang.eld"); diff --git a/x4o-driver/src/test/java/org/x4o/xml/io/X4OWriterTest.java b/x4o-driver/src/test/java/org/x4o/xml/io/X4OWriterTest.java index 015b824..f2e7732 100644 --- a/x4o-driver/src/test/java/org/x4o/xml/io/X4OWriterTest.java +++ b/x4o-driver/src/test/java/org/x4o/xml/io/X4OWriterTest.java @@ -30,6 +30,7 @@ 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 org.x4o.xml.test.swixml.Accelerator3; @@ -59,12 +60,15 @@ public class X4OWriterTest extends TestCase { X4OReader reader = driver.createReader(); X4OWriter writer = driver.createWriter(); + writer.setProperty(X4OLanguagePropertyKeys.WRITER_OUTPUT_CHAR_TAB, " "); + writer.setProperty(X4OLanguagePropertyKeys.WRITER_SCHEMA_URI_PRINT, false); + TestObjectRoot root = reader.readResource("tests/attributes/test-bean.xml"); writer.writeFile(root, outputFile); String text = new Scanner( outputFile ).useDelimiter("\\A").next(); outputFile.delete(); - //System.out.println("Output: '\n"+text+"\n' end in "+outputFile.getAbsolutePath()); + System.out.println("Output: '\n"+text+"\n' end in "+outputFile.getAbsolutePath()); assertTrue(text.startsWith("")); assertTrue(text.contains("http://test.x4o.org/xml/ns/test-root")); @@ -84,6 +88,9 @@ public class X4OWriterTest extends TestCase { X4OReader reader = driver.createReader(); X4OWriter writer = driver.createWriter(); + //reader.setProperty(key, value); + //writer.setProperty(key, value); + reader.addELBeanInstance(SwiXmlDriver.LANGUAGE_EL_SWING_ENGINE, engine); Component root = reader.readResource("tests/swixml/swixml-accelerator-3.0.xml"); writer.writeFile(root, outputFile); diff --git a/x4o-driver/src/test/java/org/x4o/xml/test/SwingTests.java b/x4o-driver/src/test/java/org/x4o/xml/test/SwingTests.java index 9d727f7..b67fe13 100644 --- a/x4o-driver/src/test/java/org/x4o/xml/test/SwingTests.java +++ b/x4o-driver/src/test/java/org/x4o/xml/test/SwingTests.java @@ -52,7 +52,7 @@ public class SwingTests extends TestCase { File f = File.createTempFile("test-swing", ".xml"); //f.deleteOnExit(); reader.setProperty(X4OLanguagePropertyKeys.DEBUG_OUTPUT_STREAM, new FileOutputStream(f)); - reader.setProperty(X4OLanguagePropertyKeys.DEBUG_OUTPUT_ELD_PARSER, true); + //reader.setProperty(X4OLanguagePropertyKeys.DEBUG_OUTPUT_ELD_PARSER, true); //reader.readResource("tests/test-swing.xml"); //Thread.sleep(30000); }