org.olat.core.util.xml
Class XStreamHelper

java.lang.Object
  extended by org.olat.core.util.xml.XStreamHelper

public class XStreamHelper
extends java.lang.Object

Description:
The XStreamHelper provides you with some methods to quickly save and restore objects on disk using XML. This helper does the try/catch and writing and reading from/to the disk.

Using xStream without configuration is not recommended. This persists the class using the class name which makes it very difficult to refactor the class at a later stage.

It is recommended to have a private XStream instance in the manager of your package. You can create such an instance using the createXStreamInstance() method. Note that the XStream instance is threadsave, it is normally ok to have one stream per manager. Now you can use field and attribute mappers to define how the XML should look like.

private static final XStream mystream; static { mystream = XStreamHelper.createXStreamInstance(); mystream.alias("user", UserImpl.class); mystream.alias("identity", IdentityImpl.class); mystream.alias("addr", AddressImpl.class); } ... XStreamHelper.writeObject(mystream, myfile, myIdentity); ... Identity ident = (Identity) XStreamHelper.readObject(mystream, myfile);

Author:
Felix Jost, Florian Gnaegi
See Also:

Initial Date: 01.12.2004


Constructor Summary
XStreamHelper()
           
 
Method Summary
static com.thoughtworks.xstream.XStream createXStreamInstance()
          Factory to create a fresh XStream instance.
static java.lang.Object fromXML(java.lang.String xml)
          Deprecated. 
static java.lang.Object readObject(java.io.File file)
          Deprecated. 
static java.lang.Object readObject(java.io.InputStream is)
          Deprecated. 
static java.lang.Object readObject(com.thoughtworks.xstream.XStream xStream, java.io.File file)
          Read an object from the given file using the xStream object.
static java.lang.Object readObject(com.thoughtworks.xstream.XStream xStream, java.io.InputStream is)
          Read an object from the given input stream using the xStream object.
static java.lang.Object readObject(com.thoughtworks.xstream.XStream xStream, VFSLeaf file)
          Read an object from the given leaf using the xStream object.
static java.lang.String toXML(java.lang.Object obj)
          Deprecated. 
static void writeObject(java.io.File file, java.lang.Object obj)
          Deprecated. 
static void writeObject(java.io.OutputStream os, java.lang.Object obj)
          Deprecated. 
static void writeObject(VFSLeaf vfsLeaf, java.lang.Object obj)
          Deprecated. 
static void writeObject(com.thoughtworks.xstream.XStream xStream, java.io.File file, java.lang.Object obj)
          Write a an object to an XML file.
static void writeObject(com.thoughtworks.xstream.XStream xStream, java.io.OutputStream os, java.lang.Object obj)
          Write a an object to an output stream.
static void writeObject(com.thoughtworks.xstream.XStream xStream, VFSLeaf vfsLeaf, java.lang.Object obj)
          Write a an object to an XML file.
static java.lang.Object xstreamClone(java.lang.Object in)
          clones an object with the library XStream.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XStreamHelper

public XStreamHelper()
Method Detail

writeObject

@Deprecated
public static void writeObject(VFSLeaf vfsLeaf,
                                          java.lang.Object obj)
Deprecated. 

Write a an object to an XML file. UTF-8 is used as encoding

This method uses an unconfigured XStream, thus a default mapping which includes the object class name will be used. This should only be used for quick prototyping. For long term persisting of data in real applications, hide classnames and attributes by mapping attributes and fields on the xStream instance.

Use writeObject(XStream stream, VFSLeaf vfsLeaf, Object obj) and configure the mapping there!

Parameters:
vfsLeaf -
obj -

writeObject

@Deprecated
public static void writeObject(java.io.File file,
                                          java.lang.Object obj)
Deprecated. 

Write a an object to an XML file. UTF-8 is used as encoding

This method uses an unconfigured XStream, thus a default mapping which includes the object class name will be used. This should only be used for quick prototyping. For long term persisting of data in real applications, hide classnames and attributes by mapping attributes and fields on the xStream instance.

Use writeObject(XStream stream, File file, Object obj) and configure the mapping there!

Parameters:
file -
obj -

writeObject

@Deprecated
public static void writeObject(java.io.OutputStream os,
                                          java.lang.Object obj)
Deprecated. 

