com.alee.utils
Class XmlUtils

java.lang.Object
  extended by com.alee.utils.XmlUtils

public final class XmlUtils
extends java.lang.Object

This class provides a set of utilities to easily serialize and deserialize objects into and from XML. There are is a lot of methods to read specific library objects using various resource files.

Author:
Mikle Garin

Field Summary
static boolean aliasJdkClasses
          Whether should offer better aliases for standard Java classes like Point and Rectangle or not.
static ColorConverter colorConverter
          Custom converters.
static InsetsConverter insetsConverter
           
static PasswordConverter passwordConverter
          Custom password converter that encrypts serialized passwords.
 
Constructor Summary
XmlUtils()
           
 
Method Summary
static void addImplicitArray(java.lang.Class type, java.lang.String field)
          Adds an implicit array.
static void addImplicitArray(java.lang.Class type, java.lang.String field, java.lang.String itemName)
          Adds an implicit array which is used for all items of the given element name defined by itemName.
static
<T extends AliasProvider>
void
alias(java.lang.Class<T> aliasProvider)
          Calls static "provideAliases" method on a class that implements AliasProvider.
static void alias(java.lang.String name, java.lang.Class type)
          Alias a Class to a shorter name to be used in XML elements.
static
<T> T
fromXML(java.io.File file)
          Returns Object deserialized from XML content.
static
<T> T
fromXML(java.io.InputStream input)
          Returns Object deserialized from XML content.
static
<T> T
fromXML(java.lang.Object source)
          Returns Object deserialized from XML content.
static
<T> T
fromXML(java.io.Reader reader)
          Returns Object deserialized from XML content.
static
<T> T
fromXML(ResourceFile resource)
          Returns Object deserialized from XML text
static
<T> T
fromXML(java.lang.String xml)
          Returns Object deserialized from XML content.
static
<T> T
fromXML(java.net.URL url)
          Returns Object deserialized from XML text
static com.thoughtworks.xstream.XStream getXStream()
          Returns global XStream instance configured with all required aliases and converters.
static javax.swing.ImageIcon loadImageIcon(java.lang.Object source)
          Returns ImageIcon which is read from the source.
static javax.swing.ImageIcon loadImageIcon(ResourceFile resource)
          Returns ImageIcon which is read from specified ResourceFile.
static java.util.List<javax.swing.ImageIcon> loadImagesList(java.lang.Object source)
          Returns ImageIcon list which is read from the source.
static java.util.List<javax.swing.ImageIcon> loadImagesList(ResourceList resourceList)
          Returns ImageIcon list which is read from specified ResourceList.
static ResourceFile loadResourceFile(java.lang.Object source)
          Returns ResourceFile which is read from the source.
static ResourceList loadResourceList(java.lang.Object source)
          Returns ResourceList which is read from the source.
static ResourceMap loadResourceMap(java.lang.Object source)
          Returns ResourceMap which is read from the source.
static java.lang.String loadString(java.lang.Object source)
          Returns text which is read from the source.
static java.lang.String loadString(ResourceFile resource)
          Returns text which is read from specified ResourceFile.
static void processAnnotations(java.lang.Class type)
          Process the annotations of the given type and configure the XStream.
static void processAnnotations(java.lang.Class[] types)
          Process the annotations of the given types and configure the XStream.
static void registerConverter(com.thoughtworks.xstream.converters.Converter converter)
          Adds a new converter into converters registry.
static void registerConverter(com.thoughtworks.xstream.converters.SingleValueConverter converter)
          Adds a new converter into converters registry.
static java.lang.String toXML(java.lang.Object obj)
          Returns Object serialized into XML.
static void toXML(java.lang.Object obj, java.io.File file)
          Serializes Object into XML and writes it into specified file.
static void toXML(java.lang.Object obj, java.io.OutputStream out)
          Serializes Object into XML and writes it using a specified OutputStream.
static void toXML(java.lang.Object obj, java.lang.String file)
          Serializes Object into XML and writes it into specified file.
static void toXML(java.lang.Object obj, java.io.Writer out)
          Serializes Object into XML and writes it using a specified Writer.
static void useAttributeFor(java.lang.Class type, java.lang.String field)
          Use an attribute for a field declared in a specific type.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

aliasJdkClasses

public static boolean aliasJdkClasses
Whether should offer better aliases for standard Java classes like Point and Rectangle or not.


colorConverter

public static final ColorConverter colorConverter
Custom converters.


insetsConverter

public static final InsetsConverter insetsConverter

passwordConverter

public static final PasswordConverter passwordConverter
Custom password converter that encrypts serialized passwords.

Constructor Detail

XmlUtils

public XmlUtils()
Method Detail

getXStream

public static com.thoughtworks.xstream.XStream getXStream()
Returns global XStream instance configured with all required aliases and converters.

Returns:
XStream

processAnnotations

public static void processAnnotations(java.lang.Class type)
Process the annotations of the given type and configure the XStream. A call of this method will automatically turn the auto-detection mode for annotations off.

Parameters:
type - the type with XStream annotations

processAnnotations

public static void processAnnotations(java.lang.Class[] types)
Process the annotations of the given types and configure the XStream.

Parameters:
types - the types with XStream annotations

alias

public static void alias(java.lang.String name,
                         java.lang.Class type)
Alias a Class to a shorter name to be used in XML elements.

Parameters:
name - Short name
type - Type to be aliased

useAttributeFor

public static void useAttributeFor(java.lang.Class type,
                                   java.lang.String field)
Use an attribute for a field declared in a specific type.

