001: /*
002: * Copyright (c) 2001-2007, Jean Tessier
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * * Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * * Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in the
014: * documentation and/or other materials provided with the distribution.
015: *
016: * * Neither the name of Jean Tessier nor the names of his contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032:
033: package com.jeantessier.metrics;
034:
035: import java.io.*;
036: import javax.xml.parsers.*;
037:
038: import org.apache.log4j.*;
039: import org.xml.sax.*;
040:
041: public class MetricsConfigurationLoader {
042: private static final boolean DEFAULT_VALIDATE = false;
043:
044: private MetricsConfigurationHandler handler;
045: private boolean validate;
046:
047: public MetricsConfigurationLoader() {
048: this (new MetricsConfiguration(), DEFAULT_VALIDATE);
049: }
050:
051: public MetricsConfigurationLoader(MetricsConfiguration configuration) {
052: this (configuration, DEFAULT_VALIDATE);
053: }
054:
055: public MetricsConfigurationLoader(boolean validate) {
056: this (new MetricsConfiguration(), validate);
057: }
058:
059: public MetricsConfigurationLoader(
060: MetricsConfiguration configuration, boolean validate) {
061: this .handler = new MetricsConfigurationHandler(configuration);
062: this .validate = validate;
063: }
064:
065: public MetricsConfiguration load(String filename)
066: throws IOException, SAXException,
067: ParserConfigurationException {
068: MetricsConfiguration result = null;
069:
070: FileReader in = null;
071:
072: try {
073: in = new FileReader(filename);
074: result = load(in);
075: } finally {
076: if (in != null) {
077: in.close();
078: }
079: }
080:
081: return result;
082: }
083:
084: public MetricsConfiguration load(InputStream in)
085: throws IOException, SAXException,
086: ParserConfigurationException {
087: return load(new InputSource(in));
088: }
089:
090: public MetricsConfiguration load(Reader in) throws IOException,
091: SAXException, ParserConfigurationException {
092: return load(new InputSource(in));
093: }
094:
095: public MetricsConfiguration load(InputSource in)
096: throws IOException, SAXException,
097: ParserConfigurationException {
098: XMLReader reader = SAXParserFactory.newInstance()
099: .newSAXParser().getXMLReader();
100: reader.setDTDHandler(handler);
101: reader.setContentHandler(handler);
102: reader.setErrorHandler(handler);
103:
104: try {
105: if (validate) {
106: Logger.getLogger(getClass()).warn(
107: "XML validation turned on");
108: reader.setFeature(
109: "http://xml.org/sax/features/validation", true);
110: reader
111: .setFeature(
112: "http://apache.org/xml/features/nonvalidating/load-external-dtd",
113: true);
114: } else {
115: Logger.getLogger(getClass()).info(
116: "XML validation turned off");
117: reader
118: .setFeature(
119: "http://xml.org/sax/features/validation",
120: false);
121: reader
122: .setFeature(
123: "http://apache.org/xml/features/nonvalidating/load-external-dtd",
124: false);
125: }
126: } catch (Exception ex) {
127: Logger.getLogger(getClass()).warn(
128: "Problem setting validation feature on XML reader",
129: ex);
130: }
131:
132: reader.parse(in);
133:
134: return handler.getMetricsConfiguration();
135: }
136: }
|