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/08/07 19:19:35 UTC
svn commit: r230684 - in /jakarta/commons/proper/configuration/trunk:
src/java/org/apache/commons/configuration/XMLConfiguration.java
src/test/org/apache/commons/configuration/TestXMLConfiguration.java
xdocs/changes.xml
Author: oheger
Date: Sun Aug 7 10:19:16 2005
New Revision: 230684
URL: http://svn.apache.org/viewcvs?rev=230684&view=rev
Log:
Implemented a validating flag in XMLConfiguration which allows for easy DTD validation
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
jakarta/commons/proper/configuration/trunk/xdocs/changes.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=230684&r1=230683&r2=230684&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 Sun Aug 7 10:19:16 2005
@@ -48,6 +48,9 @@
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXParseException;
+import org.xml.sax.helpers.DefaultHandler;
import org.apache.commons.configuration.reloading.ReloadingStrategy;
/**
@@ -88,6 +91,9 @@
/** Stores the document builder that should be used for loading.*/
private DocumentBuilder documentBuilder;
+ /** Stores a flag whether DTD validation should be performed.*/
+ private boolean validating;
+
/**
* Creates a new instance of <code>XMLConfiguration</code>.
*/
@@ -212,6 +218,27 @@
}
/**
+ * Returns the value of the validating flag.
+ * @return the validating flag
+ * @since 1.2
+ */
+ public boolean isValidating()
+ {
+ return validating;
+ }
+
+ /**
+ * Sets the value of the validating flag. This flag determines whether
+ * DTD validation should be performed when loading XML documents. This
+ * flag is evaluated only if no custom <code>DocumentBuilder</code> was set.
+ * @param validating the validating flag
+ */
+ public void setValidating(boolean validating)
+ {
+ this.validating = validating;
+ }
+
+ /**
* 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
* document.
@@ -384,7 +411,9 @@
* 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.
+ * one is used. Otherwise a default builder is created. Depending on the
+ * value of the validating flag this builder will be a validating or a non
+ * validating <code>DocumentBuilder</code>.
*
* @return the <code>DocumentBuilder</code> for loading configuration
* files
@@ -402,7 +431,21 @@
{
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
- return factory.newDocumentBuilder();
+ factory.setValidating(isValidating());
+ DocumentBuilder result = factory.newDocumentBuilder();
+
+ if (isValidating())
+ {
+ // register an error handler which detects validation errors
+ result.setErrorHandler(new DefaultHandler()
+ {
+ public void error(SAXParseException ex) throws SAXException
+ {
+ throw ex;
+ }
+ });
+ }
+ return result;
}
}
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=230684&r1=230683&r2=230684&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 Sun Aug 7 10:19:16 2005
@@ -598,4 +598,31 @@
assertEquals("value1", conf.getString("entry(0)"));
assertEquals("test2", conf.getString("entry(1)[@key]"));
}
+
+ /**
+ * Tests DTD validation using the setValidating() method.
+ */
+ public void testValidating() throws ConfigurationException
+ {
+ File nonValidFile = new File("conf/testValidateInvalid.xml");
+ conf = new XMLConfiguration();
+ assertFalse(conf.isValidating());
+
+ // Load a non valid XML document. Should work for isValidating() == false
+ conf.load(nonValidFile);
+ assertEquals("customers", conf.getString("table.name"));
+ assertFalse(conf.containsKey("table.fields.field(1).type"));
+
+ // Now set the validating flag to true
+ conf.setValidating(true);
+ try
+ {
+ conf.load(nonValidFile);
+ fail("Validation was not performed!");
+ }
+ catch(ConfigurationException cex)
+ {
+ //ok
+ }
+ }
}
Modified: jakarta/commons/proper/configuration/trunk/xdocs/changes.xml
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/configuration/trunk/xdocs/changes.xml?rev=230684&r1=230683&r2=230684&view=diff
==============================================================================
--- jakarta/commons/proper/configuration/trunk/xdocs/changes.xml (original)
+++ jakarta/commons/proper/configuration/trunk/xdocs/changes.xml Sun Aug 7 10:19:16 2005
@@ -69,9 +69,10 @@
that can be used instead.
</action>
<action dev="oheger" type="add" issue="31616">
- XMLConfiguration now supports setting a custom DocumentBuilder for
- loading XML documents. This makes it possible e.g. to enable validation
- when a configuration file is loaded.
+ XMLConfiguration now provides some support for validating XML
+ documents. With the setValidating() method DTD validation can be
+ enabled. It is also possible to set a custom DocumentBuilder allowing
+ a caller to perform enhanced configuration of the XML loading process.
</action>
<action dev="oheger" type="update" issue="35621">
AbstractFileConfiguration now always sets a valid base path if the
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org