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 */
016 package org.opengion.fukurou.xml;
017
018 import java.io.File ;
019 import java.io.Writer ;
020 import java.io.BufferedWriter ;
021 import java.io.OutputStreamWriter ;
022 import java.io.FileOutputStream ;
023 import java.io.IOException ;
024
025 import javax.xml.transform.dom.DOMSource ;
026 import javax.xml.transform.stream.StreamResult ;
027 import javax.xml.transform.Transformer ;
028 import javax.xml.transform.TransformerFactory ;
029 import javax.xml.transform.TransformerException ;
030 import javax.xml.transform.TransformerConfigurationException ;
031 import javax.xml.transform.OutputKeys ;
032
033 import javax.xml.parsers.DocumentBuilder ;
034 import javax.xml.parsers.DocumentBuilderFactory ;
035 import javax.xml.parsers.ParserConfigurationException ;
036 import org.xml.sax.SAXException ;
037 import org.opengion.fukurou.util.Closer;
038 import org.w3c.dom.Document ;
039
040 /**
041 * XMLファイルを読み取って、Document オブジェクトを取得するã?ユーãƒ?‚£リãƒ?‚£ークラスですã?
042 *
043 * javax.xml.parsers および、org.w3c.dom の簡易å?ç�?‚’行いますã?
044 * read で、Document を読み込み、write で、ファイルに書きå?しますã?
045 * なおã?書きå?しに関しては、UTF-8 固定で、かつ、Transformer で行いますã?でã€?
046 * 属æ?の並びé ??、保障されませんã€?つまりã?簡易的な書きå?し機è?ですã?)
047 *
048 * @og.rev 5.1.7.0 (2010/06/01) 新規作æ?
049 *
050 * @version 5.0
051 * @author Kazuhiko Hasegawa
052 * @since JDK6.0,
053 */
054
055 public final class DomParser {
056
057 /**
058 * プライベã?ãƒ?コンストラクター
059 *
060 * オブジェクトã?作æ?を拒否しますã?
061 */
062 private DomParser() {}
063
064 /**
065 * XMLファイルを読み込み、org.w3c.dom.Documentを返す
066 * @param aFile XMLファイル
067 *
068 * @return 構築しã�?Document( nullは読み込み失æ•?)
069 */
070 public static Document read( final File aFile ) {
071
072 Document document = null;
073 try {
074 //----------------------------------------------------
075 // step1. DocumentBuilderを構築すã‚?
076 //----------------------------------------------------
077 DocumentBuilderFactory bfactory = DocumentBuilderFactory.newInstance();
078 DocumentBuilder builder = bfactory.newDocumentBuilder();
079
080 //----------------------------------------------------
081 // step2. XMLファイルを読み込んで、Documentを構築すã‚?
082 //----------------------------------------------------
083 document = builder.parse( aFile );
084
085 } catch (ParserConfigurationException ex) {
086 System.err.println( ex.getMessage() );
087 ex.printStackTrace();
088 } catch (SAXException ex) {
089 // æ–?³•エラーが発生したå?å�?
090 System.err.println( ex.getMessage() );
091 ex.printStackTrace();
092 } catch (IOException ex) {
093 // ファイルが読み込めなかったå?å�?
094 System.err.println( ex.getMessage() );
095 ex.printStackTrace();
096 }
097
098 // 完äº?
099 return document;
100 }
101
102 /**
103 * Documentを指定ファイルに保存すã‚?
104 * @param aFile 保存å?ファイル
105 * @param aDocument Documentインスタンス
106 *
107 * @og.rev 5.1.9.0 (2010/08/01) Closeされなã�?ƒ�グを修正
108 * @og.rev 5.6.6.0 (2013/07/05) 若干のインãƒ?ƒƒクス?ˆらしきもã??‰を入れまã�?
109 */
110 public static void write( final File aFile, final Document aDocument ){
111 Writer out = null;
112 try {
113 //---------------------------------
114 // step1. Transformerの準備
115 //---------------------------------
116 TransformerFactory tfactory = TransformerFactory.newInstance();
117 Transformer transformer = tfactory.newTransformer();
118
119 //---------------------------------
120 // step2. Transformerの動作設å®?
121 //---------------------------------
122 transformer.setOutputProperty(OutputKeys.INDENT, "yes");
123 transformer.setOutputProperty(OutputKeys.METHOD, "xml"); // 5.6.6.0 (2013/07/05)
124
125 // 5.6.6.0 (2013/07/05) インãƒ?ƒ³トを入れるには、OutputKeys.INDENT ã�?�‘ではã�?‚�ã€?
126 // Xalan の メインの xalan.jarじゃなくて、serializer.jar に入ってã�?‚‹ OutputPropertiesFactory がå¿?¦�ã?
127 // transformer.setOutputPropert( org.apache.xml.serializer.OutputPropertiesFactory.S_KEY_INDENT_AMOUNT, "2" );
128
129 // こã?為ã�?�‘に、またã?java の JAVA_HOME/jre/lib/ext を増やすã?は嫌なので、OutputPropertiesFactory.S_KEY_INDENT_AMOUNT
130 // で作æ?してã�?‚‹æ–?—å?を直接記述しちã‚?�„ますã?良ã�?�ã?マネしなã�?�§ねã€?
131 transformer.setOutputProperty( "{http://xml.apache.org/xalan}indent-amount", "2" );
132
133 transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
134
135 //---------------------------------
136 // step3. Writerの準備
137 //---------------------------------
138 // Writer out = new BufferedWriter( new OutputStreamWriter( new FileOutputStream(aFile),"UTF-8" )) ;
139 out = new BufferedWriter( new OutputStreamWriter( new FileOutputStream(aFile),"UTF-8" )) ;
140
141 //---------------------------------
142 // step4. 書きå?ã�?
143 //---------------------------------
144 transformer.transform( new DOMSource( aDocument ), new StreamResult( out ) );
145
146 } catch (TransformerConfigurationException ex) {
147 System.err.println( ex.getMessage() );
148 ex.printStackTrace();
149 } catch (TransformerException ex) {
150 // 書きå?しエラー発ç”?
151 System.err.println( ex.getMessage() );
152 ex.printStackTrace();
153 } catch (IOException ex) {
154 // ファイルが読み込めなかったå?å�?
155 System.err.println( ex.getMessage() );
156 ex.printStackTrace();
157 // 5.1.9.0 (2010/08/01) Closeされなã�?ƒ�グを修正
158 } finally {
159 Closer.ioClose( out );
160 }
161 }
162 }