1
2
3
4
5
6
7
8 package jp.sourceforge.jindolf.corelib;
9
10 import java.io.IOException;
11 import java.io.InputStream;
12 import java.net.URI;
13 import java.net.URISyntaxException;
14 import java.net.URL;
15 import java.text.MessageFormat;
16 import java.util.ArrayList;
17 import java.util.List;
18 import javax.xml.parsers.DocumentBuilder;
19 import org.w3c.dom.Attr;
20 import org.w3c.dom.Document;
21 import org.w3c.dom.Element;
22 import org.w3c.dom.Node;
23 import org.w3c.dom.NodeList;
24 import org.xml.sax.SAXException;
25
26
27
28
29 final class DomUtils{
30
31 private static final String ERR_NOATTR = "no attribute[{0}]";
32 private static final String ERR_NOVAL = "no value[{0}]";
33
34
35
36
37
38 private DomUtils(){
39 assert false;
40 return;
41 }
42
43
44
45
46
47
48
49
50
51
52
53
54 static List<Element> loadElemList(
55 DocumentBuilder builder,
56 URL url,
57 String childName )
58 throws IOException, SAXException{
59 Element root = loadElement(builder, url);
60 List<Element> result = getChildElemList(root, childName);
61 return result;
62 }
63
64
65
66
67
68
69
70
71
72
73 private static Element loadElement(DocumentBuilder builder, URL url)
74 throws IOException, SAXException{
75 InputStream istream = url.openStream();
76
77 Document document;
78 try{
79 document = builder.parse(istream);
80 }finally{
81 istream.close();
82 }
83
84 Element root = document.getDocumentElement();
85
86 return root;
87 }
88
89
90
91
92
93
94
95
96 private static List<Element> getChildElemList(
97 Element root, String childName){
98 NodeList nodeList = root.getElementsByTagName(childName);
99 int childNum = nodeList.getLength();
100
101 List<Element> result = new ArrayList<>(childNum);
102
103 for(int index = 0; index < childNum; index++){
104 Node node = nodeList.item(index);
105 Element element = (Element) node;
106 result.add(element);
107 }
108
109 return result;
110 }
111
112
113
114
115
116
117
118
119
120 static String attrRequired(Element elem, String attrName)
121 throws SAXException{
122 Attr attr = elem.getAttributeNode(attrName);
123 if(attr == null){
124 String msg = MessageFormat.format(ERR_NOATTR, attrName);
125 throw new SAXException(msg);
126 }
127 String result = attr.getValue();
128 if(result == null){
129 String msg = MessageFormat.format(ERR_NOVAL, attrName);
130 throw new SAXException(msg);
131 }
132 return result;
133 }
134
135
136
137
138
139
140
141
142 static String attrValue(Element elem, String attrName){
143 Attr attr = elem.getAttributeNode(attrName);
144 if(attr == null) return null;
145
146 String result = attr.getValue();
147
148 return result;
149 }
150
151
152
153
154
155
156
157
158
159 static URI attrToUri(Element elem, String attrName)
160 throws SAXException{
161 Attr attr = elem.getAttributeNode(attrName);
162 if(attr == null) return null;
163
164 String uriText = attr.getValue();
165 if(uriText == null) return null;
166
167 URI uri;
168 try{
169 uri = new URI(uriText).normalize();
170 }catch(URISyntaxException e){
171 throw new SAXException("illegal URI " + uriText, e);
172 }
173
174 return uri;
175 }
176
177 }