MappedXml2Json.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.util.Enumeration;
  25. import java.util.Map;

  26. import javax.xml.stream.XMLOutputFactory;

  27. import org.codehaus.jettison.mapped.Configuration;
  28. import org.codehaus.jettison.mapped.MappedXMLOutputFactory;
  29. import org.openspcoop2.utils.UtilsException;
  30. import org.openspcoop2.utils.xml.AbstractXMLUtils;
  31. import org.openspcoop2.utils.xml.DynamicNamespaceContext;
  32. import org.openspcoop2.utils.xml.XMLUtils;
  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 class MappedXml2Json extends AbstractXml2Json {

  41.     private AbstractXMLUtils xmlUtils;
  42.     // private Map<String, String> jsonNamespaceMap;
  43.     private MappedXMLOutputFactory mappedXMLOutputFactory;
  44.     private Configuration configuration;
  45.     public MappedXml2Json() {
  46.         super();
  47.         this.configuration = new Configuration();
  48.         this.init();
  49.     }
  50.     public MappedXml2Json(Map<String, String> jsonNamespaceMap) {
  51.         super();
  52.         //this.jsonNamespaceMap = jsonNamespaceMap;
  53.         this.configuration = new Configuration(jsonNamespaceMap);
  54.         this.init();
  55.     }
  56.     public MappedXml2Json(Configuration configuration) {
  57.         super();
  58.         this.configuration = configuration;
  59.         //this.jsonNamespaceMap = configuration.getXmlToJsonNamespaces();
  60.         /*if(this.jsonNamespaceMap!=null && this.jsonNamespaceMap.isEmpty()) {
  61.             this.jsonNamespaceMap = null;
  62.         }*/
  63.         this.init();
  64.     }
  65.     private void init() {
  66.         this.xmlUtils = XMLUtils.getInstance();
  67.         this.mappedXMLOutputFactory = new MappedXMLOutputFactory(this.configuration);          
  68.     }
  69.    
  70.     public Configuration getConfiguration() {
  71.         return this.configuration;
  72.     }
  73.    
  74.     @Override
  75.     protected XMLOutputFactory getOutputFactory() {
  76.         return this.mappedXMLOutputFactory;
  77.     }

  78.     @Override
  79.     public String xml2json(Node node) throws UtilsException {
  80.         if(this.configuration.getXmlToJsonNamespaces() == null || this.configuration.getXmlToJsonNamespaces().isEmpty()) {
  81.             DynamicNamespaceContext dnc = new DynamicNamespaceContext();
  82.             dnc.findPrefixNamespace(node);
  83.             this.refreshOutputFactory(dnc);
  84.         }
  85.         return super.xml2json(node);
  86.     }
  87.     @Override
  88.     public String xml2json(String xmlString) throws UtilsException {
  89.         try {
  90.             return xml2json(this.xmlUtils.newElement(xmlString.getBytes()));
  91.         }catch(Exception e) {
  92.             throw new UtilsException(e.getMessage(),e);
  93.         }
  94.     }

  95.     @SuppressWarnings("unchecked")
  96.     private void refreshOutputFactory(DynamicNamespaceContext f) {
  97.     /*  if(this.configuration.getXmlToJsonNamespaces() == null) {
  98.             this.configuration.setXmlToJsonNamespaces(new java.util.HashMap<>());
  99.         }*/
  100.         Enumeration<?> prefixes = f.getPrefixes();
  101.         while(prefixes.hasMoreElements()){
  102.             Object nextElement = prefixes.nextElement();
  103.             if(!nextElement.equals("xmlns"))
  104.                 this.configuration.getXmlToJsonNamespaces().put(f.getNamespaceURI((String)nextElement),(String) nextElement);
  105.         }
  106.         this.mappedXMLOutputFactory = new MappedXMLOutputFactory(this.configuration);      
  107.     }
  108.    

  109. }