uk.ac.bham.gloss
Class XRPrinter

java.lang.Object
  extended by uk.ac.bham.gloss.XRNormalizer
      extended by uk.ac.bham.gloss.XRPrinter

public class XRPrinter
extends XRNormalizer

This is the class representing objects that take a DOM with XR mark-up and prints xml to an output stream. This class is currently being developed/simplified/tidied up. Essential methods are near the top. Methods that should be removed near the bottom. Copyright Richard Kaye 2007-8 for GLOSS, http://gloss.bham.ac.uk Usage permitted according to the GPL. No waranty.


Field Summary
protected  java.lang.String idAttrName
          The name of any ID attribute other than the standard xml:id, null if none
protected  java.lang.String idAttrNS
          The namespace of any ID attribute other than the standard xml:id, null if none
 
Fields inherited from class uk.ac.bham.gloss.XRNormalizer
xmlnsURI, xmlURI, xrPrefix, xrURI
 
Constructor Summary
XRPrinter(java.io.OutputStream os)
          Constructor: specify the output stream for documents to go to.
 
Method Summary
protected static java.lang.String escapeComment(java.lang.String buff)
          Returns a version of buff that can be placed inside a comment.
protected static java.lang.String escapePI(java.lang.String buff)
          Returns a version of buff that can be placed inside a comment.
protected  org.w3c.dom.Element findLocalNodePath(org.w3c.dom.Element node, java.lang.String nodePath)
          find the element in this document corresponding to nodePath starting at node
protected  org.w3c.dom.Element findNodeID(org.w3c.dom.Element node, java.lang.String id, boolean local)
          find the element in this document with the given ID
protected  org.w3c.dom.Element findNodePath(org.w3c.dom.Element node, java.lang.String nodePath, boolean local)
          find the element in this document corresponding to nodePath starting at node
 java.lang.String getAttributeData(org.w3c.dom.Node node)
          Get character data, etc, in node as an escaped String suitable for direct printing as an attribute value.
 java.lang.String getCDATAData(org.w3c.dom.Node node)
          Get character data in the node as an String escaped suitably for direct printing as a CDATA value, and with leading "".
protected  void getCharacterData(org.w3c.dom.Node node, java.lang.StringBuffer buff)
          Internal method that collects character data in child TEXT, CDATA and other descendents nodes of "node" and appends it to the StringBuffer buff without any escaping.
 java.lang.String getCommentData(org.w3c.dom.Node node)
          Get character data in the node as an String escaped suitably for direct printing as a comment, with <!-- and --> to wrap it.
 java.lang.String getLiteralData(org.w3c.dom.Node node)
          Get character data in the node as an String escaped suitably for direct printing as a literal value.
 java.lang.String getPIData(org.w3c.dom.Node node, java.lang.String target)
          Get character data in the node as an String escaped suitably for direct printing as a PI value.
 java.lang.String getTextData(org.w3c.dom.Node node)
          Get character data in the node as an String, escaped suitable for direct printing as text (PCDATA).
protected  void getTextOrAttributeData(org.w3c.dom.Node node, java.lang.StringBuffer buff)
          Appends that part of the content of node that can be interpreted as an attribute value or text data to buff.
 void print(org.w3c.dom.Document doc)
          Main method: parses and prints the document at doc.
 void print(org.w3c.dom.Node node, ParameterContainer ns_pars)
          Main method that prints a node, with ns_pars listing a list of prefixes and their namespaces that are in scope and therefore do not have to be printed.
protected  void printAttributes(org.w3c.dom.Element node, ParameterContainer ns_pars, ParameterContainer attrsPrinted, ParameterContainer prefixesUsed)
          Internal method that prints the attributes of a given element.
protected  void printChildren(org.w3c.dom.Node node, ParameterContainer ns_pars)
          Internal method that prints all child nodes of the given one.
protected  void printChildren(org.w3c.dom.Node node, ParameterContainer ns_pars, boolean escapeChars)
          Internal method that prints all child nodes of the given one, and with escaping of text nodes specified.
protected  void printChildren(org.w3c.dom.Node node, ParameterContainer ns_pars, java.lang.String sep)
          Internal method that prints all child nodes of the given one, separated by sep and with escaping of text nodes true.
