OSDN Git Service

1c988fc82756f08d64165ff3e5bc95381c84c24a
[jindolf/JinArchiver.git] / src / main / java / jp / sourceforge / jindolf / archiver / TopicData.java
1 /*
2  * topic data
3  *
4  * License : The MIT License
5  * Copyright(c) 2008 olyutorskii
6  */
7
8 package jp.sourceforge.jindolf.archiver;
9
10 import java.io.IOException;
11 import java.io.Writer;
12 import java.util.LinkedList;
13 import java.util.List;
14 import jp.sourceforge.jindolf.parser.DecodedContent;
15
16 /**
17  * テキスト行の集合。
18  */
19 public abstract class TopicData{
20
21     private static final DecodedContent BREAK = new DecodedContent("\n");
22
23     private final List<DecodedContent> lineList = new LinkedList<>();
24
25     /**
26      * コンストラクタ。
27      */
28     protected TopicData(){
29         super();
30         return;
31     }
32
33     /**
34      * 行を追加する。
35      * @param content 行を構成する文字列
36      */
37     public void addLine(DecodedContent content){
38         this.lineList.add(content);
39         return;
40     }
41
42     /**
43      * 行ブレークを追加する。
44      */
45     public void addBreak(){
46         this.lineList.add(BREAK);
47         return;
48     }
49
50     /**
51      * 行数を取得する。
52      * @return 行数
53      */
54     public int getLineNum(){
55         return this.lineList.size();
56     }
57
58     /**
59      * 最初の行を取得する。
60      * @return 最初の行
61      */
62     public DecodedContent get1stLine(){
63         return this.lineList.get(0);
64     }
65
66     /**
67      * 1行li要素をXML出力する。
68      * @param writer 出力先
69      * @throws IOException 出力エラー
70      */
71     public void dumpLines(Writer writer) throws IOException{
72         DecodedContent lastLine = null;
73         DecodedContent lastContent = null;
74
75         for(DecodedContent content : this.lineList){
76             lastContent = content;
77             if(content == BREAK){
78                 if(lastLine != null){
79                     writer.append("</li>\n");
80                     lastLine = null;
81                 }else{
82                     writer.append("<li/>\n");
83                 }
84             }else{
85                 if(lastLine == null){
86                     writer.append("<li>");
87                 }
88                 XmlUtils.dumpDecodedContent(writer, content);
89                 lastLine = content;
90             }
91         }
92
93         if(lastLine != null){
94             writer.append("</li>\n");
95         }else if(lastContent == BREAK){
96             writer.append("<li/>\n");
97         }
98
99         return;
100     }
101
102     /**
103      * 要素をXML出力する。
104      * @param writer 出力先
105      * @throws IOException 出力エラー
106      */
107     public abstract void dumpXml(Writer writer) throws IOException;
108
109 }