Class BXMLSerializer
- java.lang.Object
-
- org.apache.pivot.beans.BXMLSerializer
-
- All Implemented Interfaces:
Resolvable,Serializer<java.lang.Object>
public class BXMLSerializer extends java.lang.Object implements Serializer<java.lang.Object>, Resolvable
Loads an object hierarchy from an XML document.
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringBIND_MAPPING_DELIMITERstatic java.lang.StringBXML_EXTENSIONstatic java.lang.StringBXML_PREFIXstatic java.lang.StringDEFAULT_LANGUAGEstatic java.lang.StringDEFINE_TAGstatic java.lang.StringID_ATTRIBUTEstatic java.lang.StringINCLUDE_INLINE_ATTRIBUTEstatic java.lang.StringINCLUDE_MIME_TYPE_ATTRIBUTEstatic java.lang.StringINCLUDE_RESOURCES_ATTRIBUTEstatic java.lang.StringINCLUDE_SRC_ATTRIBUTEstatic java.lang.StringINCLUDE_TAGstatic java.lang.StringINTERNAL_ID_PREFIXstatic java.lang.StringLANGUAGE_PROCESSING_INSTRUCTIONstatic java.lang.StringMIME_TYPEstatic java.lang.StringNAMESPACE_BINDING_PREFIXstatic java.lang.StringNAMESPACE_BINDING_SUFFIXstatic java.lang.StringNASHORN_COMPAT_SCRIPTstatic java.lang.StringNASHORN_GLOBALstatic charOBJECT_REFERENCE_PREFIXstatic java.lang.StringREFERENCE_ID_ATTRIBUTEstatic java.lang.StringREFERENCE_TAGstatic charRESOURCE_KEY_PREFIXstatic java.lang.StringSCRIPT_SRC_ATTRIBUTEstatic java.lang.StringSCRIPT_TAGstatic charSLASH_PREFIXstatic charURL_PREFIX
-
Constructor Summary
Constructors Constructor Description BXMLSerializer()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidbind(java.lang.Object object)Applies BXML binding annotations to an object.voidbind(java.lang.Object object, java.lang.Class<?> type)Applies BXML binding annotations to an object.javax.xml.stream.LocationgetCurrentLocation()Return the current location of the XML parser.protected java.lang.StringgetDefaultLanguage()static Map<java.lang.String,java.lang.String>getFileExtensions()Returns the file extension/MIME type map.java.net.URLgetLocation()java.lang.StringgetMIMEType(java.lang.Object object)Returns the MIME type of the data read and written by this serializer.static Map<java.lang.String,java.lang.Class<? extends Serializer<?>>>getMimeTypes()Returns the MIME type/serializer class map.Map<java.lang.String,java.lang.Object>getNamespace()Access the serializer's namespace.ResourcesgetResources()java.lang.ObjectgetRoot()Retrieves the root of the object hierarchy most recently processed by this serializer.protected javax.xml.stream.XMLStreamReadergetXMLStreamReader()Gets a read-only version of the XML stream reader that's being used by this serializer.protected Serializer<?>newIncludeSerializer(java.lang.Class<? extends Serializer<?>> type)Creates a new serializer to be used on a nested include.protected java.lang.ObjectnewTypedObject(java.lang.Class<?> type)Creates a new typed object as part of the deserialization process.java.lang.ObjectreadObject(java.io.InputStream inputStream)Deserializes an object hierarchy from a BXML resource.java.lang.ObjectreadObject(java.lang.Class<?> baseType, java.lang.String resourceName)Deserializes an object hierarchy from a BXML resource, and do not localize any text.java.lang.ObjectreadObject(java.lang.Class<?> baseType, java.lang.String resourceName, boolean localize)Deserializes an object hierarchy from a BXML resource.java.lang.ObjectreadObject(java.net.URL locationArgument)Deserializes an object hierarchy from a BXML resource.java.lang.ObjectreadObject(java.net.URL locationArgument, Resources resourcesArgument)Deserializes an object hierarchy from a BXML resource.protected voidreportException(java.lang.Throwable exception)Hook used for standardized reporting of exceptions during this process.protected voidsetDefaultLanguage(java.lang.String defaultLanguage)Set the default script language to use for all scripts.voidsetLocation(java.net.URL location)Sets the serializer's location.voidsetNamespace(Map<java.lang.String,java.lang.Object> namespace)Sets the serializer's namespace.voidsetResources(Resources resources)Sets the serializer's resource bundle.voidwriteObject(java.lang.Object object, java.io.OutputStream outputStream)Writes an object to an output stream.
-
-
-
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 java.lang.String NAMESPACE_BINDING_PREFIX
- See Also:
- Constant Field Values
-
NAMESPACE_BINDING_SUFFIX
public static final java.lang.String NAMESPACE_BINDING_SUFFIX
- See Also:
- Constant Field Values
-
BIND_MAPPING_DELIMITER
public static final java.lang.String BIND_MAPPING_DELIMITER
- See Also:
- Constant Field Values
-
INTERNAL_ID_PREFIX
public static final java.lang.String INTERNAL_ID_PREFIX
- See Also:
- Constant Field Values
-
LANGUAGE_PROCESSING_INSTRUCTION
public static final java.lang.String LANGUAGE_PROCESSING_INSTRUCTION
- See Also:
- Constant Field Values
-
NASHORN_GLOBAL
public static final java.lang.String NASHORN_GLOBAL
- See Also:
- Constant Field Values
-
NASHORN_COMPAT_SCRIPT
public static final java.lang.String NASHORN_COMPAT_SCRIPT
- See Also:
- Constant Field Values
-
BXML_PREFIX
public static final java.lang.String BXML_PREFIX
- See Also:
- Constant Field Values
-
BXML_EXTENSION
public static final java.lang.String BXML_EXTENSION
- See Also:
- Constant Field Values
-
ID_ATTRIBUTE
public static final java.lang.String ID_ATTRIBUTE
- See Also:
- Constant Field Values
-
INCLUDE_TAG
public static final java.lang.String INCLUDE_TAG
- See Also:
- Constant Field Values
-
INCLUDE_SRC_ATTRIBUTE
public static final java.lang.String INCLUDE_SRC_ATTRIBUTE
- See Also:
- Constant Field Values
-
INCLUDE_RESOURCES_ATTRIBUTE
public static final java.lang.String INCLUDE_RESOURCES_ATTRIBUTE
- See Also:
- Constant Field Values
-
INCLUDE_MIME_TYPE_ATTRIBUTE
public static final java.lang.String INCLUDE_MIME_TYPE_ATTRIBUTE
- See Also:
- Constant Field Values
-
INCLUDE_INLINE_ATTRIBUTE
public static final java.lang.String INCLUDE_INLINE_ATTRIBUTE
- See Also:
- Constant Field Values
-
SCRIPT_TAG
public static final java.lang.String SCRIPT_TAG
- See Also:
- Constant Field Values
-
SCRIPT_SRC_ATTRIBUTE
public static final java.lang.String SCRIPT_SRC_ATTRIBUTE
- See Also:
- Constant Field Values
-
DEFINE_TAG
public static final java.lang.String DEFINE_TAG
- See Also:
- Constant Field Values
-
REFERENCE_TAG
public static final java.lang.String REFERENCE_TAG
- See Also:
- Constant Field Values
-
REFERENCE_ID_ATTRIBUTE
public static final java.lang.String REFERENCE_ID_ATTRIBUTE
- See Also:
- Constant Field Values
-
DEFAULT_LANGUAGE
public static final java.lang.String DEFAULT_LANGUAGE
- See Also:
- Constant Field Values
-
MIME_TYPE
public static final java.lang.String MIME_TYPE
- See Also:
- Constant Field Values
-
-
Method Detail
-
readObject
public java.lang.Object readObject(java.io.InputStream inputStream) throws java.io.IOException, SerializationExceptionDeserializes 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)orsetResources(Resources), respectively, before calling this method.- Specified by:
readObjectin interfaceSerializer<java.lang.Object>- Parameters:
inputStream- The data stream from which the object will be read.- Returns:
- The deserialized object hierarchy.
- Throws:
java.io.IOException- for any errors accessing or reading the object.SerializationException- for any formatting errors encountered while deserializing the object.
-
readObject
public final java.lang.Object readObject(java.lang.Class<?> baseType, java.lang.String resourceName) throws java.io.IOException, SerializationExceptionDeserializes an object hierarchy from a BXML resource, and do not localize any text.- Parameters:
baseType- The base type from which to access needed resources.resourceName- Name of the BXML resource to deserialize.- Returns:
- the top-level deserialized object.
- Throws:
java.lang.IllegalArgumentException- fornulltype or resource name or if the resource could not be found.java.io.IOException- for any error reading the BXML resource.SerializationException- for any other errors encountered deserializing the resource.- See Also:
readObject(Class, String, boolean)
-
readObject
public final java.lang.Object readObject(java.lang.Class<?> baseType, java.lang.String resourceName, boolean localize) throws java.io.IOException, SerializationExceptionDeserializes 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- Iftrue, 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.- Returns:
- the top-level deserialized object.
- Throws:
java.lang.IllegalArgumentException- fornulltype or resource name or if the resource could not be found.java.io.IOException- for any error reading the BXML resource.SerializationException- for any other errors encountered deserializing the resource.- See Also:
readObject(URL, Resources)
-
readObject
public final java.lang.Object readObject(java.net.URL locationArgument) throws java.io.IOException, SerializationExceptionDeserializes 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.- Returns:
- The top-level deserialized object.
- Throws:
java.io.IOException- for any error reading the BXML resource.SerializationException- for any other errors encountered deserializing the resource.- See Also:
readObject(URL, Resources)
-
readObject
public final java.lang.Object readObject(java.net.URL locationArgument, Resources resourcesArgument) throws java.io.IOException, SerializationExceptionDeserializes 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.- Returns:
- The top-level deserialized object.
- Throws:
java.io.IOException- for any error reading the BXML resource.SerializationException- for any other errors encountered deserializing the resource.- See Also:
readObject(InputStream)
-
getCurrentLocation
public javax.xml.stream.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 ownXMLStreamException.getLocation()method).- Returns:
- The current location in the XML stream.
-
reportException
protected void reportException(java.lang.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.- Parameters:
exception- Whatever exception has been thrown during processing.
-
writeObject
@UnsupportedOperation public void writeObject(java.lang.Object object, java.io.OutputStream outputStream) throws java.io.IOException, SerializationException
Description copied from interface:SerializerWrites an object to an output stream.- Specified by:
writeObjectin interfaceSerializer<java.lang.Object>- Parameters:
object- The object to serialize.outputStream- The data stream to which the object will be written.- Throws:
java.lang.UnsupportedOperationException- because we don't support writing BXML objects.java.io.IOException- for any errors accessing or reading the object.SerializationException- for any formatting errors encountered while deserializing the object.
-
getMIMEType
public java.lang.String getMIMEType(java.lang.Object object)
Description copied from interface:SerializerReturns the MIME type of the data read and written by this serializer.- Specified by:
getMIMETypein interfaceSerializer<java.lang.Object>- Parameters:
object- If provided, allows the serializer to attach parameters to the returned MIME type containing more detailed information about the data. Ifnull, the base MIME type is returned.- Returns:
- The MIME type of the current data.
-
getRoot
public java.lang.Object getRoot()
Retrieves the root of the object hierarchy most recently processed by this serializer.- Returns:
- The root object, or
nullif this serializer has not yet read an object from an input stream.
-
getNamespace
public Map<java.lang.String,java.lang.Object> getNamespace()
Description copied from interface:ResolvableAccess the serializer's namespace.- Specified by:
getNamespacein interfaceResolvable- Returns:
- The namespace of the serializer (namely the mapping of names to values).
-
setNamespace
public void setNamespace(Map<java.lang.String,java.lang.Object> namespace)
Description copied from interface:ResolvableSets the serializer's namespace. This is used for variable resolution.- Specified by:
setNamespacein interfaceResolvable- Parameters:
namespace- The new namespace to use for this serializer.
-
getLocation
public java.net.URL getLocation()
- Specified by:
getLocationin interfaceResolvable- Returns:
- The serializer's location.
-
setLocation
public void setLocation(java.net.URL location)
Description copied from interface:ResolvableSets the serializer's location. This is used for URL resolution.- Specified by:
setLocationin interfaceResolvable- Parameters:
location- Where we are to load resources for this serializer.
-
getResources
public Resources getResources()
- Specified by:
getResourcesin interfaceResolvable- Returns:
- The serializer's resource bundle.
-
setResources
public void setResources(Resources resources)
Description copied from interface:ResolvableSets the serializer's resource bundle. This is used for resource resolution.- Specified by:
setResourcesin interfaceResolvable- Parameters:
resources- Resource bundle to use to resolve text resources.
-
bind
public void bind(java.lang.Object object)
Applies BXML binding annotations to an object.- Parameters:
object- The object to bind BXML values to.- Throws:
BindException- If an error occurs during binding.- See Also:
bind(Object, Class)
-
bind
public void bind(java.lang.Object object, java.lang.Class<?> type) throws BindExceptionApplies 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- The object to bind BXML values to.type- The type of the object.- Throws:
BindException- If an error occurs during binding.
-
newIncludeSerializer
protected Serializer<?> newIncludeSerializer(java.lang.Class<? extends Serializer<?>> type) throws java.lang.InstantiationException, java.lang.IllegalAccessException, java.lang.NoSuchMethodException, java.lang.reflect.InvocationTargetException
Creates a new serializer to be used on a nested include. The base implementation simply callsClass.getDeclaredConstructor().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.- Returns:
- The new serializer to use.
- Throws:
java.lang.InstantiationException- if an object of the given type cannot be instantiated.java.lang.IllegalAccessException- if the class cannot be accessed in the current security environment.java.lang.NoSuchMethodException- if there is not a no-arg constructor declared in the class.java.lang.reflect.InvocationTargetException- if there was an exception thrown by the constructor.
-
newTypedObject
protected java.lang.Object newTypedObject(java.lang.Class<?> type) throws java.lang.InstantiationException, java.lang.IllegalAccessException, java.lang.NoSuchMethodException, java.lang.reflect.InvocationTargetExceptionCreates a new typed object as part of the deserialization process. The base implementation simply callsClass.getDeclaredConstructor().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.- Returns:
- The newly created object.
- Throws:
java.lang.InstantiationException- if an object of the given type cannot be instantiated.java.lang.IllegalAccessException- if the class cannot be accessed in the current security environment.java.lang.NoSuchMethodException- if there is not a no-arg constructor declared in the class.java.lang.reflect.InvocationTargetException- if there was an exception thrown by the constructor.
-
getXMLStreamReader
protected final javax.xml.stream.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.- Returns:
- The read-only reader.
-
getFileExtensions
public static Map<java.lang.String,java.lang.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.- Returns:
- The map between file extensions and MIME types.
- See Also:
getMimeTypes()
-
getMimeTypes
public static Map<java.lang.String,java.lang.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.- Returns:
- The map associating MIME types with serializers.
-
setDefaultLanguage
protected void setDefaultLanguage(java.lang.String defaultLanguage)
Set the default script language to use for all scripts.- Parameters:
defaultLanguage- Name of the new default script language, ornullto set the default, default value.- See Also:
DEFAULT_LANGUAGE
-
getDefaultLanguage
protected java.lang.String getDefaultLanguage()
-
-