uk.ac.bham.gloss
Class XRNormalizer

java.lang.Object
  extended by uk.ac.bham.gloss.XRNormalizer
Direct Known Subclasses:
XRPrinter

public class XRNormalizer
extends java.lang.Object

This is a class that checks and normalizes an XR document with xmlrepresentation mark-up. It also provides basic printing facility of the XML to an output stream. Copyright Richard Kaye 2007-8 for GLOSS, http://gloss.bham.ac.uk Usage permitted according to the GPL. No waranty.


Field Summary
 java.lang.String xmlnsURI
           
 java.lang.String xmlURI
           
 java.lang.String xrPrefix
          The URI default prefix and namespace of the xmlrepresentation tags
 java.lang.String xrURI
           
 
Constructor Summary
XRNormalizer()
           
 
Method Summary
protected static org.w3c.dom.Document doc(org.w3c.dom.Node n)
          Returns the owner document for node n, provided as a shorthand for n.getOwnerDocument()
protected static java.lang.String escapeXML(java.lang.String s)
          Returns "s" as an escaped String with ampersand converted to standard XML escape sequence, etc.
protected  java.lang.String getXRAttribute(org.w3c.dom.Node node, java.lang.String attrName, boolean preserveSpace)
          Returns an attribute for an XR element which is not in a namespace.
 java.lang.String getXRAttributeData(org.w3c.dom.Node node, boolean preserveSpace)
          Returns that part of the content of input Node node that can be interpreted as an attribute value for an xr: element.
protected  void getXRAttributeData(org.w3c.dom.Node node, java.lang.StringBuffer buff, boolean preserveSpace)
          Appends that part of the content of input Node node that can be interpreted as an attribute value for an xr: element to buff.
protected  java.lang.String getXRAttributeNS(org.w3c.dom.Node node, java.lang.String attrName, java.lang.String ns, ParameterContainer ns_pars, boolean preserveSpace)
          Returns an attribute for an XR element: this will be the string value of the attribute, unescaped, and any xr:attribute node used to provide it will be removed.
static boolean isWhiteSpace(java.lang.String text)
          Returns true if the string is entirely whitespace, and false otherwise.
static java.lang.String localName(java.lang.String qname)
          Returns the localName part of qname.
 void normalize(org.w3c.dom.Document doc)
          Main method: checks and transforms the document at doc.
 void normalize(org.w3c.dom.Node node, ParameterContainer ns_pars, boolean preserveSpace)
          checks and transforms the document at node; ns_pars contain a list of prefix=NSURI pairs that have been declared on ancestor elements and may be assumed and do not need to be re-declared.
protected  void normalizeChildren(org.w3c.dom.Node node, ParameterContainer ns_pars, boolean preserveSpace)
           
protected  void normalizeElement(org.w3c.dom.Node node, ParameterContainer ns_pars, boolean preserveSpace)
           
protected  void normalizeEmptyNode(org.w3c.dom.Node node)
          normalise an empty node
protected  org.w3c.dom.Node normalizeText(org.w3c.dom.Node node, boolean preserveSpace, boolean allowrefs)
          normalise a node (xr:attribute, xr:text, xr:literal, xr:cdata, xr:comment, xr:processing-instruction) with just text content.
protected  void normalizeText(org.w3c.dom.Node node, boolean allowrefs, org.w3c.dom.Node parent, java.lang.StringBuffer buff, boolean preserveSpace)
          normalise a node (xr:attribute, xr:text, xr:literal, xr:cdata, xr:comment, xr:processing-instruction) with just text content
 java.io.PrintStream out()
           
static java.lang.String prefix(java.lang.String qname)
          Returns the prefix part of qname or null if none.
 void printRawDocument(org.w3c.dom.Document d)
          Prints the XML document d to OutputStream out using the built-in java serializer.
 void printXRDocument(org.w3c.dom.Document d)
          Prints the XML document d to OutputStream out using the built-in java serializer.
 void setOS(java.io.OutputStream os)
           
 void setOutputEncoding(java.lang.String enc)
          set output stream encoding
static java.lang.String trimSpace(java.lang.String text)
          Returns text with leading and trailing white space (\n, \r, \t, ' ' characters) removed.
protected  void warning(java.lang.String wtext)
          Prints a warning message, or throws and exception, etc.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

xrPrefix

public final java.lang.String xrPrefix
The URI default prefix and namespace of the xmlrepresentation tags

See Also:
Constant Field Values

xrURI

public final java.lang.String xrURI
See Also:
Constant Field Values

xmlnsURI

public final java.lang.String xmlnsURI
See Also:
Constant Field Values

xmlURI

public final java.lang.String xmlURI
See Also:
Constant Field Values
Constructor Detail

XRNormalizer

public XRNormalizer()
Method Detail

setOS

public void setOS(java.io.OutputStream os)

setOutputEncoding

public void setOutputEncoding(java.lang.String enc)
                       throws GlossException
set output stream encoding

Throws:
GlossException

out

public java.io.PrintStream out()

printRawDocument

public void printRawDocument(org.w3c.dom.Document d)
                      throws java.io.IOException,
                             GlossException,
                             javax.xml.transform.TransformerException
Prints the XML document d to OutputStream out using the built-in java serializer.

