|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object edu.nrao.sss.util.JaxbUtility
public class JaxbUtility
A utility for converting an object to XML and vice versa.
This class will be used primarily in the toXml
and fromXml
methods of other classes.
How Schema Files are Located and Used
If a transformation method (one that turns XML into an object or vice versa)
takes a Schema
parameter, that schema will be the one used for
validation. If, however, the Schema
parameter is null,
this utility will either attempt to find a default schema or bypass
schema validation, depending on the state of the lookForDefaultSchema
property of this utility.
The location of the default schema files is given by this utility's
schemaBaseUrl
property. If this property has been set to
null, then the default location for the schema files will be
the location on the classpath of the targeted class.
The name of the primary default schema file, regardless of its default location, is theClass.getSimpleName() + ".xsd". If a primary default schema file cannot be found, a secondary default is used. The name of the secondary default file is packageName.xsd where packageName is the "simple" package name. For example, the simple package name of edu.nrao.sss.model.source is "source", thus the schema file would be "source.xsd".
New 2008-12-30: If, in the example above, "source.xsd" is not found, an attempt will be made to find "model.xsd", "sss.xsd", and so on up the line. If none of those files exist a log entry will be created and a null schema will be used. Depending no the particular usage this could result in an exception or marshalling / unmarshalling without schema verification.
Note that a default schema file
is sought only when the lookForDefaultSchema
property
is true. If we seek a default schema file but fail to find it,
what happens next depends on the value of the
failIfDefaultSchemaNotFound
property. If that property is
false, transformation occurs without validation. If it is
true, the transformation methods will throw JaxbException
s.
At this time the schemaBaseUrl
can be set only
programatically
. A possible future
enhancement would allow the specification of this value via a
properties file.
Version Info:
$Revision: 2276 $ |
$Date: 2009-04-29 10:34:57 -0600 (Wed, 29 Apr 2009) $ |
$Author: dharland $ |
Constructor Summary | |
---|---|
JaxbUtility()
Creates a new instance. |
Method Summary | ||
---|---|---|
URL |
getBaseUrlForSchemaFiles()
Returns the base URL that this utility will use for locating schema files. |
|
Schema |
getSchema(String relativeUrl,
Class<?> c)
Attempts to build and return a Schema based on the given
URL to use for objects of the given class. |
|
Schema |
getSchemaFor(Class<?> c)
Attempts to build and return a Schema to use for objects of the
given class. |
|
static JaxbUtility |
getSharedInstance()
Returns a shared, pre-constructed, instance of this utility. |
|
String |
objectToXmlString(Object o)
Returns an XML string for the given object. |
|
String |
objectToXmlString(Object o,
Schema schema)
Returns an XML string for the given object, using schema for
validation. |
|
String |
objectToXmlString(Object o,
String schemaFileName)
Returns an XML string for the given object, using the schema found in the given file for validation. |
|
|
readObjectAsXmlFrom(InputStream stream,
Class<T> c,
Schema schema)
Creates and returns a new object based on the data read from an XML source. |
|
|
readObjectAsXmlFrom(Reader reader,
Class<T> c,
Schema schema)
Creates and returns a new object based on the data read from an XML source. |
|
void |
setBaseUrlForSchemaFiles(URL baseUrl)
Sets the base URL that this utility will use for locating schema files. |
|
void |
setFailIfDefaultSchemaNotFound(boolean fail)
Tells this utility whether or not failure to find a default schema should result in failure of the transformation methods. |
|
void |
setLookForDefaultSchema(boolean look)
Tells this utility whether or not it should look for a default schema if one is not specified. |
|
void |
writeObjectAsXmlTo(Writer writer,
Object o,
Schema schema)
Writes o as XML to writer , using schema for
validation. |
|
|
xmlFileToObject(String fileName,
Class<T> c)
Creates and returns a new object based on the data in an XML file. |
|
|
xmlFileToObject(String fileName,
Class<T> c,
Schema schema)
Creates and returns a new object based on the data in an XML file. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public JaxbUtility()
Method Detail |
---|
public static JaxbUtility getSharedInstance()
public void setLookForDefaultSchema(boolean look)
The default value of this property is true.
See the note, How Schema Files are Located and Used, at the
top
of this class for more information about
schema acquisition and usage.
look
- true if this utility should seek out a default schema.public void setFailIfDefaultSchemaNotFound(boolean fail)
JaxbException
s. If this property is false, those
methods will perform transformations without validation.
The default value of this property is true.
fail
- signals the transformations to fail if a default schema
is sought but not found.public void setBaseUrlForSchemaFiles(URL baseUrl)
The default value of this property is https://e2e.nrao.edu/schemas/.
See the note, How Schema Files are Located and Used, at the
top
of this class for more information about
schema acquisition and usage.
baseUrl
- the base URL that this utility will use for locating schema
files. A value of null may be used to tell this
utility to look for default schema files on the classpath.public URL getBaseUrlForSchemaFiles()
See the note, How Schema Files are Located and Used, at the
top
of this class for more information about
schema acquisition and usage.
public Schema getSchemaFor(Class<?> c) throws JAXBException
Schema
to use for objects of the
given class. This method does not look at the value of the
lookForDefaultSchema
property.
c
- the class for which a schema is desired.
c
, or null
if no such schema could be found.
JAXBException
- if a schema file was found for c
but
something went wrong in the process of turning the file into
a Schema
object.getSchema(String, Class)
public Schema getSchema(String relativeUrl, Class<?> c) throws FileNotFoundException, SAXException
Schema
based on the given
URL to use for objects of the given class. The class c
is used only if the base URL
used by this utility is null.
relativeUrl
- typically the name of the schema filec
- the class used to locate the schema file, but only in
the event that the base URL
used by this utility is null.
FileNotFoundException
- if URL can't be found.
SAXException
- if URL found but can't be turned into schema.public void writeObjectAsXmlTo(Writer writer, Object o, Schema schema) throws JAXBException
o
as XML to writer
, using schema
for
validation.
If schema
is non-null, it is used to validate the contents
of this object while converting it to XML.
If schema
is null, one of two approaches will be
taken, depending on the state of the lookForDefaultSchema
property:
o
will be transformed into XML
without validation against a schema.setLookForDefaultSchema(boolean)
. If found, it
will be used to validate the object o
during the
transformation to XML. If not found, the object o
will be
transformed into XML without validation against a schema.
If there are any problems encountered during the transformation to XML,
a JAXBException
will be thrown.
writer
- the device to which XML is written.
If this value is null, no action is taken.o
- the object to be expressed in XML.
If this value is null, no action is taken.schema
- the holder of the rules for validating the XML output.
If this value is null, and if
setLookForDefaultSchema(boolean)
was called
most recently with a value of true, an attempt
will be made to locate a default schema.
JAXBException
- if any problems are encountered during the
transformation to XML.public <T> T readObjectAsXmlFrom(Reader reader, Class<T> c, Schema schema) throws JAXBException, XMLStreamException
See writeObjectAsXmlTo(Writer, Object, Schema)
to understand how
the schema
parameter is treated.
T
- the runtime type of the returned object.reader
- the source of the XML data.
If this value is null, null is returned.c
- the class of which the returned object is an instance.
If this value is null, null is returned.schema
- the schema used to verify the XML file.
XMLStreamException
- if the XML is not well-formed,
or for some other "unexpected processing conditions".
JAXBException
- if anything else goes wrong during the
transformation.public <T> T readObjectAsXmlFrom(InputStream stream, Class<T> c, Schema schema) throws JAXBException, XMLStreamException
See writeObjectAsXmlTo(Writer, Object, Schema)
to understand how
the schema
parameter is treated.
T
- the runtime type of the returned object.stream
- the source of the XML data.
If this value is null, null is returned.c
- the class of which the returned object is an instance.
If this value is null, null is returned.schema
- the schema used to verify the XML file.
XMLStreamException
- if the XML is not well-formed,
or for some other "unexpected processing conditions".
JAXBException
- if anything else goes wrong during the
transformation.public String objectToXmlString(Object o, Schema schema) throws JAXBException
schema
for
validation.
This is a convenience method that is equivalent to calling
writeObjectAsXmlTo(Writer, Object, Schema)
and using the
Writer
to get a String
. See that method for more
details about schema and exception handling.
o
- the object to be expressed in XML.schema
- the holder of the rules for validating the XML output.
o
.
JAXBException
- if any problems are encountered during the
transformation to XML.public String objectToXmlString(Object o) throws JAXBException
objectToXmlString(o, (Schema)null)
.
o
- the object to be expressed in XML.
o
.
JAXBException
public String objectToXmlString(Object o, String schemaFileName) throws JAXBException, FileNotFoundException
This is a convenience method that behaves like
objectToXmlString(Object, Schema)
, after schemaFileName
has been turned into a schema.
The location of the schema file is determined by
o.getClass().getResource(schemaFileName)
.
If the schema file cannot be found, a FileNotFoundException
is
thrown.
o
- the object to be expressed in XML.schemaFileName
- the name of the file that holds the schema used to
validate the XML transformation.
o
.
FileNotFoundException
- if the schema file cannot be found.
JAXBException
- if any problems are encountered during the
transformation to XML.public <T> T xmlFileToObject(String fileName, Class<T> c, Schema schema) throws JAXBException, XMLStreamException, FileNotFoundException
This is a convenience method that is equivalent to calling
readObjectAsXmlFrom(Reader, Class, Schema)
after turning
fileName
into a Reader
. See that method for more
details about schema and exception handling.
T
- the runtime type of the returned object.fileName
- the name of an XML file.c
- the class of which the returned object is an instance.schema
- the schema used to verify the XML file.
FileNotFoundException
- if the XML file could not be found.
XMLStreamException
- if there is a problem opening the XML file,
if the XML is not well-formed, or for some other
"unexpected processing conditions".
JAXBException
- if anything else goes wrong during the
transformation.public <T> T xmlFileToObject(String fileName, Class<T> c) throws JAXBException, XMLStreamException, FileNotFoundException
xmlFileToObject(fileName, c, (Schema)null)
.
T
- the runtime type of the returned object.fileName
- the name of an XML file.c
- the class of which the returned object is an instance.
FileNotFoundException
- if the XML file could not be found.
XMLStreamException
- if there is a problem opening the XML file,
if the XML is not well-formed, or for some other
"unexpected processing conditions".
JAXBException
- if anything else goes wrong during the
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |