AbstractMappedConverter.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.io.File;
  22. import java.util.ArrayList;
  23. import java.util.List;
  24. import java.util.Scanner;

  25. import org.apache.commons.lang.StringUtils;
  26. import org.openspcoop2.utils.SortedMap;
  27. import org.openspcoop2.utils.UtilsException;
  28. import org.openspcoop2.utils.resources.FileSystemUtilities;

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

  36.     protected boolean camelCase = false;
  37.     protected boolean camelCase_firstLower = false;
  38.     protected SortedMap<String> renameFields = new SortedMap<String>();
  39.    
  40.     protected List<String> arrays = new ArrayList<>();

  41.     protected SortedMap<String[]> reorderChildren = new SortedMap<String[]>();
  42.     protected boolean forceReorder = false;
  43.    
  44.     protected boolean prettyPrint = false;
  45.    
  46.    
  47.     // -- CamelCase e Rename Field
  48.    
  49.     public void activeCamelCase(boolean firstLowerCase) {
  50.         this.camelCase = true;
  51.         this.camelCase_firstLower = firstLowerCase;
  52.     }
  53.     public void addRenameField(String path, String newName) throws UtilsException {
  54.         this.renameFields.add(path, newName);
  55.     }  
  56.     public void readRenameFieldConfigFromFile(String path) throws UtilsException {
  57.         this.readRenameFieldConfigFromFile(new File(path));
  58.     }
  59.     public void readRenameFieldConfigFromFile(File path) throws UtilsException {
  60.         if(path.exists()==false) {
  61.             throw new UtilsException("File ["+path.getAbsolutePath()+"] not exists");
  62.         }
  63.         if(path.canRead()==false) {
  64.             throw new UtilsException("File ["+path.getAbsolutePath()+"] cannot read");
  65.         }
  66.         byte [] r = null;
  67.         try {
  68.             r = FileSystemUtilities.readBytesFromFile(path);
  69.         }catch(Exception e) {
  70.             throw new UtilsException(e.getMessage(),e);
  71.         }
  72.         this.readRenameFieldConfigFromFile(r);
  73.     }
  74.     public void readRenameFieldConfigFromFile(byte[] resource) throws UtilsException {
  75.         readFileConfig(resource, this.renameFields);
  76.     }  
  77.    
  78.    
  79.     // -- Array
  80.    
  81.     public void addArrayMapping(String path) throws UtilsException {
  82.         this.arrays.add(path);
  83.     }
  84.    
  85.     public void readArrayMappingFromFile(String path) throws UtilsException {
  86.         this.readArrayMappingFromFile(new File(path));
  87.     }
  88.     public void readArrayMappingFromFile(File path) throws UtilsException {
  89.         if(path.exists()==false) {
  90.             throw new UtilsException("File ["+path.getAbsolutePath()+"] not exists");
  91.         }
  92.         if(path.canRead()==false) {
  93.             throw new UtilsException("File ["+path.getAbsolutePath()+"] cannot read");
  94.         }
  95.         byte [] r = null;
  96.         try {
  97.             r = FileSystemUtilities.readBytesFromFile(path);
  98.         }catch(Exception e) {
  99.             throw new UtilsException(e.getMessage(),e);
  100.         }
  101.         this.readArrayMappingFromFile(r);
  102.     }
  103.     public void readArrayMappingFromFile(byte[] resource) throws UtilsException {
  104.         readFileConfig(resource, this.arrays);
  105.     }  
  106.            
  107.    
  108.     // -- Reorder elements
  109.    
  110.     public void addReorderChildren(String path, List<String> children) throws UtilsException {
  111.         if(children==null || children.size()<=0) {
  112.             throw new UtilsException("Children undefined");
  113.         }
  114.         addReorderChildren(path, children.toArray(new String[1]));
  115.     }
  116.     public void addReorderChildren(String path, String children) throws UtilsException {
  117.         List<String> l = toList(children);
  118.         if(l.isEmpty()) {
  119.             throw new UtilsException("Children not found");
  120.         }
  121.         addReorderChildren(path, l);
  122.     }
  123.     public void addReorderChildren(String path, String ... children) throws UtilsException {
  124.         if(children==null || children.length<=0) {
  125.             throw new UtilsException("Children undefined");
  126.         }
  127.         this.reorderChildren.add(path, children);
  128.     }  
  129.     public void readReorderChildrenConfigFromFile(String path) throws UtilsException {
  130.         this.readReorderChildrenConfigFromFile(new File(path));
  131.     }
  132.     public void readReorderChildrenConfigFromFile(File path) throws UtilsException {
  133.         if(path.exists()==false) {
  134.             throw new UtilsException("File ["+path.getAbsolutePath()+"] not exists");
  135.         }
  136.         if(path.canRead()==false) {
  137.             throw new UtilsException("File ["+path.getAbsolutePath()+"] cannot read");
  138.         }
  139.         byte [] r = null;
  140.         try {
  141.             r = FileSystemUtilities.readBytesFromFile(path);
  142.         }catch(Exception e) {
  143.             throw new UtilsException(e.getMessage(),e);
  144.         }
  145.         this.readReorderChildrenConfigFromFile(r);
  146.     }
  147.     public void readReorderChildrenConfigFromFile(byte[] resource) throws UtilsException {
  148.         readFileConfig_mapStringArray(resource, this.reorderChildren);
  149.     }  
  150.    
  151.     public void setForceReorder(boolean forceReorder) {
  152.         this.forceReorder = forceReorder;
  153.     }
  154.    
  155.    
  156.     // -- Pretty Print
  157.    
  158.     public void setPrettyPrint(boolean prettyPrint) {
  159.         this.prettyPrint = prettyPrint;
  160.     }
  161.    
  162.    
  163.    
  164.    
  165.    
  166.     // -- Utilities interne
  167.    
  168.     protected static String getParentPath(String name) throws UtilsException {
  169.         if(name.contains(".")) {
  170.             if(name.startsWith(".") || name.endsWith(".")) {
  171.                 throw new UtilsException("Uncorrect format path ["+name+"]");
  172.             }
  173.             int l = name.lastIndexOf(".");
  174.             String parentPath = name.substring(0, l);
  175.             return parentPath;
  176.         }
  177.         return null;
  178.     }
  179.     protected static String getLastNamePath(String name) throws UtilsException {
  180.         if(name.contains(".")) {
  181.             if(name.startsWith(".") || name.endsWith(".")) {
  182.                 throw new UtilsException("Uncorrect format path ["+name+"]");
  183.             }
  184.             int l = name.lastIndexOf(".");
  185.             String last = name.substring(l+1,name.length());
  186.             return last;
  187.         }
  188.         return name;
  189.     }
  190.    
  191.     protected List<String> correctPath(String tipologia, List<String> params) throws UtilsException {
  192.         List<String> newListArray = new ArrayList<>();
  193.         List<String> removePath = new ArrayList<>();
  194.         if(!params.isEmpty()) {
  195.            
  196.             for (String path : params) {
  197.                 String [] tmp = path.split("\\.");
  198.                 StringBuilder sbNewPath = new StringBuilder();
  199.                 int limit = 1;
  200.                
  201.                 while(limit<=tmp.length) {
  202.                        
  203.                     StringBuilder sbPathCheck = new StringBuilder();
  204.                     for (int i = 0; i < limit; i++) {
  205.                         if(i>0) {
  206.                             sbPathCheck.append(".");
  207.                         }
  208.                         sbPathCheck.append(tmp[i]);
  209.                     }
  210.                     String patchCheck = sbPathCheck.toString();
  211.                     if(sbNewPath.length()>0) {
  212.                         sbNewPath.append(".");
  213.                     }
  214.                     String nome = tmp[limit-1];
  215.                     if(this.renameFields.containsKey(patchCheck)) {
  216.                         nome = this.renameFields.get(patchCheck);
  217.                     }
  218.                     sbNewPath.append(nome);
  219.                                            
  220.                     limit++;
  221.                 }
  222.                 String newPath = sbNewPath.toString();
  223.                 //System.out.println("@"+tipologia+" PRIMA path["+path+"] DOPO path["+newPath+"]");
  224.                 newListArray.add(newPath);
  225.                 if("reorder".equals(tipologia)) {
  226.                     removePath.add(path);
  227.                 }
  228.             }
  229.         }
  230.        
  231.         if("reorder".equals(tipologia) && !newListArray.isEmpty()) {
  232.             for (int i = 0; i < newListArray.size(); i++) {
  233.                 String newPath = newListArray.get(i);
  234.                 String path = removePath.get(i);
  235.                 String [] v = this.reorderChildren.remove(path);
  236.                 String [] newV = new String[v.length];
  237.                 for (int j = 0; j < v.length; j++) {
  238.                     String old = v[j];
  239.                     String key = ("".equals(path)) ? old : path+"."+old;
  240.                     String newK = old;
  241.                     if(this.renameFields.containsKey(key)) {
  242.                         newK = this.renameFields.get(key);
  243.                     }
  244.                     newV[j] = newK;
  245.                 }
  246.                 //System.out.println("@"+tipologia+" PRIMA path["+path+"] DOPO path["+newPath+"] ARRAY-PRIMA["+Arrays.asList(v)+"] ARRAY["+Arrays.asList(newV)+"]");
  247.                 this.reorderChildren.add(newPath, newV);
  248.             }
  249.         }
  250.                
  251.         return newListArray;
  252.     }
  253.    
  254.     protected void readFileConfig(byte [] file, List<String> list) throws UtilsException {
  255.         _readFileConfig(file, false, list, null, null);
  256.     }
  257.     protected void readFileConfig_mapStringArray(byte [] file, SortedMap<String[]> mapStringArray) throws UtilsException {
  258.         _readFileConfig(file, true, null, mapStringArray, null);
  259.     }
  260.     protected void readFileConfig(byte [] file, SortedMap<String> mapString) throws UtilsException {
  261.         _readFileConfig(file, true, null, null, mapString);
  262.     }
  263.     private void _readFileConfig(byte [] file, boolean isProperty, List<String> listString, SortedMap<String[]> mapStringArray, SortedMap<String> mapString) throws UtilsException {
  264.         Scanner scanner = new Scanner(new String(file));
  265.         try {
  266.             while (scanner.hasNextLine()) {
  267.                 String line = scanner.nextLine();
  268.                 if(line==null || line.trim().equals("") || line.trim().startsWith("#")) {
  269.                     continue;
  270.                 }
  271.                 if(isProperty) {
  272.                     if(line.contains("=")==false) {
  273.                         throw new UtilsException("In ogni riga deve essere indicata una coppia (nome=valore); non รจ stato riscontrato il carattere separatore '=' nella riga '"+line+"'");
  274.                     }
  275.                     int indexOf = line.indexOf("=");
  276.                     String key = null;
  277.                     String value = null;
  278.                     if(indexOf==0) {
  279.                         // senza chiave (es. per root)
  280.                         key = "";
  281.                         value=line.substring(indexOf+1, line.length());
  282.                     }
  283.                     else if(indexOf==(line.length()-1)) {
  284.                         // senza valore
  285.                         key=line.substring(0,indexOf);
  286.                         value="";
  287.                     }
  288.                     else {
  289.                         key = line.substring(0, indexOf);
  290.                         value = line.substring(indexOf+1, line.length());
  291.                     }
  292.                    
  293.                     if(value.contains("#")) {
  294.                         // commento inserito dopo la definizione
  295.                         int indexOfCommento = value.indexOf("#");
  296.                         value = value.substring(0, indexOfCommento);
  297.                     }
  298.                    
  299.                     if(mapStringArray!=null) {
  300.                         List<String> l = toList(value);
  301.                         if(l.isEmpty()) {
  302.                             throw new UtilsException("element for key '"+key+"' not found");
  303.                         }
  304.                         //System.out.println("INIT ["+key+"] ["+l+"]");
  305.                         mapStringArray.add(key, l.toArray(new String[1]));
  306.                     }
  307.                     else {
  308.                         mapString.add(key, value);
  309.                     }
  310.                 }
  311.                 else {
  312.                     listString.add(line);
  313.                 }
  314.             }
  315.         }finally {
  316.             scanner.close();
  317.         }
  318.     }
  319.    
  320.     protected List<String> toList(String param){
  321.         List<String> l = new ArrayList<>();
  322.         if(param!=null && param.contains(",")) {
  323.             String [] tmp = param.split(",");
  324.             if(tmp!=null && tmp.length>0) {
  325.                 for (String c : tmp) {
  326.                     if(c!=null && !StringUtils.isEmpty(c.trim())) {
  327.                         l.add(c.trim());
  328.                     }
  329.                 }
  330.             }
  331.         }
  332.         else {
  333.             if(param!=null && !StringUtils.isEmpty(param.trim())) {
  334.                 l.add(param.trim());
  335.             }
  336.         }
  337.         return l;
  338.     }
  339. }