FileDocCategorySizeDatePackage
SimpleWriter.javaAPI DocExample1679Mon Nov 09 12:45:52 GMT 1998None

SimpleWriter

public class SimpleWriter extends AbstractWriter

Fields Summary
Constructors Summary
public SimpleWriter(Writer w, Document d)

    super(w, d);
    setLineLength(80);
    setIndentSpace(4);
  
Methods Summary
public static voidmain(java.lang.String[] args)

    DefaultStyledDocument doc = new DefaultStyledDocument();

    try {
      // Create some simple attributed text
      SimpleAttributeSet attrs1 = new SimpleAttributeSet();
      StyleConstants.setBold(attrs1, true);
      StyleConstants.setItalic(attrs1, true);
      doc.insertString(0, "Line 2\n", attrs1);

      SimpleAttributeSet attrs2 = new SimpleAttributeSet();
      StyleConstants.setForeground(attrs2, Color.black);
      StyleConstants.setBackground(attrs2, Color.gray);
      doc.insertString(0, "Line 1\n", attrs2);

      // Write out the document
      FileWriter out = new FileWriter("test.txt");
      SimpleWriter sw = new SimpleWriter(out, doc);
      sw.write();
      out.close();
    }
    catch (BadLocationException ex) {ex.printStackTrace();}
    catch (IOException ex) {ex.printStackTrace();}
  
protected voidwrite()

    ElementIterator it = getElementIterator();

    Element e = it.first();
    while (e != null) {
      write("<" + e.getName());
      writeAttributes(e.getAttributes());
      write(">");

      if (e.isLeaf()) {
        text(e);
      }
      write(NEWLINE);
      e = it.next();
    }