Package org.planit.io.xml.util
Class JAXBUtils
- java.lang.Object
-
- org.planit.io.xml.util.JAXBUtils
-
public class JAXBUtils extends Object
Utility methods for parsing XML data- Author:
- gman6028
-
-
Constructor Summary
Constructors Constructor Description JAXBUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> T
generateInstanceFromXml(Class<T> clazz, File[] potentialXmlFileNames)
create populated instance of class based from the first compatible potential filesstatic Object
generateObjectFromXml(Class<?> clazz, File xmlFileLocation)
Generates a Java object populated with the data from an XML input file.static void
generateXmlFileFromObject(Object object, Class<?> clazz, Path xmlFileLocation, String noNameSpaceUri)
Creates an XML output file populated with data from an Objectstatic void
setPlanitNamespacePrefixes(javax.xml.bind.Marshaller marshaller)
Let any marshaller use the PLANit preferred prefixes for namespaces that it uses.static void
validateXml(File xmlFileLocation, String schemaFileLocation)
Method to validate an XML input file against an XSD schema using Java XML Validator
-
-
-
Field Detail
-
LOGGER
public static final Logger LOGGER
the logger
-
-
Method Detail
-
validateXml
public static void validateXml(File xmlFileLocation, String schemaFileLocation) throws Exception
Method to validate an XML input file against an XSD schema using Java XML Validator- Parameters:
xmlFileLocation
- location of the XML input fileschemaFileLocation
- location of the XSD schema file to validate the XML against- Throws:
Exception
- thrown if the input file fails the validation
-
generateObjectFromXml
public static Object generateObjectFromXml(Class<?> clazz, File xmlFileLocation) throws Exception
Generates a Java object populated with the data from an XML input file. This method creates a JAXB Unmarshaller object which it uses to populate the Java class. The output object will be of a generated class, created from the same XSD file which is used to validate the input XML file.- Parameters:
clazz
- Class of the object to be populatedxmlFileLocation
- location of the input XML file- Returns:
- an instance of the output class, populated with the data from the XML file.
- Throws:
Exception
- thrown if the XML file is invalid or cannot be opened
-
generateXmlFileFromObject
public static void generateXmlFileFromObject(Object object, Class<?> clazz, Path xmlFileLocation, String noNameSpaceUri) throws Exception
Creates an XML output file populated with data from an Object- Parameters:
object
- input object containing the data to be written to the XML fileclazz
- Class of the object containing the dataxmlFileLocation
- location of the output XML filenoNameSpaceUri
- the namespace uri to use for the default namespace schema (no prefix), not used when null- Throws:
Exception
- thrown if the object is not of the correct class, or the output file cannot be opened
-
setPlanitNamespacePrefixes
public static void setPlanitNamespacePrefixes(javax.xml.bind.Marshaller marshaller) throws javax.xml.bind.PropertyException
Let any marshaller use the PLANit preferred prefixes for namespaces that it uses. the namespaces used are based on a custom implementation of a NamespacePrefixMapperPlanitNamespacePrefixMapper
- Parameters:
marshaller
- to inject mapper on- Throws:
javax.xml.bind.PropertyException
- thrown if error setting properties
-
generateInstanceFromXml
public static <T> T generateInstanceFromXml(Class<T> clazz, File[] potentialXmlFileNames)
create populated instance of class based from the first compatible potential files- Type Parameters:
T
- raw XML to find- Parameters:
clazz
- of type TpotentialXmlFileNames
- to search among- Returns:
- parsed result, null if not found
-
-