deegree 2.5 (2011/06/29 09:44 build-8-official)

org.deegree.framework.xml
Class XMLFragment

java.lang.Object
  extended by org.deegree.framework.xml.XMLFragment
All Implemented Interfaces:
java.io.Serializable
Direct Known Subclasses:
AuthenticationDocument, AuthenticationDocument, CatalogueManagerConfiguration, CommonsDocument, DescribeUserResponse, DoServiceDocument, ExceptionDocument, FilterCapabilities100Fragment, FilterCapabilities110Fragment, GetCapabilitiesDocument, GetRecordByIdResultDocument, GetRecordsResultDocument, GetResourceByIDDocument, GMLDocument, IODocument, ISO19115Document, MapModelDocument, OGCDocument, OWSCommonCapabilitiesDocument, ProcessDescriptionDocument, SAMLDocument, TransactionResultDocument, TriggerConfigurationDocument, WASGetCapabilitiesDocument, WCTSRequestBaseDocument, WSSGetCapabilitiesDocument, XMLFileResource, XSDocument, XSLTDocument

public class XMLFragment
extends java.lang.Object
implements java.io.Serializable

An instance of XMLFragment encapsulates an underlying Element which acts as the root element of the document (which may be a fragment or a whole document).

Basically, XMLFragment provides easy loading and proper saving (automatically generated CDATA-elements for text nodes that need to be escaped) and acts as base class for all XML parsers in deegree. TODO: automatically generated CDATA-elements are not implemented yet

Additionally, XMLFragment tries to make the handling of relative paths inside the document's content as painless as possible. This means that after initialization of the XMLFragment with the correct SystemID (i.e. the URL of the document):

Version:
$Revision: 23962 $, $Date: 2010-04-29 09:56:44 +0200 (Do, 29 Apr 2010) $
Author:
Torsten Friebe , Markus Schneider , last edited by: $Author: jwilden $
See Also:
XMLTools, Serialized Form

Field Summary
static java.lang.String DEFAULT_URL
          Use this URL as SystemID only if an XMLFragment cannot be pinpointed to a URL - in this case it may not use any relative references!
protected static NamespaceContext nsContext
          The namespace map containing the prefixes mapped to the namespaces.
protected static java.net.URI XLNNS
          The xlink namespace
 
Constructor Summary
XMLFragment()
          Creates a new XMLFragment which is not initialized.
XMLFragment(org.w3c.dom.Document doc, java.lang.String systemId)
          Creates a new XMLFragment instance based on the submitted Document.
XMLFragment(org.w3c.dom.Element element)
          Creates a new XMLFragment instance based on the submitted Element.
XMLFragment(java.io.File file)
          Creates a new XMLFragment which is loaded from the given File.
XMLFragment(QualifiedName elementName)
          Constructs an empty document with the given QualifiedName as root node.
XMLFragment(java.io.Reader reader, java.lang.String systemId)
          Creates a new XMLFragment which is loaded from the given Reader.
XMLFragment(java.net.URL url)
          Creates a new XMLFragment which is loaded from the given URL.
 
Method Summary
 java.lang.String getAsPrettyString()
          Returns a string representation of the XML Document, pretty printed.
 java.lang.String getAsString()
          Returns a string representation of the XML Document
 java.util.Map<java.net.URI,java.net.URL> getAttachedSchemas()
          Determines the namespace URIs and the bound schema URLs from the 'xsi:schemaLocation' attribute of the document element.
protected  QualifiedName getQualifiedName(org.w3c.dom.Element element)
          Returns the qualified name of the given element.
 org.w3c.dom.Element getRootElement()
           
 java.net.URL getSystemId()
          Returns the systemId (the URL of the XMLFragment).
 boolean hasSchema()
          Returns whether the document has a schema reference.
 void load(java.io.InputStream istream, java.lang.String systemId)
          Initializes the XMLFragment with the content from the given InputStream.
 void load(java.io.Reader reader, java.lang.String systemId)
          Initializes the XMLFragment with the content from the given Reader.
 void load(java.net.URL url)
          Initializes the XMLFragment with the content from the given URL.
static QualifiedName parseQualifiedName(org.w3c.dom.Node node)
          Parses the value of the submitted Node as a QualifiedName.
