home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 144 / DPCS0200.iso / Internet / Supanet / system / swing.jar / javax / swing / text / html / HTMLDocument$HTMLReader.class (.txt) < prev    next >
Encoding:
Java Class File  |  1998-11-05  |  11.3 KB  |  428 lines

  1. package javax.swing.text.html;
  2.  
  3. import java.util.Hashtable;
  4. import java.util.Stack;
  5. import java.util.Vector;
  6. import javax.swing.text.AbstractDocument;
  7. import javax.swing.text.AttributeSet;
  8. import javax.swing.text.BadLocationException;
  9. import javax.swing.text.DefaultStyledDocument;
  10. import javax.swing.text.MutableAttributeSet;
  11. import javax.swing.text.SimpleAttributeSet;
  12. import javax.swing.text.StyleConstants;
  13. import javax.swing.text.html.HTML.Attribute;
  14. import javax.swing.text.html.HTML.Tag;
  15.  
  16. public class HTMLDocument$HTMLReader extends HTMLEditorKit.ParserCallback {
  17.    // $FF: synthetic field
  18.    private final HTMLDocument this$0;
  19.    int threshold;
  20.    int offset;
  21.    int tableCount;
  22.    boolean inParagraph;
  23.    boolean impliedP;
  24.    boolean inPre;
  25.    boolean inTextArea;
  26.    TextAreaDocument textAreaDocument;
  27.    boolean inTitle;
  28.    boolean lastWasNewline;
  29.    boolean emptyAnchor;
  30.    HTML.Tag insertTag;
  31.    boolean foundInsertTag;
  32.    int popDepth;
  33.    int pushDepth;
  34.    Option option;
  35.    protected Vector parseBuffer;
  36.    protected MutableAttributeSet charAttr;
  37.    Stack charAttrStack;
  38.    Hashtable tagMap;
  39.    int inBlock;
  40.  
  41.    public HTMLDocument$HTMLReader(HTMLDocument var1, int var2) {
  42.       this(var1, var2, 0, 0, (HTML.Tag)null);
  43.    }
  44.  
  45.    public HTMLDocument$HTMLReader(HTMLDocument var1, int var2, int var3, int var4, HTML.Tag var5) {
  46.       this.this$0 = var1;
  47.       this.inParagraph = false;
  48.       this.impliedP = false;
  49.       this.inPre = false;
  50.       this.inTextArea = false;
  51.       this.textAreaDocument = null;
  52.       this.inTitle = false;
  53.       this.lastWasNewline = true;
  54.       this.parseBuffer = new Vector();
  55.       this.charAttr = new SimpleAttributeSet();
  56.       this.charAttrStack = new Stack();
  57.       this.inBlock = 0;
  58.       this.offset = var2;
  59.       this.tableCount = 0;
  60.       this.threshold = var1.getTokenThreshold();
  61.       this.tagMap = new Hashtable(57);
  62.       new HTMLDocument.HTMLReader.TagAction(this);
  63.       HTMLDocument.HTMLReader.BlockAction var7 = new HTMLDocument.HTMLReader.BlockAction(this);
  64.       HTMLDocument.HTMLReader.ParagraphAction var8 = new HTMLDocument.HTMLReader.ParagraphAction(this);
  65.       HTMLDocument.HTMLReader.CharacterAction var9 = new HTMLDocument.HTMLReader.CharacterAction(this);
  66.       HTMLDocument.HTMLReader.SpecialAction var10 = new HTMLDocument.HTMLReader.SpecialAction(this);
  67.       HTMLDocument.HTMLReader.FormAction var11 = new HTMLDocument.HTMLReader.FormAction(this);
  68.       HTMLDocument.HTMLReader.HiddenAction var12 = new HTMLDocument.HTMLReader.HiddenAction(this);
  69.       this.tagMap.put(Tag.A, new HTMLDocument.HTMLReader.AnchorAction(this));
  70.       this.tagMap.put(Tag.ADDRESS, var9);
  71.       this.tagMap.put(Tag.APPLET, var12);
  72.       this.tagMap.put(Tag.AREA, var12);
  73.       this.tagMap.put(Tag.B, var9);
  74.       this.tagMap.put(Tag.BASE, new HTMLDocument.HTMLReader.BaseAction(this));
  75.       this.tagMap.put(Tag.BASEFONT, var9);
  76.       this.tagMap.put(Tag.BIG, var9);
  77.       this.tagMap.put(Tag.BLOCKQUOTE, var7);
  78.       this.tagMap.put(Tag.BODY, var7);
  79.       this.tagMap.put(Tag.BR, var10);
  80.       this.tagMap.put(Tag.CAPTION, var7);
  81.       this.tagMap.put(Tag.CENTER, var7);
  82.       this.tagMap.put(Tag.CITE, var9);
  83.       this.tagMap.put(Tag.CODE, var9);
  84.       this.tagMap.put(Tag.DD, var7);
  85.       this.tagMap.put(Tag.DFN, var9);
  86.       this.tagMap.put(Tag.DIR, var7);
  87.       this.tagMap.put(Tag.DIV, var7);
  88.       this.tagMap.put(Tag.DL, var7);
  89.       this.tagMap.put(Tag.DT, var8);
  90.       this.tagMap.put(Tag.EM, var9);
  91.       this.tagMap.put(Tag.FONT, var9);
  92.       this.tagMap.put(Tag.FORM, var9);
  93.       this.tagMap.put(Tag.FRAME, var10);
  94.       this.tagMap.put(Tag.FRAMESET, var7);
  95.       this.tagMap.put(Tag.H1, var8);
  96.       this.tagMap.put(Tag.H2, var8);
  97.       this.tagMap.put(Tag.H3, var8);
  98.       this.tagMap.put(Tag.H4, var8);
  99.       this.tagMap.put(Tag.H5, var8);
  100.       this.tagMap.put(Tag.H6, var8);
  101.       this.tagMap.put(Tag.HEAD, var12);
  102.       this.tagMap.put(Tag.HR, var10);
  103.       this.tagMap.put(Tag.HTML, var7);
  104.       this.tagMap.put(Tag.I, var9);
  105.       this.tagMap.put(Tag.IMG, var10);
  106.       this.tagMap.put(Tag.INPUT, var11);
  107.       this.tagMap.put(Tag.ISINDEX, new HTMLDocument.HTMLReader.IsindexAction(this));
  108.       this.tagMap.put(Tag.KBD, var9);
  109.       this.tagMap.put(Tag.LI, var7);
  110.       this.tagMap.put(Tag.LINK, var12);
  111.       this.tagMap.put(Tag.MAP, var12);
  112.       this.tagMap.put(Tag.MENU, var7);
  113.       this.tagMap.put(Tag.META, var12);
  114.       this.tagMap.put(Tag.NOFRAMES, var7);
  115.       this.tagMap.put(Tag.OBJECT, var10);
  116.       this.tagMap.put(Tag.OL, var7);
  117.       this.tagMap.put(Tag.OPTION, var11);
  118.       this.tagMap.put(Tag.P, var8);
  119.       this.tagMap.put(Tag.PARAM, new HTMLDocument.HTMLReader.ObjectAction(this));
  120.       this.tagMap.put(Tag.PRE, new HTMLDocument.HTMLReader.PreAction(this));
  121.       this.tagMap.put(Tag.SAMP, var9);
  122.       this.tagMap.put(Tag.SCRIPT, var12);
  123.       this.tagMap.put(Tag.SELECT, var11);
  124.       this.tagMap.put(Tag.SMALL, var9);
  125.       this.tagMap.put(Tag.STRIKE, var9);
  126.       this.tagMap.put(Tag.S, var9);
  127.       this.tagMap.put(Tag.STRONG, var9);
  128.       this.tagMap.put(Tag.STYLE, var12);
  129.       this.tagMap.put(Tag.SUB, var9);
  130.       this.tagMap.put(Tag.SUP, var9);
  131.       this.tagMap.put(Tag.TABLE, var7);
  132.       this.tagMap.put(Tag.TD, var7);
  133.       this.tagMap.put(Tag.TEXTAREA, var11);
  134.       this.tagMap.put(Tag.TH, var7);
  135.       this.tagMap.put(Tag.TITLE, new HTMLDocument.HTMLReader.TitleAction(this));
  136.       this.tagMap.put(Tag.TR, var7);
  137.       this.tagMap.put(Tag.TT, var9);
  138.       this.tagMap.put(Tag.U, var9);
  139.       this.tagMap.put(Tag.UL, var7);
  140.       this.tagMap.put(Tag.VAR, var9);
  141.       ((AbstractDocument)var1).putProperty("AdditionalComments", (Object)null);
  142.       if (var5 != null) {
  143.          this.insertTag = var5;
  144.          this.popDepth = var3;
  145.          this.pushDepth = var4;
  146.          this.foundInsertTag = false;
  147.       } else {
  148.          this.foundInsertTag = true;
  149.       }
  150.  
  151.    }
  152.  
  153.    // $FF: synthetic method
  154.    static HTMLDocument access$0(HTMLDocument$HTMLReader var0) {
  155.       return var0.this$0;
  156.    }
  157.  
  158.    protected void addContent(char[] var1, int var2, int var3) {
  159.       this.addContent(var1, var2, var3, true);
  160.    }
  161.  
  162.    protected void addContent(char[] var1, int var2, int var3, boolean var4) {
  163.       if (this.foundInsertTag) {
  164.          if (var4 && !this.inParagraph && !this.inPre) {
  165.             this.blockOpen(Tag.IMPLIED, new SimpleAttributeSet());
  166.             this.inParagraph = true;
  167.             this.impliedP = true;
  168.          }
  169.  
  170.          this.emptyAnchor = false;
  171.          this.charAttr.addAttribute(StyleConstants.NameAttribute, Tag.CONTENT);
  172.          AttributeSet var5 = this.charAttr.copyAttributes();
  173.          DefaultStyledDocument.ElementSpec var6 = new DefaultStyledDocument.ElementSpec(var5, (short)3, var1, var2, var3);
  174.          this.parseBuffer.addElement(var6);
  175.          if (this.tableCount == 0 && this.parseBuffer.size() > this.threshold) {
  176.             try {
  177.                this.flushBuffer();
  178.             } catch (BadLocationException var7) {
  179.             }
  180.          }
  181.  
  182.          if (var3 > 0) {
  183.             this.lastWasNewline = var1[var2 + var3 - 1] == '\n';
  184.          }
  185.  
  186.       }
  187.    }
  188.  
  189.    protected void addSpecialElement(HTML.Tag var1, MutableAttributeSet var2) {
  190.       if (!this.foundInsertTag) {
  191.          if (!this.isInsertTag(var1)) {
  192.             return;
  193.          }
  194.  
  195.          this.foundInsertTag();
  196.       }
  197.  
  198.       if (var1 != Tag.FRAME && var1 != Tag.HR && !this.inParagraph && !this.inPre) {
  199.          this.blockOpen(Tag.IMPLIED, new SimpleAttributeSet());
  200.          this.inParagraph = true;
  201.          this.impliedP = true;
  202.       }
  203.  
  204.       this.emptyAnchor = false;
  205.       var2.addAttributes(this.charAttr);
  206.       var2.addAttribute(StyleConstants.NameAttribute, var1);
  207.       char[] var3 = new char[]{' '};
  208.       DefaultStyledDocument.ElementSpec var4 = new DefaultStyledDocument.ElementSpec(var2.copyAttributes(), (short)3, var3, 0, 1);
  209.       this.parseBuffer.addElement(var4);
  210.       if (var1 == Tag.FRAME) {
  211.          this.lastWasNewline = true;
  212.       }
  213.  
  214.    }
  215.  
  216.    protected void blockClose(HTML.Tag var1) {
  217.       if (this.foundInsertTag) {
  218.          if (!this.lastWasNewline) {
  219.             this.addContent(HTMLDocument.access$0(), 0, 1, this.insertTag != null);
  220.             this.lastWasNewline = true;
  221.          }
  222.  
  223.          if (this.impliedP) {
  224.             this.impliedP = false;
  225.             this.inParagraph = false;
  226.             this.blockClose(Tag.IMPLIED);
  227.          }
  228.  
  229.          --this.inBlock;
  230.          DefaultStyledDocument.ElementSpec var2 = this.parseBuffer.size() > 0 ? (DefaultStyledDocument.ElementSpec)this.parseBuffer.lastElement() : null;
  231.          if (var2 != null && var2.getType() == 1) {
  232.             char[] var3 = new char[]{' '};
  233.             this.addContent(var3, 0, 1);
  234.          }
  235.  
  236.          DefaultStyledDocument.ElementSpec var4 = new DefaultStyledDocument.ElementSpec((AttributeSet)null, (short)2);
  237.          this.parseBuffer.addElement(var4);
  238.          if (var1 == Tag.TABLE) {
  239.             --this.tableCount;
  240.          }
  241.  
  242.       }
  243.    }
  244.  
  245.    protected void blockOpen(HTML.Tag var1, MutableAttributeSet var2) {
  246.       if (var1 == Tag.TABLE) {
  247.          ++this.tableCount;
  248.       }
  249.  
  250.       if (this.impliedP) {
  251.          this.impliedP = false;
  252.          this.inParagraph = false;
  253.          this.blockClose(Tag.IMPLIED);
  254.       }
  255.  
  256.       ++this.inBlock;
  257.       if (!this.foundInsertTag) {
  258.          if (!this.isInsertTag(var1)) {
  259.             return;
  260.          }
  261.  
  262.          this.foundInsertTag();
  263.       }
  264.  
  265.       this.lastWasNewline = false;
  266.       var2.addAttribute(StyleConstants.NameAttribute, var1);
  267.       DefaultStyledDocument.ElementSpec var3 = new DefaultStyledDocument.ElementSpec(var2.copyAttributes(), (short)1);
  268.       this.parseBuffer.addElement(var3);
  269.    }
  270.  
  271.    public void flush() throws BadLocationException {
  272.       this.flushBuffer();
  273.    }
  274.  
  275.    void flushBuffer() throws BadLocationException {
  276.       int var1 = this.this$0.getLength();
  277.       DefaultStyledDocument.ElementSpec[] var2 = new DefaultStyledDocument.ElementSpec[this.parseBuffer.size()];
  278.       this.parseBuffer.copyInto(var2);
  279.       if (var1 == 0 && this.insertTag == null) {
  280.          this.this$0.create(var2);
  281.       } else {
  282.          this.this$0.insert(this.offset, var2);
  283.       }
  284.  
  285.       this.parseBuffer.removeAllElements();
  286.       this.offset += this.this$0.getLength() - var1;
  287.    }
  288.  
  289.    private void foundInsertTag() {
  290.       this.foundInsertTag = true;
  291.       if (this.popDepth > 0 || this.pushDepth > 0) {
  292.          try {
  293.             if (this.offset > 0 && !this.this$0.getText(this.offset - 1, 1).equals("\n")) {
  294.                DefaultStyledDocument.ElementSpec var1 = new DefaultStyledDocument.ElementSpec((AttributeSet)null, (short)3, HTMLDocument.access$0(), 0, HTMLDocument.access$0().length);
  295.                var1.setDirection((short)4);
  296.                this.parseBuffer.addElement(var1);
  297.             }
  298.          } catch (BadLocationException var4) {
  299.          }
  300.       }
  301.  
  302.       for(int var5 = 0; var5 < this.popDepth; ++var5) {
  303.          this.parseBuffer.addElement(new DefaultStyledDocument.ElementSpec((AttributeSet)null, (short)2));
  304.       }
  305.  
  306.       for(int var2 = 0; var2 < this.pushDepth; ++var2) {
  307.          DefaultStyledDocument.ElementSpec var3 = new DefaultStyledDocument.ElementSpec((AttributeSet)null, (short)1);
  308.          var3.setDirection((short)5);
  309.          this.parseBuffer.addElement(var3);
  310.       }
  311.  
  312.    }
  313.  
  314.    public void handleComment(char[] var1, int var2) {
  315.       if (this.this$0.getPreservesUnknownTags()) {
  316.          if (this.inBlock == 0) {
  317.             Object var4 = this.this$0.getProperty("AdditionalComments");
  318.             if (var4 != null && !(var4 instanceof Vector)) {
  319.                return;
  320.             }
  321.  
  322.             if (var4 == null) {
  323.                var4 = new Vector();
  324.                this.this$0.putProperty("AdditionalComments", var4);
  325.             }
  326.  
  327.             ((Vector)var4).addElement(new String(var1));
  328.             return;
  329.          }
  330.  
  331.          SimpleAttributeSet var3 = new SimpleAttributeSet();
  332.          var3.addAttribute(Attribute.COMMENT, new String(var1));
  333.          this.addSpecialElement(Tag.COMMENT, var3);
  334.       }
  335.  
  336.    }
  337.  
  338.    public void handleEndTag(HTML.Tag var1, int var2) {
  339.       HTMLDocument.HTMLReader.TagAction var3 = (HTMLDocument.HTMLReader.TagAction)this.tagMap.get(var1);
  340.       if (var3 != null) {
  341.          var3.end(var1);
  342.       }
  343.  
  344.    }
  345.  
  346.    public void handleSimpleTag(HTML.Tag var1, MutableAttributeSet var2, int var3) {
  347.       HTMLDocument.HTMLReader.TagAction var4 = (HTMLDocument.HTMLReader.TagAction)this.tagMap.get(var1);
  348.       if (var4 != null) {
  349.          var4.start(var1, var2);
  350.          var4.end(var1);
  351.       } else if (this.this$0.getPreservesUnknownTags()) {
  352.          this.addSpecialElement(var1, var2);
  353.       }
  354.  
  355.    }
  356.  
  357.    public void handleStartTag(HTML.Tag var1, MutableAttributeSet var2, int var3) {
  358.       HTMLDocument.HTMLReader.TagAction var4 = (HTMLDocument.HTMLReader.TagAction)this.tagMap.get(var1);
  359.       if (var4 != null) {
  360.          var4.start(var1, var2);
  361.       }
  362.  
  363.    }
  364.  
  365.    public void handleText(char[] var1, int var2) {
  366.       if (this.inTextArea) {
  367.          this.textAreaContent(var1);
  368.       } else if (this.inPre) {
  369.          this.preContent(var1);
  370.       } else if (this.inTitle) {
  371.          this.this$0.putProperty("title", new String(var1));
  372.       } else if (this.option != null) {
  373.          this.option.setLabel(new String(var1));
  374.       } else if (this.inBlock > 0 && var1.length >= 1) {
  375.          this.addContent(var1, 0, var1.length);
  376.       }
  377.  
  378.    }
  379.  
  380.    private boolean isInsertTag(HTML.Tag var1) {
  381.       return this.insertTag == var1 || var1 == Tag.IMPLIED && var1 == Tag.P;
  382.    }
  383.  
  384.    protected void popCharacterStyle() {
  385.       if (!this.charAttrStack.empty()) {
  386.          this.charAttr = (MutableAttributeSet)this.charAttrStack.peek();
  387.          this.charAttrStack.pop();
  388.       }
  389.  
  390.    }
  391.  
  392.    protected void preContent(char[] var1) {
  393.       int var2 = 0;
  394.  
  395.       for(int var3 = 0; var3 < var1.length; ++var3) {
  396.          if (var1[var3] == '\n') {
  397.             this.addContent(var1, var2, var3 - var2 + 1);
  398.             this.blockClose(Tag.IMPLIED);
  399.             SimpleAttributeSet var4 = new SimpleAttributeSet();
  400.             var4.addAttribute(javax.swing.text.html.CSS.Attribute.WHITE_SPACE, "pre");
  401.             this.blockOpen(Tag.IMPLIED, var4);
  402.             var2 = var3 + 1;
  403.          }
  404.       }
  405.  
  406.       if (var2 < var1.length) {
  407.          this.addContent(var1, var2, var1.length - var2);
  408.       }
  409.  
  410.    }
  411.  
  412.    protected void pushCharacterStyle() {
  413.       this.charAttrStack.push(this.charAttr.copyAttributes());
  414.    }
  415.  
  416.    protected void registerTag(HTML.Tag var1, HTMLDocument.HTMLReader.TagAction var2) {
  417.       this.tagMap.put(var1, var2);
  418.    }
  419.  
  420.    protected void textAreaContent(char[] var1) {
  421.       try {
  422.          this.textAreaDocument.insertString(this.textAreaDocument.getLength(), new String(var1), (AttributeSet)null);
  423.       } catch (BadLocationException var2) {
  424.       }
  425.  
  426.    }
  427. }
  428.