org.apache.pivot.beans
Class BXMLSerializer

java.lang.Object
  extended by org.apache.pivot.beans.BXMLSerializer
All Implemented Interfaces:
Resolvable, Serializer<Object>

public class BXMLSerializer
extends Object
implements Serializer<Object>, Resolvable

Loads an object hierarchy from an XML document.


Field Summary
static String BIND_MAPPING_DELIMITER
           
static String BXML_EXTENSION
           
static String BXML_PREFIX
           
static String DEFAULT_LANGUAGE
           
static String DEFINE_TAG
           
static String ID_ATTRIBUTE
           
static String INCLUDE_INLINE_ATTRIBUTE
           
static String INCLUDE_MIME_TYPE_ATTRIBUTE
           
static String INCLUDE_RESOURCES_ATTRIBUTE
           
static String INCLUDE_SRC_ATTRIBUTE
           
static String INCLUDE_TAG
           
static String INTERNAL_ID_PREFIX
           
static String LANGUAGE_PROCESSING_INSTRUCTION
           
static String MIME_TYPE
           
static String NAMESPACE_BINDING_PREFIX
           
static String NAMESPACE_BINDING_SUFFIX
           
static String NASHORN_COMPAT_SCRIPT
           
static String NASHORN_GLOBAL
           
static char OBJECT_REFERENCE_PREFIX
           
static String REFERENCE_ID_ATTRIBUTE
           
static String REFERENCE_TAG
           
static char RESOURCE_KEY_PREFIX
           
static String SCRIPT_SRC_ATTRIBUTE
           
static String SCRIPT_TAG
           
static char SLASH_PREFIX
           
static char URL_PREFIX
           
 
Constructor Summary
BXMLSerializer()
           
BXMLSerializer(ClassLoader classLoader)
          Deprecated. 
 
Method Summary
 void bind(Object object)
          Applies BXML binding annotations to an object.
 void bind(Object object, Class<?> type)
          Applies BXML binding annotations to an object.
 Location getCurrentLocation()
          Return the current location of the XML parser.
protected  String getDefaultLanguage()
           
static Map<String,String> getFileExtensions()
          Returns the file extension/MIME type map.
 URL getLocation()
          Returns the serializer's location.
 String getMIMEType(Object object)
          Returns the MIME type of the data read and written by this serializer.
static Map<String,Class<? extends Serializer<?>>> getMimeTypes()
          Returns the MIME type/serializer class map.
 Map<String,Object> getNamespace()
          Returns's the serializer's namespace.
 Resources getResources()
          Returns the serializer's resource bundle.
 Object getRoot()
          Retrieves the root of the object hierarchy most recently processed by this serializer.
protected  XMLStreamReader getXMLStreamReader()
          Gets a read-only version of the XML stream reader that's being used by this serializer.
protected  Serializer<?> newIncludeSerializer(Class<? extends Serializer<?>> type)
          Creates a new serializer to be used on a nested include.
protected  Object newTypedObject(Class<?> type)
          Creates a new typed object as part of the deserialization process.
 Object readObject(Class<?> baseType, String resourceName)
          Deserializes an object hierarchy from a BXML resource.
 Object readObject(Class<?> baseType, String resourceName, boolean localize)
          Deserializes an object hierarchy from a BXML resource.
 Object readObject(InputStream inputStream)
          Deserializes an object hierarchy from a BXML resource.
 Object readObject(URL locationArgument)
          Deserializes an object hierarchy from a BXML resource.
 Object readObject(URL locationArgument, Resources resourcesArgument)
          Deserializes an object hierarchy from a BXML resource.
protected  void reportException(Throwable exception)
          Hook used for standardized reporting of exceptions during this process.
protected  void setDefaultLanguage(String defaultLanguage)
          Set the default script language to use for all scripts.
 void setLocation(URL location)
          Sets the serializer's location.
 void setNamespace(Map<String,Object> namespace)
          Sets the serializer's namespace.
 void setResources(Resources resources)
          Sets the serializer's resource bundle.
 void writeObject(Object object, OutputStream outputStream)
          Writes an object to an output stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

URL_PREFIX

public static final char URL_PREFIX
See Also:
Constant Field Values

RESOURCE_KEY_PREFIX

public static final char RESOURCE_KEY_PREFIX
See Also:
Constant Field Values

OBJECT_REFERENCE_PREFIX

public static final char OBJECT_REFERENCE_PREFIX
See Also:
Constant Field Values

SLASH_PREFIX

public static final char SLASH_PREFIX
See Also:
Constant Field Values

NAMESPACE_BINDING_PREFIX

public static final String NAMESPACE_BINDING_PREFIX
See Also:
Constant Field Values

NAMESPACE_BINDING_SUFFIX

public static final String NAMESPACE_BINDING_SUFFIX
See Also:
Constant Field Values

