1 package charactermanaj.util;
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.util.ArrayList;
6 import java.util.Iterator;
7 import java.util.NoSuchElementException;
9 import javax.xml.parsers.DocumentBuilder;
10 import javax.xml.parsers.DocumentBuilderFactory;
11 import javax.xml.parsers.ParserConfigurationException;
13 import org.w3c.dom.Document;
14 import org.w3c.dom.Element;
15 import org.w3c.dom.Node;
16 import org.w3c.dom.NodeList;
17 import org.xml.sax.ErrorHandler;
18 import org.xml.sax.SAXException;
19 import org.xml.sax.SAXParseException;
26 public final class XMLUtilities {
31 private XMLUtilities() {
36 * XMLドキュメントをロードして返します. 名前空間を有効とします.
39 * ロードするXMLドキュメントの入力ストリーム
44 public static Document loadDocument(InputStream is) throws IOException {
47 DocumentBuilderFactory factory = DocumentBuilderFactory
49 factory.setNamespaceAware(true);
51 DocumentBuilder builder = factory.newDocumentBuilder();
52 final ArrayList<SAXParseException> errors = new ArrayList<SAXParseException>();
53 builder.setErrorHandler(new ErrorHandler() {
54 public void error(SAXParseException exception)
56 errors.add(exception);
58 public void fatalError(SAXParseException exception)
60 errors.add(exception);
62 public void warning(SAXParseException exception)
64 errors.add(exception);
68 doc = builder.parse(is);
69 if (errors.size() > 0) {
75 } catch (ParserConfigurationException ex) {
76 throw new RuntimeException("JAXP Configuration Exception.", ex);
78 } catch (SAXException ex) {
79 IOException ex2 = new IOException("xml read failed.");
86 * 指定した名前の子要素で、lang属性が一致するものの値を返す.<br>
87 * langが一致するものがない場合は最初の要素の値を返す.<br>
96 * @return 言語属性が一致する子要素の値、もしくは最初の子要素の値、もしくはnull
98 public static String getLocalizedElementText(Element parent,
99 String elementName, String lang) {
101 for (Element childelm : getChildElements(parent, elementName)) {
102 String val = childelm.getTextContent();
104 // 最初の定義をデフォルト値として用いる.
109 // lang指定が一致すれば、それを優先する.
110 String langNm = childelm.getAttributeNS(
111 "http://www.w3.org/XML/1998/namespace", "lang");
112 if (lang.equals(langNm) && val.length() > 0) {
121 * 指定した名前の子要素の最初の値を返す.
127 * @return 値、要素がなければnull
129 public static String getElementText(Element parent, String elementName) {
130 for (Element childelm : getChildElements(parent, elementName)) {
131 return childelm.getTextContent();
137 * 指定した名前の最初の子要素を返す. なければnull.
143 * @return 最初の子要素、もしくはnull
145 public static Element getFirstChildElement(Element parent, final String name) {
146 for (Element elm : getChildElements(parent, name)) {
153 * 指定した名前の子要素の列挙子を返す. nullの場合は、すべての子要素を返す.
159 * @return 子要素の列挙子、該当がない場合は空の列挙子が返される
161 public static Iterable<Element> getChildElements(Element elm,
163 return iterable(elm.getChildNodes(), new Filter() {
164 public boolean isAccept(Node node) {
165 if (node != null && node.getNodeType() == Node.ELEMENT_NODE) {
166 if (name == null || name.equals(node.getNodeName())) {
181 public static <T extends Node> Iterable<T> iterable(final NodeList nodeList) {
182 return iterable(nodeList, null);
188 public interface Filter {
189 boolean isAccept(Node node);
193 * 指定したノードリストからフィルタ条件にマッチするものだけを列挙子として返す.
196 * ノードリスト、nullの場合は空とみなす
198 * フィルタ条件、nullの場合はすべて合致とみなす
199 * @return 合致するものだけを列挙する列挙子
201 public static <T extends Node> Iterable<T> iterable(
202 final NodeList nodeList, final Filter filter) {
204 if (nodeList == null) {
207 mx = nodeList.getLength();
209 return new Iterable<T>() {
210 public Iterator<T> iterator() {
211 return new Iterator<T>() {
214 private Node nextNode = getNextNode();
216 private Node getNextNode() {
218 Node node = nodeList.item(idx++);
219 if (filter == null || filter.isAccept(node)) {
226 public boolean hasNext() {
227 return nextNode != null;
230 @SuppressWarnings("unchecked")
234 throw new NoSuchElementException();
236 nextNode = getNextNode();
240 public void remove() {
241 throw new UnsupportedOperationException();