Top Description Inners Fields Constructors Methods
org.jdom.transform

public Class JDOMResult

extends SAXResult
Class Inheritance
Imports
java.util.*, javax.xml.transform.sax.*, org.jdom.*, org.jdom.input.*, org.xml.sax.*, org.xml.sax.ext.*, org.xml.sax.helpers.*

A holder for an XSL Transformation result, generally a list of nodes although it can be a JDOM Document also. As stated by the XSLT 1.0 specification, the result tree generated by an XSL transformation is not required to be a well-formed XML document. The result tree may have "any sequence of nodes as children that would be possible for an element node".

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.24 $, $Date: 2007/11/10 05:29:02 $
See Also
org.jdom.transform.JDOMSource

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class
private static class

Field Summary

Modifier and TypeField and Description
private static final String
private JDOMFactory
factory

The custom JDOM factory to use when building the transformation result or null to use the default JDOM classes.

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 boolean
queried

Whether the application queried the result (as a list or a document) since it was last set.

private Object
result

The result of a transformation, as set by Transformer implementations that natively support JDOM, as a JDOM document or a list of JDOM nodes.

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

Constructor Summary

AccessConstructor and Description
public
JDOMResult()

Public default constructor.

Method Summary

Modifier and TypeMethod and Description
public Document

Returns:

the transformation result as a JDOM document or null if the result of the transformation can not be converted into a well-formed document.
getDocument
()

Returns the result of an XSL Transformation as a JDOM document.

public JDOMFactory

Returns:

the custom JDOMFactory used to build the transformation result or null if the default JDOM classes are being used.
getFactory
()

Returns the custom JDOMFactory used to build the transformation result.

public List<E>

Returns:

the transformation result as a (possibly empty) list of JDOM nodes (Elements, Texts, Comments, PIs...).
getResult
()

Returns the result of an XSL Transformation as a list of JDOM nodes.

private void
retrieveResult()

Checks whether a transformation result has been set and, if not, retrieves the result tree being built by the document builder.

public void
setDocument(Document
the JDOM document result of a transformation.
document
)

Sets the document produced as result of an XSL Transformation.

public void
setFactory(JDOMFactory
the custom JDOMFactory to use or null to use the default JDOM classes.
factory
)

Sets a custom JDOMFactory to use when building the transformation result.

public void
setHandler(ContentHandler
Must be a non-null ContentHandler reference.
handler
)

Overrides javax.xml.transform.sax.SAXResult.setHandler.

Sets the target to be a SAX2 ContentHandler.
public void
setLexicalHandler(LexicalHandler
A non-null LexicalHandler for handling lexical parse events.
handler
)

Overrides javax.xml.transform.sax.SAXResult.setLexicalHandler.

Sets the SAX2 LexicalHandler for the output.
public void
setResult(List<E>
the result of a transformation as a list of JDOM nodes (Elements, Texts, Comments, PIs...).
result
)

Sets the object(s) produced as result of an XSL Transformation.

Inherited from javax.xml.transform.sax.SAXResult:
getHandlergetLexicalHandlergetSystemIdsetSystemId

Field Detail

CVS_IDback to summary
private static final String CVS_ID
factoryback to summary
private JDOMFactory factory

The custom JDOM factory to use when building the transformation result or null to use the default JDOM classes.

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 SAXResult#FEATURE value defined by its superclass to be considered as a SAXResult by Transformer implementations not natively supporting JDOM.

queriedback to summary
private boolean queried

Whether the application queried the result (as a list or a document) since it was last set.

resultback to summary
private Object result

The result of a transformation, as set by Transformer implementations that natively support JDOM, as a JDOM document or a list of JDOM nodes.

Constructor Detail

JDOMResultback to summary
public JDOMResult()

Public default constructor.

Method Detail

getDocumentback to summary
public Document getDocument()

Returns the result of an XSL Transformation as a JDOM document.

If the result of the transformation is a list of nodes, this method attempts to convert it into a JDOM document. If successful, any subsequent call to getResult will return an empty list.

Warning

The XSLT 1.0 specification states that the output of an XSL transformation is not a well-formed XML document but a list of nodes. Applications should thus use getResult instead of this method or at least expect null documents to be returned.

Returns:Document

the transformation result as a JDOM document or null if the result of the transformation can not be converted into a well-formed document.

See Also
getResult
getFactoryback to summary
public JDOMFactory getFactory()

Returns the custom JDOMFactory used to build the transformation result.

Returns:JDOMFactory

the custom JDOMFactory used to build the transformation result or null if the default JDOM classes are being used.

