Package org.restlet.ext.xml
Class XmlRepresentation
- java.lang.Object
-
- All Implemented Interfaces:
javax.xml.namespace.NamespaceContext
- Direct Known Subclasses:
DomRepresentation,SaxRepresentation
public abstract class XmlRepresentation extends WriterRepresentation implements javax.xml.namespace.NamespaceContext
Representation based on an XML document. It knows how to evaluate XPath expressions and how to manage a namespace context. This class also offers convenient methods to validate the document against a specified XML scheme.
SECURITY WARNING: Using XML parsers configured to not prevent nor limit document type definition (DTD) entity resolution can expose the parser to an XML Entity Expansion injection attack.- See Also:
- XML Entity Expansion injection attack
-
-
Field Summary
Fields Modifier and Type Field Description private booleancoalescingSpecifies that the parser will convert CDATA nodes to text nodes and append it to the adjacent (if any) text node.private org.xml.sax.EntityResolverentityResolverA SAXEntityResolverto use when resolving external entity references while parsing this type of XML representations.private org.xml.sax.ErrorHandlererrorHandlerA SAXErrorHandlerto use for signaling SAX exceptions while parsing this type of XML representations.private booleanexpandingEntityRefsSpecifies that the parser will expand entity reference nodes.private booleanignoringCommentsIndicates if the parser will ignore comments.private booleanignoringExtraWhitespacesIndicates if the parser will ignore extra white spaces in element content.private booleannamespaceAwareIndicates if processing is namespace aware.private java.util.Map<java.lang.String,java.lang.String>namespacesInternal map of namespaces.private javax.xml.validation.SchemaschemaA (compiled)Schemato use when validating this type of XML representations.private booleanvalidatingDtdIndicates the desire for validating this type of XML representations against a DTD.private booleanxIncludeAwareIndicates the desire for processing XInclude if found in this type of XML representations.static booleanXML_EXPANDING_ENTITY_REFSTrue for expanding entity references when parsing XML representations; default value provided by system property "org.restlet.ext.xml.expandingEntityRefs", false by default.static booleanXML_VALIDATING_DTDTrue for validating DTD documents when parsing XML representations; default value provided by system property "org.restlet.ext.xml.validatingDtd", false by default.-
Fields inherited from class org.restlet.representation.Representation
UNKNOWN_SIZE
-
-
Constructor Summary
Constructors Constructor Description XmlRepresentation(MediaType mediaType)Constructor.XmlRepresentation(MediaType mediaType, long expectedSize)Constructor.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description private static voidappendTextContent(org.w3c.dom.Node node, java.lang.StringBuilder sb)Appends the text content of a given node and its descendants to the given buffer.java.lang.BooleangetBoolean(java.lang.String expression)Evaluates an XPath expression as a boolean.protected org.w3c.dom.DocumentgetDocument()Returns the XML representation as a DOM document.protected javax.xml.parsers.DocumentBuildergetDocumentBuilder()Returns a document builder properly configured.javax.xml.transform.dom.DOMSourcegetDomSource()Returns a DOM source.org.xml.sax.EntityResolvergetEntityResolver()Return the possibly null current SAXEntityResolver.org.xml.sax.ErrorHandlergetErrorHandler()Return the possibly null current SAXErrorHandler.abstract org.xml.sax.InputSourcegetInputSource()Returns the XML representation as a SAX input source.java.util.Map<java.lang.String,java.lang.String>getNamespaces()Returns the map of namespaces.java.lang.StringgetNamespaceURI(java.lang.String prefix){@inheritDoc javax.xml.namespace.NamespaceContext#getNamespaceURI(java.lang.String}org.w3c.dom.NodegetNode(java.lang.String expression)Evaluates an XPath expression as a DOM Node.NodeListgetNodes(java.lang.String expression)Evaluates an XPath expression as a DOM NodeList.java.lang.DoublegetNumber(java.lang.String expression)Evaluates an XPath expression as a number.java.lang.StringgetPrefix(java.lang.String namespaceURI){@inheritDoc javax.xml.namespace.NamespaceContext#getPrefix(java.lang.String}java.util.Iterator<java.lang.String>getPrefixes(java.lang.String namespaceURI){@inheritDoc javax.xml.namespace.NamespaceContext#getPrefixes(java.lang.String}javax.xml.transform.sax.SAXSourcegetSaxSource()Returns a SAX source.static javax.xml.transform.sax.SAXSourcegetSaxSource(Representation xmlRepresentation)Returns a SAX source.javax.xml.validation.SchemagetSchema()Return the possibly nullSchemato use for this type of XML representations.private static javax.xml.validation.SchemagetSchema(Representation schemaRepresentation)Returns the wrapped schema.private static java.lang.StringgetSchemaLanguageUri(Representation schemaRepresentation)Returns the schema URI for the current schema media type.javax.xml.transform.stream.StreamSourcegetStreamSource()Returns a stream of XML markup.java.lang.StringgetText(java.lang.String expression)Evaluates an XPath expression as a string.static java.lang.StringgetTextContent(org.w3c.dom.Node node)Returns the text content of a given node and its descendants.private java.lang.ObjectinternalEval(java.lang.String expression, javax.xml.namespace.QName returnType)Evaluates an XPath expression and returns the result as in the given return type.booleanisCoalescing()Indicates if the parser should be coalescing text.booleanisExpandingEntityRefs()Indicates if the parser will expand entity reference nodes.booleanisIgnoringComments()Indicates if the parser will ignore comments.booleanisIgnoringExtraWhitespaces()Indicates if the parser will ignore extra white spaces in element content.booleanisNamespaceAware()Indicates if processing is namespace aware.booleanisValidatingDtd()Indicates the desire for validating this type of XML representations against an XML schema if one is referenced within the contents.booleanisXIncludeAware()Indicates the desire for processing XInclude if found in this type of XML representations.voidrelease()Releases the namespaces map.voidsetCoalescing(boolean coalescing)Indicates if the parser should be coalescing text.voidsetEntityResolver(org.xml.sax.EntityResolver entityResolver)Set theEntityResolverto use when resolving external entity references encountered in this type of XML representations.voidsetErrorHandler(org.xml.sax.ErrorHandler errorHandler)Set theErrorHandlerto use when signaling SAX event exceptions.voidsetExpandingEntityRefs(boolean expandEntityRefs)Indicates if the parser will expand entity reference nodes.voidsetIgnoringComments(boolean ignoringComments)Indicates if the parser will ignore comments.voidsetIgnoringExtraWhitespaces(boolean ignoringExtraWhitespaces)Indicates if the parser will ignore extra white spaces in element content.voidsetNamespaceAware(boolean namespaceAware)Indicates if processing is namespace aware.voidsetNamespaces(java.util.Map<java.lang.String,java.lang.String> namespaces)Sets the map of namespaces.voidsetSchema(javax.xml.validation.Schema schema)Set a (compiled)Schemato use when parsing and validating this type of XML representations.voidsetSchema(Representation schemaRepresentation)Set a schema representation to be compiled and used when parsing and validating this type of XML representations.voidsetValidatingDtd(boolean validating)Indicates the desire for validating this type of XML representations against an XML schema if one is referenced within the contents.voidsetXIncludeAware(boolean includeAware)Indicates the desire for processing XInclude if found in this type of XML representations.voidvalidate(javax.xml.validation.Schema schema)Validates the XML representation against a given schema.voidvalidate(javax.xml.validation.Schema schema, javax.xml.transform.Result result)Validates the XML representation against a given schema.voidvalidate(Representation schemaRepresentation)Validates the XML representation against a given schema.voidvalidate(Representation schemaRepresentation, javax.xml.transform.Result result)Validates the XML representation against a given schema.-
Methods inherited from class org.restlet.representation.WriterRepresentation
getReader
-
Methods inherited from class org.restlet.representation.CharacterRepresentation
getChannel, getStream, write, write
-
Methods inherited from class org.restlet.representation.Representation
append, exhaust, getAvailableSize, getDigest, getDisposition, getExpirationDate, getRange, getRegistration, getSize, getText, hasKnownSize, isAvailable, isEmpty, isSelectable, isTransient, setAvailable, setDigest, setDisposition, setExpirationDate, setListener, setRange, setSize, setTransient, write
-
Methods inherited from class org.restlet.representation.RepresentationInfo
getModificationDate, getTag, setModificationDate, setTag
-
Methods inherited from class org.restlet.representation.Variant
createClientInfo, equals, getCharacterSet, getEncodings, getLanguages, getLocationRef, getMediaType, hashCode, includes, isCompatible, setCharacterSet, setEncodings, setLanguages, setLocationRef, setLocationRef, setMediaType, toString
-
-
-
-
Field Detail
-
XML_EXPANDING_ENTITY_REFS
public static final boolean XML_EXPANDING_ENTITY_REFS
True for expanding entity references when parsing XML representations; default value provided by system property "org.restlet.ext.xml.expandingEntityRefs", false by default.
-
XML_VALIDATING_DTD
public static final boolean XML_VALIDATING_DTD
True for validating DTD documents when parsing XML representations; default value provided by system property "org.restlet.ext.xml.validatingDtd", false by default.
-
coalescing
private volatile boolean coalescing
Specifies that the parser will convert CDATA nodes to text nodes and append it to the adjacent (if any) text node. By default the value of this is set to false.
-
entityResolver
private volatile org.xml.sax.EntityResolver entityResolver
A SAXEntityResolverto use when resolving external entity references while parsing this type of XML representations.- See Also:
DocumentBuilder.setEntityResolver(EntityResolver)
-
errorHandler
private volatile org.xml.sax.ErrorHandler errorHandler
A SAXErrorHandlerto use for signaling SAX exceptions while parsing this type of XML representations.- See Also:
DocumentBuilder.setErrorHandler(ErrorHandler)
-
expandingEntityRefs
private volatile boolean expandingEntityRefs
Specifies that the parser will expand entity reference nodes. By default the value of this is set to true.
-
ignoringComments
private volatile boolean ignoringComments
Indicates if the parser will ignore comments. By default the value of this is set to false.
-
ignoringExtraWhitespaces
private volatile boolean ignoringExtraWhitespaces
Indicates if the parser will ignore extra white spaces in element content. By default the value of this is set to false.
-
namespaceAware
private volatile boolean namespaceAware
Indicates if processing is namespace aware.
-
namespaces
private volatile java.util.Map<java.lang.String,java.lang.String> namespaces
Internal map of namespaces.
-
schema
private volatile javax.xml.validation.Schema schema
A (compiled)Schemato use when validating this type of XML representations.- See Also:
DocumentBuilderFactory.setSchema(javax.xml.validation.Schema)
-
validatingDtd
private volatile boolean validatingDtd
Indicates the desire for validating this type of XML representations against a DTD. Note that for XML schema or Relax NG validation, use the "schema" property instead.- See Also:
DocumentBuilderFactory.setValidating(boolean)
-
xIncludeAware
private volatile boolean xIncludeAware
Indicates the desire for processing XInclude if found in this type of XML representations. By default the value of this is set to false.- See Also:
DocumentBuilderFactory.setXIncludeAware(boolean)
-
-
Constructor Detail
-
XmlRepresentation
public XmlRepresentation(MediaType mediaType)
Constructor.- Parameters:
mediaType- The representation's mediaType.
-
XmlRepresentation
public XmlRepresentation(MediaType mediaType, long expectedSize)
Constructor.- Parameters:
mediaType- The representation's mediaType.expectedSize- The expected input stream size.
-
-
Method Detail
-
appendTextContent
private static void appendTextContent(org.w3c.dom.Node node, java.lang.StringBuilder sb)Appends the text content of a given node and its descendants to the given buffer.- Parameters:
node- The node.sb- The buffer.
-
getSaxSource
public static javax.xml.transform.sax.SAXSource getSaxSource(Representation xmlRepresentation) throws java.io.IOException
Returns a SAX source.- Parameters:
xmlRepresentation- The XML representation to wrap.- Returns:
- A SAX source.
- Throws:
java.io.IOException
-
getSchema
private static javax.xml.validation.Schema getSchema(Representation schemaRepresentation) throws java.lang.Exception
Returns the wrapped schema.- Returns:
- The wrapped schema.
- Throws:
java.io.IOExceptionjava.lang.Exception
-
getSchemaLanguageUri
private static java.lang.String getSchemaLanguageUri(Representation schemaRepresentation)
Returns the schema URI for the current schema media type.- Returns:
- The schema URI.
-
getTextContent
public static java.lang.String getTextContent(org.w3c.dom.Node node)
Returns the text content of a given node and its descendants.- Parameters:
node- The node.- Returns:
- The text content of a given node.
-
getBoolean
public java.lang.Boolean getBoolean(java.lang.String expression)
Evaluates an XPath expression as a boolean. If the evaluation fails, null will be returned.- Returns:
- The evaluation result.
-
getDocument
protected org.w3c.dom.Document getDocument() throws java.lang.ExceptionReturns the XML representation as a DOM document.- Returns:
- The DOM document.
- Throws:
java.lang.Exception
-
getDocumentBuilder
protected javax.xml.parsers.DocumentBuilder getDocumentBuilder() throws java.io.IOExceptionReturns a document builder properly configured.- Returns:
- A document builder properly configured.
- Throws:
java.io.IOException
-
getDomSource
public javax.xml.transform.dom.DOMSource getDomSource() throws java.io.IOExceptionReturns a DOM source.- Returns:
- A DOM source.
- Throws:
java.io.IOException
-
getEntityResolver
public org.xml.sax.EntityResolver getEntityResolver()
Return the possibly null current SAXEntityResolver.- Returns:
- The possibly null current SAX
EntityResolver.
-
getErrorHandler
public org.xml.sax.ErrorHandler getErrorHandler()
Return the possibly null current SAXErrorHandler.- Returns:
- The possibly null current SAX
ErrorHandler.
-
getInputSource
public abstract org.xml.sax.InputSource getInputSource() throws java.io.IOExceptionReturns the XML representation as a SAX input source.- Returns:
- The SAX input source.
- Throws:
java.io.IOException
-
getNamespaces
public java.util.Map<java.lang.String,java.lang.String> getNamespaces()
Returns the map of namespaces. Namespace prefixes are keys and URI references are values.- Returns:
- The map of namespaces.
-
getNamespaceURI
public java.lang.String getNamespaceURI(java.lang.String prefix)
{@inheritDoc javax.xml.namespace.NamespaceContext#getNamespaceURI(java.lang.String}- Specified by:
getNamespaceURIin interfacejavax.xml.namespace.NamespaceContext
-
getNode
public org.w3c.dom.Node getNode(java.lang.String expression)
Evaluates an XPath expression as a DOM Node. If the evaluation fails, null will be returned.- Returns:
- The evaluation result.
-
getNodes
public NodeList getNodes(java.lang.String expression)
Evaluates an XPath expression as a DOM NodeList. If the evaluation fails, null will be returned.- Returns:
- The evaluation result.
-
getNumber
public java.lang.Double getNumber(java.lang.String expression)
Evaluates an XPath expression as a number. If the evaluation fails, null will be returned.- Returns:
- The evaluation result.
-
getPrefix
public java.lang.String getPrefix(java.lang.String namespaceURI)
{@inheritDoc javax.xml.namespace.NamespaceContext#getPrefix(java.lang.String}- Specified by:
getPrefixin interfacejavax.xml.namespace.NamespaceContext
-
getPrefixes
public java.util.Iterator<java.lang.String> getPrefixes(java.lang.String namespaceURI)
{@inheritDoc javax.xml.namespace.NamespaceContext#getPrefixes(java.lang.String}- Specified by:
getPrefixesin interfacejavax.xml.namespace.NamespaceContext
-
getSaxSource
public javax.xml.transform.sax.SAXSource getSaxSource() throws java.io.IOExceptionReturns a SAX source.- Returns:
- A SAX source.
- Throws:
java.io.IOException
-
getSchema
public javax.xml.validation.Schema getSchema()
Return the possibly nullSchemato use for this type of XML representations.- Returns:
- the
Schemaobject of this type of XML representations.
-
getStreamSource
public javax.xml.transform.stream.StreamSource getStreamSource() throws java.io.IOExceptionReturns a stream of XML markup.- Returns:
- A stream of XML markup.
- Throws:
java.io.IOException
-
getText
public java.lang.String getText(java.lang.String expression)
Evaluates an XPath expression as a string.- Returns:
- The evaluation result.
-
internalEval
private java.lang.Object internalEval(java.lang.String expression, javax.xml.namespace.QName returnType)Evaluates an XPath expression and returns the result as in the given return type.- Parameters:
returnType- The qualified name of the return type.- Returns:
- The evaluation result.
-
isCoalescing
public boolean isCoalescing()
Indicates if the parser should be coalescing text. If true the parser will convert CDATA nodes to text nodes and append it to the adjacent (if any) text node. By default the value of this is set to false.- Returns:
- True if parser should be coalescing text.
-
isExpandingEntityRefs
public boolean isExpandingEntityRefs()
Indicates if the parser will expand entity reference nodes. By default the value of this is set to true.- Returns:
- True if the parser will expand entity reference nodes.
-
isIgnoringComments
public boolean isIgnoringComments()
Indicates if the parser will ignore comments. By default the value of this is set to false.- Returns:
- True if the parser will ignore comments.
-
isIgnoringExtraWhitespaces
public boolean isIgnoringExtraWhitespaces()
Indicates if the parser will ignore extra white spaces in element content. Note that theisValidatingDtd()must be true when this property is 'true' as validation is needed for it to work. By default the value of this is set to false.- Returns:
- True if the parser will ignore extra white spaces.
-
isNamespaceAware
public boolean isNamespaceAware()
Indicates if processing is namespace aware.- Returns:
- True if processing is namespace aware.
-
isValidatingDtd
public boolean isValidatingDtd()
Indicates the desire for validating this type of XML representations against an XML schema if one is referenced within the contents.- Returns:
- True if the schema-based validation is enabled.
-
isXIncludeAware
public boolean isXIncludeAware()
Indicates the desire for processing XInclude if found in this type of XML representations. By default the value of this is set to false.- Returns:
- The current value of the xIncludeAware flag.
-
release
public void release()
Releases the namespaces map.- Overrides:
releasein classRepresentation
-
setCoalescing
public void setCoalescing(boolean coalescing)
Indicates if the parser should be coalescing text. If true the parser will convert CDATA nodes to text nodes and append it to the adjacent (if any) text node. By default the value of this is set to false.- Parameters:
coalescing- True if parser should be coalescing text.
-
setEntityResolver
public void setEntityResolver(org.xml.sax.EntityResolver entityResolver)
Set theEntityResolverto use when resolving external entity references encountered in this type of XML representations.- Parameters:
entityResolver- theEntityResolverto set.
-
setErrorHandler
public void setErrorHandler(org.xml.sax.ErrorHandler errorHandler)
Set theErrorHandlerto use when signaling SAX event exceptions.- Parameters:
errorHandler- theErrorHandlerto set.
-
setExpandingEntityRefs
public void setExpandingEntityRefs(boolean expandEntityRefs)
Indicates if the parser will expand entity reference nodes. By default the value of this is set to true.- Parameters:
expandEntityRefs- True if the parser will expand entity reference nodes.
-
setIgnoringComments
public void setIgnoringComments(boolean ignoringComments)
Indicates if the parser will ignore comments. By default the value of this is set to false.- Parameters:
ignoringComments- True if the parser will ignore comments.
-
setIgnoringExtraWhitespaces
public void setIgnoringExtraWhitespaces(boolean ignoringExtraWhitespaces)
Indicates if the parser will ignore extra white spaces in element content. Note that thesetValidatingDtd(boolean)will be invoked with 'true' if setting this property to 'true' as validation is needed for it to work.- Parameters:
ignoringExtraWhitespaces- True if the parser will ignore extra white spaces in element content.
-
setNamespaceAware
public void setNamespaceAware(boolean namespaceAware)
Indicates if processing is namespace aware.- Parameters:
namespaceAware- Indicates if processing is namespace aware.
-
setNamespaces
public void setNamespaces(java.util.Map<java.lang.String,java.lang.String> namespaces)
Sets the map of namespaces.- Parameters:
namespaces- The map of namespaces.
-
setSchema
public void setSchema(javax.xml.validation.Schema schema)
Set a (compiled)Schemato use when parsing and validating this type of XML representations.- Parameters:
schema- The (compiled)Schemaobject to set.
-
setSchema
public void setSchema(Representation schemaRepresentation)
Set a schema representation to be compiled and used when parsing and validating this type of XML representations.- Parameters:
schemaRepresentation- The schema representation to set.
-
setValidatingDtd
public void setValidatingDtd(boolean validating)
Indicates the desire for validating this type of XML representations against an XML schema if one is referenced within the contents.- Parameters:
validating- The new validation flag to set.
-
setXIncludeAware
public void setXIncludeAware(boolean includeAware)
Indicates the desire for processing XInclude if found in this type of XML representations. By default the value of this is set to false.- Parameters:
includeAware- The new value of the xIncludeAware flag.
-
validate
public void validate(javax.xml.validation.Schema schema) throws java.lang.ExceptionValidates the XML representation against a given schema.- Parameters:
schema- The XML schema to use.- Throws:
java.lang.Exception
-
validate
public void validate(javax.xml.validation.Schema schema, javax.xml.transform.Result result) throws java.lang.ExceptionValidates the XML representation against a given schema.- Parameters:
schema- The XML schema to use.result- The Result object that receives (possibly augmented) XML.- Throws:
java.lang.Exception
-
validate
public void validate(Representation schemaRepresentation) throws java.lang.Exception
Validates the XML representation against a given schema.- Parameters:
schemaRepresentation- The XML schema representation to use.- Throws:
java.lang.Exception
-
validate
public void validate(Representation schemaRepresentation, javax.xml.transform.Result result) throws java.lang.Exception
Validates the XML representation against a given schema.- Parameters:
schemaRepresentation- The XML schema representation to use.result- The Result object that receives (possibly augmented) XML.- Throws:
java.lang.Exception
-
-