You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by oh...@apache.org on 2005/07/18 21:46:12 UTC

svn commit: r219557 - in /jakarta/commons/proper/configuration/trunk: conf/ src/java/org/apache/commons/configuration/ src/test/org/apache/commons/configuration/

Author: oheger
Date: Mon Jul 18 12:36:33 2005
New Revision: 219557

URL: http://svn.apache.org/viewcvs?rev=219557&view=rev
Log:
Implemented support for setting a custom DocumentBuilder in XMLConfiguration

Added:
    jakarta/commons/proper/configuration/trunk/conf/testValidateInvalid.xml   (with props)
    jakarta/commons/proper/configuration/trunk/conf/testValidateValid.xml   (with props)
Modified:
    jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/XMLConfiguration.java
    jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestXMLConfiguration.java

Added: jakarta/commons/proper/configuration/trunk/conf/testValidateInvalid.xml
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/configuration/trunk/conf/testValidateInvalid.xml?rev=219557&view=auto
==============================================================================
--- jakarta/commons/proper/configuration/trunk/conf/testValidateInvalid.xml (added)
+++ jakarta/commons/proper/configuration/trunk/conf/testValidateInvalid.xml Mon Jul 18 12:36:33 2005
@@ -0,0 +1,32 @@
+<?xml version="1.0" encoding="ISO-8859-1"?>
+<!--
+  This is an invalid XML document, it does not conform to the declared DTD
+  (a type is missing in one field element). This document is used for testing
+  XMLConfiguration with a validating parser. It should be possible to load
+  it if validation is disabled, but if validation is enabled, an exception
+  should be thrown.
+-->
+<!DOCTYPE database [
+<!ELEMENT database (table+)>
+<!ELEMENT table (name, fields)>
+
+<!ELEMENT fields (field+)>
+<!ELEMENT field (name, type)>
+
+<!ELEMENT name (#PCDATA)>
+<!ELEMENT type (#PCDATA)>
+]>
+<database>
+  <table>
+    <name>customers</name>
+    <fields>
+      <field>
+        <name>custID</name>
+        <type>java.lang.Long</type>
+      </field>
+      <field>
+        <name>custName</name>
+      </field>
+    </fields>
+  </table>
+</database>
\ No newline at end of file

Propchange: jakarta/commons/proper/configuration/trunk/conf/testValidateInvalid.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/proper/configuration/trunk/conf/testValidateInvalid.xml
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/commons/proper/configuration/trunk/conf/testValidateInvalid.xml
------------------------------------------------------------------------------
    svn:mime-type = text/xml

Added: jakarta/commons/proper/configuration/trunk/conf/testValidateValid.xml
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/configuration/trunk/conf/testValidateValid.xml?rev=219557&view=auto
==============================================================================
--- jakarta/commons/proper/configuration/trunk/conf/testValidateValid.xml (added)
+++ jakarta/commons/proper/configuration/trunk/conf/testValidateValid.xml Mon Jul 18 12:36:33 2005
@@ -0,0 +1,30 @@
+<?xml version="1.0" encoding="ISO-8859-1"?>
+<!--
+  A valid XML document according to its DTD.
+-->
+<!DOCTYPE database [
+<!ELEMENT database (table+)>
+<!ELEMENT table (name, fields)>
+
+<!ELEMENT fields (field+)>
+<!ELEMENT field (name, type)>
+
+<!ELEMENT name (#PCDATA)>
+<!ELEMENT type (#PCDATA)>
+]>
+
+<database>
+  <table>
+    <name>customers</name>
+    <fields>
+      <field>
+        <name>custID</name>
+        <type>java.lang.Long</type>
+      </field>
+      <field>
+        <name>custName</name>
+        <type>java.lang.String</type>
+      </field>
+    </fields>
+  </table>
+</database>
\ No newline at end of file

Propchange: jakarta/commons/proper/configuration/trunk/conf/testValidateValid.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/proper/configuration/trunk/conf/testValidateValid.xml
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/commons/proper/configuration/trunk/conf/testValidateValid.xml
------------------------------------------------------------------------------
    svn:mime-type = text/xml

Modified: jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/XMLConfiguration.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/XMLConfiguration.java?rev=219557&r1=219556&r2=219557&view=diff
==============================================================================
--- jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/XMLConfiguration.java (original)
+++ jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/XMLConfiguration.java Mon Jul 18 12:36:33 2005
@@ -77,7 +77,10 @@
 
     /** Stores the name of the root element. */
     private String rootElementName;
-
+    
+    /** Stores the document builder that should be used for loading.*/
+    private DocumentBuilder documentBuilder;
+    
     /**
      * Creates a new instance of <code>XMLConfiguration</code>.
      */
@@ -172,7 +175,35 @@
         }
         rootElementName = name;
     }
-    
+
+    /**
+     * Returns the <code>DocumentBuilder</code> object that is used for
+     * loading documents. If no specific builder has been set, this method
+     * returns <b>null</b>.
+     * 
+     * @return the <code>DocumentBuilder</code> for loading new documents
+     * @since 1.2
+     */
+    public DocumentBuilder getDocumentBuilder()
+    {
+        return documentBuilder;
+    }
+
+    /**
+     * Sets the <code>DocumentBuilder</code> object to be used for loading
+     * documents. This method makes it possible to specify the exact document
+     * builder. So an application can create a builder, configure it for its
+     * special needs, and then pass it to this method.
+     * 
+     * @param documentBuilder the document builder to be used; if undefined, a
+     * default builder will be used
+     * @since 1.2
+     */
+    public void setDocumentBuilder(DocumentBuilder documentBuilder)
+    {
+        this.documentBuilder = documentBuilder;
+    }
+
     /**
      * Returns the XML document this configuration was loaded from. The return
      * value is <b>null</b> if this configuration was not loaded from a XML
@@ -306,6 +337,32 @@
             }
         }
     }
+    
+    /**
+     * Creates the <code>DocumentBuilder</code> to be used for loading files.
+     * This implementation checks whether a specific
+     * <code>DocumentBuilder</code> has been set. If this is the case, this
+     * one is used. Otherwise a default builder is created.
+     * 
+     * @return the <code>DocumentBuilder</code> for loading configuration
+     * files
+     * @throws ParserConfigurationException if an error occurs
+     * @since 1.2
+     */
+    protected DocumentBuilder createDocumentBuilder()
+            throws ParserConfigurationException
+    {
+        if (getDocumentBuilder() != null)
+        {
+            return getDocumentBuilder();
+        }
+        else
+        {
+            DocumentBuilderFactory factory = DocumentBuilderFactory
+                    .newInstance();
+            return factory.newDocumentBuilder();
+        }
+    }
 
     /**
      * Creates a DOM document from the internal tree of configuration nodes.
@@ -374,19 +431,7 @@
 
     public void load(InputStream in) throws ConfigurationException
     {
-        try
-        {
-            DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
-            Document newDocument = builder.parse(new InputSource(in));
-            Document oldDocument = document;
-            document = null;
-            initProperties(newDocument, oldDocument == null);
-            document = (oldDocument == null) ? newDocument : oldDocument;
-        }
-        catch (Exception e)
-        {
-            throw new ConfigurationException(e.getMessage(), e);
-        }
+        load(new InputSource(in));
     }
 
     public void load(InputStream in, String encoding) throws ConfigurationException
@@ -406,10 +451,20 @@
      */
     public void load(Reader in) throws ConfigurationException
     {
+        load(new InputSource(in));
+    }
+    
+    /**
+     * Loads a configuration file from the specified input source.
+     * @param source the input source
+     * @throws ConfigurationException if an error occurs
+     */
+    private void load(InputSource source) throws ConfigurationException
+    {
         try
         {
-            DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
-            Document newDocument = builder.parse(new InputSource(in));
+            DocumentBuilder builder = createDocumentBuilder();
+            Document newDocument = builder.parse(source);
             Document oldDocument = document;
             document = null;
             initProperties(newDocument, oldDocument == null);

Modified: jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestXMLConfiguration.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestXMLConfiguration.java?rev=219557&r1=219556&r2=219557&view=diff
==============================================================================
--- jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestXMLConfiguration.java (original)
+++ jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestXMLConfiguration.java Mon Jul 18 12:36:33 2005
@@ -23,7 +23,13 @@
 import java.util.Iterator;
 import java.util.List;
 
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+
 import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXParseException;
+import org.xml.sax.helpers.DefaultHandler;
 
 import junit.framework.TestCase;
 
@@ -450,6 +456,7 @@
             conf.setReloadingStrategy(strategy);
             conf.load();
             assertEquals(1, conf.getInt("test"));
+            Thread.sleep(1000);
 
             out = new PrintWriter(new FileWriter(testSaveConf));
             out.println("<?xml version=\"1.0\"?><config><test>2</test></config>");
@@ -485,5 +492,46 @@
     {
         assertEquals("Name with dot", conf.getString("complexNames.my..elem"));
         assertEquals("Another dot", conf.getString("complexNames.my..elem.sub..elem"));
+    }
+    
+    /**
+     * Tests setting a custom document builder.
+     */
+    public void testCustomDocBuilder() throws Exception
+    {
+        // Load an invalid XML file with the default (non validating)
+        // doc builder. This should work...
+        conf = new XMLConfiguration();
+        conf.load(new File("conf/testValidateInvalid.xml"));
+        assertEquals("customers", conf.getString("table.name"));
+        assertFalse(conf.containsKey("table.fields.field(1).type"));
+
+        // Now create a validating doc builder and set it.
+        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+        factory.setValidating(true);
+        DocumentBuilder builder = factory.newDocumentBuilder();
+        builder.setErrorHandler(new DefaultHandler() {
+            public void error(SAXParseException ex) throws SAXException
+            {
+                throw ex;
+            }
+        });
+        conf = new XMLConfiguration();
+        conf.setDocumentBuilder(builder);
+        try
+        {
+            conf.load(new File("conf/testValidateInvalid.xml"));
+            fail("Could load invalid file with validating set to true!");
+        }
+        catch(ConfigurationException ex)
+        {
+            //ok
+        }
+        
+        // Try to load a valid document with a validating builder
+        conf = new XMLConfiguration();
+        conf.setDocumentBuilder(builder);
+        conf.load(new File("conf/testValidateValid.xml"));
+        assertTrue(conf.containsKey("table.fields.field(1).type"));
     }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org