|
1 |
| package org.jconfig.parser; |
|
2 |
| |
|
3 |
| import java.io.InputStream; |
|
4 |
| import java.util.Enumeration; |
|
5 |
| import java.util.Properties; |
|
6 |
| |
|
7 |
| import org.jconfig.Configuration; |
|
8 |
| import org.jconfig.VariableManager; |
|
9 |
| import org.jconfig.error.ErrorReporter; |
|
10 |
| import org.jconfig.utils.*; |
|
11 |
| import org.w3c.dom.Document; |
|
12 |
| import org.w3c.dom.Element; |
|
13 |
| import org.w3c.dom.NamedNodeMap; |
|
14 |
| import org.w3c.dom.Node; |
|
15 |
| import org.w3c.dom.NodeList; |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| public abstract class AbstractConfigParser implements ConfigurationParser { |
|
21 |
| |
|
22 |
| private static ExtensionGraph eg = new ExtensionGraph(); |
|
23 |
| |
|
24 |
39
| public AbstractConfigParser() {
|
|
25 |
| } |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
37
| protected void getVariables(Document doc,Configuration config) {
|
|
34 |
37
| NodeList nl = doc.getElementsByTagName("variables");
|
|
35 |
37
| for (int i = 0; i < nl.getLength(); i++) {
|
|
36 |
20
| Node n = nl.item(i);
|
|
37 |
20
| for (Node child = n.getFirstChild(); child != null; child = child.getNextSibling()) {
|
|
38 |
| |
|
39 |
100
| if (child.getNodeName().equals("variable")) {
|
|
40 |
40
| NamedNodeMap myAtt = child.getAttributes();
|
|
41 |
40
| Node myNode = myAtt.getNamedItem("name");
|
|
42 |
40
| String name = myNode.getNodeValue();
|
|
43 |
40
| myNode = myAtt.getNamedItem("value");
|
|
44 |
40
| String value = myNode.getNodeValue();
|
|
45 |
40
| if (name != null && value != null) {
|
|
46 |
40
| config.setVariable(name, value);
|
|
47 |
| } |
|
48 |
| } |
|
49 |
| } |
|
50 |
| } |
|
51 |
| } |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
37
| protected void getBaseConfigName(Document doc,Configuration config) {
|
|
59 |
37
| Element nl = doc.getDocumentElement();
|
|
60 |
37
| NamedNodeMap myAtt = nl.getAttributes();
|
|
61 |
37
| Node myNode = myAtt.getNamedItem("extends");
|
|
62 |
37
| if ( myNode != null ) {
|
|
63 |
17
| String name = myNode.getNodeValue();
|
|
64 |
17
| eg.addExtension(config.getConfigName(),name);
|
|
65 |
17
| if ( !eg.checkDependencies(config.getConfigName())) {
|
|
66 |
16
| config.setBaseConfiguration(name);
|
|
67 |
| } |
|
68 |
| else { |
|
69 |
1
| ErrorReporter.getErrorHandler().reportError("The base configuration is invalid since there will be a circular dependency. Found dependency:"+config.getConfigName()+"/"+name);
|
|
70 |
| } |
|
71 |
| } |
|
72 |
| } |
|
73 |
| |
|
74 |
37
| protected void getIncludeProperties(Document doc,Configuration config) {
|
|
75 |
37
| NodeList nl = doc.getElementsByTagName("include");
|
|
76 |
37
| for (int i = 0; i < nl.getLength(); i++) {
|
|
77 |
30
| Node n = nl.item(i);
|
|
78 |
30
| NamedNodeMap myAtt = n.getAttributes();
|
|
79 |
30
| Node myNode = myAtt.getNamedItem("properties");
|
|
80 |
30
| if ( myNode != null ) {
|
|
81 |
30
| loadProperties(myNode.getNodeValue(),config.getConfigName());
|
|
82 |
30
| config.addInclude(IncludeEntry.PROPERTIES, myNode.getNodeValue());
|
|
83 |
| } |
|
84 |
| } |
|
85 |
| } |
|
86 |
| |
|
87 |
30
| private void loadProperties(String propName,String configName) {
|
|
88 |
30
| try {
|
|
89 |
30
| InputStream inputStream = null;
|
|
90 |
30
| ResourceLocator locator = new ResourceLocator(propName);
|
|
91 |
30
| inputStream = locator.getInputStream();
|
|
92 |
30
| if ( inputStream == null ) {
|
|
93 |
0
| ErrorReporter.getErrorHandler().reportError("Cannot find the properties file");
|
|
94 |
| } |
|
95 |
| else { |
|
96 |
30
| VariableManager varman = VariableManager.getInstance();
|
|
97 |
30
| Properties myProperties = new Properties();
|
|
98 |
30
| myProperties.load(inputStream);
|
|
99 |
30
| Enumeration propertyNames = myProperties.propertyNames();
|
|
100 |
30
| while (propertyNames.hasMoreElements()) {
|
|
101 |
60
| String name = (String) propertyNames.nextElement();
|
|
102 |
60
| String value = myProperties.getProperty(name);
|
|
103 |
60
| varman.addIncludedVariable(name,value, configName);
|
|
104 |
| } |
|
105 |
| } |
|
106 |
| } catch (Exception e) { |
|
107 |
0
| ErrorReporter.getErrorHandler().reportError("The file cannot be loaded",e);
|
|
108 |
| } |
|
109 |
| } |
|
110 |
| |
|
111 |
| public abstract Configuration parse(Document doc,String configName); |
|
112 |
| } |