XMLEnvironmentConfigurationParser(String)   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
c 0
b 0
f 0
cc 2
rs 9.232
1
package it.cnr.istc.pst.cognition.koala.reasoner.environment.parser.xml;
2
3
import java.io.FileInputStream;
4
import java.util.ArrayList;
5
import java.util.HashMap;
6
import java.util.List;
7
import java.util.Map;
8
9
import javax.xml.parsers.DocumentBuilder;
10
import javax.xml.parsers.DocumentBuilderFactory;
11
import javax.xml.xpath.XPath;
12
import javax.xml.xpath.XPathConstants;
13
import javax.xml.xpath.XPathExpression;
14
import javax.xml.xpath.XPathFactory;
15
16
import org.w3c.dom.Attr;
17
import org.w3c.dom.Document;
18
import org.w3c.dom.Node;
19
import org.w3c.dom.NodeList;
20
21
import it.cnr.istc.pst.cognition.koala.reasoner.environment.parser.EnvironmentConfigurationParser;
22
23
/**
24
 * 
25
 * @author alessandroumbrico
26
 *
27
 */
28
public class XMLEnvironmentConfigurationParser implements EnvironmentConfigurationParser
29
{
30
	private Document document;
31
	private Map<String, Element> eCache;				// element cache
32
	private Map<String, Sensor> sCache;					// sensor cache
33
	
34
	/**
35
	 * 
36
	 * @param envConfigFilePath
37
	 */
38
	public XMLEnvironmentConfigurationParser(String envConfigFilePath) 
39
	{
40
		// setup cache
41
		this.eCache = new HashMap<String, Element>();
42
		this.sCache = new HashMap<String, Sensor>();
43
		
44
		try
45
		{
46
			// parse the document file
47
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
48
			DocumentBuilder builder = factory.newDocumentBuilder();
49
			this.document = builder.parse(new FileInputStream(envConfigFilePath));
50
			
51
			
52
			// load elements
53
			this.loadElements();
54
			
55
			// set element composition
56
			this.setElementComposition();
57
			
58
			
59
			// load sensors
60
			this.loadSensors();
61
			
62
		}
63
		catch (Exception ex) {
64
			throw new RuntimeException(ex.getMessage());
0 ignored issues
show
Best Practice introduced by
Dedicated exceptions should be preferred over throwing the generic Exception.
Loading history...
65
		}
66
	}
67
	
68
	/**
69
	 * 
70
	 * @return
71
	 */
72
	public List<Element> getElements() {
73
		return new ArrayList<Element>(this.eCache.values());
74
	}
75
	
76
	/**
77
	 * 
78
	 * @return
79
	 */
80
	public List<Sensor> getSensors() {
81
		return new ArrayList<Sensor>(this.sCache.values());
82
	}
83
	
84
	
85
	/**
86
	 * 
87
	 * @return
88
	 * @throws Exception
89
	 */
90
	private void loadSensors() 
91
			throws Exception
0 ignored issues
show
Best Practice introduced by
Dedicated exceptions should be preferred over throwing the generic Exception.
Loading history...
92
	{
93
		// prepare XPath expression
94
		XPathFactory xpf = XPathFactory.newInstance();
95
		XPath xp = xpf.newXPath();
96
		
97
		// create XPATH expression
98
		XPathExpression expression = xp.compile("//sensor");
99
		NodeList nodes = (NodeList) expression.evaluate(this.document, XPathConstants.NODESET);
100
		// iterate over results
101
		for (int index = 0; index < nodes.getLength(); index++) 
102
		{
103
			// get node
104
			Node node = nodes.item(index);
105
			// get id attribute
106
			Attr attrId = (Attr) node.getAttributes().getNamedItem("id");
107
			Attr attrName = (Attr) node.getAttributes().getNamedItem("name");
108
			Attr attrType = (Attr) node.getAttributes().getNamedItem("type");
109
			Attr attrState = (Attr) node.getAttributes().getNamedItem("state");
110
			Attr attrPlacement = (Attr) node.getAttributes().getNamedItem("placement");
111
			Attr attrTarget = (Attr) node.getAttributes().getNamedItem("target");
112
			
113
			// check sensor state
114
			SensorState state = SensorState.getStateByCode(attrState.getValue());
115
			
116
			// retrieve owner from cache
117
			Element owner = this.eCache.get(attrPlacement.getValue());
118
			// retrieve target from cache
119
			Element target = this.eCache.get(attrTarget.getValue());
120
			
121
			// create sensor element
122
			Sensor sensor = new Sensor(
123
					attrId.getValue(),
124
					attrName.getValue(),
125
					attrType.getValue(),
126
					state,
127
					owner,
128
					target);
129
			
130
			
131
			// add cache entry
132
			this.sCache.put(sensor.getName(), sensor);
133
		}
134
	}
135
136
	
137
	
138
	/**
139
	 * 
140
	 * @return
141
	 * @throws Exception
142
	 */
143 View Code Duplication
	private void loadElements() 
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
144
			throws Exception
0 ignored issues
show
Best Practice introduced by
Dedicated exceptions should be preferred over throwing the generic Exception.
Loading history...
145
	{
146
		XPathFactory xpf = XPathFactory.newInstance();
147
		XPath xp = xpf.newXPath();
148
		
149
		// create XPATH expression
150
		XPathExpression expression = xp.compile("//element");
151
		
152
		NodeList nodes = (NodeList) expression.evaluate(this.document, XPathConstants.NODESET);
153
		// iterate over results
154
		for (int index = 0; index < nodes.getLength(); index++)
155
		{
156
			// get node
157
			Node node = nodes.item(index);
158
			// get id attribute
159
			Attr attrId = (Attr) node.getAttributes().getNamedItem("id");
160
			Attr attrName = (Attr) node.getAttributes().getNamedItem("name");
161
			Attr attrType = (Attr) node.getAttributes().getNamedItem("type");
162
			
163
			// create element
164
			Element element = new Element(
165
					attrId.getValue(), 
166
					attrName.getValue(), 
167
					attrType.getValue());
168
			
169
			// add cache entry
170
			this.eCache.put(element.getName(), element);
171
		}
172
	}
173
	
174
	/**
175
	 * 
176
	 * @throws Exception
177
	 */
178 View Code Duplication
	private void setElementComposition() 
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
179
			throws Exception
0 ignored issues
show
Best Practice introduced by
Dedicated exceptions should be preferred over throwing the generic Exception.
Loading history...
180
	{
181
		XPathFactory xpf = XPathFactory.newInstance();
182
		XPath xp = xpf.newXPath();
183
		
184
		// create XPATH expression
185
		XPathExpression expression = xp.compile("//element");
186
		
187
		NodeList nodes = (NodeList) expression.evaluate(this.document, XPathConstants.NODESET);
188
		// iterate over results
189
		for (int index = 0; index < nodes.getLength(); index++)
190
		{
191
			// get node
192
			Node node = nodes.item(index);
193
			// get id attribute
194
			Attr attrName = (Attr) node.getAttributes().getNamedItem("name");
195
			Attr partOf = (Attr) node.getAttributes().getNamedItem("partOf");
196
197
			// check if part of is set
198
			if (partOf != null) {
199
				
200
				// get element
201
				Element element = this.eCache.get(attrName.getValue());
202
				// get "parent" element
203
				Element parent = this.eCache.get(partOf.getValue());
204
				
205
				// set element as part of parent
206
				element.setPartOf(parent);
207
				
208
			}
209
		}
210
	}
211
	
212
	
213
	/**
214
	 * 
215
	 * @param args
216
	 */
217
	public static void main(String[] args)
218
	{
219
		try
220
		{
221
			XMLEnvironmentConfigurationParser parser = new XMLEnvironmentConfigurationParser("etc/environment/house_config.xml");
222
			List<Sensor> sensors = parser.getSensors();
223
			for (Sensor sensor : sensors) {
224
				System.out.println(sensor);
225
			}
226
			
227
			List<Element> elements = parser.getElements();
228
			for (Element element : elements) {
229
				System.out.println(element);
230
			}			
231
		}
232
		catch (Exception ex) {
233
			System.err.println(ex.getMessage());
234
		}
235
	}
236
}
237