OSDN Git Service

1.22rのソースからインポート.
[coroid/inqubus.git] / frontend / src / saccubus / conv / NicoXMLReader.java
1 package saccubus.conv;
2
3 import java.util.regex.Pattern;
4
5 import org.xml.sax.helpers.DefaultHandler;
6 import org.xml.sax.Attributes;
7
8 /**
9  * <p>
10  * \83^\83C\83g\83\8b\82³\82«\82ã\82Î\82·
11  * </p>
12  * 
13  * <p>
14  * \90à\96¾: \83j\83R\83j\83R\93®\89æ\82Ì\93®\89æ\82ð\83R\83\81\83\93\83g\82Â\82«\82Å\95Û\91
15  * </p>
16  * 
17  * <p>
18  * \92\98\8dì\8c : Copyright (c) 2007 PSI
19  * </p>
20  * 
21  * <p>
22  * \89ï\8eÐ\96¼:
23  * </p>
24  * 
25  * @author \96¢\93ü\97Í
26  * @version 1.0
27  */
28 public class NicoXMLReader extends DefaultHandler {
29         private final Packet Packet;
30
31         private Chat item;
32
33         private boolean item_kicked;
34
35         Object waitObject = new Object();
36
37         private final Pattern NG_Word;
38
39         private final Pattern NG_ID;
40
41         public NicoXMLReader(Packet packet, String ng_id, String ng_word) {
42                 Packet = packet;
43                 NG_Word = makePattern(ng_word);
44                 NG_ID = makePattern(ng_id);
45         }
46
47         private static final Pattern makePattern(String word) {
48                 if (word == null || word.length() <= 0) {
49                         return null;
50                 }
51                 String tmp[] = word.split(" ");
52                 String tmp2[] = new String[tmp.length];
53                 int tmp_index = 0;
54                 int index;
55                 for (index = 0; index < tmp.length && tmp_index < tmp.length; index++) {
56                         if (tmp[tmp_index].startsWith("/")) {
57                                 String str = tmp[tmp_index];
58                                 for (tmp_index++; tmp_index < tmp.length; tmp_index++) {
59                                         str += " " + tmp[tmp_index];
60                                         if (tmp[tmp_index].endsWith("/")) {
61                                                 tmp_index++;
62                                                 break;
63                                         }
64                                 }
65                                 tmp2[index] = str;
66                         } else if (tmp[tmp_index].startsWith("\"")) {
67                                 String str = tmp[tmp_index];
68                                 for (tmp_index++; tmp_index < tmp.length; tmp_index++) {
69                                         str += " " + tmp[tmp_index];
70                                         if (tmp[tmp_index].endsWith("\"")) {
71                                                 tmp_index++;
72                                                 break;
73                                         }
74                                 }
75                                 tmp2[index] = str;
76                         } else {
77                                 tmp2[index] = tmp[tmp_index];
78                                 tmp_index++;
79                         }
80                 }
81                 String elt[] = new String[index];
82                 for (int i = 0; i < index; i++) {
83                         elt[i] = tmp2[i];
84                 }
85                 String reg = "";
86                 for (int i = 0; i < elt.length; i++) {
87                         String e = elt[i];
88                         System.out.println(e);
89                         if (i > 0) {
90                                 reg += "|";
91                         }
92                         if (e.indexOf("/") == 0 && e.lastIndexOf("/") == e.length() - 1) {
93                                 reg += "(" + e.substring(1, e.length() - 1) + ")";
94                         } else if (e.indexOf("\"") == 0
95                                         && e.lastIndexOf("\"") == e.length() - 1) {
96                                 reg += "(" + Pattern.quote(e.substring(1, e.length() - 1))
97                                                 + ")";
98                         } else {
99                                 reg += "(.*(" + Pattern.quote(e) + ")+.*)";
100                         }
101                 }
102                 System.out.println("reg:" + reg);
103                 return Pattern.compile(reg);
104         }
105
106         private static final boolean match(Pattern pat, String word) {
107                 if (word == null || word.length() <= 0 || pat == null) {
108                         return false;
109                 }
110                 return pat.matcher(word).matches();
111         }
112
113         /**
114          * 
115          */
116         public void startDocument() {
117                 System.out.println("Start converting to interval file.");
118         }
119
120         /**
121          * 
122          * @param uri
123          *            String
124          * @param localName
125          *            String
126          * @param qName
127          *            String
128          * @param attributes
129          *            Attributes
130          */
131         public void startElement(String uri, String localName, String qName,
132                         Attributes attributes) {
133                 if (qName.toLowerCase().equals("chat")) {
134                         // System.out.println("----------");
135                         item = new Chat();
136                         item_kicked = false;
137                         //\83}\83C\83\81\83\82\83\8a\8dí\8f\9c\91Î\8fÛ
138                         String deleted = attributes.getValue("deleted");
139                         if(deleted != null && deleted.toLowerCase().equals("1")){
140                                 item_kicked = true;
141                                 return;
142                         }
143                         item.setDate(attributes.getValue("date"));
144                         String mail = attributes.getValue("mail");
145                         if (match(NG_Word, mail)) {
146                                 item_kicked = true;
147                                 return;
148                         }
149                         item.setMail(mail);
150                         item.setNo(attributes.getValue("no"));
151                         String user_id = attributes.getValue("user_id");
152                         if (match(NG_ID, user_id)) {
153                                 item_kicked = true;
154                                 return;
155                         }
156                         item.setUserID(user_id);
157                         item.setVpos(attributes.getValue("vpos"));
158
159                 }
160         }
161
162         /**
163          * 
164          * @param ch
165          *            char[]
166          * @param offset
167          *            int
168          * @param length
169          *            int
170          */
171         public void characters(char[] ch, int offset, int length) {
172                 char input[] = (new String(ch, offset, length)).toCharArray();
173                 for (int i = 0; i < input.length; i++) {
174                         if (!Character.isDefined(input[i])) {
175                                 input[i] = '?';
176                         }
177                 }
178                 if (item != null) {
179                         String com = new String(input);
180                         if (match(NG_Word, com)) {
181                                 item_kicked = true;
182                                 return;
183                         }
184                         item.setComment(com);
185                 }
186         }
187
188         /**
189          * 
190          * @param uri
191          *            String
192          * @param localName
193          *            String
194          * @param qName
195          *            String
196          */
197         public void endElement(String uri, String localName, String qName) {
198                 if (qName.toLowerCase().equals("chat")) {
199                         if (!item_kicked) {
200                                 Packet.addChat(item);
201                         }
202                         item = null;
203                 }
204         }
205
206         /**
207          * \83h\83L\83\85\83\81\83\93\83g\8fI\97¹
208          */
209         public void endDocument() {
210                 // System.out.println("----------");
211                 System.out.println("Converting finished.");
212         }
213
214 }