OSDN Git Service

未使用フィールド削除
[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  * タイトル: さきゅばす
11  * </p>
12  *
13  * <p>
14  * 説明: ニコニコ動画の動画をコメントつきで保存
15  * </p>
16  *
17  * <p>
18  * 著作権: Copyright (c) 2007 PSI
19  * </p>
20  *
21  * <p>
22  * 会社名:
23  * </p>
24  *
25  * @author 未入力
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         private final Pattern NG_Word;
36
37         private final Pattern NG_ID;
38
39         public NicoXMLReader(Packet packet, String ng_id, String ng_word) {
40                 Packet = packet;
41                 NG_Word = makePattern(ng_word);
42                 NG_ID = makePattern(ng_id);
43         }
44
45         private static Pattern makePattern(String word) {
46                 if (word == null || word.length() <= 0) {
47                         return null;
48                 }
49                 String tmp[] = word.split(" ");
50                 String tmp2[] = new String[tmp.length];
51                 int tmp_index = 0;
52                 int index;
53                 for (index = 0; index < tmp.length && tmp_index < tmp.length; index++) {
54                         if (tmp[tmp_index].startsWith("/")) {
55                                 String str = tmp[tmp_index];
56                                 for (tmp_index++; tmp_index < tmp.length; tmp_index++) {
57                                         str += " " + tmp[tmp_index];
58                                         if (tmp[tmp_index].endsWith("/")) {
59                                                 tmp_index++;
60                                                 break;
61                                         }
62                                 }
63                                 tmp2[index] = str;
64                         } else if (tmp[tmp_index].startsWith("\"")) {
65                                 String str = tmp[tmp_index];
66                                 for (tmp_index++; tmp_index < tmp.length; tmp_index++) {
67                                         str += " " + tmp[tmp_index];
68                                         if (tmp[tmp_index].endsWith("\"")) {
69                                                 tmp_index++;
70                                                 break;
71                                         }
72                                 }
73                                 tmp2[index] = str;
74                         } else {
75                                 tmp2[index] = tmp[tmp_index];
76                                 tmp_index++;
77                         }
78                 }
79                 String elt[] = new String[index];
80         System.arraycopy(tmp2, 0, elt, 0, index);
81                 String reg = "";
82                 for (int i = 0; i < elt.length; i++) {
83                         String e = elt[i];
84                         System.out.println(e);
85                         if (i > 0) {
86                                 reg += "|";
87                         }
88                         if (e.indexOf("/") == 0 && e.lastIndexOf("/") == e.length() - 1) {
89                                 reg += "(" + e.substring(1, e.length() - 1) + ")";
90                         } else if (e.indexOf("\"") == 0
91                                         && e.lastIndexOf("\"") == e.length() - 1) {
92                                 reg += "(" + Pattern.quote(e.substring(1, e.length() - 1))
93                                                 + ")";
94                         } else {
95                                 reg += "(.*(" + Pattern.quote(e) + ")+.*)";
96                         }
97                 }
98                 System.out.println("reg:" + reg);
99                 return Pattern.compile(reg);
100         }
101
102         private static boolean match(Pattern pat, String word) {
103                 if (word == null || word.length() <= 0 || pat == null) {
104                         return false;
105                 }
106                 return pat.matcher(word).matches();
107         }
108
109         /**
110          *
111          */
112     @Override
113         public void startDocument() {
114                 System.out.println("Start converting to interval file.");
115         }
116
117         /**
118          *
119          * @param uri
120          *            String
121          * @param localName
122          *            String
123          * @param qName
124          *            String
125          * @param attributes
126          *            Attributes
127          */
128     @Override
129         public void startElement(String uri, String localName, String qName,
130                         Attributes attributes) {
131                 if (qName.toLowerCase().equals("chat")) {
132                         // System.out.println("----------");
133                         item = new Chat();
134                         item_kicked = false;
135                         //マイメモリ削除対象
136                         String deleted = attributes.getValue("deleted");
137                         if(deleted != null && deleted.toLowerCase().equals("1")){
138                                 item_kicked = true;
139                                 return;
140                         }
141                         item.setDate(attributes.getValue("date"));
142                         String mail = attributes.getValue("mail");
143                         if (match(NG_Word, mail)) {
144                                 item_kicked = true;
145                                 return;
146                         }
147                         item.setMail(mail);
148                         item.setNo(attributes.getValue("no"));
149                         String user_id = attributes.getValue("user_id");
150                         if (match(NG_ID, user_id)) {
151                                 item_kicked = true;
152                                 return;
153                         }
154                         item.setUserID(user_id);
155                         item.setVpos(attributes.getValue("vpos"));
156
157                 }
158         }
159
160         /**
161          *
162          * @param ch
163          *            char[]
164          * @param offset
165          *            int
166          * @param length
167          *            int
168          */
169     @Override
170         public void characters(char[] ch, int offset, int length) {
171                 char input[] = (new String(ch, offset, length)).toCharArray();
172                 for (int i = 0; i < input.length; i++) {
173                         if (!Character.isDefined(input[i])) {
174                                 input[i] = '?';
175                         }
176                 }
177                 if (item != null) {
178                         String com = new String(input);
179                         if (match(NG_Word, com)) {
180                                 item_kicked = true;
181                                 return;
182                         }
183                         item.setComment(com);
184                 }
185         }
186
187         /**
188          *
189          * @param uri
190          *            String
191          * @param localName
192          *            String
193          * @param qName
194          *            String
195          */
196     @Override
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          * ドキュメント終了
208          */
209     @Override
210         public void endDocument() {
211                 // System.out.println("----------");
212                 System.out.println("Converting finished.");
213         }
214
215 }