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

  1. package javax.swing.text.html;
  2.  
  3. import java.io.IOException;
  4. import java.io.Writer;
  5. import java.util.Enumeration;
  6. import java.util.NoSuchElementException;
  7. import java.util.Stack;
  8. import java.util.StringTokenizer;
  9. import java.util.Vector;
  10. import javax.swing.DefaultComboBoxModel;
  11. import javax.swing.DefaultListModel;
  12. import javax.swing.text.AbstractDocument;
  13. import javax.swing.text.AbstractWriter;
  14. import javax.swing.text.AttributeSet;
  15. import javax.swing.text.BadLocationException;
  16. import javax.swing.text.Document;
  17. import javax.swing.text.Element;
  18. import javax.swing.text.ElementIterator;
  19. import javax.swing.text.StyleConstants;
  20. import javax.swing.text.html.HTML.Attribute;
  21. import javax.swing.text.html.HTML.Tag;
  22.  
  23. public class HTMLWriter extends AbstractWriter {
  24.    private Stack blockElementStack = new Stack();
  25.    private boolean inContent = false;
  26.    private boolean inPre = false;
  27.    private boolean inTextArea = false;
  28.    private boolean newlineOutputed = false;
  29.    private boolean completeDoc;
  30.    private Vector tags = new Vector(10);
  31.    private boolean indentNext = false;
  32.  
  33.    public HTMLWriter(Writer var1, HTMLDocument var2) {
  34.       super(var1, var2);
  35.       this.completeDoc = true;
  36.    }
  37.  
  38.    public HTMLWriter(Writer var1, HTMLDocument var2, int var3, int var4) {
  39.       super(var1, var2, var3, var4);
  40.       this.completeDoc = var3 == 0 && var4 == ((AbstractDocument)var2).getLength();
  41.    }
  42.  
  43.    protected void closeOutUnwantedEmbeddedTags(AttributeSet var1) throws IOException {
  44.       for(int var3 = this.tags.size() - 1; var3 >= 0; --var3) {
  45.          HTML.Tag var2 = (HTML.Tag)this.tags.elementAt(var3);
  46.          if (var1 == null || this.noMatchForTagInAttributes(var1, var2)) {
  47.             this.tags.removeElementAt(var3);
  48.             ((AbstractWriter)this).write('<');
  49.             ((AbstractWriter)this).write('/');
  50.             this.write(var2.toString());
  51.             ((AbstractWriter)this).write('>');
  52.          }
  53.       }
  54.  
  55.    }
  56.  
  57.    protected void comment(Element var1) throws BadLocationException, IOException {
  58.       AttributeSet var2 = var1.getAttributes();
  59.       if (this.matchNameAttribute(var2, Tag.COMMENT)) {
  60.          Object var3 = var2.getAttribute(Attribute.COMMENT);
  61.          if (var3 instanceof String) {
  62.             this.writeComment((String)var3);
  63.          } else {
  64.             this.writeComment((String)null);
  65.          }
  66.       }
  67.  
  68.    }
  69.  
  70.    protected void emptyTag(Element var1) throws BadLocationException, IOException {
  71.       if (!this.inContent && !this.inPre) {
  72.          ((AbstractWriter)this).indent();
  73.       }
  74.  
  75.       AttributeSet var2 = var1.getAttributes();
  76.       this.closeOutUnwantedEmbeddedTags(var2);
  77.       this.writeEmbeddedTags(var2);
  78.       if (this.matchNameAttribute(var2, Tag.CONTENT)) {
  79.          this.inContent = true;
  80.          this.text(var1);
  81.       } else if (this.matchNameAttribute(var2, Tag.COMMENT)) {
  82.          this.comment(var1);
  83.       } else {
  84.          boolean var3 = this.isBlockTag(var1.getAttributes());
  85.          if (this.inContent && var3) {
  86.             ((AbstractWriter)this).write('\n');
  87.             ((AbstractWriter)this).indent();
  88.          }
  89.  
  90.          ((AbstractWriter)this).write('<');
  91.          Object var4 = var2 != null ? var2.getAttribute(StyleConstants.NameAttribute) : null;
  92.          Object var5 = var2 != null ? var2.getAttribute(Attribute.ENDTAG) : null;
  93.          boolean var6 = false;
  94.          if (var4 != null && var5 != null && var5 instanceof String && ((String)var5).equals("true")) {
  95.             var6 = true;
  96.             ((AbstractWriter)this).write('/');
  97.          }
  98.  
  99.          this.write(var1.getName());
  100.          this.writeAttributes(var2);
  101.          ((AbstractWriter)this).write('>');
  102.          if (this.matchNameAttribute(var2, Tag.TITLE) && !var6) {
  103.             Document var7 = var1.getDocument();
  104.             String var8 = (String)var7.getProperty("title");
  105.             this.write(var8);
  106.          } else if (!this.inContent || var3) {
  107.             ((AbstractWriter)this).write('\n');
  108.             if (var3 && this.inContent) {
  109.                ((AbstractWriter)this).indent();
  110.             }
  111.          }
  112.       }
  113.  
  114.    }
  115.  
  116.    protected void endTag(Element var1) throws IOException {
  117.       if (!this.synthesizedElement(var1)) {
  118.          if (this.matchNameAttribute(var1.getAttributes(), Tag.PRE)) {
  119.             this.inPre = false;
  120.          }
  121.  
  122.          this.closeOutUnwantedEmbeddedTags(var1.getAttributes());
  123.          if (this.inContent) {
  124.             if (!this.newlineOutputed) {
  125.                ((AbstractWriter)this).write('\n');
  126.             }
  127.  
  128.             this.newlineOutputed = false;
  129.             this.inContent = false;
  130.          }
  131.  
  132.          ((AbstractWriter)this).indent();
  133.          ((AbstractWriter)this).write('<');
  134.          ((AbstractWriter)this).write('/');
  135.          this.write(var1.getName());
  136.          ((AbstractWriter)this).write('>');
  137.          ((AbstractWriter)this).write('\n');
  138.       }
  139.    }
  140.  
  141.    private boolean indentNeedsIncrementing(Element var1, Element var2) {
  142.       if (var2.getParentElement() == var1 && !this.inPre) {
  143.          if (this.indentNext) {
  144.             this.indentNext = false;
  145.             return true;
  146.          }
  147.  
  148.          if (this.synthesizedElement(var2)) {
  149.             this.indentNext = true;
  150.          } else if (!this.synthesizedElement(var1)) {
  151.             return true;
  152.          }
  153.       }
  154.  
  155.       return false;
  156.    }
  157.  
  158.    protected boolean isBlockTag(AttributeSet var1) {
  159.       Object var2 = var1.getAttribute(StyleConstants.NameAttribute);
  160.       if (var2 instanceof HTML.Tag) {
  161.          HTML.Tag var3 = (HTML.Tag)var2;
  162.          return var3.isBlock();
  163.       } else {
  164.          return false;
  165.       }
  166.    }
  167.  
  168.    private boolean isFormElementWithContent(AttributeSet var1) {
  169.       return this.matchNameAttribute(var1, Tag.TEXTAREA) || this.matchNameAttribute(var1, Tag.SELECT);
  170.    }
  171.  
  172.    protected boolean matchNameAttribute(AttributeSet var1, HTML.Tag var2) {
  173.       Object var3 = var1.getAttribute(StyleConstants.NameAttribute);
  174.       if (var3 instanceof HTML.Tag) {
  175.          HTML.Tag var4 = (HTML.Tag)var3;
  176.          if (var4 == var2) {
  177.             return true;
  178.          }
  179.       }
  180.  
  181.       return false;
  182.    }
  183.  
  184.    private boolean noMatchForTagInAttributes(AttributeSet var1, HTML.Tag var2) {
  185.       if (var1 != null) {
  186.          Enumeration var3 = var1.getAttributeNames();
  187.  
  188.          while(var3.hasMoreElements()) {
  189.             Object var4 = var3.nextElement();
  190.             if (var4 instanceof HTML.Tag && var4 == var2) {
  191.                return false;
  192.             }
  193.          }
  194.       }
  195.  
  196.       return true;
  197.    }
  198.  
  199.    protected void selectContent(AttributeSet var1) throws IOException {
  200.       Object var2 = var1.getAttribute(StyleConstants.ModelAttribute);
  201.       ((AbstractWriter)this).incrIndent();
  202.       if (var2 instanceof OptionListModel) {
  203.          OptionListModel var3 = (OptionListModel)var2;
  204.          int var4 = ((DefaultListModel)var3).getSize();
  205.  
  206.          for(int var5 = 0; var5 < var4; ++var5) {
  207.             Option var6 = (Option)((DefaultListModel)var3).getElementAt(var5);
  208.             this.writeOption(var6);
  209.          }
  210.       } else if (var2 instanceof OptionComboBoxModel) {
  211.          OptionComboBoxModel var7 = (OptionComboBoxModel)var2;
  212.          int var8 = ((DefaultComboBoxModel)var7).getSize();
  213.  
  214.          for(int var9 = 0; var9 < var8; ++var9) {
  215.             Option var10 = (Option)((DefaultComboBoxModel)var7).getElementAt(var9);
  216.             this.writeOption(var10);
  217.          }
  218.       }
  219.  
  220.       ((AbstractWriter)this).decrIndent();
  221.    }
  222.  
  223.    protected void startTag(Element var1) throws IOException, BadLocationException {
  224.       if (!this.synthesizedElement(var1)) {
  225.          if (this.matchNameAttribute(var1.getAttributes(), Tag.PRE)) {
  226.             this.inPre = true;
  227.          }
  228.  
  229.          AttributeSet var2 = var1.getAttributes();
  230.          this.closeOutUnwantedEmbeddedTags(var2);
  231.          if (this.inContent) {
  232.             ((AbstractWriter)this).write('\n');
  233.             this.inContent = false;
  234.             this.newlineOutputed = false;
  235.          }
  236.  
  237.          ((AbstractWriter)this).indent();
  238.          ((AbstractWriter)this).write('<');
  239.          this.write(var1.getName());
  240.          this.writeAttributes(var2);
  241.          ((AbstractWriter)this).write('>');
  242.          ((AbstractWriter)this).write('\n');
  243.          if (this.matchNameAttribute(var1.getAttributes(), Tag.TEXTAREA)) {
  244.             this.textAreaContent(var1.getAttributes());
  245.          } else if (this.matchNameAttribute(var1.getAttributes(), Tag.SELECT)) {
  246.             this.selectContent(var1.getAttributes());
  247.          }
  248.  
  249.       }
  250.    }
  251.  
  252.    protected boolean synthesizedElement(Element var1) {
  253.       return this.matchNameAttribute(var1.getAttributes(), Tag.IMPLIED);
  254.    }
  255.  
  256.    protected void text(Element var1) throws BadLocationException, IOException {
  257.       String var2 = ((AbstractWriter)this).getText(var1);
  258.       this.newlineOutputed = false;
  259.       if (var2.length() > 0) {
  260.          this.write(var2);
  261.          if (var2.endsWith("\n")) {
  262.             this.newlineOutputed = true;
  263.          }
  264.       }
  265.  
  266.    }
  267.  
  268.    protected void textAreaContent(AttributeSet var1) throws BadLocationException, IOException {
  269.       Document var2 = (Document)var1.getAttribute(StyleConstants.ModelAttribute);
  270.       if (var2 != null && var2.getLength() > 0) {
  271.          String var3 = var2.getText(0, var2.getLength());
  272.          if (var3 != null) {
  273.             this.inTextArea = true;
  274.             ((AbstractWriter)this).incrIndent();
  275.             ((AbstractWriter)this).indent();
  276.             this.write(var3);
  277.             ((AbstractWriter)this).write('\n');
  278.             this.inTextArea = false;
  279.             ((AbstractWriter)this).decrIndent();
  280.          }
  281.       }
  282.  
  283.    }
  284.  
  285.    public void write() throws IOException, BadLocationException {
  286.       ElementIterator var1 = ((AbstractWriter)this).getElementIterator();
  287.       Element var2 = null;
  288.       Object var3 = null;
  289.  
  290.       while((var6 = var1.next()) != null) {
  291.          if (((AbstractWriter)this).inRange(var6)) {
  292.             if (var2 != null) {
  293.                if (this.indentNeedsIncrementing(var2, var6)) {
  294.                   ((AbstractWriter)this).incrIndent();
  295.                } else if (var2.getParentElement() != var6.getParentElement()) {
  296.                   for(Element var7 = (Element)this.blockElementStack.peek(); var7 != var6.getParentElement(); var7 = (Element)this.blockElementStack.peek()) {
  297.                      this.blockElementStack.pop();
  298.                      if (!this.synthesizedElement(var7)) {
  299.                         if (!this.matchNameAttribute(var7.getAttributes(), Tag.PRE)) {
  300.                            ((AbstractWriter)this).decrIndent();
  301.                         }
  302.  
  303.                         this.endTag(var7);
  304.                      }
  305.                   }
  306.                } else if (var2.getParentElement() == var6.getParentElement()) {
  307.                   Element var4 = (Element)this.blockElementStack.peek();
  308.                   if (var4 == var2) {
  309.                      this.blockElementStack.pop();
  310.                      this.endTag(var4);
  311.                   }
  312.                }
  313.             }
  314.  
  315.             if (var6.isLeaf() && !this.isFormElementWithContent(var6.getAttributes())) {
  316.                this.emptyTag(var6);
  317.             } else {
  318.                this.blockElementStack.push(var6);
  319.                this.startTag(var6);
  320.             }
  321.  
  322.             var2 = var6;
  323.          }
  324.       }
  325.  
  326.       this.closeOutUnwantedEmbeddedTags((AttributeSet)null);
  327.  
  328.       while(!this.blockElementStack.empty()) {
  329.          var2 = (Element)this.blockElementStack.pop();
  330.          if (!this.synthesizedElement(var2)) {
  331.             if (!this.matchNameAttribute(var2.getAttributes(), Tag.PRE)) {
  332.                ((AbstractWriter)this).decrIndent();
  333.             }
  334.  
  335.             this.endTag(var2);
  336.          }
  337.       }
  338.  
  339.       if (this.completeDoc) {
  340.          this.writeAdditionalComments();
  341.       }
  342.  
  343.    }
  344.  
  345.    protected void write(String var1) throws IOException {
  346.       if (this.inTextArea) {
  347.          StringTokenizer var2 = new StringTokenizer(var1, "\n");
  348.  
  349.          try {
  350.             while(var2.hasMoreTokens()) {
  351.                super.write(var2.nextToken());
  352.                if (var2.countTokens() != 0) {
  353.                   ((AbstractWriter)this).write('\n');
  354.                   ((AbstractWriter)this).indent();
  355.                }
  356.             }
  357.          } catch (NoSuchElementException var3) {
  358.          }
  359.       } else {
  360.          super.write(var1);
  361.       }
  362.  
  363.    }
  364.  
  365.    void writeAdditionalComments() throws IOException {
  366.       Object var1 = ((AbstractWriter)this).getDocument().getProperty("AdditionalComments");
  367.       if (var1 instanceof Vector) {
  368.          Vector var2 = (Vector)var1;
  369.          int var3 = 0;
  370.  
  371.          for(int var4 = var2.size(); var3 < var4; ++var3) {
  372.             this.writeComment(var2.elementAt(var3).toString());
  373.          }
  374.       }
  375.  
  376.    }
  377.  
  378.    protected void writeAttributes(AttributeSet var1) throws IOException {
  379.       Enumeration var2 = var1.getAttributeNames();
  380.  
  381.       while(var2.hasMoreElements()) {
  382.          Object var3 = var2.nextElement();
  383.          if (!(var3 instanceof HTML.Tag) && var3 != StyleConstants.NameAttribute && var3 != Attribute.ENDTAG && var3 != StyleConstants.ModelAttribute) {
  384.             this.write(" " + var3 + "=" + var1.getAttribute(var3));
  385.          }
  386.       }
  387.  
  388.    }
  389.  
  390.    void writeComment(String var1) throws IOException {
  391.       ((AbstractWriter)this).write('<');
  392.       ((AbstractWriter)this).write('!');
  393.       ((AbstractWriter)this).write('-');
  394.       ((AbstractWriter)this).write('-');
  395.       if (var1 != null) {
  396.          this.write(var1);
  397.       }
  398.  
  399.       ((AbstractWriter)this).write('-');
  400.       ((AbstractWriter)this).write('-');
  401.       ((AbstractWriter)this).write('>');
  402.       ((AbstractWriter)this).write('\n');
  403.    }
  404.  
  405.    protected void writeEmbeddedTags(AttributeSet var1) throws IOException {
  406.       Enumeration var2 = var1.getAttributeNames();
  407.  
  408.       while(var2.hasMoreElements()) {
  409.          Object var3 = var2.nextElement();
  410.          if (var3 instanceof HTML.Tag) {
  411.             HTML.Tag var4 = (HTML.Tag)var3;
  412.             if (var4 != Tag.FORM && !this.tags.contains(var4)) {
  413.                ((AbstractWriter)this).write('<');
  414.                this.write(var4.toString());
  415.                Object var5 = var1.getAttribute(var4);
  416.                if (var5 != null && var5 instanceof AttributeSet) {
  417.                   this.writeAttributes((AttributeSet)var5);
  418.                }
  419.  
  420.                ((AbstractWriter)this).write('>');
  421.                this.tags.addElement(var4);
  422.             }
  423.          }
  424.       }
  425.  
  426.    }
  427.  
  428.    protected void writeOption(Option var1) throws IOException {
  429.       ((AbstractWriter)this).indent();
  430.       ((AbstractWriter)this).write('<');
  431.       this.write("option ");
  432.       if (var1.getValue() != null) {
  433.          this.write("value=" + var1.getValue());
  434.       }
  435.  
  436.       if (var1.isSelected()) {
  437.          this.write(" selected");
  438.       }
  439.  
  440.       ((AbstractWriter)this).write('>');
  441.       if (var1.getLabel() != null) {
  442.          this.write(var1.getLabel());
  443.       }
  444.  
  445.       ((AbstractWriter)this).write('\n');
  446.    }
  447. }
  448.