Xml2JsonMappedConverter.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.xml2json;

  21. import java.util.Collections;
  22. import java.util.Comparator;
  23. import java.util.List;

  24. import org.openspcoop2.utils.UtilsException;
  25. import org.openspcoop2.utils.json.JSONUtils;
  26. import org.w3c.dom.Node;

  27. import com.fasterxml.jackson.databind.JsonNode;

  28. /**
  29.  * Xml2JsonMappedConverter
  30.  *
  31.  * @author Bussu Giovanni (bussu@link.it)
  32.  * @author  $Author$
  33.  * @version $Rev$, $Date$
  34.  *
  35.  */
  36. public class Xml2JsonMappedConverter extends AbstractMappedConverter{

  37.     private boolean ignoreNamespaces = true;
  38.     private boolean attributeAsElement = true;
  39.     private boolean dropRootElement = true;
  40.     private boolean convertAllValuesAsString = false;
  41.        
  42.     public void setIgnoreNamespaces(boolean ignoreNamespaces) {
  43.         this.ignoreNamespaces = ignoreNamespaces;
  44.     }
  45.     public void setAttributeAsElement(boolean attributeAsElement) {
  46.         this.attributeAsElement = attributeAsElement;
  47.     }
  48.     public void setDropRootElement(boolean dropRootElement) {
  49.         this.dropRootElement = dropRootElement;
  50.     }
  51.     public void setConvertAllValuesAsString(boolean convertAllValuesAsString) {
  52.         this.convertAllValuesAsString = convertAllValuesAsString;
  53.     }
  54.    
  55.     // -- Convert
  56.    
  57.     public String convert(Node xml) throws UtilsException {
  58.         String json = getXml2Json().xml2json(xml);
  59.         return _convert(json);
  60.     }
  61.    
  62.     public String convert(String xml) throws UtilsException {
  63.         String json = getXml2Json().xml2json(xml);
  64.         return _convert(json);
  65.     }
  66.    
  67.     private IXml2Json getXml2Json(){
  68.         IXml2Json xml2json = Xml2JsonFactory.getXml2JsonMapped();
  69.         ((MappedXml2Json)xml2json).getConfiguration().setIgnoreNamespaces(this.ignoreNamespaces);
  70.         ((MappedXml2Json)xml2json).getConfiguration().setSupressAtAttributes(this.attributeAsElement);
  71.         ((MappedXml2Json)xml2json).getConfiguration().setDropRootElement(this.dropRootElement);
  72.         if(this.convertAllValuesAsString) {
  73.             ((MappedXml2Json)xml2json).getConfiguration().setTypeConverter(new org.codehaus.jettison.mapped.SimpleConverter());
  74.         }
  75.         return xml2json;
  76.     }
  77.    
  78.     private String _convert(String json) throws UtilsException {
  79.        
  80.         JSONUtils utils = JSONUtils.getInstance(this.prettyPrint);
  81.        
  82.         JsonNode node = utils.getAsNode(json);
  83.        
  84.         List<String> arrays_renamed = this.arrays;
  85.         List<String> reorderChildren_renamed = this.reorderChildren.keys();
  86.        
  87.         if(!this.renameFields.isEmpty()) {
  88.            
  89.             List<String> keys = this.renameFields.keys();
  90.             Collections.sort(keys, Comparator.reverseOrder());
  91.             for (String path : keys) {
  92.                 String newName = this.renameFields.get(path);
  93.                
  94.                 //System.out.println("RENAME ["+path+"] in ["+newName+"]");
  95.                
  96.                 boolean forceReorder = this.forceReorder;

  97.                 //System.out.println("FORCE REORDER: "+forceReorder);
  98.                 utils.renameFieldByPath(node, path, newName, forceReorder, false); // non e' obbligatoria la presenza
  99.             }
  100.                        
  101.             // sistemo path nell'array
  102.             if(!arrays_renamed.isEmpty()) {
  103.                 arrays_renamed = correctPath("arrays", arrays_renamed);
  104.             }
  105.            
  106.             // sistemo path nelle istruzioni di reorder
  107.             if(!reorderChildren_renamed.isEmpty()) {
  108.                 reorderChildren_renamed = correctPath("reorder", reorderChildren_renamed);
  109.             }
  110.            
  111.         }
  112.        
  113.         if(!arrays_renamed.isEmpty()) {
  114.             for (String path : arrays_renamed) {
  115.                 //System.out.println("ARRAY ["+path+"]");
  116.                 utils.convertFieldToArrayByPath(node, path, this.forceReorder, false); // non e' obbligatoria la presenza
  117.             }
  118.         }
  119.        
  120.         if(!reorderChildren_renamed.isEmpty()) {
  121.             for (String path : reorderChildren_renamed) {
  122.                 String [] children = this.reorderChildren.get(path);
  123.                 //System.out.println("REORDER ["+path+"] ["+children.length+"] ["+java.util.Arrays.asList(children)+"]");
  124.                 utils.reorderFieldChildrenByPath(node, path, false, // non e' obbligatoria la presenza
  125.                         children);
  126.             }
  127.         }
  128.        
  129.         // lasciare come ultimo visto che modifica i nomi
  130.         if(this.camelCase) {
  131.             boolean forceReorder = this.forceReorder;
  132.             utils.renameFieldInCamelCase(node, this.camelCase_firstLower, forceReorder);
  133.         }
  134.        
  135.         return utils.toString(node);
  136.        
  137.     }
  138. }