MessageXMLUtils.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.message.xml;

  21. import java.util.HashMap;

  22. import javax.xml.parsers.DocumentBuilderFactory;
  23. import javax.xml.parsers.SAXParserFactory;

  24. import org.openspcoop2.message.OpenSPCoop2MessageFactory;
  25. import org.openspcoop2.utils.resources.Loader;
  26. import org.openspcoop2.utils.xml.XMLException;

  27. /**
  28.  * XMLUtils
  29.  *
  30.  *
  31.  * @author Poli Andrea (apoli@link.it)
  32.  * @author $Author$
  33.  * @version $Rev$, $Date$
  34.  */
  35. public class MessageXMLUtils extends org.openspcoop2.utils.xml.XMLUtils {

  36.     private static HashMap<String, MessageXMLUtils> xmlUtilsMap = new HashMap<>();
  37.     private static synchronized void init(OpenSPCoop2MessageFactory messageFactory){
  38.         String key = messageFactory.getClass().getName();
  39.         if(!MessageXMLUtils.xmlUtilsMap.containsKey(key)){
  40.             MessageXMLUtils xmlUtils = new MessageXMLUtils(messageFactory);
  41.             xmlUtilsMap.put(key, xmlUtils);
  42.         }
  43.     }
  44.     public static MessageXMLUtils getInstance(OpenSPCoop2MessageFactory messageFactory){
  45.         String key = messageFactory.getClass().getName();
  46.         if(!MessageXMLUtils.xmlUtilsMap.containsKey(key)){
  47.             MessageXMLUtils.init(messageFactory);
  48.         }
  49.         return MessageXMLUtils.xmlUtilsMap.get(key);
  50.     }
  51.    
  52.     public static MessageXMLUtils DEFAULT = MessageXMLUtils.getInstance(OpenSPCoop2MessageFactory.getDefaultMessageFactory());
  53.    
  54.     private OpenSPCoop2MessageFactory messageFactory;
  55.    
  56.     public MessageXMLUtils(OpenSPCoop2MessageFactory messageFactory) {
  57.         this.messageFactory = messageFactory;
  58.     }
  59.    
  60.     @Override
  61.     protected DocumentBuilderFactory newDocumentBuilderFactory() throws XMLException {
  62.         try{
  63.             return (DocumentBuilderFactory) Loader.getInstance().newInstance(this.messageFactory.getDocumentBuilderFactoryClass());
  64.         }catch(Exception e){
  65.             throw new XMLException(e.getMessage(),e);
  66.         }
  67.     }
  68.    
  69.     @Override
  70.     protected SAXParserFactory newSAXParserFactory() throws XMLException {
  71.         try{
  72.             return (SAXParserFactory) Loader.getInstance().newInstance(this.messageFactory.getSAXParserFactoryClass());
  73.         }catch(Exception e){
  74.             throw new XMLException(e.getMessage(),e);
  75.         }
  76.     }

  77. }