protected  SimpleLink parseSimpleLink(org.w3c.dom.Element element)
          Parses the submitted Element as a SimpleLink.
 void prettyPrint(java.io.OutputStream os)
          Writes the XMLFragment instance to the given OutputStream using indentation so it may be read easily.
 void prettyPrint(java.io.Writer writer)
          Writes the XMLFragment instance to the given Writer using indentation so it may be read easily.
 java.net.URL resolve(java.lang.String url)
          Resolves the given URL (which may be relative) against the SystemID of the XMLFragment into a URL (which is always absolute).
 void setRootElement(org.w3c.dom.Element rootElement)
           
 void setSystemId(java.lang.String systemId)
           
 void setSystemId(java.net.URL systemId)
           
 java.lang.String toString()
          Returns a string representation of the object.
 void write(java.io.OutputStream os)
          Writes the XMLFragment instance to the given OutputStream using the default system encoding and adding CDATA-sections in for text-nodes where needed.
 void write(java.io.OutputStream os, java.util.Properties outputProperties)
          Writes the XMLFragment instance to the given OutputStream using the specified OutputKeys which allow complete control of the generated output.
 void write(java.io.Writer writer)
          Writes the XMLFragment instance to the given Writer using the default system encoding and adding CDATA-sections in for text-nodes where needed.
 void write(java.io.Writer writer, java.util.Properties outputProperties)
          Writes the XMLFragment instance to the given Writer using the specified OutputKeys.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

nsContext

protected static NamespaceContext nsContext
The namespace map containing the prefixes mapped to the namespaces.


XLNNS

protected static final java.net.URI XLNNS
The xlink namespace


DEFAULT_URL

public static final java.lang.String DEFAULT_URL
Use this URL as SystemID only if an XMLFragment cannot be pinpointed to a URL - in this case it may not use any relative references!

See Also:
Constant Field Values
Constructor Detail

XMLFragment

public XMLFragment()
Creates a new XMLFragment which is not initialized.


XMLFragment

public XMLFragment(java.net.URL url)
            throws java.io.IOException,
                   org.xml.sax.SAXException
Creates a new XMLFragment which is loaded from the given URL.

Parameters:
url -
Throws:
java.io.IOException
org.xml.sax.SAXException

XMLFragment

public XMLFragment(java.io.File file)
            throws java.net.MalformedURLException,
                   java.io.IOException,
                   org.xml.sax.SAXException
Creates a new XMLFragment which is loaded from the given File.

Parameters:
file - the file to load from
Throws:
org.xml.sax.SAXException - if the document could not be parsed
java.io.IOException - if the document could not be read
java.net.MalformedURLException - if the file cannot be transposed to a valid url

XMLFragment

public XMLFragment(java.io.Reader reader,
                   java.lang.String systemId)
            throws org.xml.sax.SAXException,
                   java.io.IOException
Creates a new XMLFragment which is loaded from the given Reader.

Parameters:
reader -
systemId - this string should represent a URL that is related to the passed reader. If this URL is not available or unknown, the string should contain the value of XMLFragment.DEFAULT_URL
Throws:
org.xml.sax.SAXException
java.io.IOException

XMLFragment

public XMLFragment(org.w3c.dom.Document doc,
                   java.lang.String systemId)
            throws java.net.MalformedURLException
Creates a new XMLFragment instance based on the submitted Document.

Parameters:
doc -
systemId - this string should represent a URL that is the source of the passed doc. If this URL is not available or unknown, the string should contain the value of XMLFragment.DEFAULT_URL
Throws:
java.net.MalformedURLException - if systemId is no valid and absolute URL

XMLFragment

public XMLFragment(org.w3c.dom.Element element)
Creates a new XMLFragment instance based on the submitted Element.

Parameters:
element -

XMLFragment

public XMLFragment(QualifiedName elementName)
Constructs an empty document with the given QualifiedName as root node.

Parameters:
elementName - if the name's namespace is set, the prefix should be set as well.
Method Detail

getSystemId

public java.net.URL getSystemId()
Returns the systemId (the URL of the XMLFragment).

Returns:
the systemId

setSystemId

public void setSystemId(java.lang.String systemId)
                 throws java.net.MalformedURLException
Parameters:
systemId - The systemId (physical location) to set (may be null).
Throws:
java.net.MalformedURLException

setSystemId

public void setSystemId(java.net.URL systemId)
Parameters:
systemId - The systemId (physical location) to set.

hasSchema

public boolean hasSchema()
Returns whether the document has a schema reference.

Returns:
true, if the document has a schema reference, false otherwise

getAttachedSchemas

public java.util.Map<java.net.URI,java.net.URL> getAttachedSchemas()
                                                            throws XMLParsingException
Determines the namespace URIs and the bound schema URLs from the 'xsi:schemaLocation' attribute of the document element.

Returns:
keys are URIs (namespaces), values are URLs (schema locations)
Throws:
XMLParsingException

load

public void load(java.net.URL url)
          throws java.io.IOException,
                 org.xml.sax.SAXException
Initializes the XMLFragment with the content from the given URL. Sets the SystemId, too.

Parameters:
url -
Throws:
java.io.IOException
org.xml.sax.SAXException

load

