-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathBeanXmlReader.java
More file actions
87 lines (75 loc) · 3.64 KB
/
Copy pathBeanXmlReader.java
File metadata and controls
87 lines (75 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package arhangel.dim.container;
/**
* Created by nexx0f on 15.03.16.
*/
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.xml.sax.SAXException;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
*
*/
public class BeanXmlReader {
private static final String TAG_BEAN = "bean";
private static final String TAG_PROPERTY = "property";
private static final String ATTR_NAME = "name";
private static final String ATTR_VALUE = "val";
private static final String ATTR_REF = "ref";
private static final String ATTR_BEAN_ID = "id";
private static final String ATTR_BEAN_CLASS = "class";
public List<Bean> parseBeans(String pathToFile) throws InvalidConfigurationException, ParserConfigurationException, IOException, SAXException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(false);
factory.setSchema(null);
factory.setIgnoringElementContentWhitespace(true);
List<Bean> beanList = new ArrayList<>();
DocumentBuilder builder = factory.newDocumentBuilder(); //throws ParserConfigurationException
File file = new File(pathToFile);
Document doc = builder.parse(file); //throws IOException
Element root = doc.getDocumentElement();
NodeList beanNodes = root.getElementsByTagName(TAG_BEAN);
for (int nodeIndex = 0; nodeIndex < beanNodes.getLength(); nodeIndex++) {
Node beanNode = beanNodes.item(nodeIndex);
if (beanNode.getNodeType() == Node.ELEMENT_NODE) {
Element beanElement = (Element) beanNode;
String beanId = beanElement.getAttribute(ATTR_BEAN_ID);
String beanClass = beanElement.getAttribute(ATTR_BEAN_CLASS);
NodeList beanPropertyNodes = beanElement.getElementsByTagName(TAG_PROPERTY);
Map<String, Property> properties = new HashMap<>();
for (int propertyIndex = 0; propertyIndex < beanPropertyNodes.getLength(); propertyIndex++) {
Node beanPropertyNode = beanPropertyNodes.item(propertyIndex);
if (beanPropertyNode.getNodeType() == Node.ELEMENT_NODE) {
Element beanPropertyElement = (Element) beanPropertyNode;
String propertyName = beanPropertyElement.getAttribute(ATTR_NAME);
Property property = null;
if (beanPropertyElement.hasAttribute(ATTR_VALUE)) {
String propertyValue = beanPropertyElement.getAttribute(ATTR_VALUE);
property = new Property(propertyName, propertyValue, ValueType.VAL);
} else if (beanPropertyElement.hasAttribute(ATTR_REF)) {
String propertyRef = beanPropertyElement.getAttribute(ATTR_REF);
property = new Property(propertyName, propertyRef, ValueType.REF);
}
properties.put(property.getName(), property);
}
Bean bean = new Bean(beanId, beanClass, properties);
beanList.add(bean);
}
}
}
return beanList;
}
}