Class JaxbUtil


  • public class JaxbUtil
    extends Object
    This class can write or read an instance of a JAXB annotated class. It handles masking of illegal characters in an XML file and formatting issues.
    • Constructor Summary

      Constructors 
      Constructor Description
      JaxbUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void queryXsd​(Scanner console, Class<?>[] domain, File destDir)
      Generates schema files for a given classes context
      static <T> T read​(InputStream input, Class<T> clazz, Class<?>... contextClasses)
      Instantiate an object of a JAXB annotated type clazz by reading it from input while using the contextClasses as the class context.
      static <T> T read​(InputStream input, Class<T> clazz, Schema schema, Class<?>... contextClasses)
      Instantiate an object of a JAXB annotated type clazz by reading it from input while using the contextClasses as the class context.
      static <T> T read​(String inFileName, Class<T> clazz, Class<?>... contextClasses)
      Instantiate an object of a JAXB annotated type clazz by reading it from input file inFileName while using the contextClasses as the class context.
      static void write​(Object o, OutputStream output, Class<?>... classesToBeBound)
      Write object o to output while using the classesToBebound as the class context.
      static void write​(Object o, OutputStream output, Collection<Class<?>> classesToBeBound)
      Write object o to output while using the classesToBebound as the class context.
      static void write​(Object o, String outFileName, Class<?>... classesToBeBound)
      Write object o to output file outFileName while using the classesToBebound as the class context.
      static void write​(Object o, String outFileName, Collection<Class<?>> classesToBeBound)
      Write object o to output file outFileName while using the classesToBebound as the class context.
    • Constructor Detail

      • JaxbUtil

        public JaxbUtil()
    • Method Detail

      • write

        public static void write​(Object o,
                                 OutputStream output,
                                 Class<?>... classesToBeBound)
                          throws javax.xml.bind.JAXBException,
                                 IOException
        Write object o to output while using the classesToBebound as the class context.
        Parameters:
        o - the instance to be written
        output - output stream
        classesToBeBound - these classes define the class context
        Throws:
        javax.xml.bind.JAXBException - if an error was encountered while preparing the Marshaller object or the JAXBContext or while writing the object.
        IOException - if an I/O specific error was encountered.
      • write

        public static void write​(Object o,
                                 String outFileName,
                                 Class<?>... classesToBeBound)
                          throws javax.xml.bind.JAXBException,
                                 IOException
        Write object o to output file outFileName while using the classesToBebound as the class context.
        Parameters:
        o - the instance to be written
        outFileName - output file name
        classesToBeBound - these classes define the class context
        Throws:
        javax.xml.bind.JAXBException - if an error was encountered while preparing the Marshaller object or the JAXBContext or while writing the object.
        IOException - if an I/O specific error was encountered.
      • write

        public static void write​(Object o,
                                 String outFileName,
                                 Collection<Class<?>> classesToBeBound)
                          throws javax.xml.bind.JAXBException,
                                 IOException
        Write object o to output file outFileName while using the classesToBebound as the class context.
        Parameters:
        o - the instance to be written
        outFileName - output file name
        classesToBeBound - these classes define the class context
        Throws:
        javax.xml.bind.JAXBException - if an error was encountered while preparing the Marshaller object or the JAXBContext or while writing the object.
        IOException - if an I/O specific error was encountered.
      • write

        public static void write​(Object o,
                                 OutputStream output,
                                 Collection<Class<?>> classesToBeBound)
                          throws javax.xml.bind.JAXBException,
                                 IOException
        Write object o to output while using the classesToBebound as the class context.
        Parameters:
        o - the instance to be written
        output - output stream
        classesToBeBound - these classes define the class context
        Throws:
        javax.xml.bind.JAXBException - if an error was encountered while preparing the Marshaller object or the JAXBContext or while writing the object.
        IOException - if an I/O specific error was encountered.
      • read

        public static <T> T read​(String inFileName,
                                 Class<T> clazz,
                                 Class<?>... contextClasses)
                          throws javax.xml.bind.JAXBException,
                                 IOException
        Instantiate an object of a JAXB annotated type clazz by reading it from input file inFileName while using the contextClasses as the class context.
        Parameters:
        inFileName - the source file
        clazz - the type of the new object
        contextClasses - these classes define the class context
        Returns:
        the new instance
        Throws:
        javax.xml.bind.JAXBException - if an error was encountered while preparing the Marshaller object or the JAXBContext or while reading the object.
        IOException - if an I/O specific error was encountered.
      • read

        public static <T> T read​(InputStream input,
                                 Class<T> clazz,
                                 Schema schema,
                                 Class<?>... contextClasses)
                          throws javax.xml.bind.JAXBException,
                                 IOException
        Instantiate an object of a JAXB annotated type clazz by reading it from input while using the contextClasses as the class context.
        Parameters:
        input - the source stream
        clazz - the type of the new object
        schema -
        contextClasses - these classes define the class context
        Returns:
        the new instance
        Throws:
        javax.xml.bind.JAXBException - if an error was encountered while preparing the Marshaller object or the JAXBContext or while reading the object.
        IOException - if an I/O specific error was encountered.
      • read

        public static <T> T read​(InputStream input,
                                 Class<T> clazz,
                                 Class<?>... contextClasses)
                          throws javax.xml.bind.JAXBException,
                                 IOException
        Instantiate an object of a JAXB annotated type clazz by reading it from input while using the contextClasses as the class context.
        Parameters:
        input - the source stream
        clazz - the type of the new object
        contextClasses - these classes define the class context
        Returns:
        the new instance
        Throws:
        javax.xml.bind.JAXBException - if an error was encountered while preparing the Marshaller object or the JAXBContext or while reading the object.
        IOException - if an I/O specific error was encountered.
      • queryXsd

        public static void queryXsd​(Scanner console,
                                    Class<?>[] domain,
                                    File destDir)
        Generates schema files for a given classes context
        Parameters:
        console - is used to get user input about filenames
        domain - these classes define the class context.
        destDir - destination directory. If null, the files are created in the current directory. The destination directory must be created by the caller, if it does not exist.