Class JAXBFiles

java.lang.Object
de.edgesoft.edgeutils.jaxb.JAXBFiles

public class JAXBFiles
extends java.lang.Object
Class providing jaxb file operations.

Legal stuff

Copyright 2010-2020 Ekkart Kleinod ekleinod@edgesoft.de

This file is part of edgeutils.

edgeutils is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

edgeutils is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with edgeutils. If not, see http://www.gnu.org/licenses/.

Since:
0.1
Version:
0.13.0
Author:
Ekkart Kleinod
  • Constructor Summary

    Constructors 
    Constructor Description
    JAXBFiles()  
  • Method Summary

    Modifier and Type Method Description
    static <T> void marshal​(javax.xml.bind.JAXBElement<T> theDataElement, java.lang.String theFileName, java.lang.String theSchema)
    Writes an XML file from the given xml data object to a file.
    static <T> java.lang.String marshalToString​(javax.xml.bind.JAXBElement<T> theDataElement, java.lang.String theSchema)
    Writes a given xml data object to a string.
    static void setEncoding​(java.nio.charset.Charset newEncoding)
    Sets the file encoding.
    static <T> T unmarshal​(java.io.Reader theReader, java.lang.Class<T> theClass)
    Returns the xml data object saved in the supplied reader.
    static <T> T unmarshal​(java.lang.String theFileName, java.lang.Class<T> theClass)
    Returns the xml data object saved in the supplied file.
    static <T> T unmarshalInclude​(java.lang.String theFileName, java.lang.Class<T> theClass)
    Returns the xml data object saved in the supplied file, uses includes.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

  • Method Details

    • setEncoding

      public static void setEncoding​(java.nio.charset.Charset newEncoding)
      Sets the file encoding.

      For the standard encodings see java.nio.charsets.StandardCharsets.

      Use them as follows, example: UTF-8:

      JAXBFiles.setEncoding(StandardCharsets.UTF_8);
      Parameters:
      newEncoding - the file encoding
    • unmarshal

      public static <T> T unmarshal​(java.lang.String theFileName, java.lang.Class<T> theClass) throws EdgeUtilsException
      Returns the xml data object saved in the supplied file.
      Type Parameters:
      T - xml data object type
      Parameters:
      theFileName - the filename of the file to unmarshal
      theClass - class of return type (needed for package information)
      Returns:
      xml data object
      Throws:
      EdgeUtilsException - if some JAXB-error happened
    • unmarshalInclude

      public static <T> T unmarshalInclude​(java.lang.String theFileName, java.lang.Class<T> theClass) throws EdgeUtilsException
      Returns the xml data object saved in the supplied file, uses includes.

      Code from https://stackoverflow.com/questions/10212781/facing-issue-while-parsing-xml-containing-xiincludes-with-jaxb

      Type Parameters:
      T - xml data object type
      Parameters:
      theFileName - the filename of the file to unmarshal
      theClass - class of return type (needed for package information)
      Returns:
      xml data object
      Throws:
      EdgeUtilsException - if some JAXB-error happened
      Since:
      0.2
    • unmarshal

      public static <T> T unmarshal​(java.io.Reader theReader, java.lang.Class<T> theClass) throws EdgeUtilsException
      Returns the xml data object saved in the supplied reader.

      In order to unmarshal a String, use a StringReader:

      
         String sXML = ...;
         JAXBFiles.unmarshal(new StringReader(sXML), XAZ.class);
       
      Type Parameters:
      T - xml data object type
      Parameters:
      theReader - the filename of the file to unmarshal
      theClass - class of return type (needed for package information)
      Returns:
      xml data object
      Throws:
      EdgeUtilsException - if some JAXB-error happened
    • marshal

      public static <T> void marshal​(javax.xml.bind.JAXBElement<T> theDataElement, java.lang.String theFileName, java.lang.String theSchema) throws EdgeUtilsException
      Writes an XML file from the given xml data object to a file.

      Call:

      marshal(new ObjectFactory().create<T>(the<T>), file, schema)

      Example (<T> = IssuesType):

      
         IssuesType theIssues = new IssuesType();
         ...
         JAXBFiles.marshal(new ObjectFactory().createIssues(theIssues), "test.xml", null);
       
      Type Parameters:
      T - xml data object type
      Parameters:
      theDataElement - data model
      theFileName - filename of the file to write to
      theSchema - schema location (null allowed)
      Throws:
      EdgeUtilsException - if some JAXB-error happened
    • marshalToString

      public static <T> java.lang.String marshalToString​(javax.xml.bind.JAXBElement<T> theDataElement, java.lang.String theSchema) throws EdgeUtilsException
      Writes a given xml data object to a string.

      Call:

      marshal(new ObjectFactory().create<T>(the<T>), file, schema)

      Example (<T> = IssuesType):

      
         IssuesType theIssues = new IssuesType();
         ...
         String sXML = JAXBFiles.marshal(new ObjectFactory().createIssues(theIssues), "test.xml", null);
       
      Type Parameters:
      T - xml data object type
      Parameters:
      theDataElement - data model
      theSchema - schema location (null allowed)
      Returns:
      string representation
      Throws:
      EdgeUtilsException - if some JAXB-error happened