File: HtmlTokenizer.java

package info (click to toggle)
herold 8.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,304 kB
  • sloc: java: 40,460; xml: 513; makefile: 37; sh: 29
file content (660 lines) | stat: -rw-r--r-- 18,934 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
/* 
 * Copyright (C) 2001-2013 Michael Fuchs
 *
 * This file is part of herold.
 * 
 * herold is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * herold is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with herold.  If not, see <http://www.gnu.org/licenses/>.  
 */
package org.dbdoclet.html.tokenizer.parser;

import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dbdoclet.progress.ProgressEvent;
import org.dbdoclet.progress.ProgressListener;
import org.dbdoclet.progress.ProgressManager;


public class HtmlTokenizer/*@bgen(jjtree)*/implements HtmlTokenizerTreeConstants, HtmlTokenizerConstants {/*@bgen(jjtree)*/
  protected JJTHtmlTokenizerState jjtree = new JJTHtmlTokenizerState();private static Log logger = LogFactory.getLog(HtmlTokenizer.class);

  private ArrayList<org.dbdoclet.html.tokenizer.Token> tokenList;
  private ArrayList<ProgressListener> listeners;

  public HtmlTokenizer(String buffer, String encoding) throws UnsupportedEncodingException
  {
    this (new ByteArrayInputStream(buffer.getBytes(encoding)), encoding);
  }

  public synchronized ArrayList < org.dbdoclet.html.tokenizer.Token > parse() throws ParseException
  {
    tokenList = new ArrayList < org.dbdoclet.html.tokenizer.Token > ();
    start();
    return tokenList;
  }