See Also
setFactory
getResultback to summary
public List<E> getResult()

Returns the result of an XSL Transformation as a list of JDOM nodes.

If the result of the transformation is a JDOM document, this method converts it into a list of JDOM nodes; any subsequent call to getDocument will return null.

Returns:List<E>

the transformation result as a (possibly empty) list of JDOM nodes (Elements, Texts, Comments, PIs...).

retrieveResultback to summary
private void retrieveResult()

Checks whether a transformation result has been set and, if not, retrieves the result tree being built by the document builder.

setDocumentback to summary
public void setDocument(Document document)

Sets the document produced as result of an XSL Transformation.

Note

This method shall be used by the javax.xml.transform.Transformer implementations that natively support JDOM to directly set the transformation result rather than considering this object as a SAXResult. Applications should not use this method.

Parameters
document:Document

the JDOM document result of a transformation.

See Also
setResult, getDocument
setFactoryback to summary
public void setFactory(JDOMFactory factory)

Sets a custom JDOMFactory to use when building the transformation result. Use a custom factory to build the tree with your own subclasses of the JDOM classes.

Parameters
factory:JDOMFactory

the custom JDOMFactory to use or null to use the default JDOM classes.

See Also
getFactory
setHandlerback to summary
public void setHandler(ContentHandler handler)

Overrides javax.xml.transform.sax.SAXResult.setHandler.

Sets the target to be a SAX2 ContentHandler.

Parameters
handler:ContentHandler

Must be a non-null ContentHandler reference.

setLexicalHandlerback to summary
public void setLexicalHandler(LexicalHandler handler)

Overrides javax.xml.transform.sax.SAXResult.setLexicalHandler.

Sets the SAX2 LexicalHandler for the output.

This is needed to handle XML comments and the like. If the lexical handler is not set, an attempt should be made by the transformer to cast the ContentHandler to a LexicalHandler.

Parameters
handler:LexicalHandler

A non-null LexicalHandler for handling lexical parse events.

setResultback to summary
public void setResult(List<E> result)

Sets the object(s) produced as result of an XSL Transformation.

Note

This method shall be used by the javax.xml.transform.Transformer implementations that natively support JDOM to directly set the transformation result rather than considering this object as a SAXResult. Applications should not use this method.

Parameters
result:List<E>

the result of a transformation as a list of JDOM nodes (Elements, Texts, Comments, PIs...).

See Also
getResult
org.jdom.transform back to summary

private Class JDOMResult.DocumentBuilder

extends XMLFilterImpl
implements LexicalHandler
Class Inheritance
All Implemented Interfaces
org.xml.sax.ext.LexicalHandler

Field Summary

Modifier and TypeField and Description
private JDOMResult.FragmentHandler
saxHandler

The actual JDOM document builder.

private boolean
startDocumentReceived

Whether the startDocument event was received.

Constructor Summary

AccessConstructor and Description
public
DocumentBuilder()

Public default constructor.

Method Summary

Modifier and TypeMethod and Description
public void
characters(char[]
An array of characters.
ch
,
int
The starting position in the array.
start
,
int
The number of characters to use from the array.
length
)

Overrides org.xml.sax.helpers.XMLFilterImpl.characters.

Implements org.xml.sax.ContentHandler.characters.

[SAX ContentHandler interface support] Receives notification of character data.
public void
comment(char[]
an array holding the characters in the comment.
ch
,
int
the starting position in the array.
start
,
int
the number of characters to use from the array.
length
)

Implements org.xml.sax.ext.LexicalHandler.comment.

[SAX LexicalHandler interface support] Reports an XML comment anywhere in the document.
public void
endCDATA()

Implements org.xml.sax.ext.LexicalHandler.endCDATA.

[SAX LexicalHandler interface support] Reports the end of a CDATA section.
public void
endDTD()

Implements org.xml.sax.ext.LexicalHandler.endDTD.

[SAX LexicalHandler interface support] Reports the end of DTD declarations.
public void
endEntity(String
the name of the entity that is ending.
name
)

Implements org.xml.sax.ext.LexicalHandler.endEntity.

[SAX LexicalHandler interface support] Reports the end of an entity.
private void
public List<E>

Returns:

the transformation result as a (possibly empty) list of JDOM nodes (Elements, Texts, Comments, PIs...) or null if no new transformation occurred since the result of the previous one was returned.
getResult
()

Returns the result of an XSL Transformation.

public void
ignorableWhitespace(char[]
An array of characters.
ch
,
int
The starting position in the array.
start
,
int
The number of characters to use from the array.
length
)

