I l@ve RuBoard |
![]() ![]() |
23.8 Writing HTMLThe write( ) method from JEditorPane takes advantage of the writer installed as part of the HTMLEditorKit. In our previous example, that writer is the HTMLWriter class. Starting with a more generic styled document, you could also write HTML with MinimalHTMLWriter. The classes described in this section both extend from the AbstractWriter class. (See Figure 23-9.) Figure 23-9. HTML document-writing class diagram![]() 23.8.1 The AbstractWriter ClassIn this chapter, we've talked about a variety of strategies for saving document content. As of SDK 1.2, a new class provides some assistance in creating a rendition of an in-memory document structure suitable for saving as human-readable text. It relies on the ElementIterator class. AbstractWriter supports indentation to clarify the document structure as well as maximum line length to keep the generated output easy to read. ElementIterator is a simple iterator class (somewhat obviously) devoted to working with Element objects. It has the usual next( ) and previous( ) methods of any bidirectional iterator. Both return objects of type Element. Unlike the new iterators in the Collections API, there is no hasNext( ) method. Instead, next( ) or previous( ) return null to signal the "end" of the stream. As with the constructors for AbstractWriter, an ElementIterator can be built on a Document or start from a particular Element. This class is covered in more depth in Chapter 22. 23.8.1.1 PropertiesAbstractWriter defines the properties shown in Table 23-14.
Several properties are read-only and simply help describe the state of the current writer. The document itself is available through the document property. The elementIterator, endOffset, indentLevel, startOffset, and writer properties all give you information about the writer (and a reference to the writer itself). If you are writing the entire document, startOffset is 0, and endOffset is the length of the document. The other read-only property, lineEmpty, can tell you if the current line the writer is working on is empty. The mutable properties help you configure how the output looks. The canWrapLines and lineLength let you dictate how long lines are "rendered" when written. If canWrapLines is true, and currentLineLength is greater than lineLength, the text is wrapped. How much space is used for each level of indentation is controlled by the indentSpace property. Likewise, the lineSeparator property dictates the line ending used during output. 23.8.1.2 ConstantTable 23-15 shows the single, protected constant defined by AbstractWriter.
23.8.1.3 Constructors
23.8.1.4 Output-generating methodsThe following methods write text to the Writer supplied in the constructor:
23.8.1.5 Formatting methods
23.8.2 The HTMLWriter ClassThe HTMLWriter class is a fairly intricate example of extending AbstractWriter. HTMLWriter outputs HTMLDocument objects to a stream. The writer keeps all the tags (even ones it doesn't understand) intact and produces reasonably formatted HTML. Tags carry attributes exactly as you would expect. All attributes are enclosed in double quotes (<tag attribute="value">) to keep embedded spaces of attribute values intact and to conform to the HTML specification. 23.8.2.1 ConstructorsTwo constructors build a writer from a given HTMLDocument:
23.8.2.2 Writing methodOnly one public writing method exists:
23.8.3 MinimalHTMLWriterIf you have a simple StyledDocument that was not initially an HTML document, you can still write it as HTML using the MinimalHTMLWriter. For example, we could add a "Save As" option to our StyledEditor class to output the document as HTML in addition to the usual method. Here's the code for the new Action: class SaveAsHtmlAction extends AbstractAction { public SaveAsHtmlAction( ) { super("Save As...", null); } // Query user for a filename and attempt to open and write the text // component's content to the file. public void actionPerformed(ActionEvent ev) { JFileChooser chooser = new JFileChooser( ); if (chooser.showSaveDialog(StyledEditor.this) != JFileChooser.APPROVE_OPTION) return; File file = chooser.getSelectedFile( ); if (file == null) return; FileWriter writer = null; try { writer = new FileWriter(file); MinimalHTMLWriter htmlWriter = new MinimalHTMLWriter(writer, (StyledDocument)textComp.getDocument( )); htmlWriter.write( ); } catch (IOException ex) { JOptionPane.showMessageDialog(StyledEditor.this, "HTML File Not Saved", "ERROR", JOptionPane.ERROR_MESSAGE); } catch (BadLocationException ex) { JOptionPane.showMessageDialog(StyledEditor.this, "HTML File Corrupt", "ERROR", JOptionPane.ERROR_MESSAGE); } finally { if (writer != null) { try { writer.close( ); } catch (IOException x) {} } } } } You should see two substantive changes to the SaveAction class from the previous example. First, rather than let the textComp object (an instance of JEditorPane) write itself out, we create our MinimalHTMLWriter from a FileWriter object and the document from our textComp. The write( ) method of the htmlWriter( ) spits out an HTML version of our document. Unfortunately, you still have to be careful with this writer—it's not perfect yet. Figure 23-10 shows a screenshot of a simple document in our styled editor and the same document as HTML displayed inside a browser. Note that the HTML version is not quite perfect, as you can see from the fact that it displays differently in the browser than it does in our editor. Figure 23-10. The StyledEditor document saved (not perfectly) as HTML and displayed in a browser![]() 23.8.3.1 ConstructorsMinimalHTMLWriter has two constructors:
23.8.3.2 Public writing methodMinimalHTMLWriter has one public method for starting the writing process:
|
I l@ve RuBoard |
![]() ![]() |