KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jeantessier > metrics > MetricsConfigurationLoader


1 /*
2  * Copyright (c) 2001-2005, Jean Tessier
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Jean Tessier nor the names of his contributors
17  * may be used to endorse or promote products derived from this software
18  * without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */

32
33 package com.jeantessier.metrics;
34
35 import java.io.*;
36 import java.net.*;
37 import java.util.*;
38
39 import org.apache.log4j.*;
40
41 import org.xml.sax.*;
42 import org.xml.sax.helpers.*;
43
44 public class MetricsConfigurationLoader {
45     private static final String JavaDoc DEFAULT_READER_CLASSNAME = "org.apache.xerces.parsers.SAXParser";
46     private static final boolean DEFAULT_VALIDATE = false;
47
48     private MetricsConfigurationHandler handler;
49     private String JavaDoc readerClassname;
50     private boolean validate;
51
52     public MetricsConfigurationLoader() {
53         this(new MetricsConfiguration(), DEFAULT_READER_CLASSNAME, DEFAULT_VALIDATE);
54     }
55
56     public MetricsConfigurationLoader(MetricsConfiguration configuration) {
57         this(configuration, DEFAULT_READER_CLASSNAME, DEFAULT_VALIDATE);
58     }
59
60     public MetricsConfigurationLoader(String JavaDoc readerClassname) {
61         this(new MetricsConfiguration(), readerClassname, DEFAULT_VALIDATE);
62     }
63
64     public MetricsConfigurationLoader(boolean validate) {
65         this(new MetricsConfiguration(), DEFAULT_READER_CLASSNAME, validate);
66     }
67
68     public MetricsConfigurationLoader(MetricsConfiguration configuration, String JavaDoc readerClassname) {
69         this(configuration, readerClassname, DEFAULT_VALIDATE);
70     }
71
72     public MetricsConfigurationLoader(MetricsConfiguration configuration, boolean validate) {
73         this(configuration, DEFAULT_READER_CLASSNAME, validate);
74     }
75
76     public MetricsConfigurationLoader(String JavaDoc readerClassname, boolean validate) {
77         this(new MetricsConfiguration(), readerClassname, validate);
78     }
79     
80     public MetricsConfigurationLoader(MetricsConfiguration configuration, String JavaDoc readerClassname, boolean validate) {
81         this.handler = new MetricsConfigurationHandler(configuration);
82         this.readerClassname = readerClassname;
83         this.validate = validate;
84     }
85
86     public MetricsConfiguration load(String JavaDoc filename) throws IOException, SAXException {
87         MetricsConfiguration result = null;
88
89         FileReader in = null;
90
91         try {
92             in = new FileReader(filename);
93             result = load(in);
94         } finally {
95             if (in != null) {
96                 in.close();
97             }
98         }
99
100         return result;
101     }
102
103     public MetricsConfiguration load(InputStream in) throws IOException, SAXException {
104         return load(new InputSource(in));
105     }
106
107     public MetricsConfiguration load(Reader in) throws IOException, SAXException {
108         return load(new InputSource(in));
109     }
110
111     public MetricsConfiguration load(InputSource in) throws IOException, SAXException {
112         XMLReader reader = XMLReaderFactory.createXMLReader(readerClassname);
113         reader.setDTDHandler(handler);
114         reader.setContentHandler(handler);
115         reader.setErrorHandler(handler);
116
117         try {
118             if (validate) {
119                 Logger.getLogger(getClass()).warn("XML validation turned on");
120                 reader.setFeature("http://xml.org/sax/features/validation", true);
121                 reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", true);
122             } else {
123                 Logger.getLogger(getClass()).info("XML validation turned off");
124                 reader.setFeature("http://xml.org/sax/features/validation", false);
125                 reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
126             }
127         } catch (Exception JavaDoc ex) {
128             Logger.getLogger(getClass()).warn("Problem setting validation feature on XML reader",ex);
129         }
130     
131         reader.parse(in);
132     
133         return handler.getMetricsConfiguration();
134     }
135 }
136
Popular Tags