4 * License : The MIT License
5 * Copyright(c) 2009 olyutorskii
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;
26 public class SampleParser{
28 private static final CharsetDecoder ud;
31 ud = Charset.forName("UTF-8").newDecoder();
34 private SampleParser(){
39 public static SortedMap<String, ZipEntry> createEntryMap(ZipFile file){
40 TreeMap<String, ZipEntry> result = new TreeMap<String, ZipEntry>();
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);
49 return Collections.unmodifiableSortedMap(result);
52 public static DecodedContent contentFromStream(InputStream istream)
53 throws IOException, DecodeException{
54 StreamDecoder decoder = new StreamDecoder(ud);
55 ContentBuilderUCS2 builder = new ContentBuilderUCS2();
57 decoder.setDecodeHandler(builder);
59 decoder.decode(istream);
61 DecodedContent content = builder.getContent();
66 public static void parseContent(DecodedContent content)
67 throws HtmlParseException{
68 HtmlParser parser = new HtmlParser();
69 HtmlHandler handler = new SampleHandler();
71 parser.setBasicHandler (handler);
72 parser.setTalkHandler (handler);
73 parser.setSysEventHandler(handler);
75 parser.parseAutomatic(content);
80 public static void parseStream(InputStream istream)
84 DecodedContent content = contentFromStream(istream);
86 parseContent(content);
91 public static void main(String[] args)
97 "標準入力から人狼BBSのXHTML文書の読み取りを"
100 parseStream(System.in);
105 }else if(args[0].endsWith(".zip")){
107 "ZIPアーカイブ内の*.htmlファイルから"
108 +"人狼BBSのXHTML文書の読み取りを開始します...");
110 ZipFile zipfile = new ZipFile(args[0]);
112 SortedMap<String, ZipEntry> map = createEntryMap(zipfile);
114 for(ZipEntry entry : map.values()){
115 String name = entry.getName();
116 if( ! name.endsWith(".html") ) continue;
118 System.out.println(name + "のパースを開始...");
120 InputStream istream = zipfile.getInputStream(entry);
121 parseStream(istream);
132 System.out.println(args[0] + "のパースを開始...");
134 InputStream istream = new FileInputStream(args[0]);
135 parseStream(istream);