Overrides org.xml.sax.helpers.XMLFilterImpl.ignorableWhitespace.

Implements org.xml.sax.ContentHandler.ignorableWhitespace.

[SAX ContentHandler interface support] Receives notification of ignorable whitespace in element content.
public void
processingInstruction(String
The processing instruction target.
target
,
String
The text following the target.
data
)

Overrides org.xml.sax.helpers.XMLFilterImpl.processingInstruction.

Implements org.xml.sax.ContentHandler.processingInstruction.

[SAX ContentHandler interface support] Receives notification of a processing instruction.
public void
skippedEntity(String
The name of the skipped entity.
name
)

Overrides org.xml.sax.helpers.XMLFilterImpl.skippedEntity.

Implements org.xml.sax.ContentHandler.skippedEntity.

[SAX ContentHandler interface support] Receives notification of a skipped entity.
public void
startCDATA()

Implements org.xml.sax.ext.LexicalHandler.startCDATA.

[SAX LexicalHandler interface support] Reports the start of a CDATA section.
public void
startDocument()

Overrides org.xml.sax.helpers.XMLFilterImpl.startDocument.

Implements org.xml.sax.ContentHandler.startDocument.

[SAX ContentHandler interface support] Processes a start of document event.
public void
startDTD(String
the document type name.
name
,
String
the declared public identifier for the external DTD subset, or null if none was declared.
publicId
,
String
the declared system identifier for the external DTD subset, or null if none was declared.
systemId
)

Implements org.xml.sax.ext.LexicalHandler.startDTD.

[SAX LexicalHandler interface support] Reports the start of DTD declarations, if any.
public void
startElement(String
the Namespace URI, or the empty string if the element has no Namespace URI or if Namespace processing is not being performed.
nsURI
,
String
the local name (without prefix), or the empty string if Namespace processing is not being performed.
localName
,
String
the qualified name (with prefix), or the empty string if qualified names are not available.
qName
,
Attributes
The attributes attached to the element. If there are no attributes, it shall be an empty Attributes object.
atts
)

Overrides org.xml.sax.helpers.XMLFilterImpl.startElement.

Implements org.xml.sax.ContentHandler.startElement.

[SAX ContentHandler interface support] Receives notification of the beginning of an element.
public void
startEntity(String
the name of the entity. If it is a parameter entity, the name will begin with '%', and if it is the external DTD subset, it will be "[dtd]".
name
)

Implements org.xml.sax.ext.LexicalHandler.startEntity.

[SAX LexicalHandler interface support] Reports the beginning of some internal and external XML entities.
public void
startPrefixMapping(String
The Namespace prefix.
prefix
,
String
The Namespace URI.
uri
)

Overrides org.xml.sax.helpers.XMLFilterImpl.startPrefixMapping.

Implements org.xml.sax.ContentHandler.startPrefixMapping.

[SAX ContentHandler interface support] Begins the scope of a prefix-URI Namespace mapping.
Inherited from org.xml.sax.helpers.XMLFilterImpl:
endDocumentendElementendPrefixMappingerrorfatalErrorgetContentHandlergetDTDHandlergetEntityResolvergetErrorHandlergetFeaturegetParentgetPropertynotationDeclparseparseresolveEntitysetContentHandlersetDocumentLocatorsetDTDHandlersetEntityResolversetErrorHandlersetFeaturesetParentsetPropertyunparsedEntityDeclwarning

Field Detail

saxHandlerback to summary
private JDOMResult.FragmentHandler saxHandler

The actual JDOM document builder.

startDocumentReceivedback to summary
private boolean startDocumentReceived

Whether the startDocument event was received. Some XSLT processors such as Oracle's do not fire this event.

Constructor Detail

DocumentBuilderback to summary
public DocumentBuilder()

Public default constructor.

Method Detail

charactersback to summary
public void characters(char[] ch, int start, int length) throws SAXException

Overrides org.xml.sax.helpers.XMLFilterImpl.characters.

Implements org.xml.sax.ContentHandler.characters.

[SAX ContentHandler interface support] Receives notification of character data.

Parameters
ch:char[]

Doc from org.xml.sax.helpers.XMLFilterImpl.characters.

An array of characters.

start:int

Doc from org.xml.sax.helpers.XMLFilterImpl.characters.

The starting position in the array.

length:int

Doc from org.xml.sax.helpers.XMLFilterImpl.characters.

The number of characters to use from the array.

Exceptions
SAXException:

Doc from org.xml.sax.helpers.XMLFilterImpl.characters.

The client may throw an exception during processing.

commentback to summary
public void comment(char[] ch, int start, int length) throws SAXException

