OSDN Git Service

IDE警告の除去
[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         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 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         System.arraycopy(tmp2, 0, elt, 0, index);
83                 String reg = "";
84                 for (int i = 0; i < elt.length; i++) {
85                         String e = elt[i];
86                         System.out.println(e);
87                         if (i > 0) {
88                                 reg += "|";
89                         }
90                         if (e.indexOf("/") == 0 && e.lastIndexOf("/") == e.length() - 1) {
91                                 reg += "(" + e.substring(1, e.length() - 1) + ")";
92                         } else if (e.indexOf("\"") == 0
93                                         && e.lastIndexOf("\"") == e.length() - 1) {
94                                 reg += "(" + Pattern.quote(e.substring(1, e.length() - 1))
95                                                 + ")";
96                         } else {
97                                 reg += "(.*(" + Pattern.quote(e) + ")+.*)";
98                         }
99                 }
100                 System.out.println("reg:" + reg);
101                 return Pattern.compile(reg);
102         }
103
104         private static boolean match(Pattern pat, String word) {
105                 if (word == null || word.length() <= 0 || pat == null) {
106                         return false;
107                 }
108                 return pat.matcher(word).matches();
109         }
110
111         /**
112          *
113          */
114     @Override
115         public void startDocument() {
116                 System.out.println("Start converting to interval file.");
117         }
118
119         /**
120          *
121          * @param uri
122          *            String
123          * @param localName
124          *            String
125          * @param qName
126          *            String
127          * @param attributes
128          *            Attributes
129          */
130     @Override
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                         //マイメモリ削除対象
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     @Override
172         public void characters(char[] ch, int offset, int length) {
173                 char input[] = (new String(ch, offset, length)).toCharArray();
174                 for (int i = 0; i < input.length; i++) {
175                         if (!Character.isDefined(input[i])) {
176                                 input[i] = '?';
177                         }
178                 }
179                 if (item != null) {
180                         String com = new String(input);
181                         if (match(NG_Word, com)) {
182                                 item_kicked = true;
183                                 return;
184                         }
185                         item.setComment(com);
186                 }
187         }
188
189         /**
190          *
191          * @param uri
192          *            String
193          * @param localName
194          *            String
195          * @param qName
196          *            String
197          */
198     @Override
199         public void endElement(String uri, String localName, String qName) {
200                 if (qName.toLowerCase().equals("chat")) {
201                         if (!item_kicked) {
202                                 Packet.addChat(item);
203                         }
204                         item = null;
205                 }
206         }
207
208         /**
209          * ドキュメント終了
210          */
211     @Override
212         public void endDocument() {
213                 // System.out.println("----------");
214                 System.out.println("Converting finished.");
215         }
216
217 }