001: /*
002: * Copyright 2004-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.compass.core.config.builder;
018:
019: import java.io.IOException;
020: import java.io.InputStream;
021: import javax.xml.parsers.DocumentBuilder;
022: import javax.xml.parsers.DocumentBuilderFactory;
023: import javax.xml.parsers.ParserConfigurationException;
024:
025: import org.apache.commons.logging.Log;
026: import org.compass.core.config.CompassConfiguration;
027: import org.compass.core.config.ConfigurationException;
028: import org.w3c.dom.Document;
029: import org.xml.sax.*;
030:
031: /**
032: * @author kimchy
033: */
034: public abstract class AbstractXmlConfigurationBuilder extends
035: AbstractInputStreamConfigurationBuilder {
036:
037: private class SimpleSaxErrorHandler implements ErrorHandler {
038:
039: private final Log log;
040:
041: /**
042: * Create a new SimpleSaxErrorHandler for the given
043: * Commons Logging logger instance.
044: */
045: public SimpleSaxErrorHandler(Log log) {
046: this .log = log;
047: }
048:
049: public void warning(SAXParseException ex) throws SAXException {
050: log.warn("Ignored XML validation warning ["
051: + ex.getMessage() + "]", ex);
052: }
053:
054: public void error(SAXParseException ex) throws SAXException {
055: throw ex;
056: }
057:
058: public void fatalError(SAXParseException ex)
059: throws SAXException {
060: throw ex;
061: }
062:
063: }
064:
065: protected void doConfigure(InputStream is, String resourceName,
066: CompassConfiguration config) throws ConfigurationException {
067: InputSource inputSource = new InputSource(is);
068: try {
069: DocumentBuilderFactory factory = createDocumentBuilderFactory();
070: DocumentBuilder builder = createDocumentBuilder(factory);
071: Document doc = builder.parse(inputSource);
072: doProcess(doc, config);
073: } catch (ParserConfigurationException ex) {
074: throw new ConfigurationException(
075: "Parser configuration exception parsing XML from ["
076: + resourceName + "]", ex);
077: } catch (SAXParseException ex) {
078: throw new ConfigurationException("Line ["
079: + ex.getLineNumber() + "] in XML document from ["
080: + resourceName + "] is invalid", ex);
081: } catch (SAXException ex) {
082: throw new ConfigurationException("XML document from ["
083: + resourceName + "] is invalid", ex);
084: } catch (IOException ex) {
085: throw new ConfigurationException(
086: "IOException parsing XML document from ["
087: + resourceName + "]", ex);
088: }
089:
090: }
091:
092: protected abstract void doProcess(Document doc,
093: CompassConfiguration config) throws ConfigurationException;
094:
095: protected DocumentBuilderFactory createDocumentBuilderFactory()
096: throws ParserConfigurationException {
097: DocumentBuilderFactory factory = DocumentBuilderFactory
098: .newInstance();
099: factory.setValidating(true);
100: return factory;
101: }
102:
103: protected DocumentBuilder createDocumentBuilder(
104: DocumentBuilderFactory factory)
105: throws ParserConfigurationException {
106: DocumentBuilder docBuilder = factory.newDocumentBuilder();
107: docBuilder.setErrorHandler(doGetErrorHandler());
108: docBuilder.setEntityResolver(doGetEntityResolver());
109: return docBuilder;
110: }
111:
112: protected ErrorHandler doGetErrorHandler() {
113: return new SimpleSaxErrorHandler(log);
114: }
115:
116: protected abstract EntityResolver doGetEntityResolver();
117: }
|