AbstractXml2Json.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 java.io.Writer;

  27. import javax.xml.stream.XMLEventReader;
  28. import javax.xml.stream.XMLEventWriter;
  29. import javax.xml.stream.XMLInputFactory;
  30. import javax.xml.stream.XMLOutputFactory;

  31. import org.openspcoop2.utils.UtilsException;
  32. import org.openspcoop2.utils.xml.DOMSourceFix;
  33. import org.w3c.dom.Node;

  34. /**
  35.  * @author Bussu Giovanni (bussu@link.it)
  36.  * @author  $Author$
  37.  * @version $Rev$, $Date$
  38.  *
  39.  */
  40. public abstract class AbstractXml2Json implements IXml2Json{

  41.     private XMLInputFactory inputFactory;

  42.     public AbstractXml2Json() {
  43.         this.inputFactory = XMLInputFactory.newInstance();
  44.     }

  45.     protected abstract XMLOutputFactory getOutputFactory();

  46.     @Override
  47.     public String xml2json(String xmlString) throws UtilsException {
  48.         XMLEventReader reader = null;
  49.         try {
  50.             reader = this.inputFactory.createXMLEventReader(new StringReader(xmlString));
  51.             StringWriter stringWriter = new StringWriter();
  52.             xml2json(reader, stringWriter);
  53.             return stringWriter.toString();
  54.         }catch(Exception e) {
  55.             throw new UtilsException(e.getMessage(),e);
  56.         } finally {
  57.             if(reader != null) {
  58.                 try {
  59.                     reader.close();
  60.                 } catch(Exception e) {
  61.                     // close
  62.                 }
  63.             }
  64.         }
  65.     }

  66.     @Override
  67.     public String xml2json(Node node) throws UtilsException {
  68.         XMLEventReader reader = null;
  69.         try {
  70.             reader = this.inputFactory.createXMLEventReader(new DOMSourceFix(node));
  71.             StringWriter stringWriter = new StringWriter();
  72.             xml2json(reader, stringWriter);
  73.             return stringWriter.toString();
  74.         }catch(Exception e) {
  75.             throw new UtilsException(e.getMessage(),e);
  76.         } finally {
  77.             if(reader != null){
  78.                 try {
  79.                     reader.close();
  80.                 } catch(Exception e) {
  81.                     // close
  82.                 }
  83.             }
  84.         }
  85.     }

  86.     private void xml2json(XMLEventReader reader, Writer writer) throws Exception {
  87.         XMLEventWriter eventWriter = null;
  88.         try {
  89.             eventWriter = this.getOutputFactory().createXMLEventWriter(writer);
  90.             eventWriter.add(reader);
  91.         }catch(Exception e) {
  92.             throw new UtilsException(e.getMessage(),e);
  93.         } finally {
  94.             if(eventWriter != null){
  95.                 try {
  96.                     eventWriter.close();
  97.                 } catch(Exception e) {
  98.                     // close
  99.                 }
  100.             }
  101.         }
  102.     }

  103. }