Throws:
java.io.IOException
GlossException
javax.xml.transform.TransformerException

printXRDocument

public void printXRDocument(org.w3c.dom.Document d)
                     throws java.io.IOException,
                            GlossException,
                            javax.xml.transform.TransformerException
Prints the XML document d to OutputStream out using the built-in java serializer.

Throws:
java.io.IOException
GlossException
javax.xml.transform.TransformerException

doc

protected static org.w3c.dom.Document doc(org.w3c.dom.Node n)
Returns the owner document for node n, provided as a shorthand for n.getOwnerDocument()


trimSpace

public static java.lang.String trimSpace(java.lang.String text)
Returns text with leading and trailing white space (\n, \r, \t, ' ' characters) removed.


isWhiteSpace

public static boolean isWhiteSpace(java.lang.String text)
Returns true if the string is entirely whitespace, and false otherwise.


escapeXML

protected static java.lang.String escapeXML(java.lang.String s)
Returns "s" as an escaped String with ampersand converted to standard XML escape sequence, etc. For compatability, ' is not escaped, so if the result is used as an attribute value the resulting string MUST be printed using double quotes (") not single quotes (').


prefix

public static java.lang.String prefix(java.lang.String qname)
Returns the prefix part of qname or null if none.


localName

public static java.lang.String localName(java.lang.String qname)
Returns the localName part of qname.


getXRAttributeData

protected void getXRAttributeData(org.w3c.dom.Node node,
                                  java.lang.StringBuffer buff,
                                  boolean preserveSpace)
                           throws GlossException
Appends that part of the content of input Node node that can be interpreted as an attribute value for an xr: element to buff.

Throws:
GlossException

getXRAttributeData

public java.lang.String getXRAttributeData(org.w3c.dom.Node node,
                                           boolean preserveSpace)
                                    throws GlossException
Returns that part of the content of input Node node that can be interpreted as an attribute value for an xr: element. Will always return a string, possibly empty but never null.

Throws:
GlossException

getXRAttributeNS

protected java.lang.String getXRAttributeNS(org.w3c.dom.Node node,
                                            java.lang.String attrName,
                                            java.lang.String ns,
                                            ParameterContainer ns_pars,
                                            boolean preserveSpace)
                                     throws GlossException
Returns an attribute for an XR element: this will be the string value of the attribute, unescaped, and any xr:attribute node used to provide it will be removed. Returns null if no such attribute exists. Arguments ns and ns_pars may be null. In this case the attribute searched for will be either in no namespace at all or else is in one of the standard namespaces for xml: xmlns: if this prefix is part of attrName.

Throws:
GlossException

getXRAttribute

protected java.lang.String getXRAttribute(org.w3c.dom.Node node,
                                          java.lang.String attrName,
                                          boolean preserveSpace)
                                   throws GlossException
Returns an attribute for an XR element which is not in a namespace.

Throws:
GlossException

normalize

public void normalize(org.w3c.dom.Document doc)
               throws GlossException
Main method: checks and transforms the document at doc.

Throws:
GlossException

warning

protected void warning(java.lang.String wtext)
                throws GlossException
Prints a warning message, or throws and exception, etc. Redefine this in subclasses for other behaviour.

Throws:
GlossException

normalize

public void normalize(org.w3c.dom.Node node,
                      ParameterContainer ns_pars,
                      boolean preserveSpace)
               throws GlossException
checks and transforms the document at node; ns_pars contain a list of prefix=NSURI pairs that have been declared on ancestor elements and may be assumed and do not need to be re-declared. preserveSpace is the current value of the xml:space="preserve" attribute.

Throws:
GlossException

normalizeEmptyNode

protected void normalizeEmptyNode(org.w3c.dom.Node node)
                           throws GlossException
normalise an empty node

Throws:
GlossException

normalizeText

protected org.w3c.dom.Node normalizeText(org.w3c.dom.Node node,
                                         boolean preserveSpace,
                                         boolean allowrefs)
                                  throws GlossException
normalise a node (xr:attribute, xr:text, xr:literal, xr:cdata, xr:comment, xr:processing-instruction) with just text content. The input node "node" is replaced in the DOM with a new normalized node. The method returns a reference to the new version of the node "node". boolean parameter preserveSpace in the current xml:space="preserve" value. The allowrefs says the text may contain entity or character refs (encoded via xr:{entity|character}-reference nodes in the resulting tree.

Throws:
GlossException

normalizeText

protected void normalizeText(org.w3c.dom.Node node,
                             boolean allowrefs,
                             org.w3c.dom.Node parent,
                             java.lang.StringBuffer buff,
                             boolean preserveSpace)
                      throws GlossException
normalise a node (xr:attribute, xr:text, xr:literal, xr:cdata, xr:comment, xr:processing-instruction) with just text content

Throws:
GlossException

normalizeChildren

protected void normalizeChildren(org.w3c.dom.Node node,
                                 ParameterContainer ns_pars,
                                 boolean preserveSpace)
                          throws GlossException
Throws:
GlossException

normalizeElement

protected void normalizeElement(org.w3c.dom.Node node,
                                ParameterContainer ns_pars,
                                boolean preserveSpace)
                         throws GlossException
Throws:
GlossException