XMLUtils.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 javax.xml.XMLConstants;
  22. import javax.xml.datatype.DatatypeFactory;
  23. import javax.xml.parsers.DocumentBuilderFactory;
  24. import javax.xml.transform.TransformerFactory;
  25. import javax.xml.validation.SchemaFactory;
  26. import javax.xml.xpath.XPathFactory;

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

  35.     protected XMLUtils() {}
  36.    
  37.     private static XMLUtils xmlUtils = null;
  38.     private static synchronized void init(){
  39.         if(XMLUtils.xmlUtils==null){
  40.             XMLUtils.xmlUtils = new XMLUtils();
  41.         }
  42.     }
  43.     public static XMLUtils getInstance(){
  44.         if(XMLUtils.xmlUtils==null){
  45.             // spotbugs warning 'SING_SINGLETON_GETTER_NOT_SYNCHRONIZED'
  46.             synchronized (XMLUtils.class) {
  47.                 XMLUtils.init();
  48.             }
  49.         }
  50.         return XMLUtils.xmlUtils;
  51.     }
  52.    
  53.     // XERCES
  54.     @Override
  55.     protected DocumentBuilderFactory newDocumentBuilderFactory() throws XMLException{
  56.         try{
  57.             // force xerces impl
  58.             /**System.setProperty("javax.xml.parsers.DocumentBuilderFactory", org.apache.xerces.jaxp.DocumentBuilderFactoryImpl.class.getName());
  59.             return DocumentBuilderFactory.newInstance();*/
  60.             DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(org.apache.xerces.jaxp.DocumentBuilderFactoryImpl.class.getName(),this.getClass().getClassLoader());
  61.             if(DISABLE_DTDs) {
  62.                 documentBuilderFactory.setFeature(getFeatures_disallow_doctype_decl(), true);
  63.             }
  64.             return documentBuilderFactory;
  65.         }catch(Exception e){
  66.             throw new XMLException(e.getMessage(),e);
  67.         }
  68.     }
  69.     @Override
  70.     protected DatatypeFactory newDatatypeFactory() throws XMLException{
  71.         try{
  72.             // force xerces impl
  73.             /**System.setProperty("javax.xml.datatype.DatatypeFactory", org.apache.xerces.jaxp.datatype.DatatypeFactoryImpl.class.getName());
  74.             return DatatypeFactory.newInstance();*/
  75.             return DatatypeFactory.newInstance(org.apache.xerces.jaxp.datatype.DatatypeFactoryImpl.class.getName(), this.getClass().getClassLoader());
  76.         }catch(Exception e){
  77.             throw new XMLException(e.getMessage(),e);
  78.         }
  79.     }
  80.     @Override
  81.     protected javax.xml.parsers.SAXParserFactory newSAXParserFactory() throws XMLException{
  82.         try{
  83.             // force xerces impl
  84.             /**System.setProperty("javax.xml.parsers.SAXParserFactory", org.apache.xerces.jaxp.SAXParserFactoryImpl.class.getName());
  85.             return javax.xml.parsers.SAXParserFactory.newInstance();*/
  86.             javax.xml.parsers.SAXParserFactory saxParserFactory = javax.xml.parsers.SAXParserFactory.newInstance(org.apache.xerces.jaxp.SAXParserFactoryImpl.class.getName(), this.getClass().getClassLoader());
  87.             if(DISABLE_DTDs) {
  88.                 saxParserFactory.setFeature(getFeatures_disallow_doctype_decl(), true);
  89.             }
  90.             return saxParserFactory;
  91.         }catch(Exception e){
  92.             throw new XMLException(e.getMessage(),e);
  93.         }
  94.     }
  95.     /**
  96.     @Override
  97.     protected javax.xml.stream.XMLEventFactory newXMLEventFactory() throws XMLException{
  98.         try{            
  99.             // force xerces impl
  100. //          System.setProperty("javax.xml.stream.XMLEventFactory", org.apache.xerces.stax.XMLEventFactoryImpl.class.getName());
  101. //          return javax.xml.stream.XMLEventFactory.newFactory();
  102.             return javax.xml.stream.XMLEventFactory.newFactory(org.apache.xerces.stax.XMLEventFactoryImpl.class.getName(), this.getClass().getClassLoader());
  103.         }catch(Exception e){
  104.             throw new XMLException(e.getMessage(),e);
  105.         }
  106.     }
  107.     */
  108.     @Override
  109.     protected SchemaFactory newSchemaFactory() throws XMLException{
  110.         try{
  111.             // force xerces impl
  112.             SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI,
  113.                     org.apache.xerces.jaxp.validation.XMLSchemaFactory.class.getName(), this.getClass().getClassLoader());
  114.             if(DISABLE_DTDs) {
  115.                 schemaFactory.setFeature(getFeatures_disallow_doctype_decl(), true);
  116.             }
  117.             return schemaFactory;
  118.         }catch(Exception e){
  119.             throw new XMLException(e.getMessage(),e);
  120.         }
  121.     }
  122.    
  123.     // XALAN
  124.     @Override
  125.     protected TransformerFactory newTransformerFactory() throws XMLException{
  126.         try{
  127.             // force xalan impl
  128.             /**System.setProperty("javax.xml.transform.TransformerFactory", org.apache.xalan.processor.TransformerFactoryImpl.class.getName());
  129.             return TransformerFactory.newInstance();*/
  130.             TransformerFactory transformerFactory = TransformerFactory.newInstance(org.apache.xalan.processor.TransformerFactoryImpl.class.getName(), this.getClass().getClassLoader());
  131.             /**if(DISABLE_DTDs) {
  132.                 transformerFactory.setAttribute(javax.xml.XMLConstants.ACCESS_EXTERNAL_DTD, "");
  133.                 transformerFactory.setAttribute(javax.xml.XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "");
  134.             }*/
  135.             return transformerFactory;
  136.         }catch(Exception e){
  137.             throw new XMLException(e.getMessage(),e);
  138.         }
  139.     }
  140.     @Override
  141.     protected XPathFactory newXPathFactory() throws XMLException{
  142.         try{
  143.             /**return XPathFactory.newInstance();*/
  144.             // force xalan impl
  145.             return XPathFactory.newInstance(XPathFactory.DEFAULT_OBJECT_MODEL_URI,
  146.                     org.apache.xpath.jaxp.XPathFactoryImpl.class.getName(), this.getClass().getClassLoader());
  147.         }catch(Exception e){
  148.             throw new XMLException(e.getMessage(),e);
  149.         }
  150.     }

  151. }