public void load(java.io.InputStream istream,
                 java.lang.String systemId)
          throws org.xml.sax.SAXException,
                 java.io.IOException,
                 XMLException
Initializes the XMLFragment with the content from the given InputStream. Sets the SystemId, too.

Parameters:
istream -
systemId - cannot be null. This string should represent a URL that is related to the passed istream. If this URL is not available or unknown, the string should contain the value of XMLFragment.DEFAULT_URL
Throws:
org.xml.sax.SAXException
java.io.IOException
XMLException
java.lang.NullPointerException

load

public void load(java.io.Reader reader,
                 java.lang.String systemId)
          throws org.xml.sax.SAXException,
                 java.io.IOException
Initializes the XMLFragment with the content from the given Reader. Sets the SystemId, too.

Parameters:
reader -
systemId - can not be null. This string should represent a URL that is related to the passed reader. If this URL is not available or unknown, the string should contain the value of XMLFragment.DEFAULT_URL
Throws:
org.xml.sax.SAXException
java.io.IOException
java.lang.NullPointerException

setRootElement

public void setRootElement(org.w3c.dom.Element rootElement)
Parameters:
rootElement -

getRootElement

public org.w3c.dom.Element getRootElement()
Returns:
the element

resolve

public java.net.URL resolve(java.lang.String url)
                     throws java.net.MalformedURLException
Resolves the given URL (which may be relative) against the SystemID of the XMLFragment into a URL (which is always absolute).

Parameters:
url -
Returns:
the resolved URL object
Throws:
java.net.MalformedURLException

write

public void write(java.io.Writer writer)
Writes the XMLFragment instance to the given Writer using the default system encoding and adding CDATA-sections in for text-nodes where needed. TODO: Add code for CDATA safety.

Parameters:
writer -

write

public void write(java.io.Writer writer,
                  java.util.Properties outputProperties)
Writes the XMLFragment instance to the given Writer using the specified OutputKeys.

Parameters:
writer - cannot be null
outputProperties - output properties for the Transformer that is used to serialize the document see javax.xml.OutputKeys

write

public void write(java.io.OutputStream os)
Writes the XMLFragment instance to the given OutputStream using the default system encoding and adding CDATA-sections in for text-nodes where needed. TODO: Add code for CDATA safety.

Parameters:
os -

write

public void write(java.io.OutputStream os,
                  java.util.Properties outputProperties)
Writes the XMLFragment instance to the given OutputStream using the specified OutputKeys which allow complete control of the generated output.

Parameters:
os - cannot be null
outputProperties - output properties for the Transformer used to serialize the document
See Also:
OutputKeys

prettyPrint

public void prettyPrint(java.io.OutputStream os)
                 throws javax.xml.transform.TransformerException
Writes the XMLFragment instance to the given OutputStream using indentation so it may be read easily.

Parameters:
os -
Throws:
javax.xml.transform.TransformerException

prettyPrint

public void prettyPrint(java.io.Writer writer)
                 throws javax.xml.transform.TransformerException
Writes the XMLFragment instance to the given Writer using indentation so it may be read easily.

Parameters:
writer -
Throws:
javax.xml.transform.TransformerException

parseSimpleLink

protected SimpleLink parseSimpleLink(org.w3c.dom.Element element)
                              throws XMLParsingException
Parses the submitted Element as a SimpleLink.

Possible escaping of the attributes "xlink:href", "xlink:role" and "xlink:arcrole" is performed automatically.

Parameters:
element -
Returns:
the object representation of the element
Throws:
XMLParsingException

parseQualifiedName

public static QualifiedName parseQualifiedName(org.w3c.dom.Node node)
                                        throws XMLParsingException
Parses the value of the submitted Node as a QualifiedName.

To parse the text contents of an Element node, the actual text node must be given, not the Element node itself.

Parameters:
node -
Returns:
object representation of the element
Throws:
XMLParsingException

getQualifiedName

protected QualifiedName getQualifiedName(org.w3c.dom.Element element)
                                  throws XMLParsingException
Returns the qualified name of the given element.

Parameters:
element -
Returns:
the qualified name of the given element.
Throws:
XMLParsingException

getAsString

public java.lang.String getAsString()
Returns a string representation of the XML Document

Returns:
the string

getAsPrettyString

public java.lang.String getAsPrettyString()
Returns a string representation of the XML Document, pretty printed. Note that pretty printing can mess up XML documents in some cases (GML, for instance).

Returns:
the string

toString

public java.lang.String toString()
Returns a string representation of the object.

Overrides:
toString in class java.lang.Object
Returns:
a string representation of the object.

deegree 2.5 (2011/06/29 09:44 build-8-official)

an open source project founded by lat/lon, Bonn, Germany.
For more information visit: http://www.deegree.org