public class XBProjector extends Object implements Serializable, ProjectionFactory
Overview
The class XMLProjector is a tool to create, read or write so called "projections". Projections
are Java interfaces associated to XML documents. Projections may contain methods annotated with
XPath selectors. These XPath expressions define the subset of XML data which is "projected" to
Java values and objects.
Getters
For getter methods (methods with a name-prefix "get", returning a value) the XPath-selected nodes
are converted to the method return type. This works with all java primitive types, Strings, and
lists or arrays containing primitives or Strings.
Setters
Setter methods (method with a name starting with "set", having a parameter) can be defined to
modify the content of the associated XML document. Not all XPath capabilities define writable
projections, so the syntax is limited to selectors of elements and attributes. In contrast to
Java Beans a setter method may define a return value with the projection interface type. If this
return value is defined, the current projection instance is returned. This allows the definition
of projections according to the fluent interface pattern (aka Builder Pattern).
Sub Projections
For the purpose of accessing structured data elements in the XML document you may define
"sub projections" which are projections associated to elements instead to documents. Sub
projections can be used as return type of getters and as parameters of setters. This works even
in arrays or lists. Because of the infamous Java type erasure you have to specify the component
type of the sub projection for a getter returning a list of sub projections. This type is defined
as second parameter "targetType" in the XBRead
annotation.
Dynamic Projections
XPath expressions are evaluated during runtime when the corresponding methods are called. Its
possible to use placeholder ("{0}, {1}, {2},... ) in the expression that will be substituted with
method parameters before the expression is evaluated. Therefore getters and setters may have
multiple parameters which will be applied via a MessageFormat
to build up the final XPath
expression. The first parameter of a setter will be used for both, setting the document value and
replacing the placeholder "{0}".
Projection Mixins
A mixin is defined as an object implementing a super interface of a projection. You may associate
a mixin with a projection type to add your own code to a projection. This way you can implement
validators, make a projection comparable or even share common business logic between multiple
projections.
Modifier and Type | Class and Description |
---|---|
class |
XBProjector.ConfigBuilder
A variation of the builder pattern.
|
static class |
XBProjector.Flags
Global projector configuration options.
|
Constructor and Description |
---|
XBProjector(XBProjector.Flags... optionalFlags)
Constructor.
|
XBProjector(XMLFactoriesConfig xMLFactoriesConfig,
XBProjector.Flags... optionalFlags) |
Modifier and Type | Method and Description |
---|---|
String |
asString(Object projection)
Convert a projection to XML string.
|
<T> XBAutoMap<T> |
autoMapEmptyDocument(Class<T> valueType)
Create an empty document and bind an XBAutoMap to it.
|
XBProjector.ConfigBuilder |
config()
Shortcut for creating a
XBProjector.ConfigBuilder object to change the projectors configuration. |
Set<XBProjector.Flags> |
getFlags()
read only access to flags.
|
ProjectionIO |
io()
Access to the input/output features of this projector.
|
MixinHolder |
mixins()
Shortcut for creating a
MixinHolder object add or remove mixins to projections. |
CanEvaluateOrProject |
onXMLString(String xmlContent) |
<T> T |
projectDOMNode(Node documentOrElement,
Class<T> projectionInterface)
Creates a projection from XML Documents or Elements to Java.
|
<T> T |
projectEmptyDocument(Class<T> projectionInterface)
Create a new projection for an empty document.
|
<T> T |
projectEmptyElement(String name,
Class<T> projectionInterface)
Create a new projection for an empty element.
|
<T> T |
projectXMLString(String xmlContent,
Class<T> projectionInterface)
Creates a projection from XML content to Java.
|
public XBProjector(XBProjector.Flags... optionalFlags)
optionalFlags
- public XBProjector(XMLFactoriesConfig xMLFactoriesConfig, XBProjector.Flags... optionalFlags)
xMLFactoriesConfig
- optionalFlags
- public <T> T projectEmptyDocument(Class<T> projectionInterface)
projectEmptyDocument
in interface ProjectionFactory
public <T> T projectEmptyElement(String name, Class<T> projectionInterface)
projectEmptyElement
in interface ProjectionFactory
name
- Element namepublic <T> T projectDOMNode(Node documentOrElement, Class<T> projectionInterface)
projectDOMNode
in interface ProjectionFactory
documentOrElement
- XML DOM Node. May be a document or just an element.projectionInterface
- A Java interface to project the data on.public <T> T projectXMLString(String xmlContent, Class<T> projectionInterface)
projectXMLString
in interface ProjectionFactory
xmlContent
- a string with XML contentprojectionInterface
- A Java interface to project the data on.public CanEvaluateOrProject onXMLString(String xmlContent)
xmlContent
- DefaultXPathEvaluator
public XBProjector.ConfigBuilder config()
XBProjector.ConfigBuilder
object to change the projectors configuration.public MixinHolder mixins()
MixinHolder
object add or remove mixins to projections.public ProjectionIO io()
io
in interface ProjectionFactory
public String asString(Object projection)
ProjectionFactory
asString
in interface ProjectionFactory
projection
- public Set<XBProjector.Flags> getFlags()
Copyright © 2012-12-21-2018. All Rights Reserved.