package jomora.io; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.io.UnsupportedEncodingException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.Result; import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.xml.sax.SAXException; /** *

Documentに関するユーティリティクラス

* @author jomora@jomora.net * @version 2002.11.06 作成 * @version 2006.05.05 コメント追加(CheckStyle,FindBugs対応) */ public final class DocumentUtil { /** * プライベートコンストラクタ。 * インスタンス化防止用。 */ private DocumentUtil() { } /** * XMLドキュメントを文字列に変換する。 * 文字コードはUTF-8 * @param document XMLドキュメント * @return 文字列化XML * @throws UnsupportedEncodingException プラットフォームがUTF-8に対応していない場合の例外 * @throws TransformerException XML変換例外 */ public static String document2String(Document document) throws UnsupportedEncodingException, TransformerException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = tf.newTransformer(); Source source = new DOMSource(document); Result result = new StreamResult(baos); t.transform(source, result); return baos.toString("UTF-8"); } /** * 文字列をXMLドキュメント化する。 * 文字コードはUTF-8 * @param str 文字列 * @return XMLドキュメント * @throws ParserConfigurationException XMLParser構成例外 * @throws IOException 入出力例外 * @throws SAXException XML解析例外 */ public static Document string2Document(String str) throws ParserConfigurationException, IOException, SAXException { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); ByteArrayInputStream bais = new ByteArrayInputStream(str.getBytes("UTF-8")); return db.parse(bais); } /** * XMLファイルを読み込み、Documentオブジェクトを返す * @param xmlfile XMLファイル * @return Documentオブジェクト * @throws ParserConfigurationException XMLParser構成例外 * @throws IOException 入出力例外 * @throws SAXException XML解析例外 */ public static Document readDocument(File xmlfile) throws ParserConfigurationException, IOException, SAXException { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(false); DocumentBuilder db = dbf.newDocumentBuilder(); return db.parse(xmlfile); } /** * XMLファイルを読み込み、Documentオブジェクトを返す * @param xmlfilename XMLファイル名 * @return Documentオブジェクト * @throws ParserConfigurationException XMLParser構成例外 * @throws IOException 入出力例外 * @throws SAXException XML解析例外 */ public static Document readDocument(String xmlfilename) throws ParserConfigurationException, IOException, SAXException { Document document = readDocument(new File(xmlfilename)); //document.normalize(); return document; } /** * Documentから、XMLファイルを書き出す。その際、文字コードを指定する。 * @param xmlfile XMLファイル * @param document Documentオブジェクト * @param encoding 文字コード * @throws TransformerException XML変換例外 */ public static void writeDocument(File xmlfile, Document document, String encoding) throws TransformerException { TransformerFactory tff = TransformerFactory.newInstance(); Transformer transformer = tff.newTransformer(); transformer.setOutputProperty("encoding", encoding); //transformer.setOutputProperty("standalone", "yes"); transformer.setOutputProperty("indent", "yes"); transformer.transform(new DOMSource(document), new StreamResult(xmlfile)); } /** * Documentから、XMLファイルを書き出す。その際、文字コード指定を行う。 * @param xmlfilename XMLファイル名 * @param document Documentオブジェクト * @param encoding 文字コード * @throws TransformerException XML変換例外 */ public static void writeDocument(String xmlfilename, Document document, String encoding) throws TransformerException { writeDocument(new File(xmlfilename), document, encoding); } /** * Documentから、XMLファイルを書き出す。文字コードはUTF-8となる。 * @param xmlfile XMLファイル * @param document Documentオブジェクト * @throws TransformerException XML変換例外 */ public static void writeDocument(File xmlfile, Document document) throws TransformerException { writeDocument(xmlfile, document, "UTF-8"); } /** * Documentから、XMLファイルを書き出す。文字コードはUTF-8となる。 * @param xmlfile XMLファイル * @param document Documentオブジェクト * @throws TransformerException XML変換例外 */ public static void writeDocument(String xmlfile, Document document) throws TransformerException { writeDocument(new File(xmlfile), document, "UTF-8"); } /** *

NodeからTAB文字で始まるテキストノードを再帰的に削除する。

* ※ このメソッド適応後にファイル出力すると、 * TABおよびスペース文字によるインデントがなくなってしまうことに注意。 * @param node ノードツリー * @return 不要なテキストノードが消えたノードツリー */ public static Node clean(Node node) { if (node.hasChildNodes()) { Node childnode = node.getFirstChild(); while (childnode != null) { if (childnode.getNodeType() == Node.TEXT_NODE && childnode.getNodeValue().trim().length() == 0) { Node delnode = childnode; childnode = childnode.getNextSibling(); node.removeChild(delnode); } else { if (childnode.hasChildNodes()) { clean(childnode); } childnode = childnode.getNextSibling(); } } } return node; } }