  final public void start() throws ParseException {
 /*@bgen(jjtree) start */
  SimpleNode jjtn000 = new SimpleNode(JJTSTART);
  boolean jjtc000 = true;
  jjtree.openNodeScope(jjtn000);
    try {
      label_1:
      while (true) {
        switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
        case TAG_START:
        case QUOTE:
        case TEXT:
        case COMMENT:
          ;
          break;
        default:
          jj_la1[0] = jj_gen;
          break label_1;
        }
        switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
        case TAG_START:
          tag();
          break;
        case QUOTE:
        case TEXT:
          text();
          break;
        case COMMENT:
          comment();
          break;
        default:
          jj_la1[1] = jj_gen;
          jj_consume_token(-1);
          throw new ParseException();
        }
      }
    } catch (Throwable jjte000) {
    if (jjtc000) {
      jjtree.clearNodeScope(jjtn000);
      jjtc000 = false;
    } else {
      jjtree.popNode();
    }
    if (jjte000 instanceof RuntimeException) {
      {if (true) throw (RuntimeException)jjte000;}
    }
    if (jjte000 instanceof ParseException) {
      {if (true) throw (ParseException)jjte000;}
    }
    {if (true) throw (Error)jjte000;}
    } finally {
    if (jjtc000) {
      jjtree.closeNodeScope(jjtn000, true);
    }
    }
  }

  final public void tag() throws ParseException {
 /*@bgen(jjtree) tag */
  SimpleNode jjtn000 = new SimpleNode(JJTTAG);
  boolean jjtc000 = true;
  jjtree.openNodeScope(jjtn000);Token tag = null;
  Token name = null;
  Token value = null;
  Token slash1 = null;
  Token slash2 = null;
  HashMap < String, String > attributeMap = new HashMap < String, String > ();
    try {
    org.dbdoclet.html.tokenizer.Token token = new org.dbdoclet.html.tokenizer.Token();
    tokenList.add(token);
    fireProgressEvent(token.toString());
      try {
        jj_consume_token(TAG_START);
        switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
        case SLASH:
          slash1 = jj_consume_token(SLASH);
          break;
        default:
          jj_la1[2] = jj_gen;
          ;
        }
        tag = ncname();
      token.setTagName(tag.image);
      token.setLine(tag.beginLine);
      token.setColumn(tag.beginColumn);
          trace("Tag: " + tag.image);
        label_2:
        while (true) {
          switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
          case NCNAME:
            ;
            break;
          default:
            jj_la1[3] = jj_gen;
            break label_2;
          }
          name = attrName();
          switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
          case EQUALS:
            equals();
            value = attrValue();
            break;
          default:
            jj_la1[4] = jj_gen;
            ;
          }
      if (value != null)
      {

        String buffer = value.image;
        if (buffer != null)
        {
                if (buffer.startsWith("\u005c"") || buffer.startsWith("'"))
                {
                        buffer = buffer.substring(1, buffer.length());
                }

                if (buffer.endsWith("\u005c"") || buffer.endsWith("'"))
                {
                        buffer = buffer.substring(0, buffer.length() - 1);
                }

                attributeMap.put(name.image, buffer);
        }
      }
        }
        switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
        case SLASH:
          slash2 = jj_consume_token(SLASH);
          break;
        default:
          jj_la1[5] = jj_gen;
          ;
        }
        jj_consume_token(TAG_END);
      } catch (Throwable oops) {
    logger.warn(String.format("Invalid attributes in Element %s line %d column %d",
                         token.getTagName(), token.getLine(), token.getColumn()));
    logger.warn(oops.getMessage());
    Token t = skipTo(DEFAULT, DEFAULT, token.toString(), TAG_END, TAG_START);
    token.setValue("&lt;" + token.getTagName() + toAttributesAsText(attributeMap) + t.image);
    {if (true) return;}
      }
    jjtree.closeNodeScope(jjtn000, true);
    jjtc000 = false;
    StringBuilder buffer = new StringBuilder();
    buffer.append('<');
    if (slash1 != null)
    {
      buffer.append('/');
    }
    if (tag != null)
    {
      buffer.append(tag.image);
    }
    buffer.append(toAttributesAsText(attributeMap));
    if (slash2 != null)
    {
      buffer.append('/');
    }
    buffer.append('>');
    trace(buffer.toString());
    token.setValue(buffer.toString());
    token.setAttributes(attributeMap);
    } catch (Throwable jjte000) {
    if (jjtc000) {
      jjtree.clearNodeScope(jjtn000);
      jjtc000 = false;
    } else {
      jjtree.popNode();
    }
    if (jjte000 instanceof RuntimeException) {
      {if (true) throw (RuntimeException)jjte000;}
    }
    if (jjte000 instanceof ParseException) {
      {if (true) throw (ParseException)jjte000;}
    }
    {if (true) throw (Error)jjte000;}
    } finally {
    if (jjtc000) {
      jjtree.closeNodeScope(jjtn000, true);
    }
    }
  }

  final public void comment() throws ParseException {
 /*@bgen(jjtree) comment */
  SimpleNode jjtn000 = new SimpleNode(JJTCOMMENT);
  boolean jjtc000 = true;
  jjtree.openNodeScope(jjtn000);Token comment;
    try {
      comment = jj_consume_token(COMMENT);
    jjtree.closeNodeScope(jjtn000, true);
    jjtc000 = false;
    org.dbdoclet.html.tokenizer.Token token = new org.dbdoclet.html.tokenizer.Token(comment.image);
    tokenList.add(token);
    fireProgressEvent(token.toString());
    } finally {
    if (jjtc000) {
      jjtree.closeNodeScope(jjtn000, true);
    }
    }
  }

  final public void text() throws ParseException {
 /*@bgen(jjtree) text */
  SimpleNode jjtn000 = new SimpleNode(JJTTEXT);
  boolean jjtc000 = true;
  jjtree.openNodeScope(jjtn000);Token text;
    try {
      switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
      case TEXT:
        text = jj_consume_token(TEXT);
        break;
      case QUOTE:
        text = jj_consume_token(QUOTE);
        break;
      default:
        jj_la1[6] = jj_gen;
        jj_consume_token(-1);
        throw new ParseException();
      }
    jjtree.closeNodeScope(jjtn000, true);
    jjtc000 = false;
    org.dbdoclet.html.tokenizer.Token token = new org.dbdoclet.html.tokenizer.Token(text.image);
        trace("text() " + token);
    tokenList.add(token);
    fireProgressEvent(token.toString());
    } finally {
    if (jjtc000) {
      jjtree.closeNodeScope(jjtn000, true);
    }
    }
  }

  @SuppressWarnings("unused")
