JaxbUtils.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */


  20. package org.openspcoop2.utils.xml;

  21. import java.io.File;
  22. import java.io.FileInputStream;
  23. import java.io.FileOutputStream;
  24. import java.io.IOException;
  25. import java.io.InputStream;
  26. import java.io.OutputStream;
  27. import java.util.Map;
  28. import java.util.concurrent.ConcurrentHashMap;

  29. import javax.xml.bind.JAXBContext;
  30. import javax.xml.bind.JAXBElement;
  31. import javax.xml.bind.JAXBException;
  32. import javax.xml.bind.Marshaller;
  33. import javax.xml.bind.Unmarshaller;


  34. /**
  35. * Utility per la gestione di xml tramite Jaxb
  36. *
  37. * @author Poli Andrea (apoli@link.it)
  38.  * @author $Author$
  39.  * @version $Rev$, $Date$
  40. */
  41. public class JaxbUtils {

  42.     private static Map<String, JAXBContext> mapJAXBContext = new ConcurrentHashMap<String, JAXBContext>();
  43.     private static synchronized void initJAXBContext(String packageName) throws JAXBException{
  44.         if(JaxbUtils.mapJAXBContext.containsKey(packageName)==false){
  45.             JaxbUtils.mapJAXBContext.put(packageName, JAXBContext.newInstance(packageName) );
  46.         }
  47.     }
  48.     private static JAXBContext getJAXBContext(String packageName) throws JAXBException{
  49.         if(JaxbUtils.mapJAXBContext.containsKey(packageName)==false){
  50.             JaxbUtils.initJAXBContext(packageName);
  51.         }
  52.         return JaxbUtils.mapJAXBContext.get(packageName);
  53.     }
  54.    
  55.    
  56.     /**
  57.      * Metodo che si occupa della generazione di un oggetto a partire da un file XML.
  58.      * Sono richiesti interattivamente i parametri che identificano il file XML.
  59.      *
  60.      * @param xmlFileName file XML
  61.      * @param classType Tipo di classe dell'oggetto da leggere
  62.      * @return L'oggetto letto dal file XML.
  63.      * @throws JAXBException
  64.      */  
  65.     public static Object xmlToObj(String xmlFileName,Class<?> classType)
  66.             throws java.io.FileNotFoundException, JAXBException{  

  67.         JAXBContext jc = JaxbUtils.getJAXBContext(classType.getPackage().getName());
  68.         Unmarshaller uctx = jc.createUnmarshaller();
  69.         FileInputStream fis = new FileInputStream(xmlFileName);
  70.         Object objectRead = null;
  71.         try{
  72.             objectRead = uctx.unmarshal(fis);
  73.         }finally{
  74.             try{
  75.                 fis.close();
  76.             }catch(Exception eis){
  77.                 // close
  78.             }
  79.         }
  80.         if(objectRead instanceof JAXBElement<?>){
  81.             return ((JAXBElement<?>)objectRead).getValue();
  82.         }
  83.         else{
  84.             return objectRead;
  85.         }
  86.     }      

  87.     /**
  88.      * Metodo che si occupa della generazione di un file XML a partire da un oggetto RegistroServizi.
  89.      * Sono richiesti interattivamente i parametri che identificano il file XML da generare.
  90.      *
  91.      * @param xmlFileName Nome del file XML da utilizzare
  92.      * @param classType Tipo di classe dell'oggetto da scrivere
  93.      * @param object Oggetto da Scrivere
  94.      * @throws JAXBException
  95.      *
  96.      */  
  97.     public static void objToXml(String xmlFileName,Class<?> classType,Object object)
  98.             throws java.io.FileNotFoundException, JAXBException{
  99.         JaxbUtils.objToXml(xmlFileName,classType,object,false);
  100.     }
  101.     public static void objToXml(String xmlFileName,Class<?> classType,Object object,boolean prettyDocument)
  102.             throws java.io.FileNotFoundException, JAXBException{

  103.         // Se esiste gia' lo sovrascrive
  104.         File file = new File(xmlFileName);
  105.         if(file.exists()){
  106.             if(!file.delete()) {
  107.                 // ignore
  108.             }
  109.         }  

  110.         JAXBContext jc = JaxbUtils.getJAXBContext(classType.getPackage().getName());
  111.         Marshaller uctx = jc.createMarshaller();
  112.         if(prettyDocument)
  113.             uctx.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
  114.         else
  115.             uctx.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.FALSE);
  116.         uctx.marshal(object, file);
  117.        
  118.     }    
  119.    
  120.    
  121.     /**
  122.      * Metodo che si occupa di generare un file a partire da un oggetto
  123.      *
  124.      * @param xmlFileName Nome del file XML da utilizzare
  125.      * @param object Oggetto da Scrivere
  126.      *
  127.      */  
  128.     public static void objToXml(String xmlFileName,byte[] object)
  129.             throws java.io.FileNotFoundException,IOException{

  130.         // Se esiste gia' lo sovrascrive
  131.         File file = new File(xmlFileName);
  132.         if(file.exists()){
  133.             if(!file.delete()) {
  134.                 // ignore
  135.             }
  136.         }  

  137.         FileOutputStream fileOut =  new FileOutputStream(xmlFileName);
  138.         try{
  139.             fileOut.write(object);
  140.             fileOut.flush();
  141.         }catch(IOException e){
  142.             throw e;
  143.         }finally{
  144.             try{
  145.                 fileOut.close();
  146.             }catch(Exception eis){}
  147.         }
  148.     }  
  149.    
  150.    
  151.     /**
  152.      * Metodo che si occupa della generazione di un oggetto a partire da un file XML.
  153.      * Sono richiesti interattivamente i parametri che identificano il file XML.
  154.      *
  155.      * @param classType Tipo di classe dell'oggetto da leggere
  156.      * @return L'oggetto letto dal file XML.
  157.      * @throws JAXBException
  158.      */  
  159.     public static Object xmlToObj(InputStream i,Class<?> classType)
  160.             throws java.io.FileNotFoundException, JAXBException{  

  161.         JAXBContext jc = JaxbUtils.getJAXBContext(classType.getPackage().getName());
  162.         Unmarshaller uctx = jc.createUnmarshaller();
  163.         Object objectRead = null;
  164.         try{
  165.             objectRead = uctx.unmarshal(i);
  166.         }finally{
  167.             try{
  168.                 i.close();
  169.             }catch(Exception eis){}
  170.         }
  171.         if(objectRead instanceof JAXBElement<?>){
  172.             return ((JAXBElement<?>)objectRead).getValue();
  173.         }
  174.         else{
  175.             return objectRead;
  176.         }

  177.     }      

  178.     /**
  179.      * Metodo che si occupa della generazione di un file XML a partire da un oggetto RegistroServizi.
  180.      * Sono richiesti interattivamente i parametri che identificano il file XML da generare.
  181.      *
  182.      * @param classType Tipo di classe dell'oggetto da scrivere
  183.      * @param object Oggetto da Scrivere
  184.      * @throws JAXBException
  185.      *
  186.      */  
  187.     public static void objToXml(OutputStream out,Class<?> classType,Object object)
  188.             throws java.io.FileNotFoundException, JAXBException{
  189.         JaxbUtils.objToXml(out,classType,object,false);
  190.     }
  191.     public static void objToXml(OutputStream out,Class<?> classType,Object object,boolean prettyDocument)
  192.             throws java.io.FileNotFoundException, JAXBException{
  193.         JaxbUtils.objToXml(out,classType,object,prettyDocument,false);
  194.     }
  195.     public static void objToXml(OutputStream out,Class<?> classType,Object object,boolean prettyDocument, boolean omitXmlDeclaration)
  196.             throws java.io.FileNotFoundException, JAXBException{

  197.         JAXBContext jc = JaxbUtils.getJAXBContext(classType.getPackage().getName());
  198.         Marshaller uctx = jc.createMarshaller();
  199.         if(prettyDocument)
  200.             uctx.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
  201.         else
  202.             uctx.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.FALSE);
  203.        
  204.         if(omitXmlDeclaration)
  205.             uctx.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
  206.        
  207.         uctx.marshal(object, out);
  208.        
  209.     }    
  210.    
  211. }