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 static List<Element> loadElemList(
54 DocumentBuilder builder,
55 URL url,
56 String childName )
57 throws IOException, SAXException{
58 Element root = loadElement(builder, url);
59 List<Element> result = getChildElemList(root, childName);
60 return result;
61 }
62
63
64
65
66
67
68
69
70
71 private static Element loadElement(DocumentBuilder builder, URL url)
72 throws IOException, SAXException{
73 InputStream istream = url.openStream();
74
75 Document document;
76 try{
77 document = builder.parse(istream);
78 }finally{
79 istream.close();
80 }
81
82 Element root = document.getDocumentElement();
83
84 return root;
85 }
86
87
88
89
90
91
92
93 private static List<Element> getChildElemList(
94 Element root, String childName){
95 NodeList nodeList = root.getElementsByTagName(childName);
96 int childNum = nodeList.getLength();
97
98 List<Element> result = new ArrayList<>(childNum);
99
100 for(int index = 0; index < childNum; index++){
101 Node node = nodeList.item(index);
102 Element element = (Element) node;
103 result.add(element);
104 }
105
106 return result;
107 }
108
109
110
111
112
113
114
115
116 static String attrRequired(Element elem, String attrName)
117 throws SAXException{
118 Attr attr = elem.getAttributeNode(attrName);
119 if(attr == null){
120 String msg = MessageFormat.format(ERR_NOATTR, attrName);
121 throw new SAXException(msg);
122 }
123 String result = attr.getValue();
124 if(result == null){
125 String msg = MessageFormat.format(ERR_NOVAL, attrName);
126 throw new SAXException(msg);
127 }
128 return result;
129 }
130
131
132
133
134
135
136
137 static String attrValue(Element elem, String attrName){
138 Attr attr = elem.getAttributeNode(attrName);
139 if(attr == null) return null;
140
141 String result = attr.getValue();
142
143 return result;
144 }
145
146
147
148
149
150
151
152
153 static URI attrToUri(Element elem, String attrName)
154 throws SAXException{
155 Attr attr = elem.getAttributeNode(attrName);
156 if(attr == null) return null;
157
158 String uriText = attr.getValue();
159 if(uriText == null) return null;
160
161 URI uri;
162 try{
163 uri = new URI(uriText).normalize();
164 }catch(URISyntaxException e){
165 throw new SAXException("illegal URI " + uriText, e);
166 }
167
168 return uri;
169 }
170
171 }