final public Token ncname() throws ParseException {
 /*@bgen(jjtree) ncname */
  SimpleNode jjtn000 = new SimpleNode(JJTNCNAME);
  boolean jjtc000 = true;
  jjtree.openNodeScope(jjtn000);Token t;
    try {
      t = jj_consume_token(NCNAME);
    jjtree.closeNodeScope(jjtn000, true);
    jjtc000 = false;
    {if (true) return t;}
    } finally {
    if (jjtc000) {
      jjtree.closeNodeScope(jjtn000, true);
    }
    }
    throw new Error("Missing return statement in function");
  }

  final public void equals() throws ParseException {
 /*@bgen(jjtree) equals */
  SimpleNode jjtn000 = new SimpleNode(JJTEQUALS);
  boolean jjtc000 = true;
  jjtree.openNodeScope(jjtn000);
    try {
      jj_consume_token(EQUALS);
    } finally {
    if (jjtc000) {
      jjtree.closeNodeScope(jjtn000, true);
    }
    }
  }

  @SuppressWarnings("unused")
final public Token attrName() throws ParseException {
 /*@bgen(jjtree) attrName */
  SimpleNode jjtn000 = new SimpleNode(JJTATTRNAME);
  boolean jjtc000 = true;
  jjtree.openNodeScope(jjtn000);Token token = null;
    try {
      try {
        token = jj_consume_token(NCNAME);
    // System.out.println(token.image);
    {if (true) return token;}
      } catch (Throwable oops) {
    logger.warn(String.format("Invalid attributes in Element %s line %d column %d",
                         token.image, token.beginLine, token.beginColumn));
        logger.warn(oops.getMessage());

        {if (true) return skipTo(DEFAULT, TAG_CONTEXT, "", TAG_END, QUOTE);}
      }
    } finally {
    if (jjtc000) {
      jjtree.closeNodeScope(jjtn000, true);
    }
    }
    throw new Error("Missing return statement in function");
  }

  @SuppressWarnings("unused")