Parameters:
type - the name of the field
field - the Class containing such field

addImplicitArray

public static void addImplicitArray(java.lang.Class type,
                                    java.lang.String field)
Adds an implicit array.

Parameters:
type - class owning the implicit array
field - name of the array field

addImplicitArray

public static void addImplicitArray(java.lang.Class type,
                                    java.lang.String field,
                                    java.lang.String itemName)
Adds an implicit array which is used for all items of the given element name defined by itemName.

Parameters:
type - class owning the implicit array
field - name of the array field in the ownerType
itemName - alias name of the items

registerConverter

public static void registerConverter(com.thoughtworks.xstream.converters.Converter converter)
Adds a new converter into converters registry.

Parameters:
converter - the new converter

registerConverter

public static void registerConverter(com.thoughtworks.xstream.converters.SingleValueConverter converter)
Adds a new converter into converters registry.

Parameters:
converter - the new converter

alias

public static <T extends AliasProvider> void alias(java.lang.Class<T> aliasProvider)
Calls static "provideAliases" method on a class that implements AliasProvider. This method is created mostly for internal library usage.

Type Parameters:
T - specific class type
Parameters:
aliasProvider - AliasProvider ancestor class

toXML

public static void toXML(java.lang.Object obj,
                         java.lang.String file)
Serializes Object into XML and writes it into specified file.

Parameters:
obj - object to serialize
file - output file

toXML

public static void toXML(java.lang.Object obj,
                         java.io.File file)
Serializes Object into XML and writes it into specified file.

Parameters:
obj - object to serialize
file - output file

toXML

public static java.lang.String toXML(java.lang.Object obj)
Returns Object serialized into XML.

Parameters:
obj - object to serialize
Returns:
serialized into XML object representation

toXML

public static void toXML(java.lang.Object obj,
                         java.io.Writer out)
Serializes Object into XML and writes it using a specified Writer.

Parameters:
obj - object to serialize
out - output writer

toXML

public static void toXML(java.lang.Object obj,
                         java.io.OutputStream out)
Serializes Object into XML and writes it using a specified OutputStream.

Parameters:
obj - object to serialize
out - output stream

fromXML

public static <T> T fromXML(java.io.Reader reader)
Returns Object deserialized from XML content.

Type Parameters:
T - read object type
Parameters:
reader - XML text source
Returns:
deserialized object

fromXML

public static <T> T fromXML(java.io.InputStream input)
Returns Object deserialized from XML content.

Type Parameters:
T - read object type
Parameters:
input - XML text source
Returns:
deserialized object

fromXML

public static <T> T fromXML(java.net.URL url)
Returns Object deserialized from XML text

Type Parameters:
T - read object type
Parameters:
url - XML text source
Returns:
deserialized object

fromXML

public static <T> T fromXML(java.io.File file)
Returns Object deserialized from XML content.

Type Parameters:
T - read object type
Parameters:
file - file with XML content
Returns:
deserialized object

fromXML

public static <T> T fromXML(java.lang.String xml)
Returns Object deserialized from XML content.

Type Parameters:
T - read object type
Parameters:
xml - XML content
Returns:
deserialized object

fromXML

public static <T> T fromXML(java.lang.Object source)
Returns Object deserialized from XML content.

Type Parameters:
T - read object type
Parameters:
source - XML text source
Returns:
deserialized object

fromXML

public static <T> T fromXML(ResourceFile resource)
Returns Object deserialized from XML text

Type Parameters:
T - read object type
Parameters:
resource - XML text source description
Returns:
deserialized object

loadString

public static java.lang.String loadString(java.lang.Object source)
Returns text which is read from the source.

Parameters:
source - one of possible sources: URL, String, File, Reader, InputStream
Returns:
text as String

loadString

public static java.lang.String loadString(ResourceFile resource)
Returns text which is read from specified ResourceFile.

Parameters:
resource - file description
Returns:
text as String

loadImageIcon

public static javax.swing.ImageIcon loadImageIcon(java.lang.Object source)
Returns ImageIcon which is read from the source.

Parameters:
source - one of possible sources: URL, String, File, Reader, InputStream
Returns:
ImageIcon

loadImageIcon

public static javax.swing.ImageIcon loadImageIcon(ResourceFile resource)
Returns ImageIcon which is read from specified ResourceFile.

Parameters:
resource - file description
Returns:
ImageIcon

loadImagesList

public static java.util.List<javax.swing.ImageIcon> loadImagesList(java.lang.Object source)
Returns ImageIcon list which is read from the source.

Parameters:
source - one of possible sources: URL, String, File, Reader, InputStream
Returns:
ImageIcon list

loadImagesList

public static java.util.List<javax.swing.ImageIcon> loadImagesList(ResourceList resourceList)
Returns ImageIcon list which is read from specified ResourceList.

Parameters:
resourceList - ResourceFile list
Returns:
ImageIcon list

loadResourceMap

public static ResourceMap loadResourceMap(java.lang.Object source)
Returns ResourceMap which is read from the source.

Parameters:
source - one of possible sources: URL, String, File, Reader, InputStream
Returns:
ResourceMap

loadResourceList

public static ResourceList loadResourceList(java.lang.Object source)
Returns ResourceList which is read from the source.

Parameters:
source - one of possible sources: URL, String, File, Reader, InputStream
Returns:
ResourceList

loadResourceFile

public static ResourceFile loadResourceFile(java.lang.Object source)
Returns ResourceFile which is read from the source.

Parameters:
source - one of possible sources: URL, String, File, Reader, InputStream
Returns:
ResourceFile