XML Configurations
An important sub category of hierarchical configurations files are XML
documents. Commons Configuration ships with the
Validation of XML configuration files
XML parsers provide support for validation of XML documents to ensure that they
conform to a certain DTD or XML Schema. This feature can be useful for
configuration files, too.
The easiest way to turn on validation is to simply set the
Parameters params = new Parameters(); FileBasedConfigurationBuilder<XMLConfiguration> builder = new FileBasedConfigurationBuilder<XMLConfiguration>(XMLConfiguration.class) .configure(params.xml() .setFileName("myconfig.xml") .setValidating(true)); // This will throw a ConfigurationException if the XML document does not // conform to its DTD. XMLConfiguration config = builder.getConfiguration();
Setting the
XML Parsers also provide support for validating XML documents using an
XML Schema.
Parameters params = new Parameters(); FileBasedConfigurationBuilder<XMLConfiguration> builder = new FileBasedConfigurationBuilder<XMLConfiguration>(XMLConfiguration.class) .configure(params.xml() .setFileName("myconfig.xml") .setSchemaValidation(true)); // This will throw a ConfigurationException if the XML document does not // conform to its Schema. XMLConfiguration config = builder.getConfiguration(); Default Entity Resolution
There is also some support for dealing with DTD files. Often the
DTD of an XML document is stored locally so that it can be quickly
accessed. However the <?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2.2.dtd">
When working with XML documents directly you would use an
Parameters params = new Parameters(); DefaultEntityResolver resolver = new DefaultEntityResolver(); // load the URL to the DTD file from class path URL dtdURL = getClass().getResource("web-app_2.2.dtd"); // register it at the resolver resolver.registerEntityId("-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN", dtdURL); FileBasedConfigurationBuilder<XMLConfiguration> builder = new FileBasedConfigurationBuilder<XMLConfiguration>(XMLConfiguration.class) .configure(params.xml() .setFileName("web.xml") .setEntityResolver(resolver) .setValidating(true)); XMLConfiguration config = builder.getConfiguration(); This basically tells the XML configuration to use the specified URL when it encounters the given public ID. Enhanced Entity ResolutionWhile the default entity resolver can be used under certain circumstances, it does not work well when using combined configurations from multiple sources. Furthermore, in many circumstances the programmatic nature of registering entities will tie the application tightly to the XML content. In addition, because it only works with the public id it cannot support XML documents using an XML Schema.
XML
Entity and URI Resolvers describes using a set of catalog files to
resolve entities. Commons Configuration provides support for
this Catalog Resolver through its own
<?xml version="1.0" encoding="ISO-8859-1"?> <Employees xmlns="https://commons.apache.org/employee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://commons.apache.org/employee https://commons.apache.org/sample.xsd"> <Employee> <SSN>555121211</SSN> <Name>John Doe</Name> <DateOfBirth>1975-05-15</DateOfBirth> <EmployeeType>Exempt</EmployeeType> <Salary>100000</Salary> </Employee> </Employees> The XML sample above is an XML document using a default namespace of https://commons.apache.org/employee. The schemaLocation allows a set of namespaces and hints to the location of their corresponding schemas. When processing the document the parser will pass the hint, in this case https://commons.apache.org/sample.xsd, to the entity resolver as the system id. More information on using schema locations can be found at schemaLocation.
The example that follows shows how to use the Parameters params = new Parameters(); CatalogResolver resolver = new CatalogResolver(); resolver.setCatalogFiles("local/catalog.xml","http://test.org/catalogs/catalog1.xml"); FileBasedConfigurationBuilder<XMLConfiguration> builder = new FileBasedConfigurationBuilder<XMLConfiguration>(XMLConfiguration.class) .configure(params.xml() .setFileName("config.xml") .setEntityResolver(resolver) .setSchemaValidation(true)); // enable schema validation XMLConfiguration config = builder.getConfiguration(); Extending Validation and Entity Resolution
The mechanisms provided with Commons Configuration will hopefully be
sufficient in most cases; however, there will certainly be circumstances
where they are not.
So an application can create a Parameters params = new Parameters(); DocumentBuilder docBuilder = ... // set up a custom document builder FileBasedConfigurationBuilder<XMLConfiguration> builder = new FileBasedConfigurationBuilder<XMLConfiguration>(XMLConfiguration.class) .configure(params.xml() .setFileName("config.xml") .setDocumentBuilder(docBuilder); XMLConfiguration config = builder.getConfiguration(); Builder Configuration Related to XML Configurations
XML configurations are created in the typical way: via configuration
builders. The initialization parameters supported for them include all
the settings available for hierarchical configurations and file-based
configurations. In addition, the
A parameters object for an XML configuration can be obtained using
the |