Package org.verapdf.core
Class XmlSerialiser
- java.lang.Object
-
- org.verapdf.core.XmlSerialiser
-
public final class XmlSerialiser extends Object
Acknowledgements to Arno Moonen's gist which got me going.- Version:
- 0.1 Created 3 Nov 2016:15:17:10
- Author:
- Carl Wilson carlwilson AT github
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> List<T>
collectionFromXml(Class<T> type, InputStream source)
Converts the contents of the InputStream to a List with objects of the given class.static <T> List<T>
collectionFromXml(Class<T> type, Reader source)
Converts the contents of the Reader to a List with objects of the given class.static <T> List<T>
collectionFromXml(Class<T> type, String source)
Converts the contents of the string to a List with objects of the given class.static <T> List<T>
collectionFromXml(Class<T> type, Source source)
Converts the contents of the Source to a List with objects of the given class.protected static <T> JAXBElement<?>
createCollectionElement(String rootName, Collection<T> c)
Create a JAXBElement containing a JAXBCollection.protected static <T> Class<?>[]
findTypes(Collection<T> c)
Discovers all the classes in the given Collection.static <T> String
schema(T obj)
static <T> void
schema(T obj, OutputStream schemaDest)
static <T> void
schema(T obj, Writer schemaDest)
static <T> String
toXml(String rootName, Collection<T> c, boolean format, boolean fragment)
Convert a collection to a string.static <T> void
toXml(String rootName, Collection<T> c, File f, boolean format, boolean fragment)
Convert a collection to a string and stores it in a File.static <T> void
toXml(String rootName, Collection<T> c, OutputStream dest, boolean format, boolean fragment)
Convert a collection to a string and sends it to the OutputStream.static <T> void
toXml(String rootName, Collection<T> c, Writer w, boolean format, boolean fragment)
Convert a collection to a string and sends it to the Writer.static <T> String
toXml(T obj, boolean format, boolean fragment)
Convert an object to a string.static <T> void
toXml(T obj, File f, boolean format, boolean fragment)
Convert an object to a string and save it to a File.static <T> void
toXml(T obj, OutputStream source, boolean format, boolean fragment)
Convert an object to a string and send it to an OutputStream.static <T> void
toXml(T obj, Writer wr, boolean format, boolean fragment)
Convert an object to a string and send it to a Writer.static <T> void
toXml(T obj, XMLStreamWriter wr, boolean format, boolean fragment)
Convert an object to a string and send it to a Writer.static <T,C>
CtypeFromXml(Class<T> type, File source)
Convert the contents of a file to an object of a given class.static <T,C>
CtypeFromXml(Class<T> type, InputStream source)
Convert the contents of an InputStream to an object of a given class.static <T,C>
CtypeFromXml(Class<T> type, Reader source)
Convert the contents of a Reader to an object of a given class.static <T,C>
CtypeFromXml(Class<T> type, String source)
Convert a string to an object of a given class.static <T,C>
CtypeFromXml(Class<T> type, Source source)
Convert the contents of a Source to an object of a given class.
-
-
-
Method Detail
-
typeFromXml
public static <T,C> C typeFromXml(Class<T> type, String source) throws JAXBException
Convert a string to an object of a given class.- Parameters:
type
- Type of objectsource
- Input string- Returns:
- Object of the given type
- Throws:
JAXBException
-
typeFromXml
public static <T,C> C typeFromXml(Class<T> type, File source) throws JAXBException
Convert the contents of a file to an object of a given class.- Parameters:
type
- Type of objectsource
- File to be read- Returns:
- Object of the given type
- Throws:
JAXBException
-
typeFromXml
public static <T,C> C typeFromXml(Class<T> type, Reader source) throws JAXBException
Convert the contents of a Reader to an object of a given class.- Parameters:
type
- Type of objectsource
- Reader to be read- Returns:
- Object of the given type
- Throws:
JAXBException
-
typeFromXml
public static <T,C> C typeFromXml(Class<T> type, InputStream source) throws JAXBException
Convert the contents of an InputStream to an object of a given class.- Parameters:
type
- Type of objectsource
- InputStream to be read- Returns:
- Object of the given type
- Throws:
JAXBException
-
typeFromXml
public static <T,C> C typeFromXml(Class<T> type, Source source) throws JAXBException
Convert the contents of a Source to an object of a given class.- Parameters:
type
- Type of objectsource
- Source to be used- Returns:
- Object of the given type
- Throws:
JAXBException
-
collectionFromXml
public static <T> List<T> collectionFromXml(Class<T> type, String source) throws JAXBException
Converts the contents of the string to a List with objects of the given class.- Parameters:
type
- Type to be usedsource
- Input string- Returns:
- List with objects of the given type
- Throws:
JAXBException
-
collectionFromXml
public static <T> List<T> collectionFromXml(Class<T> type, Reader source) throws JAXBException
Converts the contents of the Reader to a List with objects of the given class.- Parameters:
type
- Type to be usedsource
- Input- Returns:
- List with objects of the given type
- Throws:
JAXBException
-
collectionFromXml
public static <T> List<T> collectionFromXml(Class<T> type, InputStream source) throws JAXBException
Converts the contents of the InputStream to a List with objects of the given class.- Parameters:
type
- Type to be usedsource
- Input- Returns:
- List with objects of the given type
- Throws:
JAXBException
-
collectionFromXml
public static <T> List<T> collectionFromXml(Class<T> type, Source source) throws JAXBException
Converts the contents of the Source to a List with objects of the given class.- Parameters:
type
- Type to be usedsource
- Input- Returns:
- List with objects of the given type
- Throws:
JAXBException
-
toXml
public static <T> String toXml(T obj, boolean format, boolean fragment) throws JAXBException
Convert an object to a string.- Parameters:
obj
- Object that needs to be serialized / marshalled.- Returns:
- String representation of obj
- Throws:
JAXBException
-
toXml
public static <T> void toXml(T obj, Writer wr, boolean format, boolean fragment) throws JAXBException
Convert an object to a string and send it to a Writer.- Parameters:
obj
- Object that needs to be serialized / marshalledwr
- Writer used for outputting the marshalled object- Throws:
JAXBException
-
toXml
public static <T> void toXml(T obj, XMLStreamWriter wr, boolean format, boolean fragment) throws JAXBException
Convert an object to a string and send it to a Writer.- Parameters:
obj
- Object that needs to be serialized / marshalledwr
- Writer used for outputting the marshalled object- Throws:
JAXBException
-
toXml
public static <T> void toXml(T obj, File f, boolean format, boolean fragment) throws JAXBException
Convert an object to a string and save it to a File.- Parameters:
obj
- Object that needs to be serialized / marshalledf
- Save file- Throws:
JAXBException
-
toXml
public static <T> void toXml(T obj, OutputStream source, boolean format, boolean fragment) throws JAXBException
Convert an object to a string and send it to an OutputStream.- Parameters:
obj
- Object that needs to be serialized / marshalledsource
- Stream used for output- Throws:
JAXBException
-
toXml
public static <T> String toXml(String rootName, Collection<T> c, boolean format, boolean fragment) throws JAXBException
Convert a collection to a string.- Parameters:
rootName
- Name of the XML root elementc
- Collection that needs to be marshalled- Returns:
- String representation of the collection
- Throws:
JAXBException
-
toXml
public static <T> void toXml(String rootName, Collection<T> c, Writer w, boolean format, boolean fragment) throws JAXBException
Convert a collection to a string and sends it to the Writer.- Parameters:
rootName
- Name of the XML root elementc
- Collection that needs to be marshalledw
- Output- Throws:
JAXBException
-
toXml
public static <T> void toXml(String rootName, Collection<T> c, File f, boolean format, boolean fragment) throws JAXBException
Convert a collection to a string and stores it in a File.- Parameters:
rootName
- Name of the XML root elementc
- Collection that needs to be marshalledf
- Output file- Throws:
JAXBException
-
toXml
public static <T> void toXml(String rootName, Collection<T> c, OutputStream dest, boolean format, boolean fragment) throws JAXBException
Convert a collection to a string and sends it to the OutputStream.- Parameters:
rootName
- Name of the XML root elementc
- Collection that needs to be marshalleddest
- Output- Throws:
JAXBException
-
schema
public static <T> void schema(T obj, Writer schemaDest) throws IOException, JAXBException
- Throws:
IOException
JAXBException
-
schema
public static <T> void schema(T obj, OutputStream schemaDest) throws IOException, JAXBException
- Throws:
IOException
JAXBException
-
schema
public static <T> String schema(T obj) throws IOException, JAXBException
- Throws:
IOException
JAXBException
-
findTypes
protected static <T> Class<?>[] findTypes(Collection<T> c)
Discovers all the classes in the given Collection. These need to be in the JAXBContext if you want to marshal those objects. Unfortunatly there's no way of getting the generic type at runtime.- Parameters:
c
- Collection that needs to be scanned- Returns:
- Classes found in the collection, including JAXBCollection.
-
createCollectionElement
protected static <T> JAXBElement<?> createCollectionElement(String rootName, Collection<T> c)
Create a JAXBElement containing a JAXBCollection. Needed for marshalling a generic collection without a separate wrapper class.- Parameters:
rootName
- Name of the XML root elementc
-- Returns:
- JAXBElement containing the given Collection, wrapped in a JAXBCollection.
-
-