Class JAXBUtils


  • public class JAXBUtils
    extends Object
    Utility methods for parsing XML data
    Author:
    gman6028
    • Field Detail

      • LOGGER

        public static final Logger LOGGER
        the logger
    • Constructor Detail

      • JAXBUtils

        public JAXBUtils()
    • 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 file
        schemaFileLocation - 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 populated
        xmlFileLocation - 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 file
        clazz - Class of the object containing the data
        xmlFileLocation - location of the output XML file
        noNameSpaceUri - 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 NamespacePrefixMapper PlanitNamespacePrefixMapper
        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 T
        potentialXmlFileNames - to search among
        Returns:
        parsed result, null if not found