protected  void printChildren(org.w3c.dom.Node node, ParameterContainer ns_pars, java.lang.String sep, boolean escapeChars)
          Internal method that prints all child nodes of the given one, separated by sep.
protected  void printElement(org.w3c.dom.Element node, ParameterContainer ns_pars)
          Internal method that prints the Element node "node"; All known namespace prefixes and URIs are in ns_pars.
protected  boolean printElementContent(org.w3c.dom.Element node, ParameterContainer ns_pars)
          Internal method that prints the content of an element.
 void printExternal(org.w3c.dom.Node node, ParameterContainer ns_pars)
           
 void printExternalChildren(org.w3c.dom.Node node, ParameterContainer ns_pars)
           
 
Methods inherited from class uk.ac.bham.gloss.XRNormalizer
doc, escapeXML, getXRAttribute, getXRAttributeData, getXRAttributeData, getXRAttributeNS, isWhiteSpace, localName, normalize, normalize, normalizeChildren, normalizeElement, normalizeEmptyNode, normalizeText, normalizeText, out, prefix, printRawDocument, printXRDocument, setOS, setOutputEncoding, trimSpace, warning
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

idAttrName

protected java.lang.String idAttrName
The name of any ID attribute other than the standard xml:id, null if none


idAttrNS

protected java.lang.String idAttrNS
The namespace of any ID attribute other than the standard xml:id, null if none

Constructor Detail

XRPrinter

public XRPrinter(java.io.OutputStream os)
Constructor: specify the output stream for documents to go to.

Method Detail

print

public void print(org.w3c.dom.Document doc)
           throws java.io.IOException,
                  GlossException
Main method: parses and prints the document at doc.

Throws:
java.io.IOException
GlossException

getTextData

public java.lang.String getTextData(org.w3c.dom.Node node)
                             throws GlossException
Get character data in the node as an String, escaped suitable for direct printing as text (PCDATA). Note that, for compatability with some browsers, "'" is not escaped to "&apos;".

Throws:
GlossException

getCDATAData

public java.lang.String getCDATAData(org.w3c.dom.Node node)
                              throws GlossException
Get character data in the node as an String escaped suitably for direct printing as a CDATA value, and with leading "".

Throws:
GlossException

getLiteralData

public java.lang.String getLiteralData(org.w3c.dom.Node node)
                                throws GlossException
Get character data in the node as an String escaped suitably for direct printing as a literal value.

Throws:
GlossException

getCommentData

public java.lang.String getCommentData(org.w3c.dom.Node node)
                                throws GlossException
Get character data in the node as an String escaped suitably for direct printing as a comment, with <!-- and --> to wrap it.

Throws:
GlossException

getPIData

public java.lang.String getPIData(org.w3c.dom.Node node,
                                  java.lang.String target)
                           throws GlossException
Get character data in the node as an String escaped suitably for direct printing as a PI value.

Throws:
GlossException

escapeComment

protected static java.lang.String escapeComment(java.lang.String buff)
Returns a version of buff that can be placed inside a comment.


escapePI

protected static java.lang.String escapePI(java.lang.String buff)
Returns a version of buff that can be placed inside a comment.


printElement

protected void printElement(org.w3c.dom.Element node,
                            ParameterContainer ns_pars)
                     throws GlossException
Internal method that prints the Element node "node"; All known namespace prefixes and URIs are in ns_pars. It is assumed that these have already been declared and do not need to be declared again.

Throws:
GlossException

printChildren

protected void printChildren(org.w3c.dom.Node node,
                             ParameterContainer ns_pars)
                      throws GlossException
Internal method that prints all child nodes of the given one. Default method with escaping of text nodes and separator="".

Throws:
GlossException

printChildren

protected void printChildren(org.w3c.dom.Node node,
                             ParameterContainer ns_pars,
                             boolean escapeChars)
                      throws GlossException
Internal method that prints all child nodes of the given one, and with escaping of text nodes specified.

Throws:
GlossException

printChildren

