public class XmlField extends Object
// Source Xml String xml ="<modelRootTag></modelRootTag>"; // Read doc XmlField xf = new XmlField(); IModel model = xf.xmlToObject(xmlRessource, IModel.class) // Back to XML. xml = xf.objectToXml( model);
This class and return objects are not thread safe. If the same objects are used concurrently in multiple threads, be sure to synchronize or use XmlFieldFactory with ThreadLocal enabled.
Constructor and Description |
---|
XmlField()
Create XmlField object for xml/object manipulations.
|
XmlField(Map<String,String> parserConfiguration)
Create XmlField object for xml/object manipulations.
|
Modifier and Type | Method and Description |
---|---|
XmlFieldNodeModifier |
_getModifier()
Returns the modifier associated with this XmlField object.
|
XmlFieldNodeParser |
_getParser()
Returns the parser associated with this XmlField object.
|
XmlFieldSelector |
_getSelector()
Returns the selector associated with this XmlField object.
|
<T> T |
castObject(Object o,
Class<T> type)
Changes interface of an already attached node.
|
Map<String,String> |
getParserConfiguration()
Returns the current parser configuration.
|
boolean |
isGetterCache() |
<T> T |
newObject(Class<T> type)
instantiate a new XmlField interface.
|
<T> T[] |
nodeToArray(String resourceXPath,
XmlFieldNode node,
Class<T> type) |
<T> T[] |
nodeToArray(XmlFieldNode node,
Class<T> type) |
Object[] |
nodeToExplicitArray(String resourceXPath,
XmlFieldNode node,
Map<String,Class<?>> explicitCollection)
Function to attach an array of different objects type .
|
<T> T |
nodeToObject(String resourceXPath,
XmlFieldNode node,
Class<T> resourceType)
Bind a node located by the xpath expression to the specified type
|
<T> T |
nodeToObject(XmlFieldNode node,
Class<T> type)
Bind an xml field node to the specified type.
|
String |
nodeToXml(XmlFieldNode node) |
void |
nodeToXml(XmlFieldNode node,
Writer writer) |
XmlFieldNode |
objectToNode(Object o) |
String |
objectToXml(Object o) |
void |
objectToXml(Object o,
Writer writer) |
void |
setGetterCache(boolean getterCache)
Enables caching for get methods.
|
<T> T[] |
xmlToArray(String xml,
Class<T> type)
Bind an xml string to an array of entities.
|
XmlFieldNode |
xmlToNode(InputStream xmlInputStream)
Load the XML document from an input stream, load it internally in a tree
and return the root node.
|
XmlFieldNode |
xmlToNode(String xml)
Load the XML document from a string, load it internally in a tree and
return the root node.
|
<T> T |
xmlToObject(InputStream xmlContent,
Class<T> type) |
<T> T |
xmlToObject(String xml,
Class<T> type)
Create an interface for the given xml and matching the given interface.
|
public XmlField()
Use default configuration.
public XmlField(Map<String,String> parserConfiguration)
Configuration is forwarded to xml transformer.
See OutputKeys
for universal valid keys and values.
For specific transformer implementation see available documentation, for exemple, if xalan is used see org.apache.xml.serializer.OutputPropertiesFactory
BEWARE : if OutputKeys.MEDIA_TYPE
is set to anything but xml, it
can defeat XmlField purpose. this is applicable to other configuration
too.
parserConfiguration
- parser configuration. Allowed keys are specific to the parser
implementations. See DomNodeParser
for default
implementation.public XmlFieldNodeModifier _getModifier()
XmlFieldNodeModifier
instance is created on demand at the first
call.
public XmlFieldNodeParser _getParser()
XmlFieldNodeParser
instance is created on demand at the first
call.
public XmlFieldSelector _getSelector()
XmlFieldSelector
instance is created on demand at the first call.
public <T> T castObject(Object o, Class<T> type)
o
- an object obtained by any #attach
call.type
- the new interface for dom manipulation.public Map<String,String> getParserConfiguration()
The returned object is cannot be updated.
public boolean isGetterCache()
public <T> T newObject(Class<T> type) throws XmlFieldParsingException
#bind
methodsT
- Class of interface to instantiatetype
- Class of interface to instantiatetype and XmlFieldObject
ParserConfigurationException
SAXException
IOException
XmlFieldParsingException
public <T> T[] nodeToArray(String resourceXPath, XmlFieldNode node, Class<T> type) throws XmlFieldXPathException
XmlFieldXPathException
public <T> T[] nodeToArray(XmlFieldNode node, Class<T> type) throws XmlFieldXPathException
XmlFieldXPathException
public Object[] nodeToExplicitArray(String resourceXPath, XmlFieldNode node, Map<String,Class<?>> explicitCollection) throws XmlFieldXPathException
resourceXPath
- Xpath to the collection.explicitCollection
- Hashmap for matching name of xpath and clas.node
- node of java object.XmlFieldXPathException
public <T> T nodeToObject(String resourceXPath, XmlFieldNode node, Class<T> resourceType)
resourceXPath
- xpath expression used to locate the node to bindnode
- the root node XmlFieldNode
resourceType
- the expected interface classpublic <T> T nodeToObject(XmlFieldNode node, Class<T> type)
T
- interface typenode
- nodetype
- interface to bind topublic String nodeToXml(XmlFieldNode node) throws XmlFieldParsingException
XmlFieldParsingException
public void nodeToXml(XmlFieldNode node, Writer writer) throws XmlFieldParsingException
XmlFieldParsingException
public XmlFieldNode objectToNode(Object o)
public String objectToXml(Object o) throws XmlFieldParsingException
XmlFieldParsingException
public void objectToXml(Object o, Writer writer) throws XmlFieldParsingException
XmlFieldParsingException
public void setGetterCache(boolean getterCache)
Warning : this cache is experimental and cannot get changes mades when using different objects to access the same XML Node. The use of this cache is NOT recommended at the moment.
getterCache
- public <T> T[] xmlToArray(String xml, Class<T> type) throws XmlFieldException
T
- interface typexml
- an xml string with a root element enclosing a list of node to
bindtype
- interface to bind toXmlFieldParsingException
XmlFieldXPathException
XmlFieldException
public XmlFieldNode xmlToNode(InputStream xmlInputStream) throws XmlFieldParsingException
As XmlField supports multiple XML parsing engines, the return object is a generic type which wraps the actual implementation of the tree.
The document root is intended to be used with XmlField#nodeToObject(
XmlFieldNode node, Class
xmlInputStream
- Input stream on an XML document.XmlFieldParsingException
- When document is invalid, and cannot be parsed or when an
exception occurs.public XmlFieldNode xmlToNode(String xml) throws XmlFieldParsingException
As XmlField supports multiple XML parsing engines, the return object is a generic type which wraps the actual implementation of the tree.
The document root is intended to be used with XmlField#nodeToObject(
XmlFieldNode node, Class
xml
- An XML document in a single stringXmlFieldParsingException
- When document is invalid and cannot be parsed.public <T> T xmlToObject(InputStream xmlContent, Class<T> type) throws XmlFieldParsingException
xmlContent
- type
- XmlFieldParsingException
public <T> T xmlToObject(String xml, Class<T> type) throws XmlFieldParsingException
xml
- the xml String to loadtype
- the expected interfaceParserConfigurationException
- when parsing xml failedSAXException
- when parsing xml failedIOException
- when parsing xml failedXmlFieldParsingException
Copyright © 2011–2014. All rights reserved.