AbstractJson2Xml.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. /**
  21.  *
  22.  */
  23. package org.openspcoop2.utils.xml2json;

  24. import java.io.StringReader;
  25. import java.io.StringWriter;

  26. import javax.xml.stream.XMLInputFactory;
  27. import javax.xml.stream.XMLStreamReader;
  28. import javax.xml.transform.OutputKeys;
  29. import javax.xml.transform.Transformer;
  30. import javax.xml.transform.TransformerFactory;
  31. import javax.xml.transform.stax.StAXSource;
  32. import javax.xml.transform.stream.StreamResult;

  33. import org.openspcoop2.utils.UtilsException;
  34. import org.openspcoop2.utils.xml.XMLUtils;
  35. import org.w3c.dom.Node;

  36. /**
  37.  * @author Bussu Giovanni (bussu@link.it)
  38.  * @author  $Author$
  39.  * @version $Rev$, $Date$
  40.  *
  41.  */
  42. public abstract class AbstractJson2Xml implements IJson2Xml{

  43.     private TransformerFactory transformerFactory;
  44.     private XMLUtils xmlUtils;
  45.    
  46.     public AbstractJson2Xml() {
  47.         this.xmlUtils = XMLUtils.getInstance();
  48.         // si trova in: jaxp-ri-1.4.5-gov4j-1.jar
  49.         this.transformerFactory = TransformerFactory.newInstance("com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl", this.getClass().getClassLoader());
  50.     }

  51.     protected abstract XMLInputFactory getInputFactory();

  52.     @Override
  53.     public String json2xml(String jsonString) throws UtilsException {
  54.         try {
  55.             StringWriter stringWriter = new StringWriter();
  56.             XMLStreamReader xmlStreamReader = this.getInputFactory().createXMLStreamReader(new StringReader(jsonString));
  57.             Transformer transformer = this.transformerFactory.newTransformer();
  58.             transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
  59.    
  60.             transformer.transform(new StAXSource(xmlStreamReader), new StreamResult(stringWriter));
  61.             return stringWriter.toString();
  62.         }catch(Exception e) {
  63.             throw new UtilsException(e.getMessage(),e);
  64.         }
  65.     }

  66.     @Override
  67.     public Node json2xmlNode(String jsonString) throws UtilsException {
  68.         try {
  69.             String xmlstring = json2xml(jsonString);
  70.             return this.xmlUtils.newElement(xmlstring.getBytes());
  71.         }catch(Exception e) {
  72.             throw new UtilsException(e.getMessage(),e);
  73.         }
  74.     }

  75. }