protected void printChildren(org.w3c.dom.Node node,
                             ParameterContainer ns_pars,
                             java.lang.String sep)
                      throws GlossException
Internal method that prints all child nodes of the given one, separated by sep and with escaping of text nodes true.

Throws:
GlossException

printChildren

protected void printChildren(org.w3c.dom.Node node,
                             ParameterContainer ns_pars,
                             java.lang.String sep,
                             boolean escapeChars)
                      throws GlossException
Internal method that prints all child nodes of the given one, separated by sep. Escaping of immediate text and cdata children is controlled by escapeChars.

Throws:
GlossException

printAttributes

protected void printAttributes(org.w3c.dom.Element node,
                               ParameterContainer ns_pars,
                               ParameterContainer attrsPrinted,
                               ParameterContainer prefixesUsed)
                        throws GlossException
Internal method that prints the attributes of a given element. If there is an attempt to print the same attribute more than once only the first will succeed. The first ParameterContainer argument, ns_pars, contains a list of namespace prefix-to-URI maps that are in scope. The other parameter container arguments are as follows. attrsPrinted is a set of full attribute names against their namespace URIs ("" for no NS), being a list of attributes already printed and therefore subsequent attributes with the same name should be ignored; prefixesUsed is a set of namespace prefixes with namespace URIs already used in this element.

Throws:
GlossException

printElementContent

protected boolean printElementContent(org.w3c.dom.Element node,
                                      ParameterContainer ns_pars)
                               throws GlossException
Internal method that prints the content of an element. This method scans children and ignores xr:attribute children. If the resulting element is nonempty it closes the open-element tag, prints the content and returns true. Else it prints "/>" and returns false.

Throws:
GlossException

print

public void print(org.w3c.dom.Node node,
                  ParameterContainer ns_pars)
           throws GlossException
Main method that prints a node, with ns_pars listing a list of prefixes and their namespaces that are in scope and therefore do not have to be printed. This method checks the node type, but only Node.ELEMENT_NODE and Node.DOCUMENT_NODE need to be supported. All other node-types are ignored, with an error message generated.

Throws:
GlossException

findNodeID

protected org.w3c.dom.Element findNodeID(org.w3c.dom.Element node,
                                         java.lang.String id,
                                         boolean local)
find the element in this document with the given ID


findLocalNodePath

protected org.w3c.dom.Element findLocalNodePath(org.w3c.dom.Element node,
                                                java.lang.String nodePath)
                                         throws GlossException
find the element in this document corresponding to nodePath starting at node

Throws:
GlossException

findNodePath

protected org.w3c.dom.Element findNodePath(org.w3c.dom.Element node,
                                           java.lang.String nodePath,
                                           boolean local)
                                    throws GlossException
find the element in this document corresponding to nodePath starting at node

Throws:
GlossException

printExternal

public void printExternal(org.w3c.dom.Node node,
                          ParameterContainer ns_pars)
                   throws GlossException
Throws:
GlossException

printExternalChildren

public void printExternalChildren(org.w3c.dom.Node node,
                                  ParameterContainer ns_pars)
                           throws GlossException
Throws:
GlossException

getCharacterData

protected void getCharacterData(org.w3c.dom.Node node,
                                java.lang.StringBuffer buff)
                         throws GlossException
Internal method that collects character data in child TEXT, CDATA and other descendents nodes of "node" and appends it to the StringBuffer buff without any escaping.

Throws:
GlossException

getTextOrAttributeData

protected void getTextOrAttributeData(org.w3c.dom.Node node,
                                      java.lang.StringBuffer buff)
                               throws GlossException
Appends that part of the content of node that can be interpreted as an attribute value or text data to buff. Result is escaped for printing out. If used as attribute value, the result may be delimited by " only, since (for compatability) ' is not escaped to '.

Throws:
GlossException

getAttributeData

public java.lang.String getAttributeData(org.w3c.dom.Node node)
                                  throws GlossException
Get character data, etc, in node as an escaped String suitable for direct printing as an attribute value. For compatability with some browsers, "'" is not escaped to "&apos;" so if used as an attribute value the resulting string MUST be printed using double quotes (") not single quotes (').

Throws:
GlossException