FormatUtils.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.pdd.logger.info;

  21. import java.io.ByteArrayInputStream;
  22. import java.io.InputStream;
  23. import java.io.InputStreamReader;

  24. import org.openspcoop2.message.exception.MessageException;
  25. import org.openspcoop2.utils.resources.Charset;
  26. import org.openspcoop2.utils.xml.PrettyPrintXMLUtils;
  27. import org.openspcoop2.utils.xml.XMLUtils;
  28. import org.slf4j.Logger;
  29. import org.w3c.dom.Element;
  30. import org.xml.sax.InputSource;

  31. /**
  32.  * FormatUtils
  33.  *
  34.  * @author Andrea Poli (apoli@link.it)
  35.  * @author $Author$
  36.  * @version $Rev$, $Date$
  37.  *
  38.  */
  39. public class FormatUtils {

  40.     public static String getTestoVisualizzabile(Logger log, byte [] b,StringBuilder stringBuffer, boolean logError) {
  41.         // 1024 = 1K
  42.         // Visualizzo al massimo 250K
  43.         int max = 250 * 1024;

  44.         return getTestoVisualizzabile(log, b, stringBuffer, logError, max);
  45.     }
  46.     public static String getTestoVisualizzabile(Logger log, byte [] b,StringBuilder stringBuffer, boolean logError, int max) {
  47.         try{

  48.             //           if(b.length>max){
  49.             //               return "Visualizzazione non riuscita: la dimensione supera 250K";
  50.             //           }
  51.             //
  52.             //           for (int i = 0; i < b.length; i++) {
  53.             //               if(!Utilities.isPrintableChar((char)b[i])){
  54.             //
  55.             //                   return "Visualizzazione non riuscita: il documento contiene caratteri non visualizzabili";
  56.             //               }
  57.             //           }
  58.             stringBuffer.append(org.openspcoop2.utils.Utilities.convertToPrintableText(b, max));
  59.             return null;

  60.         }catch(Exception e){
  61.             if(logError) {
  62.                 log.error("getTestoVisualizzabile error", e);
  63.             }
  64.             else {
  65.                 log.debug("getTestoVisualizzabile error", e);
  66.             }
  67.             return e.getMessage();
  68.         }

  69.     }

  70.     public static String prettifyXml(Logger log, String xml) {
  71.         return prettifyXml(log, xml, null);
  72.     }
  73.     public static String prettifyXml(Logger log, String xml, String charset) {
  74.         if (xml == null || "".equals(xml))
  75.             return "";
  76.         try {
  77.             //return PrettyPrintXMLUtils.prettyPrintWithTrAX(XMLUtils.getInstance().newDocument(xml.getBytes()));
  78.             if(charset==null) {
  79.                 charset = Charset.UTF_8.getValue();
  80.             }
  81.             Element element = buildContent(xml.getBytes(), charset);
  82.             return PrettyPrintXMLUtils.prettyPrintWithTrAX(element,false,charset);
  83.         } catch (Exception e) {
  84.             // non sono riuscito a formattare il messaggio
  85.             log.error(e.getMessage(),e);
  86.         }
  87.         return xml;
  88.     }
  89.     public static String prettifyXml(Logger log, byte[] xml) {
  90.         return prettifyXml(log, xml, null);
  91.     }
  92.     public static String prettifyXml(Logger log, byte[] xml, String charset) {
  93.         if (xml == null)
  94.             return "";
  95.         String res = "";
  96.         try {
  97.             //return PrettyPrintXMLUtils.prettyPrintWithTrAX(XMLUtils.getInstance().newDocument(xml));
  98.             if(charset==null) {
  99.                 charset = Charset.UTF_8.getValue();
  100.             }
  101.             Element element = buildContent(xml, charset);
  102.             return PrettyPrintXMLUtils.prettyPrintWithTrAX(element,false,charset);
  103.         } catch (Throwable e) {
  104.             // non sono riuscito a formattare il messaggio
  105.             log.error(e.getMessage(),e);
  106.         }
  107.         return res;
  108.     }
  109.     private static Element buildContent(byte [] content, String charset) throws MessageException{
  110.         InputStream is = null;
  111.         InputStreamReader isr = null;
  112.         InputSource isSax = null;
  113.         try{
  114.             is = new ByteArrayInputStream(content);
  115.             isr = new InputStreamReader(is,charset);
  116.             isSax = new InputSource(isr);
  117.             isSax.setEncoding(charset);
  118.             return XMLUtils.getInstance().newElement(isSax);
  119.         }catch(Exception e){
  120.             throw new MessageException(e.getMessage(),e);
  121.         }finally{
  122.             try{
  123.                 if(isr!=null){
  124.                     isr.close();
  125.                 }
  126.             }catch(Exception eClose){}
  127.             try{
  128.                 if(is!=null){
  129.                     is.close();
  130.                 }
  131.             }catch(Exception eClose){
  132.                 // close
  133.             }
  134.         }
  135.     }

  136. }