View Javadoc
1   /*
2    * DOM Utilities
3    *
4    * License : The MIT License
5    * Copyright(c) 2011 olyutorskii
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   * DOM ユーティリティ。
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       * DOM要素のリストをXMLリソースからロードする。
46       * @param builder DOMビルダ
47       * @param url URL
48       * @param childName 要素名
49       * @return DOM要素のリスト
50       * @throws IOException 入力エラー
51       * @throws SAXException XMLパースエラー
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       * root要素をXMLリソースからロードする。
65       * @param builder DOMビルダ
66       * @param url URL
67       * @return root要素
68       * @throws IOException 入力エラー
69       * @throws SAXException XMLパースエラー
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       * root要素から子要素のリストを得る。
89       * @param root root要素
90       * @param childName 子要素のタグ名
91       * @return 子要素のリスト
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      * XMLタグの必須属性値を得る。
111      * @param elem XML要素
112      * @param attrName 属性名
113      * @return 属性値
114      * @throws SAXException 必須属性が無かった。
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      * XMLタグの属性値を得る。
133      * @param elem XML要素
134      * @param attrName 属性名
135      * @return 属性値。なければnull
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      * XML属性値からURIを展開する。
148      * @param elem XML国定義要素
149      * @param attrName 属性名
150      * @return URI
151      * @throws SAXException 属性が未定義もしくはURI形式を満たさない。
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 }