001/* 002 * Copyright (c) 2009 The openGion Project. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 013 * either express or implied. See the License for the specific language 014 * governing permissions and limitations under the License. 015 */ 016package org.opengion.fukurou.xml; 017 018import java.io.File ; 019import java.io.Writer ; 020import java.io.IOException ; 021 022import javax.xml.transform.dom.DOMSource ; 023import javax.xml.transform.stream.StreamResult ; 024import javax.xml.transform.Transformer ; 025import javax.xml.transform.TransformerFactory ; 026import javax.xml.transform.TransformerException ; 027// import javax.xml.transform.TransformerConfigurationException ; 028import javax.xml.transform.OutputKeys ; 029 030import javax.xml.parsers.DocumentBuilder ; 031import javax.xml.parsers.DocumentBuilderFactory ; 032import javax.xml.parsers.ParserConfigurationException ; 033import org.xml.sax.SAXException ; 034import org.w3c.dom.Document ; 035import org.opengion.fukurou.system.Closer; 036import org.opengion.fukurou.util.FileUtil; // 6.3.8.0 (2015/09/11) 037import org.opengion.fukurou.system.ThrowUtil; // 6.4.2.0 (2016/01/29) 038 039/** 040 * XMLファイルを読み取って、Document オブジェクトを取得する、ユーティリティークラスです。 041 * 042 * javax.xml.parsers および、org.w3c.dom の簡易処理を行います。 043 * read で、Document を読み込み、write で、ファイルに書き出します。 044 * なお、書き出しに関しては、UTF-8 固定で、かつ、Transformer で行いますので、 045 * 属性の並び順は、保障されません。(つまり、簡易的な書き出し機能です。) 046 * 047 * @og.rev 5.1.7.0 (2010/06/01) 新規作成 048 * 049 * @version 5.0 050 * @author Kazuhiko Hasegawa 051 * @since JDK6.0, 052 */ 053 054public final class DomParser { 055 056 /** 057 * プライベート コンストラクター 058 * 059 * オブジェクトの作成を拒否します。 060 */ 061 private DomParser() {} 062 063 /** 064 * XMLファイルを読み込み、org.w3c.dom.Documentを返す。 065 * 066 * @og.rev 6.4.2.0 (2016/01/29) ex.printStackTrace() を、ThrowUtil#ogStackTrace(Throwable) に置き換え。 067 * 068 * @param aFile XMLファイル 069 * 070 * @return 構築した Document( nullは読み込み失敗 ) 071 */ 072 public static Document read( final File aFile ) { 073 074 Document document = null; 075 try { 076 //---------------------------------------------------- 077 // step1. DocumentBuilderを構築する 078 //---------------------------------------------------- 079 final DocumentBuilderFactory bfactory = DocumentBuilderFactory.newInstance(); 080 final DocumentBuilder builder = bfactory.newDocumentBuilder(); 081 082 //---------------------------------------------------- 083 // step2. XMLファイルを読み込んで、Documentを構築する 084 //---------------------------------------------------- 085 document = builder.parse( aFile ); 086 // 7.2.9.5 (2020/11/28) PMD:'catch' branch identical to 'ParserConfigurationException' branch 087 } catch( final ParserConfigurationException | SAXException | IOException ex) { 088 System.err.println( ex.getMessage() ); 089 System.err.println( ThrowUtil.ogStackTrace( ex ) ); // 6.4.2.0 (2016/01/29) 090 } 091// } catch( final ParserConfigurationException ex) { 092// System.err.println( ex.getMessage() ); 093// System.err.println( ThrowUtil.ogStackTrace( ex ) ); // 6.4.2.0 (2016/01/29) 094// } catch( final SAXException ex) { 095// // 文法エラーが発生した場合 096// System.err.println( ex.getMessage() ); 097// System.err.println( ThrowUtil.ogStackTrace( ex ) ); // 6.4.2.0 (2016/01/29) 098// } catch( final IOException ex) { 099// // ファイルが読み込めなかった場合 100// System.err.println( ex.getMessage() ); 101// System.err.println( ThrowUtil.ogStackTrace( ex ) ); // 6.4.2.0 (2016/01/29) 102// } 103 104 // 完了 105 return document; 106 } 107 108 /** 109 * Documentを指定ファイルに保存する。 110 * 111 * @param aFile 保存先ファイル 112 * @param aDocument Documentインスタンス 113 * 114 * @og.rev 5.1.9.0 (2010/08/01) Closeされないバグを修正 115 * @og.rev 5.6.6.0 (2013/07/05) 若干のインデックス(らしきもの)を入れます 116 * @og.rev 6.3.8.0 (2015/09/11) FileUtil#getPrintWriter( File,String ) を使用。 117 * @og.rev 6.4.0.2 (2015/12/11) Transformer のエラーを、より詳細に出力します。 118 * @og.rev 6.4.2.0 (2016/01/29) ex.printStackTrace() を、ThrowUtil#ogStackTrace(Throwable) に置き換え。 119 */ 120 public static void write( final File aFile, final Document aDocument ){ 121 final HybsErrorListener errListener = new HybsErrorListener(); 122 Writer out = null; 123 try { 124 //--------------------------------- 125 // step1. Transformerの準備 126 //--------------------------------- 127 final TransformerFactory tFactory = TransformerFactory.newInstance(); 128 // 6.4.0.2 (2015/12/11) TransformerFactory のエラーを、より詳細に出力します。 129 tFactory.setErrorListener( errListener ); 130 131 final Transformer transformer = tFactory.newTransformer(); 132 // 6.4.0.2 (2015/12/11) Transformer のエラーを、より詳細に出力します。 133 transformer.setErrorListener( errListener ); 134 135 //--------------------------------- 136 // step2. Transformerの動作設定 137 //--------------------------------- 138 transformer.setOutputProperty( OutputKeys.INDENT, "yes" ); 139 transformer.setOutputProperty( OutputKeys.METHOD, "xml" ); // 5.6.6.0 (2013/07/05) 140 141 // 5.6.6.0 (2013/07/05) インデントを入れるには、OutputKeys.INDENT だけではだめ。 142 // Xalan の メインの xalan.jarじゃなくて、serializer.jar に入っている OutputPropertiesFactory が必要。 143 // transformer.setOutputPropert( org.apache.xml.serializer.OutputPropertiesFactory.S_KEY_INDENT_AMOUNT, "2" ); 144 145 // この為だけに、また、java の JAVA_HOME/jre/lib/ext を増やすのは嫌なので、OutputPropertiesFactory.S_KEY_INDENT_AMOUNT 146 // で作成している文字列を直接記述しちゃいます。良い子はマネしないでね。 147 transformer.setOutputProperty( "{http://xml.apache.org/xalan}indent-amount", "2" ); 148 149 transformer.setOutputProperty( OutputKeys.ENCODING, "UTF-8" ); 150 151 //--------------------------------- 152 // step3. Writerの準備 153 //--------------------------------- 154 // 6.3.8.0 (2015/09/11) FileUtil#getPrintWriter( File,String ) を使用。 155 out = FileUtil.getPrintWriter( aFile,"UTF-8" ) ; // 6.3.8.0 (2015/09/11) 156 157 //--------------------------------- 158 // step4. 書き出し 159 //--------------------------------- 160 transformer.transform( new DOMSource( aDocument ), new StreamResult( out ) ); 161 } 162 // 7.2.9.5 (2020/11/28) PMD:'catch' branch identical to 'TransformerConfigurationException' branch 163// catch( final TransformerConfigurationException ex ) { 164// // System.err.println( ex.getMessageAndLocation() ); // 6.4.0.2 (2015/12/11) 行番号がうまく取り出せない。 165// System.err.println( errListener.toString() ); // 6.4.0.2 (2015/12/11) エラー内容が重複するかも。 166// System.err.println( ThrowUtil.ogStackTrace( ex ) ); // 6.4.2.0 (2016/01/29) 167// } 168 catch( final TransformerException ex ) { 169 // 書き出しエラー発生 170 // System.err.println( ex.getMessageAndLocation() ); // 6.4.0.2 (2015/12/11) 行番号がうまく取り出せない。 171 System.err.println( errListener.toString() ); // 6.4.0.2 (2015/12/11) エラー内容が重複するかも。 172 System.err.println( ThrowUtil.ogStackTrace( ex ) ); // 6.4.2.0 (2016/01/29) 173 } 174 // 5.1.9.0 (2010/08/01) Closeされないバグを修正 175 finally { 176 Closer.ioClose( out ); 177 } 178 } 179}