Top Description Inners Fields Constructors Methods
org.jdom.transform

public Class JDOMSource

extends SAXSource
Class Inheritance
Imports
java.io.*, java.util.*, javax.xml.transform.sax.*, org.jdom.*, org.jdom.output.*, org.xml.sax.*

A holder for an XML Transformation source: a Document, Element, or list of nodes.

The is provides input to a JAXP TrAX Transformer.

The following example shows how to apply an XSL Transformation to a JDOM document and get the transformation result in the form of a list of JDOM nodes:


  public static List transform(Document doc, String stylesheet)
                                       throws JDOMException {
    try {
      Transformer transformer = TransformerFactory.newInstance()
                            .newTransformer(new StreamSource(stylesheet));
      JDOMSource in = new JDOMSource(doc);
      JDOMResult out = new JDOMResult();
      transformer.transform(in, out);
      return out.getResult();
    }
    catch (TransformerException e) {
      throw new JDOMException("XSLT Transformation failed", e);
    }
  }
Authors
Laurent Bihanic, Jason Hunter
Version
$Revision: 1.20 $, $Date: 2007/11/10 05:29:02 $
See Also
org.jdom.transform.JDOMResult

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
JDOMSource.DocumentReader

An implementation of the SAX2 XMLReader interface that presents a SAX view of a JDOM Document.

private static class
JDOMSource.JDOMInputSource

A subclass of the SAX InputSource interface that wraps a JDOM Document.

Field Summary

Modifier and TypeField and Description
private static final String
public static final String
JDOM_FEATURE

If javax.xml.transform.TransformerFactory#getFeature returns true when passed this value as an argument, the Transformer natively supports JDOM.

private EntityResolver
resolver

Optional entity resolver associated to the source of this document or null if no EntityResolver was supplied with this JDOMSource.

private XMLReader
xmlReader

The XMLReader object associated to this source or null if no XMLReader has yet been requested.

Inherited from javax.xml.transform.sax.SAXSource:
FEATURE

Constructor Summary

AccessConstructor and Description
public
JDOMSource(Document
the JDOM document to use as source for the transformations
source
)

Creates a JDOM TrAX source wrapping a JDOM document.

public
JDOMSource(List<E>
the JDOM nodes to use as source for the transformations
source
)

Creates a JDOM TrAX source wrapping a list of JDOM nodes.

public
JDOMSource(Element
the JDOM element to use as source for the transformations
source
)

Creates a JDOM TrAX source wrapping a JDOM element.

public
JDOMSource(Document
The JDOM Element to use as source for the transformations
source
,
EntityResolver
Entity resolver to use for the source transformation
resolver
)

Creates a JDOM TrAX source wrapping a JDOM element with an associated EntityResolver to resolve external entities.

Method Summary

Modifier and TypeMethod and Description
private XMLReader

Returns:

XMLReader reading the XML data from the source JDOM document with an optional EntityResolver
buildDocumentReader
()

Build an XMLReader to be used for the source.

public Document

Returns:

the source document used by this TrAX source or null if the source is a node list.
getDocument
()

Returns the source document used by this TrAX source.

public List<E>

Returns:

the source node list used by this TrAX source or null if the source is a JDOM document.
getNodes
()

Returns the source node list used by this TrAX source.

public XMLReader

Returns:

an XMLReader reading the XML data from the source JDOM document.
getXMLReader
()

Overrides javax.xml.transform.sax.SAXSource.getXMLReader.

Returns the XMLReader to be used for the Source.
public void
setDocument(Document
the JDOM document to use as source for the transformations
source
)

Sets the source document used by this TrAX source.

public void
setInputSource(InputSource
a valid InputSource reference.
inputSource
)

Overrides javax.xml.transform.sax.SAXSource.setInputSource.

Sets the SAX InputSource to be used for the Source.
public void
setNodes(List<E>
the JDOM nodes to use as source for the transformations
source
)

Sets the source node list used by this TrAX source.

public void
setXMLReader(XMLReader
a valid XMLReader or XMLFilter reference.
reader
)

Overrides javax.xml.transform.sax.SAXSource.setXMLReader.

Set the XMLReader to be used for the Source.
Inherited from javax.xml.transform.sax.SAXSource:
getInputSourcegetSystemIdisEmptysetSystemIdsourceToInputSource

Field Detail

CVS_IDback to summary
private static final String CVS_ID
JDOM_FEATUREback to summary
public static final String JDOM_FEATURE

If javax.xml.transform.TransformerFactory#getFeature returns true when passed this value as an argument, the Transformer natively supports JDOM.

Note

This implementation does not override the SAXSource#FEATURE value defined by its superclass to be considered as a SAXSource by Transformer implementations not natively supporting JDOM.

resolverback to summary
private EntityResolver resolver

Optional entity resolver associated to the source of this document or null if no EntityResolver was supplied with this JDOMSource.

