KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > configuration > ConfigurationEl


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 scriptella.spi.ParametersCallback;
19
20 import java.net.URL JavaDoc;
21 import java.util.HashSet JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.Set JavaDoc;
25
26
27 /**
28  * TODO: Add documentation
29  *
30  * @author Fyodor Kupolov
31  * @version 1.0
32  */

33 public class ConfigurationEl extends XmlConfigurableBase {
34     private List JavaDoc<ConnectionEl> connections;
35     private List JavaDoc<ScriptingElement> scriptingElements;
36     private PropertiesMerger propertiesMerger;
37     private URL JavaDoc documentUrl;
38
39     public ConfigurationEl(XmlElement element, PropertiesMerger merger) {
40         propertiesMerger = merger;
41         configure(element);
42     }
43
44     public List JavaDoc<ConnectionEl> getConnections() {
45         return connections;
46     }
47
48     public void setConnections(final List JavaDoc<ConnectionEl> connections) {
49         this.connections = connections;
50     }
51
52     public List JavaDoc<ScriptingElement> getScriptingElements() {
53         return scriptingElements;
54     }
55
56     public void setScriptingElements(final List JavaDoc<ScriptingElement> scriptingElements) {
57         this.scriptingElements = scriptingElements;
58     }
59
60     /**
61      * Returns this configuration properties merged with external ones specified in a factory.
62      */

63     public ParametersCallback getProperties() {
64         return propertiesMerger;
65     }
66
67     public URL JavaDoc getDocumentUrl() {
68         return documentUrl;
69     }
70
71     public void setDocumentUrl(final URL JavaDoc documentUrl) {
72         this.documentUrl = documentUrl;
73     }
74
75     public void configure(final XmlElement element) {
76         documentUrl = element.getDocumentUrl();
77
78         Map JavaDoc<String JavaDoc,?> xmlProps = new PropertiesEl(element.getChild("properties")).getMap();
79         //Now merge external and local xml properties
80
propertiesMerger.addProperties(xmlProps);
81
82         setConnections(load(element.getChildren("connection"),
83                 ConnectionEl.class));
84         if (connections.isEmpty()) {
85             throw new ConfigurationException("At least one connection element must be declared", element);
86         }
87         scriptingElements = QueryEl.loadScriptingElements(element, null);
88         validateScriptingElements(element);
89     }
90
91     void validateScriptingElements(final XmlElement element) {
92         //validating scriptingElements
93
Set JavaDoc<String JavaDoc> allowedConIds = new HashSet JavaDoc<String JavaDoc>();
94         for (ConnectionEl connectionEl : connections) {
95             final String JavaDoc cid = connectionEl.getId();
96             if (!allowedConIds.add(cid)) {
97                 throw new ConfigurationException("Connection ID must be unique for ETL file", element);
98             }
99             if (cid==null && connections.size()>1) {
100                 throw new ConfigurationException("Connection ID is required if more than one connection specified in ETL script.", element);
101             }
102         }
103
104         validateScriptingElements(allowedConIds, element, scriptingElements);
105     }
106
107     void validateScriptingElements(final Set JavaDoc<String JavaDoc> allowedConIds, final XmlElement element, final List JavaDoc<ScriptingElement> elements) {
108         for (ScriptingElement se : elements) {
109             //If one connection check
110
final int allowedConSize = allowedConIds.size();
111             final String JavaDoc seConnectionId = se.getConnectionId();
112             if (allowedConSize == 1 && seConnectionId != null &&
113                     !allowedConIds.contains(seConnectionId)) {
114                 throw new ConfigurationException("Element " + se.getLocation() + " has invalid connection-id");
115             } else if (allowedConSize > 1 && seConnectionId == null && scriptingElements==elements) {
116                 //Nulls are allowed only in nested scripts and queries
117
throw new ConfigurationException("connection-id is a required attribute for element " + se.getLocation());
118             } else
119             if (allowedConSize > 1 && seConnectionId != null && !allowedConIds.contains(seConnectionId))
120             {
121                 throw new ConfigurationException("Element " + se.getLocation() + " has invalid connection-id");
122             }
123             if (se instanceof QueryEl) {
124                 validateScriptingElements(allowedConIds, element, ((QueryEl) se).getChildScriptinglElements());
125             }
126         }
127     }
128
129     public String JavaDoc toString() {
130         return "ConfigurationEl{" + "connections=" + connections +
131                 ", scriptingElements=" + scriptingElements + ", properties=" + propertiesMerger +
132                 ", documentUrl=" + documentUrl + "}";
133     }
134 }
135
Popular Tags