deegree 2.3 (2010/04/09 10:10 build-4-official)

org.deegree.framework.xml
Class XMLFragment

java.lang.Object
  extended by org.deegree.framework.xml.XMLFragment
All Implemented Interfaces:
Serializable
Direct Known Subclasses:
AuthenticationDocument, AuthenticationDocument, 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 Object
implements 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: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
Author:
Torsten Friebe , Markus Schneider , last edited by: $Author: mschneider $
See Also:
XMLTools, Serialized Form

Field Summary
static 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 URI XLNNS
          The xlink namespace
 
Constructor Summary
XMLFragment()
          Creates a new XMLFragment which is not initialized.
XMLFragment(Document doc, String systemId)
          Creates a new XMLFragment instance based on the submitted Document.
XMLFragment(Element element)
          Creates a new XMLFragment instance based on the submitted Element.
XMLFragment(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(Reader reader, String systemId)
          Creates a new XMLFragment which is loaded from the given Reader.
XMLFragment(URL url)
          Creates a new XMLFragment which is loaded from the given URL.
 
Method Summary
 String getAsPrettyString()
          Returns a string representation of the XML Document, pretty printed.
 String getAsString()
          Returns a string representation of the XML Document
 Map<URI,URL> getAttachedSchemas()
          Determines the namespace URIs and the bound schema URLs from the 'xsi:schemaLocation' attribute of the document element.
protected  QualifiedName getQualifiedName(Element element)
          Returns the qualified name of the given element.
 Element getRootElement()
           
 URL getSystemId()
          Returns the systemId (the URL of the XMLFragment).
 boolean hasSchema()
          Returns whether the document has a schema reference.
 void load(InputStream istream, String systemId)
          Initializes the XMLFragment with the content from the given InputStream.
 void load(Reader reader, String systemId)
          Initializes the XMLFragment with the content from the given Reader.
 void load(URL url)
          Initializes the XMLFragment with the content from the given URL.
static QualifiedName parseQualifiedName(Node node)
          Parses the value of the submitted Node as a QualifiedName.
protected  SimpleLink parseSimpleLink(Element element)
          Parses the submitted Element as a SimpleLink.
 void prettyPrint(OutputStream os)
          Writes the XMLFragment instance to the given OutputStream using indentation so it may be read easily.
 void prettyPrint(Writer writer)
          Writes the XMLFragment instance to the given Writer using indentation so it may be read easily.
 URL resolve(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(Element rootElement)
           
 void setSystemId(String systemId)
           
 void setSystemId(URL systemId)
           
 String toString()
          Returns a string representation of the object.
 void write(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(OutputStream os, Properties outputProperties)
          Writes the XMLFragment instance to the given OutputStream using the specified OutputKeys which allow complete control of the generated output.
 void write(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(Writer writer, 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 URI XLNNS
The xlink namespace


DEFAULT_URL

public static final 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(URL url)
            throws IOException,
                   SAXException
Creates a new XMLFragment which is loaded from the given URL.

Parameters:
url -
Throws:
IOException
SAXException

XMLFragment

public XMLFragment(File file)
            throws MalformedURLException,
                   IOException,
                   SAXException
Creates a new XMLFragment which is loaded from the given File.

Parameters:
file - the file to load from
Throws:
SAXException - if the document could not be parsed
IOException - if the document could not be read
MalformedURLException - if the file cannot be transposed to a valid url

XMLFragment

public XMLFragment(Reader reader,
                   String systemId)
            throws SAXException,
                   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:
SAXException
IOException

XMLFragment

public XMLFragment(Document doc,
                   String systemId)
            throws 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:
MalformedURLException - if systemId is no valid and absolute URL

XMLFragment

public XMLFragment(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 URL getSystemId()
Returns the systemId (the URL of the XMLFragment).

Returns:
the systemId

setSystemId

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

setSystemId

public void setSystemId(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 Map<URI,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(URL url)
          throws IOException,
                 SAXException
Initializes the XMLFragment with the content from the given URL. Sets the SystemId, too.

Parameters:
url -
Throws:
IOException
SAXException

load

public void load(InputStream istream,
                 String systemId)
          throws SAXException,
                 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:
SAXException
IOException
XMLException
NullPointerException

load

public void load(Reader reader,
                 String systemId)
          throws SAXException,
                 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:
SAXException
IOException
NullPointerException

setRootElement

public void setRootElement(Element rootElement)
Parameters:
rootElement -

getRootElement

public Element getRootElement()
Returns:
the element

resolve

public URL resolve(String url)
            throws 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:
MalformedURLException

write

public void write(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(Writer writer,
                  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(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(OutputStream os,
                  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(OutputStream os)
                 throws TransformerException
Writes the XMLFragment instance to the given OutputStream using indentation so it may be read easily.

Parameters:
os -
Throws:
TransformerException

prettyPrint

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

Parameters:
writer -
Throws:
TransformerException

parseSimpleLink

protected SimpleLink parseSimpleLink(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(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(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 String getAsString()
Returns a string representation of the XML Document

Returns:
the string

getAsPrettyString

public 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 String toString()
Returns a string representation of the object.

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

deegree 2.3 (2010/04/09 10:10 build-4-official)

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