See Also
buildDocumentReader()
xmlReaderback to summary
private XMLReader xmlReader

The XMLReader object associated to this source or null if no XMLReader has yet been requested.

See Also
getXMLReader

Constructor Detail

JDOMSourceback to summary
public JDOMSource(Document source)

Creates a JDOM TrAX source wrapping a JDOM document.

Parameters
source:Document

the JDOM document to use as source for the transformations

Exceptions
IllegalArgumentException:
if source is null.
JDOMSourceback to summary
public JDOMSource(List<E> source)

Creates a JDOM TrAX source wrapping a list of JDOM nodes.

Parameters
source:List<E>

the JDOM nodes to use as source for the transformations

Exceptions
IllegalArgumentException:
if source is null.
JDOMSourceback to summary
public JDOMSource(Element source)

Creates a JDOM TrAX source wrapping a JDOM element.

Parameters
source:Element

the JDOM element to use as source for the transformations

Exceptions
IllegalArgumentException:
if source is null.
JDOMSourceback to summary
public JDOMSource(Document source, EntityResolver resolver)

Creates a JDOM TrAX source wrapping a JDOM element with an associated EntityResolver to resolve external entities.

Parameters
source:Document

The JDOM Element to use as source for the transformations

resolver:EntityResolver

Entity resolver to use for the source transformation

Exceptions
IllegalArgumentException:
ifsource is null

Method Detail

buildDocumentReaderback to summary
private XMLReader buildDocumentReader()

Build an XMLReader to be used for the source. This will create a new instance of DocumentReader with an EntityResolver instance if available.

Returns:XMLReader

XMLReader reading the XML data from the source JDOM document with an optional EntityResolver

getDocumentback to summary
public Document getDocument()

Returns the source document used by this TrAX source.

Returns:Document

the source document used by this TrAX source or null if the source is a node list.

See Also
setDocument
getNodesback to summary
public List<E> getNodes()

Returns the source node list used by this TrAX source.

Returns:List<E>

the source node list used by this TrAX source or null if the source is a JDOM document.

See Also
setDocument
getXMLReaderback to summary
public XMLReader getXMLReader()

Overrides javax.xml.transform.sax.SAXSource.getXMLReader.

Returns the XMLReader to be used for the Source.

This implementation returns a specific XMLReader reading the XML data from the source JDOM document.

Returns:XMLReader

an XMLReader reading the XML data from the source JDOM document.

setDocumentback to summary
public void setDocument(Document source)

Sets the source document used by this TrAX source.

Parameters
source:Document

the JDOM document to use as source for the transformations

Exceptions
IllegalArgumentException:
if source is null.
See Also
getDocument
setInputSourceback to summary
public void setInputSource(InputSource inputSource) throws UnsupportedOperationException

Overrides javax.xml.transform.sax.SAXSource.setInputSource.

Sets the SAX InputSource to be used for the Source.

As this implementation only supports JDOM document as data source, this method always throws an UnsupportedOperationException.

Parameters
inputSource:InputSource

a valid InputSource reference.

Exceptions
UnsupportedOperationException:
always!
setNodesback to summary
public void setNodes(List<E> source)

Sets the source node list used by this TrAX source.

Parameters
source:List<E>

the JDOM nodes to use as source for the transformations

Exceptions
IllegalArgumentException:
if source is null.
See Also
getNodes
setXMLReaderback to summary
public void setXMLReader(XMLReader reader) throws UnsupportedOperationException

Overrides javax.xml.transform.sax.SAXSource.setXMLReader.

Set the XMLReader to be used for the Source.

As this implementation only supports JDOM document as data source, this method throws an UnsupportedOperationException if the provided reader object does not implement the SAX XMLFilter interface. Otherwise, the JDOM document reader will be attached as parent of the filter chain.

Parameters
reader:XMLReader

a valid XMLReader or XMLFilter reference.

Exceptions
UnsupportedOperationException:
if reader is not a SAX XMLFilter.
See Also
getXMLReader
org.jdom.transform back to summary

private Class JDOMSource.DocumentReader

extends SAXOutputter
implements XMLReader
Class Inheritance
All Implemented Interfaces
org.xml.sax.XMLReader

An implementation of the SAX2 XMLReader interface that presents a SAX view of a JDOM Document. The actual generation of the SAX events is delegated to JDOM's SAXOutputter.
See Also
org.jdom.Document, org.jdom.output.SAXOutputter

Constructor Summary

AccessConstructor and Description
public
DocumentReader()

Public default constructor.

Method Summary

Modifier and TypeMethod and Description
public void
parse(String
the system identifier (URI).
systemId
)

Implements org.xml.sax.XMLReader.parse.

Parses an XML document from a system identifier (URI).
public void
parse(InputSource
the input source for the top-level of the XML document.
input
)

Implements org.xml.sax.XMLReader.parse.

