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

  1. package javax.swing.text.html;
  2.  
  3. import java.awt.Color;
  4. import java.io.IOException;
  5. import java.io.Writer;
  6. import java.util.Enumeration;
  7. import javax.swing.text.AbstractDocument;
  8. import javax.swing.text.AbstractWriter;
  9. import javax.swing.text.AttributeSet;
  10. import javax.swing.text.BadLocationException;
  11. import javax.swing.text.DefaultStyledDocument;
  12. import javax.swing.text.Element;
  13. import javax.swing.text.ElementIterator;
  14. import javax.swing.text.Style;
  15. import javax.swing.text.StyleConstants;
  16. import javax.swing.text.StyleContext;
  17. import javax.swing.text.StyledDocument;
  18.  
  19. public class MinimalHTMLWriter extends AbstractWriter {
  20.    private static final int BOLD = 1;
  21.    private static final int ITALIC = 2;
  22.    private static final int UNDERLINE = 4;
  23.    private int fontMask = 0;
  24.    int startOffset = 0;
  25.    int endOffset = 0;
  26.    private AttributeSet fontAttributes;
  27.  
  28.    public MinimalHTMLWriter(Writer var1, StyledDocument var2) {
  29.       super(var1, var2);
  30.    }
  31.  
  32.    public MinimalHTMLWriter(Writer var1, StyledDocument var2, int var3, int var4) {
  33.       super(var1, var2, var3, var4);
  34.    }
  35.  
  36.    protected void endFontTag() throws IOException {
  37.       ((AbstractWriter)this).write('\n');
  38.       this.writeEndTag("</font>");
  39.       this.fontAttributes = null;
  40.    }
  41.  
  42.    protected boolean inFontTag() {
  43.       return this.fontAttributes != null;
  44.    }
  45.  
  46.    protected boolean isText(Element var1) {
  47.       return var1.getName() == "content";
  48.    }
  49.  
  50.    private void setFontMask(AttributeSet var1) {
  51.       if (StyleConstants.isBold(var1)) {
  52.          this.fontMask |= 1;
  53.       }
  54.  
  55.       if (StyleConstants.isItalic(var1)) {
  56.          this.fontMask |= 2;
  57.       }
  58.  
  59.       if (StyleConstants.isUnderline(var1)) {
  60.          this.fontMask |= 4;
  61.       }
  62.  
  63.    }
  64.  
  65.    protected void startFontTag(String var1) throws IOException {
  66.       boolean var2 = false;
  67.       if (this.inFontTag()) {
  68.          this.endFontTag();
  69.          var2 = true;
  70.       }
  71.  
  72.       this.writeStartTag("<font style=\"" + var1 + "\">");
  73.       if (var2) {
  74.          ((AbstractWriter)this).indent();
  75.       }
  76.  
  77.    }
  78.  
  79.    protected void text(Element var1) throws IOException, BadLocationException {
  80.       String var2 = ((AbstractWriter)this).getText(var1);
  81.       if (var2.length() > 0 && var2.charAt(var2.length() - 1) == '\n') {
  82.          var2 = var2.substring(0, var2.length() - 1);
  83.       }
  84.  
  85.       if (var2.length() > 0) {
  86.          ((AbstractWriter)this).write(var2);
  87.       }
  88.  
  89.    }
  90.  
  91.    public void write() throws IOException, BadLocationException {
  92.       this.writeStartTag("<html>");
  93.       this.writeHeader();
  94.       this.writeBody();
  95.       this.writeEndTag("</html>");
  96.    }
  97.  
  98.    protected void writeAttributes(AttributeSet var1) throws IOException {
  99.       Enumeration var2 = var1.getAttributeNames();
  100.  
  101.       while(var2.hasMoreElements()) {
  102.          Object var3 = var2.nextElement();
  103.          if (var3 instanceof StyleConstants.ParagraphConstants || var3 instanceof StyleConstants.CharacterConstants || var3 instanceof StyleConstants.FontConstants || var3 instanceof StyleConstants.ColorConstants) {
  104.             ((AbstractWriter)this).indent();
  105.             ((AbstractWriter)this).write(var3.toString());
  106.             ((AbstractWriter)this).write(':');
  107.             ((AbstractWriter)this).write(var1.getAttribute(var3).toString());
  108.             ((AbstractWriter)this).write(';');
  109.             ((AbstractWriter)this).write('\n');
  110.          }
  111.       }
  112.  
  113.    }
  114.  
  115.    protected void writeBody() throws IOException, BadLocationException {
  116.       ElementIterator var1 = ((AbstractWriter)this).getElementIterator();
  117.       var1.current();
  118.       Object var2 = null;
  119.       this.writeStartTag("<body>");
  120.       boolean var3 = false;
  121.  
  122.       while((var4 = var1.next()) != null) {
  123.          if (((AbstractWriter)this).inRange(var4)) {
  124.             if (var4 instanceof AbstractDocument.BranchElement) {
  125.                if (var3) {
  126.                   this.writeEndParagraph();
  127.                   var3 = false;
  128.                   this.fontMask = 0;
  129.                }
  130.  
  131.                this.writeStartParagraph(var4);
  132.             } else if (this.isText(var4)) {
  133.                this.writeContent(var4, var3 ^ true);
  134.                var3 = true;
  135.             } else {
  136.                this.writeLeaf(var4);
  137.                var3 = true;
  138.             }
  139.          }
  140.       }
  141.  
  142.       if (var3) {
  143.          this.writeEndParagraph();
  144.       }
  145.  
  146.       this.writeEndTag("</body>");
  147.    }
  148.  
  149.    protected void writeComponent(Element var1) throws IOException {
  150.    }
  151.  
  152.    protected void writeContent(Element var1, boolean var2) throws IOException, BadLocationException {
  153.       AttributeSet var3 = var1.getAttributes();
  154.       this.writeNonHTMLAttributes(var3);
  155.       if (var2) {
  156.          ((AbstractWriter)this).indent();
  157.       }
  158.  
  159.       this.writeHTMLTags(var3);
  160.       this.text(var1);
  161.    }
  162.  
  163.    private void writeEndMask(int var1) throws IOException {
  164.       if (var1 != 0) {
  165.          if ((var1 & 1) != 0) {
  166.             ((AbstractWriter)this).write("</b>");
  167.          }
  168.  
  169.          if ((var1 & 2) != 0) {
  170.             ((AbstractWriter)this).write("</i>");
  171.          }
  172.  
  173.          if ((var1 & 4) != 0) {
  174.             ((AbstractWriter)this).write("</u>");
  175.          }
  176.       }
  177.  
  178.    }
  179.  
  180.    protected void writeEndParagraph() throws IOException {
  181.       this.writeEndMask(this.fontMask);
  182.       if (this.inFontTag()) {
  183.          this.endFontTag();
  184.       } else {
  185.          ((AbstractWriter)this).write('\n');
  186.       }
  187.  
  188.       this.writeEndTag("</p>");
  189.    }
  190.  
  191.    protected void writeEndTag(String var1) throws IOException {
  192.       ((AbstractWriter)this).decrIndent();
  193.       ((AbstractWriter)this).indent();
  194.       ((AbstractWriter)this).write(var1);
  195.       ((AbstractWriter)this).write('\n');
  196.    }
  197.  
  198.    protected void writeHTMLTags(AttributeSet var1) throws IOException {
  199.       int var2 = this.fontMask;
  200.       this.setFontMask(var1);
  201.       int var3 = 0;
  202.       int var4 = 0;
  203.       if ((var2 & 1) != 0) {
  204.          if ((this.fontMask & 1) == 0) {
  205.             var3 |= 1;
  206.          }
  207.       } else if ((this.fontMask & 1) != 0) {
  208.          var4 |= 1;
  209.       }
  210.  
  211.       if ((var2 & 2) != 0) {
  212.          if ((this.fontMask & 2) == 0) {
  213.             var3 |= 2;
  214.          }
  215.       } else if ((this.fontMask & 2) != 0) {
  216.          var4 |= 2;
  217.       }
  218.  
  219.       if ((var2 & 4) != 0) {
  220.          if ((this.fontMask & 4) == 0) {
  221.             var3 |= 4;
  222.          }
  223.       } else if ((this.fontMask & 4) != 0) {
  224.          var4 |= 4;
  225.       }
  226.  
  227.       this.writeEndMask(var3);
  228.       this.writeStartMask(var4);
  229.    }
  230.  
  231.    protected void writeHeader() throws IOException {
  232.       this.writeStartTag("<head>");
  233.       this.writeStartTag("<style>");
  234.       this.writeStartTag("<!--");
  235.       this.writeStyles();
  236.       this.writeEndTag("-->");
  237.       this.writeEndTag("</style>");
  238.       this.writeEndTag("</head>");
  239.    }
  240.  
  241.    protected void writeImage(Element var1) throws IOException {
  242.    }
  243.  
  244.    protected void writeLeaf(Element var1) throws IOException {
  245.       ((AbstractWriter)this).indent();
  246.       if (var1.getName() == "icon") {
  247.          this.writeImage(var1);
  248.       } else if (var1.getName() == "component") {
  249.          this.writeComponent(var1);
  250.       }
  251.  
  252.    }
  253.  
  254.    protected void writeNonHTMLAttributes(AttributeSet var1) throws IOException {
  255.       String var2 = "";
  256.       String var3 = "; ";
  257.       if (!this.inFontTag() || !this.fontAttributes.isEqual(var1)) {
  258.          Color var4 = (Color)var1.getAttribute(StyleConstants.Foreground);
  259.          if (var4 != null) {
  260.             var2 = var2 + "color: " + var4 + var3;
  261.          }
  262.  
  263.          Integer var5 = (Integer)var1.getAttribute(StyleConstants.FontSize);
  264.          if (var5 != null) {
  265.             var2 = var2 + "font-size: " + var5 + var3;
  266.          }
  267.  
  268.          String var6 = (String)var1.getAttribute(StyleConstants.FontFamily);
  269.          if (var6 != null) {
  270.             var2 = var2 + "font-family: " + var6 + var3;
  271.          }
  272.  
  273.          if (var2.length() > 0) {
  274.             this.startFontTag(var2);
  275.             this.fontAttributes = var1;
  276.          }
  277.  
  278.       }
  279.    }
  280.  
  281.    private void writeStartMask(int var1) throws IOException {
  282.       if (var1 != 0) {
  283.          if ((var1 & 4) != 0) {
  284.             ((AbstractWriter)this).write("<u>");
  285.          }
  286.  
  287.          if ((var1 & 2) != 0) {
  288.             ((AbstractWriter)this).write("<i>");
  289.          }
  290.  
  291.          if ((var1 & 1) != 0) {
  292.             ((AbstractWriter)this).write("<b>");
  293.          }
  294.       }
  295.  
  296.    }
  297.  
  298.    protected void writeStartParagraph(Element var1) throws IOException {
  299.       AttributeSet var2 = var1.getAttributes();
  300.       Object var3 = var2.getAttribute(StyleConstants.ResolveAttribute);
  301.       if (var3 instanceof StyleContext.NamedStyle) {
  302.          this.writeStartTag("<p class=" + ((StyleContext.NamedStyle)var3).getName() + ">");
  303.       } else {
  304.          this.writeStartTag("<p>");
  305.       }
  306.  
  307.    }
  308.  
  309.    protected void writeStartTag(String var1) throws IOException {
  310.       ((AbstractWriter)this).indent();
  311.       ((AbstractWriter)this).write(var1);
  312.       ((AbstractWriter)this).write('\n');
  313.       ((AbstractWriter)this).incrIndent();
  314.    }
  315.  
  316.    protected void writeStyles() throws IOException {
  317.       DefaultStyledDocument var1 = (DefaultStyledDocument)((AbstractWriter)this).getDocument();
  318.       Enumeration var2 = var1.getStyleNames();
  319.  
  320.       while(var2.hasMoreElements()) {
  321.          Style var3 = var1.getStyle((String)var2.nextElement());
  322.          if (var3.getAttributeCount() != 1 || !var3.isDefined(StyleConstants.NameAttribute)) {
  323.             ((AbstractWriter)this).indent();
  324.             ((AbstractWriter)this).write("p." + var3.getName());
  325.             ((AbstractWriter)this).write(" {\n");
  326.             ((AbstractWriter)this).incrIndent();
  327.             this.writeAttributes(var3);
  328.             ((AbstractWriter)this).decrIndent();
  329.             ((AbstractWriter)this).indent();
  330.             ((AbstractWriter)this).write("}\n");
  331.          }
  332.       }
  333.  
  334.    }
  335. }
  336.