OSDN Git Service

改行コード指定
[jindolf/JinParser.git] / src / test / java / sample / SampleParser.java
1 /*
2  * sample parser
3  *
4  * License : The MIT License
5  * Copyright(c) 2009 olyutorskii
6  */
7
8 package sample;
9
10 import java.io.FileInputStream;
11 import jp.sourceforge.jindolf.parser.*;
12 import java.io.IOException;
13 import java.io.InputStream;
14 import java.nio.charset.Charset;
15 import java.nio.charset.CharsetDecoder;
16 import java.util.Collections;
17 import java.util.Enumeration;
18 import java.util.SortedMap;
19 import java.util.TreeMap;
20 import java.util.zip.ZipEntry;
21 import java.util.zip.ZipFile;
22
23 /**
24  * サンプルのパーサ
25  */
26 public class SampleParser{
27
28     private static final CharsetDecoder ud;
29
30     static{
31         ud = Charset.forName("UTF-8").newDecoder();
32     }
33
34     private SampleParser(){
35         super();
36         return;
37     }
38
39     public static SortedMap<String, ZipEntry> createEntryMap(ZipFile file){
40         TreeMap<String, ZipEntry> result = new TreeMap<String, ZipEntry>();
41
42         Enumeration<? extends ZipEntry> list = file.entries();
43         while(list.hasMoreElements()){
44             ZipEntry entry = list.nextElement();
45             String name = entry.getName();
46             result.put(name, entry);
47         }
48
49         return Collections.unmodifiableSortedMap(result);
50     }
51
52     public static DecodedContent contentFromStream(InputStream istream)
53             throws IOException, DecodeException{
54         StreamDecoder decoder = new StreamDecoder(ud);
55         ContentBuilderUCS2 builder = new ContentBuilderUCS2();
56
57         decoder.setDecodeHandler(builder);
58
59         decoder.decode(istream);
60
61         DecodedContent content = builder.getContent();
62
63         return content;
64     }
65
66     public static void parseContent(DecodedContent content)
67             throws HtmlParseException{
68         HtmlParser parser = new HtmlParser();
69         HtmlHandler handler = new SampleHandler();
70
71         parser.setBasicHandler   (handler);
72         parser.setTalkHandler    (handler);
73         parser.setSysEventHandler(handler);
74
75         parser.parseAutomatic(content);
76
77         return;
78     }
79
80     public static void parseStream(InputStream istream)
81             throws IOException,
82                    DecodeException,
83                    HtmlParseException{
84         DecodedContent content = contentFromStream(istream);
85
86         parseContent(content);
87
88         return;
89     }
90
91     public static void main(String[] args)
92             throws IOException,
93                    DecodeException,
94                    HtmlParseException {
95         if(args.length == 0){
96             System.out.println(
97                      "標準入力から人狼BBSのXHTML文書の読み取りを"
98                     +"開始します...");
99
100             parseStream(System.in);
101
102             System.exit(0);
103
104             return;
105         }else if(args[0].endsWith(".zip")){
106             System.out.println(
107                      "ZIPアーカイブ内の*.htmlファイルから"
108                     +"人狼BBSのXHTML文書の読み取りを開始します...");
109
110             ZipFile zipfile = new ZipFile(args[0]);
111
112             SortedMap<String, ZipEntry> map = createEntryMap(zipfile);
113
114             for(ZipEntry entry : map.values()){
115                 String name = entry.getName();
116                 if( ! name.endsWith(".html") ) continue;
117
118                 System.out.println(name + "のパースを開始...");
119
120                 InputStream istream = zipfile.getInputStream(entry);
121                 parseStream(istream);
122
123                 istream.close();
124             }
125
126             zipfile.close();
127
128             System.exit(0);
129
130             return;
131         }else{
132             System.out.println(args[0] + "のパースを開始...");
133
134             InputStream istream = new FileInputStream(args[0]);
135             parseStream(istream);
136             istream.close();
137
138             System.exit(0);
139         }
140
141         return;
142     }
143
144 }