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       *
47       * @param builder DOMビルダ
48       * @param url URL
49       * @param childName 要素名
50       * @return DOM要素のリスト
51       * @throws IOException 入力エラー
52       * @throws SAXException XMLパースエラー
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       * root要素をXMLリソースからロードする。
66       *
67       * @param builder DOMビルダ
68       * @param url URL
69       * @return root要素
70       * @throws IOException 入力エラー
71       * @throws SAXException XMLパースエラー
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       * root要素から子要素のリストを得る。
91       *
92       * @param root root要素
93       * @param childName 子要素のタグ名
94       * @return 子要素のリスト
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      * XMLタグの必須属性値を得る。
114      *
115      * @param elem XML要素
116      * @param attrName 属性名
117      * @return 属性値
118      * @throws SAXException 必須属性が無かった。
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      * XMLタグの属性値を得る。
137      *
138      * @param elem XML要素
139      * @param attrName 属性名
140      * @return 属性値。なければnull
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      * XML属性値からURIを展開する。
153      *
154      * @param elem XML国定義要素
155      * @param attrName 属性名
156      * @return URI
157      * @throws SAXException 属性が未定義もしくはURI形式を満たさない。
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 }