BIND_MAPPING_DELIMITER

public static final String BIND_MAPPING_DELIMITER
See Also:
Constant Field Values

INTERNAL_ID_PREFIX

public static final String INTERNAL_ID_PREFIX
See Also:
Constant Field Values

LANGUAGE_PROCESSING_INSTRUCTION

public static final String LANGUAGE_PROCESSING_INSTRUCTION
See Also:
Constant Field Values

NASHORN_GLOBAL

public static final String NASHORN_GLOBAL
See Also:
Constant Field Values

NASHORN_COMPAT_SCRIPT

public static final String NASHORN_COMPAT_SCRIPT
See Also:
Constant Field Values

BXML_PREFIX

public static final String BXML_PREFIX
See Also:
Constant Field Values

BXML_EXTENSION

public static final String BXML_EXTENSION
See Also:
Constant Field Values

ID_ATTRIBUTE

public static final String ID_ATTRIBUTE
See Also:
Constant Field Values

INCLUDE_TAG

public static final String INCLUDE_TAG
See Also:
Constant Field Values

INCLUDE_SRC_ATTRIBUTE

public static final String INCLUDE_SRC_ATTRIBUTE
See Also:
Constant Field Values

INCLUDE_RESOURCES_ATTRIBUTE

public static final String INCLUDE_RESOURCES_ATTRIBUTE
See Also:
Constant Field Values

INCLUDE_MIME_TYPE_ATTRIBUTE

public static final String INCLUDE_MIME_TYPE_ATTRIBUTE
See Also:
Constant Field Values

INCLUDE_INLINE_ATTRIBUTE

public static final String INCLUDE_INLINE_ATTRIBUTE
See Also:
Constant Field Values

SCRIPT_TAG

public static final String SCRIPT_TAG
See Also:
Constant Field Values

SCRIPT_SRC_ATTRIBUTE

public static final String SCRIPT_SRC_ATTRIBUTE
See Also:
Constant Field Values

DEFINE_TAG

public static final String DEFINE_TAG
See Also:
Constant Field Values

REFERENCE_TAG

public static final String REFERENCE_TAG
See Also:
Constant Field Values

REFERENCE_ID_ATTRIBUTE

public static final String REFERENCE_ID_ATTRIBUTE
See Also:
Constant Field Values

DEFAULT_LANGUAGE

public static final String DEFAULT_LANGUAGE
See Also:
Constant Field Values

MIME_TYPE

public static final String MIME_TYPE
See Also:
Constant Field Values
Constructor Detail

BXMLSerializer

public BXMLSerializer()

BXMLSerializer

@Deprecated
public BXMLSerializer(ClassLoader classLoader)
Deprecated. 

DO NOT USE. see https://issues.apache.org/jira/browse/PIVOT-742

Method Detail

readObject

public Object readObject(InputStream inputStream)
                  throws IOException,
                         SerializationException
Deserializes an object hierarchy from a BXML resource.

This is the base version of the method. It does not set the "location" or "resources" properties. Callers that wish to use this version of the method to load BXML that uses location or resource resolution must manually set these properties via a call to setLocation(URL) or setResources(Resources), respectively, before calling this method.

Specified by:
readObject in interface Serializer<Object>
Parameters:
inputStream - The data stream from which the object will be read.
Returns:
The deserialized object hierarchy.
Throws:
IOException
SerializationException

readObject

public final Object readObject(Class<?> baseType,
                               String resourceName)
                        throws IOException,
                               SerializationException
Deserializes an object hierarchy from a BXML resource.

Throws:
IOException
SerializationException
See Also:
readObject(Class, String, boolean)

readObject

public final Object readObject(Class<?> baseType,
                               String resourceName,
                               boolean localize)
                        throws IOException,
                               SerializationException
Deserializes an object hierarchy from a BXML resource.

The location of the resource is determined by a call to Class.getResource(String) on the given base type, passing the given resource name as an argument. If the resources is localized, the base type is also used as the base name of the resource bundle.

Parameters:
baseType - The base type.
resourceName - The name of the BXML resource.
localize - If true, the deserialized resource will be localized using the resource bundle specified by the base type. Otherwise, it will not be localized, and any use of the resource resolution operator will result in a serialization exception.
Throws:
IOException
SerializationException
See Also:
readObject(URL, Resources)

readObject

public final Object readObject(URL locationArgument)
                        throws IOException,
                               SerializationException
Deserializes an object hierarchy from a BXML resource.

This version of the method does not set the "resources" property. Callers that wish to use this version of the method to load BXML that uses resource resolution must manually set this property via a call to setResources(Resources) before calling this method.

Parameters:
locationArgument - The location of the BXML resource.
Throws:
IOException
SerializationException
See Also:
readObject(URL, Resources)

readObject

public final Object readObject(URL locationArgument,
                               Resources resourcesArgument)
                        throws IOException,
                               SerializationException