Parses an XML document.
Inherited from org.jdom.output.SAXOutputter:
createParsergetContentHandlergetDeclHandlergetDTDHandlergetEntityResolvergetErrorHandlergetFeaturegetLexicalHandlergetLocatorgetPropertygetReportDTDEventsgetReportNamespaceDeclarationsoutputoutputoutputoutputFragmentoutputFragmentsetContentHandlersetDeclHandlersetDTDHandlersetEntityResolversetErrorHandlersetFeaturesetLexicalHandlersetPropertysetReportDTDEventssetReportNamespaceDeclarations

Constructor Detail

DocumentReaderback to summary
public DocumentReader()

Public default constructor.

Method Detail

parseback to summary
public void parse(String systemId) throws SAXNotSupportedException

Implements org.xml.sax.XMLReader.parse.

Parses an XML document from a system identifier (URI).

This implementation does not support reading XML data from system identifiers, only from JDOM documents. Hence, this method always throws a SAXNotSupportedException.

Parameters
systemId:String

the system identifier (URI).

Exceptions
SAXNotSupportedException:
always!
parseback to summary
public void parse(InputSource input) throws SAXException

Implements org.xml.sax.XMLReader.parse.

Parses an XML document.

The methods accepts only JDOMInputSources instances as input sources.

Parameters
input:InputSource

the input source for the top-level of the XML document.

Exceptions
SAXException:
any SAX exception, possibly wrapping another exception.
SAXNotSupportedException:
if the input source does not wrap a JDOM document.
org.jdom.transform back to summary

private Class JDOMSource.JDOMInputSource

extends InputSource
Class Inheritance

A subclass of the SAX InputSource interface that wraps a JDOM Document.

This class is nested in JDOMSource as it is not intented to be used independently of its friend: DocumentReader.

See Also
org.jdom.Document

Field Summary

Modifier and TypeField and Description
private Object
source

The source as a JDOM document or a list of JDOM nodes.

Constructor Summary

AccessConstructor and Description
public
JDOMInputSource(Document
the source document.
document
)

Builds a InputSource wrapping the specified JDOM Document.

public
JDOMInputSource(List<E>
the source JDOM nodes.
nodes
)

Builds a InputSource wrapping a list of JDOM nodes.

Method Summary

Modifier and TypeMethod and Description
public Reader

Returns:

a Reader to a string representation of the source JDOM document.
getCharacterStream
()

Overrides org.xml.sax.InputSource.getCharacterStream.

Gets the character stream for this input source.
public Object

Returns:

the source as a JDOM document or a list of JDOM nodes.
getSource
()

Returns the source.

public void
setByteStream(InputStream
a byte stream containing an XML document.
byteStream
)

Overrides org.xml.sax.InputSource.setByteStream.

Sets the byte stream for this input source.
public void
setCharacterStream(Reader
a character stream containing an XML document.
characterStream
)

Overrides org.xml.sax.InputSource.setCharacterStream.

Sets the character stream for this input source.
Inherited from org.xml.sax.InputSource:
getByteStreamgetEncodinggetPublicIdgetSystemIdisEmptysetEncodingsetPublicIdsetSystemId

Field Detail

sourceback to summary
private Object source

The source as a JDOM document or a list of JDOM nodes.

Constructor Detail

JDOMInputSourceback to summary
public JDOMInputSource(Document document)

Builds a InputSource wrapping the specified JDOM Document.

Parameters
document:Document

the source document.

JDOMInputSourceback to summary
public JDOMInputSource(List<E> nodes)

Builds a InputSource wrapping a list of JDOM nodes.

Parameters
nodes:List<E>

the source JDOM nodes.

Method Detail

getCharacterStreamback to summary
public Reader getCharacterStream()

Overrides org.xml.sax.InputSource.getCharacterStream.

Gets the character stream for this input source.

Note that this method is only provided to make this InputSource implementation acceptable by any XML parser. As it generates an in-memory string representation of the JDOM document, it is quite inefficient from both speed and memory consumption points of view.

Returns:Reader

a Reader to a string representation of the source JDOM document.

getSourceback to summary
public Object getSource()

Returns the source.

Returns:Object

the source as a JDOM document or a list of JDOM nodes.

setByteStreamback to summary
public void setByteStream(InputStream byteStream) throws UnsupportedOperationException

Overrides org.xml.sax.InputSource.setByteStream.

Sets the byte stream for this input source.

This implementation always throws an UnsupportedOperationException as the only source stream supported is the source JDOM document.

Parameters
byteStream:InputStream

a byte stream containing an XML document.

Exceptions
UnsupportedOperationException:
always!
setCharacterStreamback to summary
public void setCharacterStream(Reader characterStream) throws UnsupportedOperationException

Overrides org.xml.sax.InputSource.setCharacterStream.

Sets the character stream for this input source.

This implementation always throws an UnsupportedOperationException as the only source stream supported is the source JDOM document.

Parameters
characterStream:Reader

a character stream containing an XML document.

Exceptions
UnsupportedOperationException:
always!