final public Token attrValue() throws ParseException {
 /*@bgen(jjtree) attrValue */
  SimpleNode jjtn000 = new SimpleNode(JJTATTRVALUE);
  boolean jjtc000 = true;
  jjtree.openNodeScope(jjtn000);Token token = null;
    try {
      try {
        switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
        case VALUE:
          token = jj_consume_token(VALUE);
          break;
        case NCNAME:
          token = jj_consume_token(NCNAME);
          break;
        default:
          jj_la1[7] = jj_gen;
          jj_consume_token(-1);
          throw new ParseException();
        }
        {if (true) return token;}
      } catch (Throwable oops) {
    logger.warn(String.format("Invalid attributes in Element %s line %d column %d",
                         token.image, token.beginLine, token.beginColumn));
    logger.warn(oops.getMessage());
        {if (true) return skipTo(DEFAULT, TAG_CONTEXT, "", TAG_END, QUOTE);}
      }
    } finally {
    if (jjtc000) {
      jjtree.closeNodeScope(jjtn000, true);
    }
    }
    throw new Error("Missing return statement in function");
  }

  private Token skipTo(int preContext, int postContext, String prev, int... kind) throws ParseException {
 /*@bgen(jjtree) skipTo */
SimpleNode jjtn000 = new SimpleNode(JJTSKIPTO);
boolean jjtc000 = true;
jjtree.openNodeScope(jjtn000);
try {StringBuilder buffer = new StringBuilder();

  boolean skip = true;

  token_source.SwitchTo(preContext);

  while (skip)
  {
    Token t = getNextToken();
        buffer.append(t.image);

        if (t.kind == EOF)
        {
          break;
        }

        for (int stop : kind)
        {
          if (t.kind == stop)
          {
                skip = false;
          }
        }
  }

  token_source.SwitchTo(postContext);
  return new Token(VALUE, buffer.toString());/*@bgen(jjtree)*/
} finally {
  if (jjtc000) {
    jjtree.closeNodeScope(jjtn000, true);
  }
}
  }

  private String toAttributesAsText(HashMap<String, String > attributeMap) throws ParseException {
 /*@bgen(jjtree) toAttributesAsText */
     SimpleNode jjtn000 = new SimpleNode(JJTTOATTRIBUTESASTEXT);
     boolean jjtc000 = true;
     jjtree.openNodeScope(jjtn000);
     try {StringBuilder buffer = new StringBuilder();

    for (String attr : attributeMap.keySet())
    {
      buffer.append(' ');
      buffer.append(attr);
      buffer.append("=\u005c"");
      buffer.append(attributeMap.get(attr));
      buffer.append("\u005c"");
    }

    return buffer.toString();/*@bgen(jjtree)*/
     } finally {
       if (jjtc000) {
         jjtree.closeNodeScope(jjtn000, true);
       }
     }
  }

  public void setProgressListeners(ArrayList<ProgressListener> listeners) throws ParseException {
 /*@bgen(jjtree) setProgressListeners */
     SimpleNode jjtn000 = new SimpleNode(JJTSETPROGRESSLISTENERS);
     boolean jjtc000 = true;
     jjtree.openNodeScope(jjtn000);
     try {this.listeners = listeners;/*@bgen(jjtree)*/
     } finally {
       if (jjtc000) {
         jjtree.closeNodeScope(jjtn000, true);
       }
     }
  }

  public void fireProgressEvent(String action) throws ParseException {
 /*@bgen(jjtree) fireProgressEvent */
     SimpleNode jjtn000 = new SimpleNode(JJTFIREPROGRESSEVENT);
     boolean jjtc000 = true;
     jjtree.openNodeScope(jjtn000);
     try {if (listeners != null)
        {
                ProgressEvent event = new ProgressEvent("Tokenizer: " + action);
                event.setStage(ProgressEvent.STAGE_PREPARE);

                ProgressManager pm = new ProgressManager(listeners);
                pm.fireProgressEvent(event);
        }/*@bgen(jjtree)*/
     } finally {
       if (jjtc000) {
         jjtree.closeNodeScope(jjtn000, true);
       }
     }
  }

  private void trace(String text) throws ParseException {
 /*@bgen(jjtree) trace */
SimpleNode jjtn000 = new SimpleNode(JJTTRACE);
boolean jjtc000 = true;
jjtree.openNodeScope(jjtn000);
try {/*@bgen(jjtree)*/
} finally {
  if (jjtc000) {
    jjtree.closeNodeScope(jjtn000, true);
  }
}
  }

  /** Generated Token Manager. */
  public HtmlTokenizerTokenManager token_source;
  SimpleCharStream jj_input_stream;
  /** Current token. */
  public Token token;
  /** Next token. */
  public Token jj_nt;
  private int jj_ntk;
  private int jj_gen;
  final private int[] jj_la1 = new int[8];
  static private int[] jj_la1_0;
  static {
      jj_la1_init_0();
   }
   private static void jj_la1_init_0() {
      jj_la1_0 = new int[] {0x400700,0x400700,0x40000,0x80000,0x20000,0x40000,0x600,0x180000,};
   }

  /** Constructor with InputStream. */
  public HtmlTokenizer(java.io.InputStream stream) {
     this(stream, null);
  }
  /** Constructor with InputStream and supplied encoding */
  public HtmlTokenizer(java.io.InputStream stream, String encoding) {
    try { jj_input_stream = new SimpleCharStream(stream, encoding, 1, 1); } catch(java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); }
    token_source = new HtmlTokenizerTokenManager(jj_input_stream);
    token = new Token();
    jj_ntk = -1;
    jj_gen = 0;
    for (int i = 0; i < 8; i++) jj_la1[i] = -1;
  }

  /** Reinitialise. */
  public void ReInit(java.io.InputStream stream) {
     ReInit(stream, null);
  }
  /** Reinitialise. */
  public void ReInit(java.io.InputStream stream, String encoding) {
    try { jj_input_stream.ReInit(stream, encoding, 1, 1); } catch(java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); }
    token_source.ReInit(jj_input_stream);
    token = new Token();
    jj_ntk = -1;
    jjtree.reset();
    jj_gen = 0;
    for (int i = 0; i < 8; i++) jj_la1[i] = -1;
  }

  /** Constructor. */
  public HtmlTokenizer(java.io.Reader stream) {
    jj_input_stream = new SimpleCharStream(stream, 1, 1);
    token_source = new HtmlTokenizerTokenManager(jj_input_stream);
    token = new Token();
    jj_ntk = -1;
    jj_gen = 0;
    for (int i = 0; i < 8; i++) jj_la1[i] = -1;
  }

  /** Reinitialise. */
  public void ReInit(java.io.Reader stream) {
    jj_input_stream.ReInit(stream, 1, 1);
    token_source.ReInit(jj_input_stream);
    token = new Token();
    jj_ntk = -1;
    jjtree.reset();
    jj_gen = 0;
    for (int i = 0; i < 8; i++) jj_la1[i] = -1;
  }

  /** Constructor with generated Token Manager. */
  public HtmlTokenizer(HtmlTokenizerTokenManager tm) {
    token_source = tm;
    token = new Token();
    jj_ntk = -1;
    jj_gen = 0;
    for (int i = 0; i < 8; i++) jj_la1[i] = -1;
  }

  /** Reinitialise. */
  public void ReInit(HtmlTokenizerTokenManager tm) {
    token_source = tm;
    token = new Token();
    jj_ntk = -1;
    jjtree.reset();
    jj_gen = 0;
    for (int i = 0; i < 8; i++) jj_la1[i] = -1;
  }

  private Token jj_consume_token(int kind) throws ParseException {
    Token oldToken;
    if ((oldToken = token).next != null) token = token.next;
    else token = token.next = token_source.getNextToken();
    jj_ntk = -1;
    if (token.kind == kind) {
      jj_gen++;
      return token;
    }
    token = oldToken;
    jj_kind = kind;
    throw generateParseException();
  }


