nl.justobjects.toolkit.xml
Class HTMLPrintVisitor

java.lang.Object
  |
  +--nl.justobjects.toolkit.xml.HTMLPrintVisitor
All Implemented Interfaces:
Visitor

public class HTMLPrintVisitor
extends java.lang.Object
implements Visitor

HTMLPrintVisitor implements the Visitor interface in the visitor design pattern for the purpose of printing in HTML-like format the various DOM-Nodes.

In HTML-like printing, only the following Nodes are printed:

Document
Only the doctype provided on this constructor is written (i.e. no XML declaration, <!DOCTYPE>, or internal DTD).
Element
All element names are uppercased.
Empty elements are written as <BR> instead of <BR/>.
Attr
All attribute names are lowercased.
Text

The following sample code uses the HTMLPrintVisitor on a hierarchy of nodes:


 PrintWriter printWriter = new PrintWriter();
 Visitor htmlPrintVisitor = new HTMLPrintVisitor(printWriter);
 TreeWalker treeWalker = new TreeWalker(htmlPrintVisitor);
 treeWalker.traverse(document);
 printWriter.close();

 

By default, this doesn't print non-specified attributes.

Version:
$Id: HTMLPrintVisitor.java,v 1.6 2003/01/06 00:23:49 just Exp $
Author:
$Author: just $ - Just van den Broecke - Just Objects B.V. ©
See Also:
Visitor, TreeWalker

Field Summary
protected  java.lang.String doctype
           
protected  int level
           
(package private) static java.util.Hashtable s_empties
           
(package private) static java.util.Hashtable s_entities
           
protected  java.io.Writer writer
           
 
Constructor Summary
HTMLPrintVisitor(java.io.Writer writer)
          Constructor for default encoding.
HTMLPrintVisitor(java.io.Writer writer, java.lang.String encoding)
          Constructor for customized encoding.
HTMLPrintVisitor(java.io.Writer writer, java.lang.String encoding, java.lang.String doctype)
          Constructor for customized encoding and doctype.
 
Method Summary
(package private) static void ()
           
private  void flush()
           
static void main(java.lang.String[] args)
          Ignore GeneralReference Nodes.
 void visitAttributePre(org.w3c.dom.Attr attr)
          Creates a formatted string representation of the specified attribute Node and its associated attributes, and directs it to the print writer.
 void visitDocumentPost(org.w3c.dom.Document document)
          Flush the writer.
 void visitDocumentPre(org.w3c.dom.Document document)
          Writes the doctype from the constructor (if any).
 void visitElementPost(org.w3c.dom.Element element)
          Creates a formatted string representation of the end of the specified element Node, and directs it to the print writer.
 void visitElementPre(org.w3c.dom.Element element)
          Creates a formatted string representation of the start of the specified element Node and its associated attributes, and directs it to the print writer.
 void visitText(org.w3c.dom.Text text)
          Creates a formatted string representation of the specified text Node, and directs it to the print writer.
private  void write(java.lang.String s)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

writer

protected java.io.Writer writer

level

protected int level

doctype

protected java.lang.String doctype

s_empties

static java.util.Hashtable s_empties

s_entities

static java.util.Hashtable s_entities
Constructor Detail

HTMLPrintVisitor

public HTMLPrintVisitor(java.io.Writer writer,
                        java.lang.String encoding,
                        java.lang.String doctype)
Constructor for customized encoding and doctype.
Parameters:
writer - The character output stream to use.
encoding - Java character encoding in use by writer.
doctype - String to be printed at the top of the document.

HTMLPrintVisitor

public HTMLPrintVisitor(java.io.Writer writer,
                        java.lang.String encoding)
Constructor for customized encoding.
Parameters:
writer - The character output stream to use.
encoding - Java character encoding in use by writer.

HTMLPrintVisitor

public HTMLPrintVisitor(java.io.Writer writer)
Constructor for default encoding.
Parameters:
writer - The character output stream to use.
Method Detail

static void ()

visitDocumentPre

public void visitDocumentPre(org.w3c.dom.Document document)
Writes the doctype from the constructor (if any).
Specified by:
visitDocumentPre in interface Visitor
Parameters:
document - Node print as HTML.

visitDocumentPost

public void visitDocumentPost(org.w3c.dom.Document document)
Flush the writer.
Specified by:
visitDocumentPost in interface Visitor
Parameters:
document - Node to print as HTML.

visitElementPre

public void visitElementPre(org.w3c.dom.Element element)
Creates a formatted string representation of the start of the specified element Node and its associated attributes, and directs it to the print writer.
Specified by:
visitElementPre in interface Visitor
Parameters:
element - Node to print as HTML.

visitElementPost

public void visitElementPost(org.w3c.dom.Element element)
Creates a formatted string representation of the end of the specified element Node, and directs it to the print writer.
Specified by:
visitElementPost in interface Visitor
Parameters:
element - Node to print as HTML.

visitAttributePre

public void visitAttributePre(org.w3c.dom.Attr attr)
Creates a formatted string representation of the specified attribute Node and its associated attributes, and directs it to the print writer.

Note that TXAttribute Nodes are not parsed into the document object hierarchy by the XML4J parser; attributes exist as part of a Element Node.

Parameters:
element - Node to print as HTML.

visitText

public void visitText(org.w3c.dom.Text text)
Creates a formatted string representation of the specified text Node, and directs it to the print writer. CDATASections are respected.
Specified by:
visitText in interface Visitor
Parameters:
text - Node to print with format.

write

private void write(java.lang.String s)

flush

private void flush()

main

public static void main(java.lang.String[] args)
Ignore GeneralReference Nodes. Print contents of GeneralReferences.
Parameters:
generalReference - CURRENTLY NOT IMPLEMENTED. public void visitGeneralReferencePre(GeneralReference generalReference) throws Exception { String name = generalReference.getName(); if (s_entities.get(name) != null && generalReference.getChildNodes().getLength() == 1) { Node child = generalReference.getFirstChild(); if (child.getNodeType() == Node.TEXT_NODE && child.getNodeValue().length() == 1) { write((char)'&' + name + (char)';'); throw new ToNextSiblingTraversalException(); } } }


Copyright © 2000-2001 - Just Objects B.V.