Deserializes an object hierarchy from a BXML resource.

Parameters:
locationArgument - The location of the BXML resource.
resourcesArgument - The resources that will be used to localize the deserialized resource. #see readObject(InputStream)
Throws:
IOException
SerializationException

getCurrentLocation

public Location getCurrentLocation()
Return the current location of the XML parser. Useful to ascertain the location where an error occurred (if the error was not an XMLStreamException, which has its own XMLStreamException.getLocation() method).


reportException

protected void reportException(Throwable exception)
Hook used for standardized reporting of exceptions during this process.

Subclasses should override this method in order to do something besides print to System.err.


writeObject

public void writeObject(Object object,
                        OutputStream outputStream)
                 throws IOException,
                        SerializationException
Description copied from interface: Serializer
Writes an object to an output stream.

Specified by:
writeObject in interface Serializer<Object>
Parameters:
object - The object to serialize.
outputStream - The data stream to which the object will be written.
Throws:
IOException
SerializationException

getMIMEType

public String getMIMEType(Object object)
Description copied from interface: Serializer
Returns the MIME type of the data read and written by this serializer.

Specified by:
getMIMEType in interface Serializer<Object>
Parameters:
object - If provided, allows the serializer to attach parameters to the returned MIME type containing more detailed information about the data. If null, the base MIME type is returned.

getRoot

public Object getRoot()
Retrieves the root of the object hierarchy most recently processed by this serializer.

Returns:
The root object, or null if this serializer has not yet read an object from an input stream.

getNamespace

public Map<String,Object> getNamespace()
Description copied from interface: Resolvable
Returns's the serializer's namespace.

Specified by:
getNamespace in interface Resolvable

setNamespace

public void setNamespace(Map<String,Object> namespace)
Description copied from interface: Resolvable
Sets the serializer's namespace. This is used for variable resolution.

Specified by:
setNamespace in interface Resolvable

getLocation

public URL getLocation()
Description copied from interface: Resolvable
Returns the serializer's location.

Specified by:
getLocation in interface Resolvable

setLocation

public void setLocation(URL location)
Description copied from interface: Resolvable
Sets the serializer's location. This is used for URL resolution.

Specified by:
setLocation in interface Resolvable

getResources

public Resources getResources()
Description copied from interface: Resolvable
Returns the serializer's resource bundle.

Specified by:
getResources in interface Resolvable

setResources

public void setResources(Resources resources)
Description copied from interface: Resolvable
Sets the serializer's resource bundle. This is used for resource resolution.

Specified by:
setResources in interface Resolvable

bind

public void bind(Object object)
Applies BXML binding annotations to an object.

Parameters:
object -
See Also:
bind(Object, Class)

bind

public void bind(Object object,
                 Class<?> type)
          throws BindException
Applies BXML binding annotations to an object.

NOTE This method uses reflection to set internal member variables. As a result, it may only be called from trusted code.

Parameters:
object -
type -
Throws:
BindException - If an error occurs during binding

newIncludeSerializer

protected Serializer<?> newIncludeSerializer(Class<? extends Serializer<?>> type)
                                      throws InstantiationException,
                                             IllegalAccessException
Creates a new serializer to be used on a nested include. The base implementation simply calls Class.newInstance(). Subclasses may override this method to provide an alternate instantiation mechanism, such as dependency-injected construction.

Parameters:
type - The type of serializer being requested.
Throws:
InstantiationException
IllegalAccessException

newTypedObject

protected Object newTypedObject(Class<?> type)
                         throws InstantiationException,
                                IllegalAccessException
Creates a new typed object as part of the deserialization process. The base implementation simply calls Class.newInstance(). Subclasses may override this method to provide an alternate instantiation mechanism, such as dependency-injected construction.

Parameters:
type - The type of object being requested.
Throws:
InstantiationException
IllegalAccessException

getXMLStreamReader

protected final XMLStreamReader getXMLStreamReader()
Gets a read-only version of the XML stream reader that's being used by this serializer. Subclasses can use this to access information about the current event.


getFileExtensions

public static Map<String,String> getFileExtensions()
Returns the file extension/MIME type map. This map associates file extensions with MIME types, which are used to automatically determine an appropriate serializer to use for an include based on file extension.

See Also:
getMimeTypes()

getMimeTypes

public static Map<String,Class<? extends Serializer<?>>> getMimeTypes()
Returns the MIME type/serializer class map. This map associates MIME types with serializer classes. The serializer for a given MIME type will be used to deserialize the data for an include that references the MIME type.


setDefaultLanguage

protected void setDefaultLanguage(String defaultLanguage)
Set the default script language to use for all scripts.

Parameters:
defaultLanguage - Name of the new default script language, or null to set the default, default value.
See Also:
DEFAULT_LANGUAGE

getDefaultLanguage

protected String getDefaultLanguage()