/** Get the next Token. */
  final public Token getNextToken() {
    if (token.next != null) token = token.next;
    else token = token.next = token_source.getNextToken();
    jj_ntk = -1;
    jj_gen++;
    return token;
  }

/** Get the specific Token. */
  final public Token getToken(int index) {
    Token t = token;
    for (int i = 0; i < index; i++) {
      if (t.next != null) t = t.next;
      else t = t.next = token_source.getNextToken();
    }
    return t;
  }

  private int jj_ntk() {
    if ((jj_nt=token.next) == null)
      return (jj_ntk = (token.next=token_source.getNextToken()).kind);
    else
      return (jj_ntk = jj_nt.kind);
  }

  private java.util.List<int[]> jj_expentries = new java.util.ArrayList<int[]>();
  private int[] jj_expentry;
  private int jj_kind = -1;

  /** Generate ParseException. */
  public ParseException generateParseException() {
    jj_expentries.clear();
    boolean[] la1tokens = new boolean[30];
    if (jj_kind >= 0) {
      la1tokens[jj_kind] = true;
      jj_kind = -1;
    }
    for (int i = 0; i < 8; i++) {
      if (jj_la1[i] == jj_gen) {
        for (int j = 0; j < 32; j++) {
          if ((jj_la1_0[i] & (1<<j)) != 0) {
            la1tokens[j] = true;
          }
        }
      }
    }
    for (int i = 0; i < 30; i++) {
      if (la1tokens[i]) {
        jj_expentry = new int[1];
        jj_expentry[0] = i;
        jj_expentries.add(jj_expentry);
      }
    }
    int[][] exptokseq = new int[jj_expentries.size()][];
    for (int i = 0; i < jj_expentries.size(); i++) {
      exptokseq[i] = jj_expentries.get(i);
    }
    return new ParseException(token, exptokseq, tokenImage);
  }

  /** Enable tracing. */
  final public void enable_tracing() {
  }

  /** Disable tracing. */
  final public void disable_tracing() {
  }

}