4 * License : The MIT License
\r
5 * Copyright(c) 2011 olyutorskii
\r
8 package jp.sourceforge.jindolf.corelib;
\r
10 import java.io.IOException;
\r
11 import java.io.InputStream;
\r
12 import java.net.URI;
\r
13 import java.net.URISyntaxException;
\r
14 import java.net.URL;
\r
15 import java.util.ArrayList;
\r
16 import java.util.List;
\r
17 import javax.xml.parsers.DocumentBuilder;
\r
18 import org.w3c.dom.Attr;
\r
19 import org.w3c.dom.Document;
\r
20 import org.w3c.dom.Element;
\r
21 import org.w3c.dom.Node;
\r
22 import org.w3c.dom.NodeList;
\r
23 import org.xml.sax.SAXException;
\r
28 final class DomUtils{
\r
40 * DOM要素のリストをXMLリソースからロードする。
\r
41 * @param builder DOMビルダ
\r
43 * @param childName 要素名
\r
45 * @throws IOException 入力エラー
\r
46 * @throws SAXException XMLパースエラー
\r
48 static List<Element> loadElemList(
\r
49 DocumentBuilder builder,
\r
52 throws IOException, SAXException{
\r
53 Element root = loadElement(builder, url);
\r
54 List<Element> result = getChildElemList(root, childName);
\r
59 * root要素をXMLリソースからロードする。
\r
60 * @param builder DOMビルダ
\r
63 * @throws IOException 入力エラー
\r
64 * @throws SAXException XMLパースエラー
\r
66 private static Element loadElement(DocumentBuilder builder, URL url)
\r
67 throws IOException, SAXException{
\r
68 InputStream istream = url.openStream();
\r
72 document = builder.parse(istream);
\r
77 Element root = document.getDocumentElement();
\r
83 * root要素から子要素のリストを得る。
\r
84 * @param root root要素
\r
85 * @param childName 子要素のタグ名
\r
88 private static List<Element> getChildElemList(
\r
89 Element root, String childName){
\r
90 NodeList nodeList = root.getElementsByTagName(childName);
\r
91 int childNum = nodeList.getLength();
\r
93 List<Element> result = new ArrayList<Element>(childNum);
\r
95 for(int index = 0; index < childNum; index++){
\r
96 Node node = nodeList.item(index);
\r
97 Element element = (Element) node;
\r
98 result.add(element);
\r
106 * @param elem XML要素
\r
107 * @param attrName 属性名
\r
109 * @throws SAXException 必須属性が無かった。
\r
111 static String attrRequired(Element elem, String attrName)
\r
112 throws SAXException{
\r
113 Attr attr = elem.getAttributeNode(attrName);
\r
115 throw new SAXException("no attribute[" + attrName + "]");
\r
117 String result = attr.getValue();
\r
118 if(result == null){
\r
119 throw new SAXException("no value[" + attrName + "]");
\r
126 * @param elem XML要素
\r
127 * @param attrName 属性名
\r
128 * @return 属性値。なければnull
\r
130 static String attrValue(Element elem, String attrName){
\r
131 Attr attr = elem.getAttributeNode(attrName);
\r
132 if(attr == null) return null;
\r
134 String result = attr.getValue();
\r
140 * XML属性値からURIを展開する。
\r
141 * @param elem XML国定義要素
\r
142 * @param attrName 属性名
\r
144 * @throws SAXException 属性が未定義もしくはURI形式を満たさない。
\r
146 static URI attrToUri(Element elem, String attrName)
\r
147 throws SAXException{
\r
148 Attr attr = elem.getAttributeNode(attrName);
\r
149 if(attr == null) return null;
\r
151 String uriText = attr.getValue();
\r
152 if(uriText == null) return null;
\r
156 uri = new URI(uriText).normalize();
\r
157 }catch(URISyntaxException e){
\r
158 throw new SAXException("illegal URI " + uriText, e);
\r