Package | org.spicefactory.lib.xml.mapper |
Class | public class PropertyMapperBuilder |
Inheritance | PropertyMapperBuilder Object |
Subclasses | MetadataMapperBuilder |
The main entry point for most basic use cases covered by the builtin PropertyMapper implementation.
Use the various map
methods to create a mapper that maps properties of a class
to XML attributes, child elements or text nodes.
This class is primarily intended for internal use. In most cases the most convenient way to create
mappings is the use of the XmlObjectMappings
class and its DSL to quickly set up mappings
that support metadata configuration.
Property | Defined By | ||
---|---|---|---|
defaultNamingStrategy : NamingStrategy [static]
The default naming strategy to use for transforming property names to
XML attribute or element names. | PropertyMapperBuilder | ||
elementName : QName [read-only]
The name of the mapped XML element. | PropertyMapperBuilder | ||
namingStrategy : NamingStrategy [read-only]
The naming strategy to use for transforming property names to XML attribute and element names. | PropertyMapperBuilder | ||
objectType : ClassInfo [read-only]
The mapped object type. | PropertyMapperBuilder |
Method | Defined By | ||
---|---|---|---|
PropertyMapperBuilder(objectType:Class, elementName:QName, namingStrategy:NamingStrategy = null, domain:ApplicationDomain = null)
Creates a new instance for building a mapper that maps the specified objectType to the XML element
with the specified name. | PropertyMapperBuilder | ||
addPropertyHandler(handler:PropertyHandler):void
Adds a custom property handler to this builder. | PropertyMapperBuilder | ||
Builds the final mapper based on the instructions that were given through the various
mapmethods of this class. | PropertyMapperBuilder | ||
createChildElementMapperBuilder(propertyName:String, type:Class = null, elementName:QName = null):PropertyMapperBuilder
Convenient short cut to produce a mapper for a child element that will then be mapped
to a property of the class mapped by this builder. | PropertyMapperBuilder | ||
ignoreProperty(propertyName:String):void
Instructs this builder not to map the property with the specified name. | PropertyMapperBuilder | ||
ignoreUnmappedAttributes():void
Instructs this builder to ignore attributes in XML that are not mapped to properties. | PropertyMapperBuilder | ||
ignoreUnmappedChildren():void
Instructs this builder to ignore child elements in XML that are not mapped to properties. | PropertyMapperBuilder | ||
mapAllToAttributes():void
Maps all properties of the mapped class to attribute of the mapped XML element. | PropertyMapperBuilder | ||
mapAllToChildTextNodes():void
Maps all properties of the mapped class to text nodes of child elements of the mapped XML element. | PropertyMapperBuilder | ||
mapToAttribute(propertyName:String, attributeName:QName = null):void
Maps the property with the specified name to the XML attribute with the specified name. | PropertyMapperBuilder | ||
mapToChildElement(propertyName:String, mapper:XmlObjectMapper):void
Maps the property with the specified name to the object mapped to the specified XML element. | PropertyMapperBuilder | ||
mapToChildElementChoice(propertyName:String, choice:Choice):void
Maps the property with the specified name to the objects of a choice of mapped child elements. | PropertyMapperBuilder | ||
mapToChildTextNode(propertyName:String, childName:QName = null):void
Maps the property with the specified name to the XML text node of the child element with the specified name. | PropertyMapperBuilder | ||
mapToTextNode(propertyName:String):void
Maps the property with the specified name to the XML text node inside the mapped XML element. | PropertyMapperBuilder |
Method | Defined By | ||
---|---|---|---|
isMappableProperty(property:Property):Boolean
Checks whether the specified property can still be mapped. | PropertyMapperBuilder | ||
updateElementName(newName:QName):void
Updates the XML element name to apply to this mapping. | PropertyMapperBuilder |
defaultNamingStrategy | property |
public static var defaultNamingStrategy:NamingStrategy
The default naming strategy to use for transforming property names to XML attribute or element names.
elementName | property |
elementName:QName
[read-only] The name of the mapped XML element.
public function get elementName():QName
namingStrategy | property |
namingStrategy:NamingStrategy
[read-only] The naming strategy to use for transforming property names to XML attribute and element names.
public function get namingStrategy():NamingStrategy
objectType | property |
objectType:ClassInfo
[read-only] The mapped object type.
public function get objectType():ClassInfo
PropertyMapperBuilder | () | Constructor |
public function PropertyMapperBuilder(objectType:Class, elementName:QName, namingStrategy:NamingStrategy = null, domain:ApplicationDomain = null)
Creates a new instance for building a mapper that maps the specified objectType to the XML element with the specified name.
ParametersobjectType:Class — the type of the object to map to the XML element
| |
elementName:QName — the name of the XML element to map
| |
namingStrategy:NamingStrategy (default = null ) — the naming strategy to use for transforming property names to XML attribute and element names
| |
domain:ApplicationDomain (default = null ) — the ApplicationDomain to use for reflecting on the mapped type
|
addPropertyHandler | () | method |
public function addPropertyHandler(handler:PropertyHandler):void
Adds a custom property handler to this builder. May be used in rare cases where the property mapping mechanisms offered by this builder are not sufficient.
Parameters
handler:PropertyHandler — the handler to add to this builder
|
build | () | method |
public function build():XmlObjectMapper
Builds the final mapper based on the instructions that were given through the various
mapmethods of this class. The returned mapper instance is lazy-initializing, so the real
mapper does not get created until it is used for the first time through a call to mapToObject
or mapToXml
. So calling some of the mapmethods of this builder will still modify the
behaviour of the returned mapper. This mechanism makes it easier to build a nested structure of mappers
with circular references.
XmlObjectMapper — the mapper produced based on instructions given to this class
|
createChildElementMapperBuilder | () | method |
public function createChildElementMapperBuilder(propertyName:String, type:Class = null, elementName:QName = null):PropertyMapperBuilder
Convenient short cut to produce a mapper for a child element that will then be mapped to a property of the class mapped by this builder.
This is equivalent to creating the PropertyMapperBuilder
yourself,
and then passing the mapper produced by that builder to mapToChildElement
Note that you must not pass the mapper created by the returned builder to this builder. This will be done automatically for you.
Parameters
propertyName:String — the name of the property to create a child element mapper for
| |
type:Class (default = null ) — the type the child element will be mapped to (the type of the specified property if omitted)
| |
elementName:QName (default = null ) — the name of the mapped child element (if omitted the same name as the property in the same
namespace as the XML element mapped by this builder)
|
PropertyMapperBuilder — a new PropertyMapperBuilder
|
ignoreProperty | () | method |
public function ignoreProperty(propertyName:String):void
Instructs this builder not to map the property with the specified name.
Parameters
propertyName:String — the name of the property to exclude from the mapping
|
ignoreUnmappedAttributes | () | method |
public function ignoreUnmappedAttributes():void
Instructs this builder to ignore attributes in XML that are not mapped to properties.
ignoreUnmappedChildren | () | method |
public function ignoreUnmappedChildren():void
Instructs this builder to ignore child elements in XML that are not mapped to properties.
isMappableProperty | () | method |
protected function isMappableProperty(property:Property):Boolean
Checks whether the specified property can still be mapped. Returns false if the specified property has already been mapped or if it has been added to the list of properties that should be ignored.
Parameters
property:Property — the property to check
|
Boolean — true if the specified property can still be mapped
|
mapAllToAttributes | () | method |
public function mapAllToAttributes():void
Maps all properties of the mapped class to attribute of the mapped XML element.
Has the same effect as calling mapToAttribute
for all properties of the class.
mapAllToChildTextNodes | () | method |
public function mapAllToChildTextNodes():void
Maps all properties of the mapped class to text nodes of child elements of the mapped XML element.
Has the same effect as calling mapToChildTextNode
for all properties of the class.
mapToAttribute | () | method |
public function mapToAttribute(propertyName:String, attributeName:QName = null):void
Maps the property with the specified name to the XML attribute with the specified name.
Example:
public class Song {
public var year:int;
public var title:String;
public var artist:String;
}
<song
year="1989"
title="Monkey Gone To Heaven"
artist="Pixies"
/>
Calling mapToAttribute("year")
for the Song class shown above would map the year property to the
attribute in the song tag with the same name.
Parameters
propertyName:String — the name of the mapped property
| |
attributeName:QName (default = null ) — the name of the mapped XML attribute
|
mapToChildElement | () | method |
public function mapToChildElement(propertyName:String, mapper:XmlObjectMapper):void
Maps the property with the specified name to the object mapped to the specified XML element.
Mapping to child elements allows you to build a hierarchy of nested mappers since it is the responsibility of the specified mapper to handle the mapping of the child element itself.
Example:
public class Album {
public var year:int;
public var title:String;
public var artist:String;
public var songs:Array;
}
public class Song {
public var duration:String;
public var title:String;
}
<album year="2000" artist="Goldfrapp" title="Felt Mountain">
<song title="Lovely Head" duration="3:50"/>
<song title="Pilots" duration="4:30"/>
<song title="Deer Stop" duration="4:07"/>
<song title="Utopia" duration="4:18"/>
</album>
In this example the song child elements will be mapped into the songs property of the Album class. This is how you would set up such a mapper:
var songBuilder:PropertyMapperBuilder = new PropertyMapperBuilder(Song, new QName("song"));
songBuilder.mapAllToAttributes();
var albumBuilder:PropertyMapperBuilder = new PropertyMapperBuilder(Album, new QName("album"));
albumBuilder.mapToChildElement("songs", songBuilder.build());
albumBuilder.mapAllToAttributes();
For the song element we simply map all properties to attributes. For the album element we map the songs Array property to the song child element, passing the mapper that we have built for the child element. We then map all remaining properties to attributes.
Parameters
propertyName:String — the name of the property to map to the child element
| |
mapper:XmlObjectMapper — the mapper responsible for mapping the child elements to objects
|
mapToChildElementChoice | () | method |
public function mapToChildElementChoice(propertyName:String, choice:Choice):void
Maps the property with the specified name to the objects of a choice of mapped child elements.
This is a variant for the child element mapping mechanism that allows for even greater flexibility. With choices you can map several different child elements into a single Array property (or single valued property).
Example:
public class Order {
public var products:Array;
}
public class Album {
public var artist:String;
public var title:String;
public var duration:String;
}
public class Book {
public var author:String;
public var title:String;
public var pageCount:String;
}
<order>
<album artist="Goldfrapp" title="Felt Mountain" duration="38:50"/>
<album artist="Unkle" title="Never, Never, Land" duration="49:27"/>
<book author="Karen Duve" title"Rain" pageCount="256"/>
<book author="Judith Hermann" title"Summerhouse, Later" pageCount="224"/>
</order>
This time we map the products Array property of the Order class to multiple different child elements. This is how you set up the mapper:
var albumBuilder:PropertyMapperBuilder = new PropertyMapperBuilder(Album, new QName("album"));
albumBuilder.mapAllToAttributes();
var bookBuilder:PropertyMapperBuilder = new PropertyMapperBuilder(Book, new QName("book"));
bookBuilder.mapAllToAttributes();
var orderBuilder:PropertyMapperBuilder = new PropertyMapperBuilder(Order, new QName("order"));
var choice:Choice = new Choice();
choice.addMapper(albumBuilder.build());
choice.addMapper(bookBuilder.build());
orderBuilder.mapAllToChildElementChoice("products", choice);
The mappers for the Book and Album classes are simple: Both simply map all properties to attributes. For the Order class we create a Choice instance, add all child element mappers to it and finally map the products property of the Order class to the choice.
Parameters
propertyName:String — the name of the property to map to the specified choice of child elements
| |
choice:Choice — the choice instance containing all mappers for the child elements
|
mapToChildTextNode | () | method |
public function mapToChildTextNode(propertyName:String, childName:QName = null):void
Maps the property with the specified name to the XML text node of the child element with the specified name.
Example:
public class Song {
public var year:int;
public var title:String;
public var artist:String;
}
<song>
<year>1989</year>
<title>Monkey Gone To Heaven</title>
<artist>Pixies</artist>
</song>
Calling mapToChildTextNode("year")
for the Song class shown above would map the year property to the
text node of the child element with the same name.
Parameters
propertyName:String — the name of the mapped property
| |
childName:QName (default = null ) — the name of the XML element that contains the mapped text node
|
mapToTextNode | () | method |
public function mapToTextNode(propertyName:String):void
Maps the property with the specified name to the XML text node inside the mapped XML element.
This is different from mapping to child text nodes. It maps a property to the text node that belongs to the same element. Since this can only apply for a single property it is often combined with attribute mapping.
Example:
public class Song {
public var year:int;
public var title:String;
public var artist:String;
}
<song year="2000" artist="Goldfrapp">Felt Mountain</song>
Calling mapToTextNode("title")
for the Song class shown above would map the title property to the
text node of the song element ("Felt Mountain").
Parameters
propertyName:String — the name of the property to map to the text node
|
updateElementName | () | method |
protected function updateElementName(newName:QName):void
Updates the XML element name to apply to this mapping.
Parameters
newName:QName — the new name to apply to this mapping
|