KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > configuration > ConfigurationFactory


1 /*
2  * Copyright 2006-2007 The Scriptella Project Team.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package scriptella.configuration;
17
18 import org.w3c.dom.Document JavaDoc;
19 import org.xml.sax.EntityResolver JavaDoc;
20 import org.xml.sax.ErrorHandler JavaDoc;
21 import org.xml.sax.InputSource JavaDoc;
22 import org.xml.sax.SAXParseException JavaDoc;
23 import scriptella.core.ThreadSafe;
24
25 import javax.xml.parsers.DocumentBuilder JavaDoc;
26 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.net.URL JavaDoc;
29 import java.util.LinkedHashMap JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.logging.Logger JavaDoc;
32
33
34 /**
35  * TODO: Add documentation
36  *
37  * @author Fyodor Kupolov
38  * @version 1.0
39  */

40 public class ConfigurationFactory {
41     private static final Logger JavaDoc LOG = Logger.getLogger(ConfigurationFactory.class.getName());
42     private static final DocumentBuilderFactory JavaDoc DBF = DocumentBuilderFactory.newInstance();
43     private static final String JavaDoc DTD_NAME = "etl.dtd";
44     private URL JavaDoc resourceURL;
45     private Map JavaDoc<String JavaDoc, ?> externalProperties;
46
47     static {
48         setValidating(true);
49     }
50
51
52     /**
53      * Sets validation option.
54      *
55      * @param validating true if XML file validation should be performed.
56      */

57     public static void setValidating(boolean validating) {
58         DBF.setValidating(validating);
59     }
60
61     public ConfigurationFactory() {
62     }
63
64     public URL JavaDoc getResourceURL() {
65         return resourceURL;
66     }
67
68     public void setResourceURL(final URL JavaDoc resourceURL) {
69         this.resourceURL = resourceURL;
70     }
71
72     /**
73      * A getter for external Properties.
74      *
75      * @return external properties set by {@link #setExternalProperties}.
76      */

77     @ThreadSafe
78     public Map JavaDoc<String JavaDoc, ?> getExternalProperties() {
79         return externalProperties;
80     }
81
82     /**
83      * Sets additional properties.
84      * <p>External properties takes precedence over properties specified
85      * in ETL &lt;properties&gt; element.
86      * <p>Intended for integration with other systems like ant.
87      *
88      * @param externalProperties external properties. Nulls allowed.
89      */

90     @ThreadSafe
91     public void setExternalProperties(final Map JavaDoc<String JavaDoc, ?> externalProperties) {
92         if (externalProperties == null) {
93             this.externalProperties = null;
94         } else {
95             this.externalProperties = new LinkedHashMap JavaDoc<String JavaDoc, Object JavaDoc>(externalProperties);
96         }
97     }
98
99     public ConfigurationEl createConfiguration() {
100         if (resourceURL == null) {
101             throw new ConfigurationException("Configuration URL is required");
102         }
103         try {
104             DocumentBuilder JavaDoc db = DBF.newDocumentBuilder();
105             db.setEntityResolver(new EntityResolver JavaDoc() {
106                 public InputSource JavaDoc resolveEntity(final String JavaDoc publicId,
107                                                  final String JavaDoc systemId) {
108                     if (systemId != null && systemId.trim().endsWith(DTD_NAME)) {
109                         return new InputSource JavaDoc(ConfigurationFactory.class.getResourceAsStream(
110                                 "/scriptella/dtd/" + DTD_NAME));
111                     }
112
113                     return null;
114                 }
115             });
116             db.setErrorHandler(new ErrorHandler JavaDoc() {
117                 public void warning(final SAXParseException JavaDoc exception) {
118                     LOG.warning(messageFor(exception));
119                 }
120
121                 public void error(final SAXParseException JavaDoc exception) {
122                     LOG.warning(messageFor(exception));
123                 }
124
125                 private String JavaDoc messageFor(final SAXParseException JavaDoc exception) {
126                     StringBuilder JavaDoc sb = new StringBuilder JavaDoc(32);
127                     sb.append("XML configuration warning in ");
128
129                     final String JavaDoc sid = exception.getSystemId();
130
131                     if (sid != null) {
132                         sb.append(sid);
133                     } else {
134                         sb.append("the document");
135                     }
136
137                     sb.append('(');
138                     sb.append(exception.getLineNumber());
139                     sb.append(':');
140                     sb.append(exception.getColumnNumber());
141                     sb.append("): ");
142                     sb.append(exception.getMessage());
143
144                     return sb.toString();
145                 }
146
147                 public void fatalError(final SAXParseException JavaDoc exception) {
148                     LOG.severe(messageFor(exception));
149                 }
150             });
151
152             final InputSource JavaDoc inputSource = new InputSource JavaDoc(resourceURL.toString());
153             final Document JavaDoc document = db.parse(inputSource);
154             PropertiesMerger merger = externalProperties == null ?
155                     new PropertiesMerger() : new PropertiesMerger(externalProperties);
156
157             return new ConfigurationEl(new XmlElement(
158                     document.getDocumentElement(), resourceURL, merger.getSubstitutor()), merger);
159         } catch (IOException JavaDoc e) {
160             throw new ConfigurationException("Unable to load document: " + e.getMessage(), e);
161         } catch (Exception JavaDoc e) {
162             throw new ConfigurationException("Unable to parse document: " + e.getMessage(), e);
163         }
164     }
165 }
166
Popular Tags