Implements org.xml.sax.ext.LexicalHandler.comment.

[SAX LexicalHandler interface support] Reports an XML comment anywhere in the document.

Parameters
ch:char[]

an array holding the characters in the comment.

start:int

the starting position in the array.

length:int

the number of characters to use from the array.

Exceptions
SAXException:
The application may raise an exception.
endCDATAback to summary
public void endCDATA() throws SAXException

Implements org.xml.sax.ext.LexicalHandler.endCDATA.

[SAX LexicalHandler interface support] Reports the end of a CDATA section.

Exceptions
SAXException:
The application may raise an exception.
endDTDback to summary
public void endDTD() throws SAXException

Implements org.xml.sax.ext.LexicalHandler.endDTD.

[SAX LexicalHandler interface support] Reports the end of DTD declarations.

Exceptions
SAXException:
The application may raise an exception.
endEntityback to summary
public void endEntity(String name) throws SAXException

Implements org.xml.sax.ext.LexicalHandler.endEntity.

[SAX LexicalHandler interface support] Reports the end of an entity.

Parameters
name:String

the name of the entity that is ending.

Exceptions
SAXException:
The application may raise an exception.
ensureInitializationback to summary
private void ensureInitialization() throws SAXException
getResultback to summary
public List<E> getResult()

Returns the result of an XSL Transformation.

Returns:List<E>

the transformation result as a (possibly empty) list of JDOM nodes (Elements, Texts, Comments, PIs...) or null if no new transformation occurred since the result of the previous one was returned.

ignorableWhitespaceback to summary
public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException

Overrides org.xml.sax.helpers.XMLFilterImpl.ignorableWhitespace.

Implements org.xml.sax.ContentHandler.ignorableWhitespace.

[SAX ContentHandler interface support] Receives notification of ignorable whitespace in element content.

Parameters
ch:char[]

Doc from org.xml.sax.helpers.XMLFilterImpl.ignorableWhitespace.

An array of characters.

start:int

Doc from org.xml.sax.helpers.XMLFilterImpl.ignorableWhitespace.

The starting position in the array.

length:int

Doc from org.xml.sax.helpers.XMLFilterImpl.ignorableWhitespace.

The number of characters to use from the array.

Exceptions
SAXException:

Doc from org.xml.sax.helpers.XMLFilterImpl.ignorableWhitespace.

The client may throw an exception during processing.

processingInstructionback to summary
public void processingInstruction(String target, String data) throws SAXException

Overrides org.xml.sax.helpers.XMLFilterImpl.processingInstruction.

Implements org.xml.sax.ContentHandler.processingInstruction.

[SAX ContentHandler interface support] Receives notification of a processing instruction.

Parameters
target:String

Doc from org.xml.sax.helpers.XMLFilterImpl.processingInstruction.

The processing instruction target.

data:String

Doc from org.xml.sax.helpers.XMLFilterImpl.processingInstruction.

The text following the target.

Exceptions
SAXException:

Doc from org.xml.sax.helpers.XMLFilterImpl.processingInstruction.

The client may throw an exception during processing.

skippedEntityback to summary
public void skippedEntity(String name) throws SAXException

Overrides org.xml.sax.helpers.XMLFilterImpl.skippedEntity.

Implements org.xml.sax.ContentHandler.skippedEntity.

[SAX ContentHandler interface support] Receives notification of a skipped entity.

Parameters
name:String

Doc from org.xml.sax.helpers.XMLFilterImpl.skippedEntity.

The name of the skipped entity.

Exceptions
SAXException:

Doc from org.xml.sax.helpers.XMLFilterImpl.skippedEntity.

The client may throw an exception during processing.

startCDATAback to summary
public void startCDATA() throws SAXException

Implements org.xml.sax.ext.LexicalHandler.startCDATA.

[SAX LexicalHandler interface support] Reports the start of a CDATA section.

Exceptions
SAXException:
The application may raise an exception.
startDocumentback to summary
public void startDocument() throws SAXException

Overrides org.xml.sax.helpers.XMLFilterImpl.startDocument.

Implements org.xml.sax.ContentHandler.startDocument.

[SAX ContentHandler interface support] Processes a start of document event.

This implementation creates a new JDOM document builder and marks the current result as "under construction".

Exceptions
SAXException:
if any error occurred while creating the document builder.
startDTDback to summary
public void startDTD(String name, String publicId, String systemId) throws SAXException

Implements org.xml.sax.ext.LexicalHandler.startDTD.

[SAX LexicalHandler interface support] Reports the start of DTD declarations, if any.

Parameters
name:String

