OSDN Git Service

パッケージ移動
[mikutoga/TogaGem.git] / src / main / java / jp / sourceforge / mikutoga / xml / BasicXmlExporter.java
1 /*
2  * basic xml exporter
3  *
4  * License : The MIT License
5  * Copyright(c) 2010 MikuToga Partners
6  */
7
8 package jp.sourceforge.mikutoga.xml;
9
10 import java.io.BufferedWriter;
11 import java.io.Closeable;
12 import java.io.Flushable;
13 import java.io.IOException;
14 import java.io.OutputStream;
15 import java.io.OutputStreamWriter;
16 import java.nio.charset.Charset;
17
18 /**
19  * Appendable用XMLエクスポータ実装。
20  */
21 public class BasicXmlExporter extends AbstractXmlExporter{
22
23     /** デフォルトエンコーディング。 */
24     private static final Charset CS_UTF8 = Charset.forName("UTF-8");
25
26
27     private final Appendable appendable;
28
29
30     /**
31      * コンストラクタ。
32      * 文字エンコーディングはUTF-8が用いられる。
33      * @param stream 出力ストリーム
34      */
35     public BasicXmlExporter(OutputStream stream){
36         this(stream, CS_UTF8);
37         return;
38     }
39
40     /**
41      * コンストラクタ。
42      * @param stream 出力ストリーム
43      * @param charSet 文字エンコーディング指定
44      */
45     public BasicXmlExporter(OutputStream stream, Charset charSet){
46         this(
47             new BufferedWriter(
48                 new OutputStreamWriter(stream, charSet)
49             )
50         );
51         return;
52     }
53
54     /**
55      * コンストラクタ。
56      * @param appendable 文字列出力
57      */
58     public BasicXmlExporter(Appendable appendable){
59         super();
60         this.appendable = appendable;
61         return;
62     }
63
64
65     /**
66      * {@inheritDoc}
67      * @param ch {@inheritDoc}
68      * @return {@inheritDoc}
69      * @throws IOException {@inheritDoc}
70      */
71     @Override
72     public BasicXmlExporter putRawCh(char ch) throws IOException{
73         this.appendable.append(ch);
74         return this;
75     }
76
77     /**
78      * {@inheritDoc}
79      * @param seq {@inheritDoc}
80      * @return {@inheritDoc}
81      * @throws IOException {@inheritDoc}
82      */
83     @Override
84     public BasicXmlExporter putRawText(CharSequence seq) throws IOException{
85         this.appendable.append(seq);
86         return this;
87     }
88
89     /**
90      * {@inheritDoc}
91      * 可能であれば出力をフラッシュする。
92      * @throws IOException {@inheritDoc}
93      */
94     @Override
95     public void flush() throws IOException{
96         if(this.appendable instanceof Flushable){
97             ((Flushable)this.appendable).flush();
98         }
99         return;
100     }
101
102     /**
103      * {@inheritDoc}
104      * 可能であれば出力をクローズする。
105      * @throws IOException {@inheritDoc}
106      */
107     @Override
108     public void close() throws IOException{
109         if(this.appendable instanceof Closeable){
110             ((Closeable)this.appendable).close();
111         }
112         return;
113     }
114
115 }