Class 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.
    • Constructor Detail

      • BXMLSerializer

        public BXMLSerializer()
    • Method Detail

      • readObject

        public java.lang.Object readObject​(java.io.InputStream inputStream)
                                    throws java.io.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<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,
                                                 SerializationException
        Deserializes 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 - for null type 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,
                                                 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.
        Returns:
        the top-level deserialized object.
        Throws:
        java.lang.IllegalArgumentException - for null type 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,
                                                 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.
        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,
                                                 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.
        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 own XMLStreamException.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: Serializer
        Writes an object to an output stream.
        Specified by:
        writeObject in interface Serializer<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: Serializer
        Returns the MIME type of the data read and written by this serializer.
        Specified by:
        getMIMEType in interface Serializer<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. If null, 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 null if 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: Resolvable
        Access the serializer's namespace.
        Specified by:
        getNamespace in interface Resolvable
        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: Resolvable
        Sets the serializer's namespace. This is used for variable resolution.
        Specified by:
        setNamespace in interface Resolvable
        Parameters:
        namespace - The new namespace to use for this serializer.
      • getLocation

        public java.net.URL getLocation()
        Specified by:
        getLocation in interface Resolvable
        Returns:
        The serializer's location.
      • setLocation

        public void setLocation​(java.net.URL location)
        Description copied from interface: Resolvable
        Sets the serializer's location. This is used for URL resolution.
        Specified by:
        setLocation in interface Resolvable
        Parameters:
        location - Where we are to load resources for this serializer.
      • 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
        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 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 - 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 calls Class.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.InvocationTargetException
        Creates a new typed object as part of the deserialization process. The base implementation simply calls Class.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, or null to set the default, default value.
        See Also:
        DEFAULT_LANGUAGE
      • getDefaultLanguage

        protected java.lang.String getDefaultLanguage()