the document type name.

publicId:String

the declared public identifier for the external DTD subset, or null if none was declared.

systemId:String

the declared system identifier for the external DTD subset, or null if none was declared.

Exceptions
SAXException:
The application may raise an exception.
startElementback to summary
public void startElement(String nsURI, String localName, String qName, Attributes atts) throws SAXException

Overrides org.xml.sax.helpers.XMLFilterImpl.startElement.

Implements org.xml.sax.ContentHandler.startElement.

[SAX ContentHandler interface support] Receives notification of the beginning of an element.

This implementation ensures that startDocument() has been called prior processing an element.

Parameters
nsURI:String

the Namespace URI, or the empty string if the element has no Namespace URI or if Namespace processing is not being performed.

localName:String

the local name (without prefix), or the empty string if Namespace processing is not being performed.

qName:String

the qualified name (with prefix), or the empty string if qualified names are not available.

atts:Attributes

The attributes attached to the element. If there are no attributes, it shall be an empty Attributes object.

Exceptions
SAXException:
if any error occurred while creating the document builder.
startEntityback to summary
public void startEntity(String name) throws SAXException

Implements org.xml.sax.ext.LexicalHandler.startEntity.

[SAX LexicalHandler interface support] Reports the beginning of some internal and external XML entities.

Parameters
name:String

the name of the entity. If it is a parameter entity, the name will begin with '%', and if it is the external DTD subset, it will be "[dtd]".

Exceptions
SAXException:
The application may raise an exception.
startPrefixMappingback to summary
public void startPrefixMapping(String prefix, String uri) throws SAXException

Overrides org.xml.sax.helpers.XMLFilterImpl.startPrefixMapping.

Implements org.xml.sax.ContentHandler.startPrefixMapping.

[SAX ContentHandler interface support] Begins the scope of a prefix-URI Namespace mapping.

Parameters
prefix:String

Doc from org.xml.sax.helpers.XMLFilterImpl.startPrefixMapping.

The Namespace prefix.

uri:String

Doc from org.xml.sax.helpers.XMLFilterImpl.startPrefixMapping.

The Namespace URI.

Exceptions
SAXException:

Doc from org.xml.sax.helpers.XMLFilterImpl.startPrefixMapping.

The client may throw an exception during processing.

org.jdom.transform back to summary

private Class JDOMResult.FragmentHandler

extends SAXHandler
Class Inheritance

Field Summary

Modifier and TypeField and Description
private Element
dummyRoot

A dummy root element required by SAXHandler that can only cope with well-formed documents.

Constructor Summary

AccessConstructor and Description
public
FragmentHandler(JDOMFactory factory)

Public constructor.

Method Summary

Modifier and TypeMethod and Description
private List<E>

Returns:

a (possibly empty) list of JDOM nodes, detached from their parent.
getDetachedContent
(Element
the element to get the content from.
elt
)

Returns the content of a JDOM Element detached from it.

public List<E>

Returns:

the transformation result as a (possibly empty) list of JDOM nodes (Elements, Texts, Comments, PIs...).
getResult
()

Returns the result of an XSL Transformation.

Inherited from org.jdom.input.SAXHandler:
attributeDeclcharacterscommentelementDeclendCDATAendDTDendElementendEntityexternalEntityDeclflushCharactersflushCharactersgetCurrentElementgetDocumentgetDocumentLocatorgetExpandEntitiesgetFactorygetIgnoringBoundaryWhitespacegetIgnoringElementContentWhitespaceignorableWhitespaceinternalEntityDeclnotationDeclprocessingInstructionpushElementsetDocumentLocatorsetExpandEntitiessetIgnoringBoundaryWhitespacesetIgnoringElementContentWhitespaceskippedEntitystartCDATAstartDocumentstartDTDstartElementstartEntitystartPrefixMappingunparsedEntityDecl

Field Detail

dummyRootback to summary
private Element dummyRoot

A dummy root element required by SAXHandler that can only cope with well-formed documents.

Constructor Detail

FragmentHandlerback to summary
public FragmentHandler(JDOMFactory factory)

Public constructor.

Method Detail

getDetachedContentback to summary
private List<E> getDetachedContent(Element elt)

Returns the content of a JDOM Element detached from it.

Parameters
elt:Element

the element to get the content from.

Returns:List<E>

a (possibly empty) list of JDOM nodes, detached from their parent.

getResultback to summary
public List<E> getResult()

Returns the result of an XSL Transformation.

Returns:List<E>

the transformation result as a (possibly empty) list of JDOM nodes (Elements, Texts, Comments, PIs...).