3 import java.util.regex.Pattern;
5 import org.xml.sax.helpers.DefaultHandler;
6 import org.xml.sax.Attributes;
14 * 説明: ニコニコ動画の動画をコメントつきで保存
18 * 著作権: Copyright (c) 2007 PSI
28 public class NicoXMLReader extends DefaultHandler {
29 private final Packet Packet;
33 private boolean item_kicked;
35 Object waitObject = new Object();
37 private final Pattern NG_Word;
39 private final Pattern NG_ID;
41 public NicoXMLReader(Packet packet, String ng_id, String ng_word) {
43 NG_Word = makePattern(ng_word);
44 NG_ID = makePattern(ng_id);
47 private static final Pattern makePattern(String word) {
48 if (word == null || word.length() <= 0) {
51 String tmp[] = word.split(" ");
52 String tmp2[] = new String[tmp.length];
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("/")) {
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("\"")) {
77 tmp2[index] = tmp[tmp_index];
81 String elt[] = new String[index];
82 for (int i = 0; i < index; i++) {
86 for (int i = 0; i < elt.length; i++) {
88 System.out.println(e);
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))
99 reg += "(.*(" + Pattern.quote(e) + ")+.*)";
102 System.out.println("reg:" + reg);
103 return Pattern.compile(reg);
106 private static final boolean match(Pattern pat, String word) {
107 if (word == null || word.length() <= 0 || pat == null) {
110 return pat.matcher(word).matches();
116 public void startDocument() {
117 System.out.println("Start converting to interval file.");
131 public void startElement(String uri, String localName, String qName,
132 Attributes attributes) {
133 if (qName.toLowerCase().equals("chat")) {
134 // System.out.println("----------");
138 String deleted = attributes.getValue("deleted");
139 if(deleted != null && deleted.toLowerCase().equals("1")){
143 item.setDate(attributes.getValue("date"));
144 String mail = attributes.getValue("mail");
145 if (match(NG_Word, mail)) {
150 item.setNo(attributes.getValue("no"));
151 String user_id = attributes.getValue("user_id");
152 if (match(NG_ID, user_id)) {
156 item.setUserID(user_id);
157 item.setVpos(attributes.getValue("vpos"));
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])) {
179 String com = new String(input);
180 if (match(NG_Word, com)) {
184 item.setComment(com);
197 public void endElement(String uri, String localName, String qName) {
198 if (qName.toLowerCase().equals("chat")) {
200 Packet.addChat(item);
209 public void endDocument() {
210 // System.out.println("----------");
211 System.out.println("Converting finished.");