com.alibaba.antx.util.configuration.DefaultConfigurationBuilder Maven / Gradle / Ivy
/*
* Copyright (c) 2002-2012 Alibaba Group Holding Limited.
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.antx.util.configuration;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
/**
* A DefaultConfigurationBuilder builds Configuration
s from XML,
* via a SAX2 compliant parser.
*
* XML namespace support is optional, and disabled by default to preserve
* backwards-compatibility. To enable it, pass the
* {@link #DefaultConfigurationBuilder(boolean)} constructor the flag
* true
, or pass a namespace-enabled XMLReader
to the
* {@link #DefaultConfigurationBuilder(XMLReader)} constructor.
*
*
* The mapping from XML namespaces to {@link Configuration} namespaces is pretty
* straightforward, with one caveat: attribute namespaces are (deliberately) not
* supported. Enabling namespace processing has the following effects:
*
*
* - Attributes starting with
xmlns:
are interpreted as declaring
* a prefix:namespaceURI mapping, and won't result in the creation of
* xmlns
-prefixed attributes in the Configuration
.
* - Prefixed XML elements, like
* <doc:title xmlns:doc="http://foo.com">, will result in a
*
Configuration
with {@link Configuration#getName
* getName()}.equals("title")
and {@link Configuration#getNamespace
* getNamespace()}.equals("http://foo.com")
.
*
*
* Whitespace handling. Since mixed content is not allowed in the
* configurations, whitespace is completely discarded in non-leaf nodes. For the
* leaf nodes the default behavior is to trim the space surrounding the value.
* This can be changed by specifying xml:space
attribute with value
* of preserve
in that case the whitespace is left intact.
*
*
* @author Avalon Development Team
*/
public class DefaultConfigurationBuilder {
private SAXConfigurationHandler m_handler;
private XMLReader m_parser;
/**
* Create a Configuration Builder with a default XMLReader that ignores
* namespaces. In order to enable namespaces, use either the constructor
* that has a boolean or that allows you to pass in your own
* namespace-enabled XMLReader.
*/
public DefaultConfigurationBuilder() {
this(false);
}
/**
* Create a Configuration Builder, specifying a flag that determines
* namespace support.
*
* @param enableNamespaces If true
, a namespace-aware
* SAXParser
is used. If false
, the
* default JAXP SAXParser
(without namespace
* support) is used.
* @since 4.1
*/
public DefaultConfigurationBuilder(final boolean enableNamespaces) {
//yaya the bugs with some compilers and final variables ..
try {
final SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
if (enableNamespaces) {
saxParserFactory.setNamespaceAware(true);
}
final SAXParser saxParser = saxParserFactory.newSAXParser();
this.setParser(saxParser.getXMLReader());
} catch (final Exception se) {
throw new Error("Unable to setup SAX parser" + se);
}
}
/**
* Create a Configuration Builder with your own XMLReader.
*
* @param parser an XMLReader
*/
public DefaultConfigurationBuilder(XMLReader parser) {
this.setParser(parser);
}
/** Internally sets up the XMLReader */
private void setParser(XMLReader parser) {
m_parser = parser;
m_handler = getHandler();
m_parser.setContentHandler(m_handler);
m_parser.setErrorHandler(m_handler);
}
/**
* Get a SAXConfigurationHandler for your configuration reading.
*
* @return a SAXConfigurationHandler
*/
protected SAXConfigurationHandler getHandler() {
try {
if (m_parser.getFeature("http://xml.org/sax/features/namespaces")) {
return new NamespacedSAXConfigurationHandler();
}
} catch (Exception e) {
// ignore error and fall through to the non-namespaced version
}
return new SAXConfigurationHandler();
}
/**
* Build a configuration object from a file using a filename.
*
* @param filename name of the file
* @return a Configuration
object
* @throws SAXException if a parsing error occurs
* @throws IOException if an I/O error occurs
* @throws ConfigurationException if an error occurs
*/
public Configuration buildFromFile(final String filename) throws SAXException, IOException, ConfigurationException {
return buildFromFile(new File(filename));
}
/**
* Build a configuration object from a file using a File object.
*
* @param file a File
object
* @return a Configuration
object
* @throws SAXException if a parsing error occurs
* @throws IOException if an I/O error occurs
* @throws ConfigurationException if an error occurs
*/
public Configuration buildFromFile(final File file) throws SAXException, IOException, ConfigurationException {
return buildFromURL(file.toURI().toURL());
}
/**
* Build a configuration object from a file using a File object.
*
* @param file a File
object
* @return a Configuration
object
* @throws SAXException if a parsing error occurs
* @throws IOException if an I/O error occurs
* @throws ConfigurationException if an error occurs
*/
public Configuration buildFromURL(final URL url) throws SAXException, IOException, ConfigurationException {
synchronized (this) {
m_handler.clear();
m_parser.parse(url.toString());
return m_handler.getConfiguration();
}
}
/**
* Build a configuration object using an InputStream.
*
* @param inputStream an InputStream
value
* @return a Configuration
object
* @throws SAXException if a parsing error occurs
* @throws IOException if an I/O error occurs
* @throws ConfigurationException if an error occurs
*/
public Configuration build(final InputStream inputStream) throws SAXException, IOException, ConfigurationException {
return build(new InputSource(inputStream));
}
/**
* Build a configuration object using an URI
*
* @param uri a String
value
* @return a Configuration
object
* @throws SAXException if a parsing error occurs
* @throws IOException if an I/O error occurs
* @throws ConfigurationException if an error occurs
*/
public Configuration build(final String uri) throws SAXException, IOException, ConfigurationException {
return build(new InputSource(uri));
}
/**
* Build a configuration object using an XML InputSource object
*
* @param input an InputSource
value
* @return a Configuration
object
* @throws SAXException if a parsing error occurs
* @throws IOException if an I/O error occurs
* @throws ConfigurationException if an error occurs
*/
public Configuration build(final InputSource input) throws SAXException, IOException, ConfigurationException {
synchronized (this) {
m_handler.clear();
m_parser.parse(input);
return m_handler.getConfiguration();
}
}
}