public class Unmarshaller
extends java.lang.Object
When an Unmarshaller encounters an XML element, it creates an instance of the corresponding AST class. It checks the "id" attribute (or assigns one) and associates this with the AST node. If an element is encountered with a "ref" attribute, it is treated as a reference and is mapped to the existing AST node whose "id" has the same value as the "ref". Other XML attributes are mapped to fields of the AST node and values are coerced to the type of the field. Dependent XML children are added as dependents of the AST node. Methods used to set attributes, or add dependent nodes, or set simple content, are reflectively discovered and cached, for use by an ASTReader or ASTBuilder.
Modifier and Type | Field and Description |
---|---|
private java.util.Map<java.lang.Class<?>,java.util.Map<java.lang.String,java.lang.reflect.Method>> |
methodMap
The map from types to property-indexed set- and add-methods.
|
private java.util.Map<java.lang.Integer,java.lang.Object> |
objectMap
The map of created AST nodes, indexed by their unique ID.
|
Constructor and Description |
---|
Unmarshaller()
Creates a Unmarshaller for mapping XML input to AST objects.
|
Modifier and Type | Method and Description |
---|---|
void |
addDependent(java.lang.Object parent,
java.lang.Object child)
Attaches a dependent AST child object to a given AST parent object.
|
void |
checkIdentity(java.lang.String identity)
Checks that the declared serial ID matches the actual serial ID of
an AST object, as it is unmarshalled.
|
private java.lang.Object |
coerceValue(java.lang.String value,
java.lang.Class<?> type)
Coerces a String value to an instance of the given type.
|
java.lang.Object |
createObject(java.lang.String javaClassID)
Factory method to create an instance of a user-defined AST class.
|
private java.lang.reflect.Method |
getAddMethod(java.lang.Class<?> type,
java.lang.Class<?> childType)
Searches the given AST class type for a public add-method for the
given child-type.
|
private java.lang.reflect.Method |
getContentMethod(java.lang.Class<?> type,
java.lang.reflect.Field field)
Searches the given AST class type for a public content-setting method
for the given content-field.
|
private java.lang.reflect.Field |
getField(java.lang.Class<?> type,
java.lang.String name)
Searches the given AST class type for a Field with the given name.
|
private java.lang.reflect.Method |
getMethodMap(java.lang.Class<?> type,
java.lang.String property)
Fetches a set-method for a property of an AST class, or an add-method
for a named dependent child of the AST class.
|
private java.lang.reflect.Method |
getSetMethod(java.lang.Class<?> type,
java.lang.reflect.Field field)
Searches the given AST class type for a public set-method for the
given Field.
|
private void |
putMethodMap(java.lang.Class<?> type,
java.lang.String property,
java.lang.reflect.Method method)
Stores a set-method for a property of an AST class, or an add-method
for a named dependent child of the AST class.
|
java.lang.Object |
restoreReference(java.lang.String reference,
java.lang.Object proxy)
Restores a re-entrant reference to an object that has already been
unmarshalled.
|
private void |
semanticError(java.lang.String message)
Reports model-building error, when the model constraints are violated.
|
void |
setContent(java.lang.Object object,
java.lang.String value)
Sets the information content of a given AST object.
|
void |
setProperty(java.lang.Object object,
java.lang.String property,
java.lang.String value)
Sets a property of an AST object to the given value.
|
private java.util.Map<java.lang.Integer,java.lang.Object> objectMap
private java.util.Map<java.lang.Class<?>,java.util.Map<java.lang.String,java.lang.reflect.Method>> methodMap
public Unmarshaller()
public void checkIdentity(java.lang.String identity) throws SemanticError
identity
- an integer serial number, as a string.SemanticError
- if the serial ID of the object is out of step
with the count of unmarshalled objects.public java.lang.Object restoreReference(java.lang.String reference, java.lang.Object proxy) throws SemanticError
reference
- an integer serial number, as a string.proxy
- the proxy object, created from the reference.SemanticError
- if there is a type mismatch between the proxy
object's type, and the type of the referenced main object.public java.lang.Object createObject(java.lang.String javaClassID) throws SemanticError
javaClassID
- the package-qualified class name.SemanticError
- if the AST class cannot be found, or cannot be
instantiated.public void addDependent(java.lang.Object parent, java.lang.Object child) throws SemanticError
parent
- the parent AST object.child
- the child AST object.SemanticError
- if no suitable add-method can be found, or if
the add-method fails.public void setProperty(java.lang.Object object, java.lang.String property, java.lang.String value) throws SemanticError
object
- an AST object.property
- a field name.value
- the value to store in the field.SemanticError
- if the AST class has no field with this name,
or no public set-method can be found, or the set-method fails.public void setContent(java.lang.Object object, java.lang.String value)
object
- an AST object.value
- the value to store as the content.private void semanticError(java.lang.String message) throws SemanticError
message
- the error message.SemanticError
- always.private java.lang.reflect.Field getField(java.lang.Class<?> type, java.lang.String name) throws SemanticError
type
- the AST class to search.name
- the name of the field.SemanticError
- if no such field exists.private java.lang.reflect.Method getSetMethod(java.lang.Class<?> type, java.lang.reflect.Field field) throws SemanticError
type
- the AST class to search.field
- the Field to be set.SemanticError
- if no such method exists.private java.lang.reflect.Method getAddMethod(java.lang.Class<?> type, java.lang.Class<?> childType) throws SemanticError
type
- the AST class to search.childType
- the AST class of the child.SemanticError
- if no such method exists.private java.lang.reflect.Method getContentMethod(java.lang.Class<?> type, java.lang.reflect.Field field) throws SemanticError
type
- the AST class to search.field
- the content-field to be set.SemanticError
- if no such method can be found.private java.lang.Object coerceValue(java.lang.String value, java.lang.Class<?> type)
value
- the String value.type
- the desired result type.private java.lang.reflect.Method getMethodMap(java.lang.Class<?> type, java.lang.String property)
type
- the AST class.property
- the property name.private void putMethodMap(java.lang.Class<?> type, java.lang.String property, java.lang.reflect.Method method)
type
- the AST class.property
- the property name.method
- the set- or add-method to use for this property.