pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / benchmark / src / java / org / apache / lucene / benchmark / byTask / feeds / demohtml / HTMLParser.java
1 /* Generated By:JavaCC: Do not edit this line. HTMLParser.java */
2 package org.apache.lucene.benchmark.byTask.feeds.demohtml;
3
4 import java.io.*;
5 import java.util.Locale;
6 import java.util.Properties;
7
8 public class HTMLParser implements HTMLParserConstants {
9   public static int SUMMARY_LENGTH = 200;
10
11   StringBuffer title = new StringBuffer(SUMMARY_LENGTH);
12   StringBuffer summary = new StringBuffer(SUMMARY_LENGTH * 2);
13   Properties metaTags=new Properties();
14   String currentMetaTag=null;
15   String currentMetaContent=null;
16   int length = 0;
17   boolean titleComplete = false;
18   boolean inTitle = false;
19   boolean inMetaTag = false;
20   boolean inStyle = false;
21   boolean afterTag = false;
22   boolean afterSpace = false;
23   String eol = System.getProperty("line.separator");
24   Reader pipeIn = null;
25   Writer pipeOut;
26   private MyPipedInputStream pipeInStream = null;
27   private PipedOutputStream pipeOutStream = null;
28
29   private class MyPipedInputStream extends PipedInputStream{
30
31     public MyPipedInputStream(){
32       super();
33     }
34
35     public MyPipedInputStream(PipedOutputStream src) throws IOException{
36       super(src);
37     }
38
39     public boolean full() throws IOException{
40       return this.available() >= PipedInputStream.PIPE_SIZE;
41     }
42   }
43
44   public String getTitle() throws IOException, InterruptedException {
45     if (pipeIn == null)
46       getReader();                                // spawn parsing thread
47     while (true) {
48       synchronized(this) {
49         if (titleComplete || pipeInStream.full())
50           break;
51         wait(10);
52       }
53     }
54     return title.toString().trim();
55   }
56
57   public Properties getMetaTags() throws IOException,
58 InterruptedException {
59     if (pipeIn == null)
60       getReader();                                // spawn parsing thread
61     while (true) {
62       synchronized(this) {
63         if (titleComplete || pipeInStream.full())
64           break;
65         wait(10);
66       }
67     }
68     return metaTags;
69   }
70
71
72   public String getSummary() throws IOException, InterruptedException {
73     if (pipeIn == null)
74       getReader();                                // spawn parsing thread
75     while (true) {
76       synchronized(this) {
77         if (summary.length() >= SUMMARY_LENGTH || pipeInStream.full())
78           break;
79         wait(10);
80       }
81     }
82     if (summary.length() > SUMMARY_LENGTH)
83       summary.setLength(SUMMARY_LENGTH);
84
85     String sum = summary.toString().trim();
86     String tit = getTitle();
87     if (sum.equals(""))
88       return tit;
89     else
90       return sum;
91   }
92
93   public Reader getReader() throws IOException {
94     if (pipeIn == null) {
95       pipeInStream = new MyPipedInputStream();
96       pipeOutStream = new PipedOutputStream(pipeInStream);
97       pipeIn = new InputStreamReader(pipeInStream, "UTF-16BE");
98       pipeOut = new OutputStreamWriter(pipeOutStream, "UTF-16BE");
99
100       Thread thread = new ParserThread(this);
101       thread.start();                             // start parsing
102     }
103
104     return pipeIn;
105   }
106
107   void addToSummary(String text) {
108     if (summary.length() < SUMMARY_LENGTH) {
109       summary.append(text);
110       if (summary.length() >= SUMMARY_LENGTH) {
111         synchronized(this) {
112           notifyAll();
113         }
114       }
115     }
116   }
117
118   void addText(String text) throws IOException {
119     if (inStyle)
120       return;
121     if (inTitle)
122       title.append(text);
123     else {
124       addToSummary(text);
125       if (!titleComplete && !(title.length() == 0)) {  // finished title
126         synchronized(this) {
127           titleComplete = true;                   // tell waiting threads
128           notifyAll();
129         }
130       }
131     }
132
133     length += text.length();
134     pipeOut.write(text);
135
136     afterSpace = false;
137   }
138
139   void addMetaTag() {
140       metaTags.setProperty(currentMetaTag, currentMetaContent);
141       currentMetaTag = null;
142       currentMetaContent = null;
143       return;
144   }
145
146   void addSpace() throws IOException {
147     if (!afterSpace) {
148       if (inTitle)
149         title.append(" ");
150       else
151         addToSummary(" ");
152
153       String space = afterTag ? eol : " ";
154       length += space.length();
155       pipeOut.write(space);
156       afterSpace = true;
157     }
158   }
159
160   final public void HTMLDocument() throws ParseException, IOException {
161   Token t;
162     label_1:
163     while (true) {
164       switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
165       case ScriptStart:
166       case TagName:
167       case DeclName:
168       case Comment1:
169       case Comment2:
170       case Word:
171       case Entity:
172       case Space:
173       case Punct:
174         ;
175         break;
176       default:
177         jj_la1[0] = jj_gen;
178         break label_1;
179       }
180       switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
181       case TagName:
182         Tag();
183                       afterTag = true;
184         break;
185       case DeclName:
186         t = Decl();
187                       afterTag = true;
188         break;
189       case Comment1:
190       case Comment2:
191         CommentTag();
192                       afterTag = true;
193         break;
194       case ScriptStart:
195         ScriptTag();
196                      afterTag = true;
197         break;
198       case Word:
199         t = jj_consume_token(Word);
200                       addText(t.image); afterTag = false;
201         break;
202       case Entity:
203         t = jj_consume_token(Entity);
204                       addText(Entities.decode(t.image)); afterTag = false;
205         break;
206       case Punct:
207         t = jj_consume_token(Punct);
208                       addText(t.image); afterTag = false;
209         break;
210       case Space:
211         jj_consume_token(Space);
212                       addSpace(); afterTag = false;
213         break;
214       default:
215         jj_la1[1] = jj_gen;
216         jj_consume_token(-1);
217         throw new ParseException();
218       }
219     }
220     jj_consume_token(0);
221   }
222
223   final public void Tag() throws ParseException, IOException {
224   Token t1, t2;
225   boolean inImg = false;
226     t1 = jj_consume_token(TagName);
227    String tagName = t1.image.toLowerCase(Locale.ENGLISH);
228    if(Tags.WS_ELEMS.contains(tagName) ) {
229       addSpace();
230     }
231     inTitle = tagName.equalsIgnoreCase("<title"); // keep track if in <TITLE>
232     inMetaTag = tagName.equalsIgnoreCase("<META"); // keep track if in <META>
233     inStyle = tagName.equalsIgnoreCase("<STYLE"); // keep track if in <STYLE>
234     inImg = tagName.equalsIgnoreCase("<img");     // keep track if in <IMG>
235
236     label_2:
237     while (true) {
238       switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
239       case ArgName:
240         ;
241         break;
242       default:
243         jj_la1[2] = jj_gen;
244         break label_2;
245       }
246       t1 = jj_consume_token(ArgName);
247       switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
248       case ArgEquals:
249         jj_consume_token(ArgEquals);
250         switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
251         case ArgValue:
252         case ArgQuote1:
253         case ArgQuote2:
254           t2 = ArgValue();
255        if (inImg && t1.image.equalsIgnoreCase("alt") && t2 != null)
256          addText("[" + t2.image + "]");
257
258         if(inMetaTag &&
259                         (  t1.image.equalsIgnoreCase("name") ||
260                            t1.image.equalsIgnoreCase("HTTP-EQUIV")
261                         )
262            && t2 != null)
263         {
264                 currentMetaTag=t2.image.toLowerCase(Locale.ENGLISH);
265                 if(currentMetaTag != null && currentMetaContent != null) {
266                 addMetaTag();
267                 }
268         }
269         if(inMetaTag && t1.image.equalsIgnoreCase("content") && t2 !=
270 null)
271         {
272                 currentMetaContent=t2.image.toLowerCase(Locale.ENGLISH);
273                 if(currentMetaTag != null && currentMetaContent != null) {
274                 addMetaTag();
275                 }
276         }
277           break;
278         default:
279           jj_la1[3] = jj_gen;
280           ;
281         }
282         break;
283       default:
284         jj_la1[4] = jj_gen;
285         ;
286       }
287     }
288     jj_consume_token(TagEnd);
289   }
290
291   final public Token ArgValue() throws ParseException {
292   Token t = null;
293     switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
294     case ArgValue:
295       t = jj_consume_token(ArgValue);
296                                               {if (true) return t;}
297       break;
298     default:
299       jj_la1[5] = jj_gen;
300       if (jj_2_1(2)) {
301         jj_consume_token(ArgQuote1);
302         jj_consume_token(CloseQuote1);
303                                               {if (true) return t;}
304       } else {
305         switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
306         case ArgQuote1:
307           jj_consume_token(ArgQuote1);
308           t = jj_consume_token(Quote1Text);
309           jj_consume_token(CloseQuote1);
310                                               {if (true) return t;}
311           break;
312         default:
313           jj_la1[6] = jj_gen;
314           if (jj_2_2(2)) {
315             jj_consume_token(ArgQuote2);
316             jj_consume_token(CloseQuote2);
317                                               {if (true) return t;}
318           } else {
319             switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
320             case ArgQuote2:
321               jj_consume_token(ArgQuote2);
322               t = jj_consume_token(Quote2Text);
323               jj_consume_token(CloseQuote2);
324                                               {if (true) return t;}
325               break;
326             default:
327               jj_la1[7] = jj_gen;
328               jj_consume_token(-1);
329               throw new ParseException();
330             }
331           }
332         }
333       }
334     }
335     throw new Error("Missing return statement in function");
336   }
337
338   final public Token Decl() throws ParseException {
339   Token t;
340     t = jj_consume_token(DeclName);
341     label_3:
342     while (true) {
343       switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
344       case ArgName:
345       case ArgEquals:
346       case ArgValue:
347       case ArgQuote1:
348       case ArgQuote2:
349         ;
350         break;
351       default:
352         jj_la1[8] = jj_gen;
353         break label_3;
354       }
355       switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
356       case ArgName:
357         jj_consume_token(ArgName);
358         break;
359       case ArgValue:
360       case ArgQuote1:
361       case ArgQuote2:
362         ArgValue();
363         break;
364       case ArgEquals:
365         jj_consume_token(ArgEquals);
366         break;
367       default:
368         jj_la1[9] = jj_gen;
369         jj_consume_token(-1);
370         throw new ParseException();
371       }
372     }
373     jj_consume_token(TagEnd);
374     {if (true) return t;}
375     throw new Error("Missing return statement in function");
376   }
377
378   final public void CommentTag() throws ParseException {
379     switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
380     case Comment1:
381       jj_consume_token(Comment1);
382       label_4:
383       while (true) {
384         switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
385         case CommentText1:
386           ;
387           break;
388         default:
389           jj_la1[10] = jj_gen;
390           break label_4;
391         }
392         jj_consume_token(CommentText1);
393       }
394       jj_consume_token(CommentEnd1);
395       break;
396     case Comment2:
397       jj_consume_token(Comment2);
398       label_5:
399       while (true) {
400         switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
401         case CommentText2:
402           ;
403           break;
404         default:
405           jj_la1[11] = jj_gen;
406           break label_5;
407         }
408         jj_consume_token(CommentText2);
409       }
410       jj_consume_token(CommentEnd2);
411       break;
412     default:
413       jj_la1[12] = jj_gen;
414       jj_consume_token(-1);
415       throw new ParseException();
416     }
417   }
418
419   final public void ScriptTag() throws ParseException {
420     jj_consume_token(ScriptStart);
421     label_6:
422     while (true) {
423       switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
424       case ScriptText:
425         ;
426         break;
427       default:
428         jj_la1[13] = jj_gen;
429         break label_6;
430       }
431       jj_consume_token(ScriptText);
432     }
433     jj_consume_token(ScriptEnd);
434   }
435
436   private boolean jj_2_1(int xla) {
437     jj_la = xla; jj_lastpos = jj_scanpos = token;
438     try { return !jj_3_1(); }
439     catch(LookaheadSuccess ls) { return true; }
440     finally { jj_save(0, xla); }
441   }
442
443   private boolean jj_2_2(int xla) {
444     jj_la = xla; jj_lastpos = jj_scanpos = token;
445     try { return !jj_3_2(); }
446     catch(LookaheadSuccess ls) { return true; }
447     finally { jj_save(1, xla); }
448   }
449
450   private boolean jj_3_2() {
451     if (jj_scan_token(ArgQuote2)) return true;
452     if (jj_scan_token(CloseQuote2)) return true;
453     return false;
454   }
455
456   private boolean jj_3_1() {
457     if (jj_scan_token(ArgQuote1)) return true;
458     if (jj_scan_token(CloseQuote1)) return true;
459     return false;
460   }
461
462   /** Generated Token Manager. */
463   public HTMLParserTokenManager token_source;
464   SimpleCharStream jj_input_stream;
465   /** Current token. */
466   public Token token;
467   /** Next token. */
468   public Token jj_nt;
469   private int jj_ntk;
470   private Token jj_scanpos, jj_lastpos;
471   private int jj_la;
472   private int jj_gen;
473   final private int[] jj_la1 = new int[14];
474   static private int[] jj_la1_0;
475   static {
476       jj_la1_init_0();
477    }
478    private static void jj_la1_init_0() {
479       jj_la1_0 = new int[] {0x2c7e,0x2c7e,0x10000,0x380000,0x20000,0x80000,0x100000,0x200000,0x3b0000,0x3b0000,0x8000000,0x20000000,0x30,0x4000,};
480    }
481   final private JJCalls[] jj_2_rtns = new JJCalls[2];
482   private boolean jj_rescan = false;
483   private int jj_gc = 0;
484
485   /** Constructor with InputStream. */
486   public HTMLParser(java.io.InputStream stream) {
487      this(stream, null);
488   }
489   /** Constructor with InputStream and supplied encoding */
490   public HTMLParser(java.io.InputStream stream, String encoding) {
491     try { jj_input_stream = new SimpleCharStream(stream, encoding, 1, 1); } catch(java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); }
492     token_source = new HTMLParserTokenManager(jj_input_stream);
493     token = new Token();
494     jj_ntk = -1;
495     jj_gen = 0;
496     for (int i = 0; i < 14; i++) jj_la1[i] = -1;
497     for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
498   }
499
500   /** Reinitialise. */
501   public void ReInit(java.io.InputStream stream) {
502      ReInit(stream, null);
503   }
504   /** Reinitialise. */
505   public void ReInit(java.io.InputStream stream, String encoding) {
506     try { jj_input_stream.ReInit(stream, encoding, 1, 1); } catch(java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); }
507     token_source.ReInit(jj_input_stream);
508     token = new Token();
509     jj_ntk = -1;
510     jj_gen = 0;
511     for (int i = 0; i < 14; i++) jj_la1[i] = -1;
512     for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
513   }
514
515   /** Constructor. */
516   public HTMLParser(java.io.Reader stream) {
517     jj_input_stream = new SimpleCharStream(stream, 1, 1);
518     token_source = new HTMLParserTokenManager(jj_input_stream);
519     token = new Token();
520     jj_ntk = -1;
521     jj_gen = 0;
522     for (int i = 0; i < 14; i++) jj_la1[i] = -1;
523     for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
524   }
525
526   /** Reinitialise. */
527   public void ReInit(java.io.Reader stream) {
528     jj_input_stream.ReInit(stream, 1, 1);
529     token_source.ReInit(jj_input_stream);
530     token = new Token();
531     jj_ntk = -1;
532     jj_gen = 0;
533     for (int i = 0; i < 14; i++) jj_la1[i] = -1;
534     for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
535   }
536
537   /** Constructor with generated Token Manager. */
538   public HTMLParser(HTMLParserTokenManager tm) {
539     token_source = tm;
540     token = new Token();
541     jj_ntk = -1;
542     jj_gen = 0;
543     for (int i = 0; i < 14; i++) jj_la1[i] = -1;
544     for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
545   }
546
547   /** Reinitialise. */
548   public void ReInit(HTMLParserTokenManager tm) {
549     token_source = tm;
550     token = new Token();
551     jj_ntk = -1;
552     jj_gen = 0;
553     for (int i = 0; i < 14; i++) jj_la1[i] = -1;
554     for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
555   }
556
557   private Token jj_consume_token(int kind) throws ParseException {
558     Token oldToken;
559     if ((oldToken = token).next != null) token = token.next;
560     else token = token.next = token_source.getNextToken();
561     jj_ntk = -1;
562     if (token.kind == kind) {
563       jj_gen++;
564       if (++jj_gc > 100) {
565         jj_gc = 0;
566         for (int i = 0; i < jj_2_rtns.length; i++) {
567           JJCalls c = jj_2_rtns[i];
568           while (c != null) {
569             if (c.gen < jj_gen) c.first = null;
570             c = c.next;
571           }
572         }
573       }
574       return token;
575     }
576     token = oldToken;
577     jj_kind = kind;
578     throw generateParseException();
579   }
580
581   static private final class LookaheadSuccess extends java.lang.Error { }
582   final private LookaheadSuccess jj_ls = new LookaheadSuccess();
583   private boolean jj_scan_token(int kind) {
584     if (jj_scanpos == jj_lastpos) {
585       jj_la--;
586       if (jj_scanpos.next == null) {
587         jj_lastpos = jj_scanpos = jj_scanpos.next = token_source.getNextToken();
588       } else {
589         jj_lastpos = jj_scanpos = jj_scanpos.next;
590       }
591     } else {
592       jj_scanpos = jj_scanpos.next;
593     }
594     if (jj_rescan) {
595       int i = 0; Token tok = token;
596       while (tok != null && tok != jj_scanpos) { i++; tok = tok.next; }
597       if (tok != null) jj_add_error_token(kind, i);
598     }
599     if (jj_scanpos.kind != kind) return true;
600     if (jj_la == 0 && jj_scanpos == jj_lastpos) throw jj_ls;
601     return false;
602   }
603
604
605 /** Get the next Token. */
606   final public Token getNextToken() {
607     if (token.next != null) token = token.next;
608     else token = token.next = token_source.getNextToken();
609     jj_ntk = -1;
610     jj_gen++;
611     return token;
612   }
613
614 /** Get the specific Token. */
615   final public Token getToken(int index) {
616     Token t = token;
617     for (int i = 0; i < index; i++) {
618       if (t.next != null) t = t.next;
619       else t = t.next = token_source.getNextToken();
620     }
621     return t;
622   }
623
624   private int jj_ntk() {
625     if ((jj_nt=token.next) == null)
626       return (jj_ntk = (token.next=token_source.getNextToken()).kind);
627     else
628       return (jj_ntk = jj_nt.kind);
629   }
630
631   private java.util.List<int[]> jj_expentries = new java.util.ArrayList<int[]>();
632   private int[] jj_expentry;
633   private int jj_kind = -1;
634   private int[] jj_lasttokens = new int[100];
635   private int jj_endpos;
636
637   private void jj_add_error_token(int kind, int pos) {
638     if (pos >= 100) return;
639     if (pos == jj_endpos + 1) {
640       jj_lasttokens[jj_endpos++] = kind;
641     } else if (jj_endpos != 0) {
642       jj_expentry = new int[jj_endpos];
643       for (int i = 0; i < jj_endpos; i++) {
644         jj_expentry[i] = jj_lasttokens[i];
645       }
646       jj_entries_loop: for (java.util.Iterator it = jj_expentries.iterator(); it.hasNext();) {
647         int[] oldentry = (int[])(it.next());
648         if (oldentry.length == jj_expentry.length) {
649           for (int i = 0; i < jj_expentry.length; i++) {
650             if (oldentry[i] != jj_expentry[i]) {
651               continue jj_entries_loop;
652             }
653           }
654           jj_expentries.add(jj_expentry);
655           break jj_entries_loop;
656         }
657       }
658       if (pos != 0) jj_lasttokens[(jj_endpos = pos) - 1] = kind;
659     }
660   }
661
662   /** Generate ParseException. */
663   public ParseException generateParseException() {
664     jj_expentries.clear();
665     boolean[] la1tokens = new boolean[31];
666     if (jj_kind >= 0) {
667       la1tokens[jj_kind] = true;
668       jj_kind = -1;
669     }
670     for (int i = 0; i < 14; i++) {
671       if (jj_la1[i] == jj_gen) {
672         for (int j = 0; j < 32; j++) {
673           if ((jj_la1_0[i] & (1<<j)) != 0) {
674             la1tokens[j] = true;
675           }
676         }
677       }
678     }
679     for (int i = 0; i < 31; i++) {
680       if (la1tokens[i]) {
681         jj_expentry = new int[1];
682         jj_expentry[0] = i;
683         jj_expentries.add(jj_expentry);
684       }
685     }
686     jj_endpos = 0;
687     jj_rescan_token();
688     jj_add_error_token(0, 0);
689     int[][] exptokseq = new int[jj_expentries.size()][];
690     for (int i = 0; i < jj_expentries.size(); i++) {
691       exptokseq[i] = jj_expentries.get(i);
692     }
693     return new ParseException(token, exptokseq, tokenImage);
694   }
695
696   /** Enable tracing. */
697   final public void enable_tracing() {
698   }
699
700   /** Disable tracing. */
701   final public void disable_tracing() {
702   }
703
704   private void jj_rescan_token() {
705     jj_rescan = true;
706     for (int i = 0; i < 2; i++) {
707     try {
708       JJCalls p = jj_2_rtns[i];
709       do {
710         if (p.gen > jj_gen) {
711           jj_la = p.arg; jj_lastpos = jj_scanpos = p.first;
712           switch (i) {
713             case 0: jj_3_1(); break;
714             case 1: jj_3_2(); break;
715           }
716         }
717         p = p.next;
718       } while (p != null);
719       } catch(LookaheadSuccess ls) { }
720     }
721     jj_rescan = false;
722   }
723
724   private void jj_save(int index, int xla) {
725     JJCalls p = jj_2_rtns[index];
726     while (p.gen > jj_gen) {
727       if (p.next == null) { p = p.next = new JJCalls(); break; }
728       p = p.next;
729     }
730     p.gen = jj_gen + xla - jj_la; p.first = token; p.arg = xla;
731   }
732
733   static final class JJCalls {
734     int gen;
735     Token first;
736     int arg;
737     JJCalls next;
738   }
739
740 //    void handleException(Exception e) {
741 //      System.out.println(e.toString());  // print the error message
742 //      System.out.println("Skipping...");
743 //      Token t;
744 //      do {
745 //        t = getNextToken();
746 //      } while (t.kind != TagEnd);
747 //    }
748 }