Write a an object to an XML file. UTF-8 is used as encoding

This method uses an unconfigured XStream, thus a default mapping which includes the object class name will be used. This should only be used for quick prototyping. For long term persisting of data in real applications, hide classnames and attributes by mapping attributes and fields on the xStream instance.

Use writeObject(XStream stream, OutputStream os, Object obj) and configure the mapping there!

Parameters:
file -
obj -

toXML

@Deprecated
public static java.lang.String toXML(java.lang.Object obj)
Deprecated. 

Create an XML string from the given object using an unconfigured XStream

Parameters:
obj -
Returns:
the Object in XStream form as an xml-String

fromXML

@Deprecated
public static java.lang.Object fromXML(java.lang.String xml)
Deprecated. 

Create an object from the given XML using an unconfigured XStream

Parameters:
xml -
Returns:
the Object reconstructed from the xml structure

xstreamClone

public static java.lang.Object xstreamClone(java.lang.Object in)
clones an object with the library XStream. The object to be cloned does not need to be serializable, but must have a default constructor.

Parameters:
in -
Returns:
the clone Object

readObject

@Deprecated
public static java.lang.Object readObject(java.io.InputStream is)
Deprecated. 

Read a structure from XML from the given input stream

This method uses an unconfigured XStream, thus a default mapping which includes the object class name will be used. This should only be used for quick prototyping. For long term persisting of data in real applications, hide classnames and attributes by mapping attributes and fields on the xStream instance.

Use readObject(XStream stream, InputStream is) and configure the mapping there!

Parameters:
is -
Returns:
the object

readObject

@Deprecated
public static java.lang.Object readObject(java.io.File file)
Deprecated. 

Read a structure from XML file within the provided folder.

This method uses an unconfigured XStream, thus a default mapping which includes the object class name will be used. This should only be used for quick prototyping. For long term persisting of data in real applications, hide classnames and attributes by mapping attributes and fields on the xStream instance.

Use readObject(XStream stream, File file) and configure the mapping there!

Parameters:
file -
Returns:
de-serialized object
Throws:
OLATRuntimeException - if de-serialization fails.

createXStreamInstance

public static com.thoughtworks.xstream.XStream createXStreamInstance()
Factory to create a fresh XStream instance. Use this when reading and writing to a configured XML mapping


readObject

public static java.lang.Object readObject(com.thoughtworks.xstream.XStream xStream,
                                          java.io.File file)
Read an object from the given file using the xStream object. It is usefull to add field and attribute mappers to the stream.

Parameters:
xStream - The (configured) xStream.
file -
Returns:

readObject

public static java.lang.Object readObject(com.thoughtworks.xstream.XStream xStream,
                                          VFSLeaf file)
Read an object from the given leaf using the xStream object. It is usefull to add field and attribute mappers to the stream.

Parameters:
xStream - The (configured) xStream.
file -
Returns:

readObject

public static java.lang.Object readObject(com.thoughtworks.xstream.XStream xStream,
                                          java.io.InputStream is)
Read an object from the given input stream using the xStream object. It is usefull to add field and attribute mappers to the stream.

Parameters:
xStream - The (configured) xStream.
is -
Returns:

writeObject

public static void writeObject(com.thoughtworks.xstream.XStream xStream,
                               VFSLeaf vfsLeaf,
                               java.lang.Object obj)
Write a an object to an XML file. UTF-8 is used as encoding. It is useful to set attribute and field mappers to allow later refactoring of the class!

Parameters:
xStream - The (configured) xStream.
vfsLeaf -
obj - the object to be serialized

writeObject

public static void writeObject(com.thoughtworks.xstream.XStream xStream,
                               java.io.File file,
                               java.lang.Object obj)
Write a an object to an XML file. UTF-8 is used as encoding. It is useful to set attribute and field mappers to allow later refactoring of the class!

Parameters:
xStream - The (configured) xStream.
file -
obj - the object to be serialized

writeObject

public static void writeObject(com.thoughtworks.xstream.XStream xStream,
                               java.io.OutputStream os,
                               java.lang.Object obj)
Write a an object to an output stream. UTF-8 is used as encoding in the XML declaration. It is useful to set attribute and field mappers to allow later refactoring of the class!

Parameters:
xStream - The (configured) xStream.
os -
obj - the object to be serialized