PrettyPrintXMLUtils.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.xml;

  21. import java.io.ByteArrayOutputStream;
  22. import java.io.File;
  23. import java.io.FileNotFoundException;
  24. import java.io.FileOutputStream;
  25. import java.io.IOException;
  26. import java.io.InputStream;
  27. import java.io.OutputStream;
  28. import java.io.Writer;

  29. import javax.xml.transform.ErrorListener;
  30. import javax.xml.transform.OutputKeys;
  31. import javax.xml.transform.Source;
  32. import javax.xml.transform.Transformer;
  33. import javax.xml.transform.TransformerException;
  34. import javax.xml.transform.dom.DOMSource;
  35. import javax.xml.transform.stream.StreamResult;
  36. import javax.xml.transform.stream.StreamSource;

  37. import org.w3c.dom.Document;
  38. import org.w3c.dom.Element;
  39. import org.w3c.dom.Node;


  40. /**
  41.  * Classe utilizzabile per ottenere documenti xml
  42.  *
  43.  *
  44.  * @author Poli Andrea (apoli@link.it)
  45.  * @author $Author$
  46.  * @version $Rev$, $Date$
  47.  */

  48. public class PrettyPrintXMLUtils {

  49.     public static AbstractXMLUtils xmlUtils = XMLUtils.getInstance();
  50.     public static synchronized void setXMLUtils(AbstractXMLUtils xmlUtilsParam) {
  51.         PrettyPrintXMLUtils.xmlUtils = xmlUtilsParam;
  52.     }


  53.    
  54.     // *********** prettyPrintWithXMLSerializer **************
  55.    
  56.     /*
  57.      * (using JDK 1.5). You can configure Xerces-J’s XMLSerialzer directly to pretty-print XML.  
  58.      * Although this is the easiest method, it depends on an internal class in the Xerces parser and is not JAXP compliant!  
  59.      * You can read more in Question 11 of the JAXP FAQ (https://jaxp.dev.java.net/1.4/JAXP-FAQ.html).
  60.      */
  61.    
  62.     @Deprecated
  63.     @SuppressWarnings("deprecation")
  64.     public static void prettyPrintWithXMLSerializer(Document doc,OutputStream os)throws TransformerException,IOException{
  65.         // All is not lost if you're still on JDK 1.5: just use XMLSerializer with the appropriate OutputFormat.
  66.         // The following will pretty-print the DOM document to XML.
  67.         org.apache.xml.serialize.OutputFormat format = new org.apache.xml.serialize.OutputFormat(doc);
  68.         format.setLineWidth(65);
  69.         format.setIndenting(true);
  70.         format.setIndent(2);
  71.         org.apache.xml.serialize.XMLSerializer serializer = new org.apache.xml.serialize.XMLSerializer(os, format);
  72.         serializer.serialize(doc);
  73.     }
  74.     @Deprecated
  75.     @SuppressWarnings("deprecation")
  76.     public static void prettyPrintWithXMLSerializer(Document doc,Writer writer)throws TransformerException,IOException{
  77.         // All is not lost if you're still on JDK 1.5: just use XMLSerializer with the appropriate OutputFormat.
  78.         // The following will pretty-print the DOM document to XML.
  79.         org.apache.xml.serialize.OutputFormat format = new org.apache.xml.serialize.OutputFormat(doc);
  80.         format.setLineWidth(65);
  81.         format.setIndenting(true);
  82.         format.setIndent(2);
  83.         org.apache.xml.serialize.XMLSerializer serializer = new org.apache.xml.serialize.XMLSerializer(writer, format);
  84.         serializer.serialize(doc);
  85.     }
  86.     @Deprecated
  87.     public static void prettyPrintWithXMLSerializer(Document doc,File file)throws TransformerException,IOException{
  88.         // All is not lost if you're still on JDK 1.5: just use XMLSerializer with the appropriate OutputFormat.
  89.         // The following will pretty-print the DOM document to XML.
  90.         FileOutputStream fout = null;
  91.         try{
  92.             fout = new FileOutputStream(file);
  93.             PrettyPrintXMLUtils.prettyPrintWithXMLSerializer(doc,fout);
  94.         }finally{
  95.             try{
  96.                 if(fout!=null) {
  97.                     fout.flush();
  98.                 }
  99.             }catch(Exception eClose){
  100.                 // close
  101.             }
  102.             try{
  103.                 if(fout!=null) {
  104.                     fout.close();
  105.                 }
  106.             }catch(Exception eClose){
  107.                 // close
  108.             }
  109.         }
  110.     }
  111.     @Deprecated
  112.     public static String prettyPrintWithXMLSerializer(Document doc)throws TransformerException,IOException{
  113.         // All is not lost if you're still on JDK 1.5: just use XMLSerializer with the appropriate OutputFormat.
  114.         // The following will pretty-print the DOM document to XML.
  115.         ByteArrayOutputStream bout = new ByteArrayOutputStream();
  116.         PrettyPrintXMLUtils.prettyPrintWithXMLSerializer(doc,bout);
  117.         bout.flush();
  118.         bout.close();
  119.         return bout.toString();
  120.     }


  121.    
  122.    
  123.    
  124.    
  125.    
  126.     // *********** prettyPrintWithTrAX **************
  127.     /*
  128.      * Using the TrAX API is straightforward, however dealing with whitespace in a DOM is rather tricky.  
  129.      * It’s important to note that if your DOM was parsed from XML containing whitespace, this will by default be preserved in the object model.  
  130.      * Furthermore, the default TrAX indentation engine (Xalan-Java) won’t reformat existing whitespace.
  131.      * Therefore, to reliably pretty-print XML with TrAX you’ll need to supply an XSLT stylesheet (pretty-print.xsl) that removes whitespace from the DOM during transformation.  
  132.      * This is a subtle point about serialization using TrAX that’s missed on the Xalan website which can conflate a bug found in JDK 1.5.  
  133.      * You can read more about this phenomenon (http://forums.sun.com/thread.jspa?threadID=562510) and (http://www.coderanch.com/t/126529/XML-Related-Technologies/do-pretty-print-xml).
  134.      * */
  135.    
  136.     public static void prettyPrintWithTrAX(Document doc,OutputStream os)throws TransformerException,IOException{
  137.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(doc, os, new XMLErrorListener(),false, null);
  138.     }
  139.     public static void prettyPrintWithTrAX(Document doc,OutputStream os,ErrorListener errorListener)throws TransformerException,IOException{
  140.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(doc, os,errorListener,false, null);
  141.     }
  142.     public static void prettyPrintWithTrAX(Document doc,OutputStream os,boolean omitXMLDeclaration)throws TransformerException,IOException{
  143.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(doc, os, new XMLErrorListener(),omitXMLDeclaration, null);
  144.     }
  145.     public static void prettyPrintWithTrAX(Document doc,OutputStream os,ErrorListener errorListener,boolean omitXMLDeclaration)throws TransformerException,IOException{
  146.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(doc, os,errorListener,omitXMLDeclaration, null);
  147.     }
  148.     public static void prettyPrintWithTrAX(Document doc,OutputStream os,String charset)throws TransformerException,IOException{
  149.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(doc, os, new XMLErrorListener(),false, charset);
  150.     }
  151.     public static void prettyPrintWithTrAX(Document doc,OutputStream os,ErrorListener errorListener,String charset)throws TransformerException,IOException{
  152.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(doc, os,errorListener,false, charset);
  153.     }
  154.     public static void prettyPrintWithTrAX(Document doc,OutputStream os,boolean omitXMLDeclaration,String charset)throws TransformerException,IOException{
  155.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(doc, os, new XMLErrorListener(),omitXMLDeclaration, charset);
  156.     }
  157.     public static void prettyPrintWithTrAX(Document doc,OutputStream os,ErrorListener errorListener,boolean omitXMLDeclaration,String charset)throws TransformerException,IOException{
  158.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(doc, os,errorListener,omitXMLDeclaration, charset);
  159.     }

  160.     public static void prettyPrintWithTrAX(Document doc,Writer writer)throws TransformerException,IOException{
  161.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(doc, writer, new XMLErrorListener(),false, null);
  162.     }
  163.     public static void prettyPrintWithTrAX(Document doc,Writer writer,ErrorListener errorListener)throws TransformerException,IOException{
  164.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(doc, writer,errorListener,false, null);
  165.     }
  166.     public static void prettyPrintWithTrAX(Document doc,Writer writer,boolean omitXMLDeclaration)throws TransformerException,IOException{
  167.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(doc, writer, new XMLErrorListener(),omitXMLDeclaration, null);
  168.     }
  169.     public static void prettyPrintWithTrAX(Document doc,Writer writer,ErrorListener errorListener,boolean omitXMLDeclaration)throws TransformerException,IOException{
  170.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(doc, writer,errorListener,omitXMLDeclaration, null);
  171.     }
  172.     public static void prettyPrintWithTrAX(Document doc,Writer writer,String charset)throws TransformerException,IOException{
  173.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(doc, writer, new XMLErrorListener(),false, charset);
  174.     }
  175.     public static void prettyPrintWithTrAX(Document doc,Writer writer,ErrorListener errorListener,String charset)throws TransformerException,IOException{
  176.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(doc, writer,errorListener,false, charset);
  177.     }
  178.     public static void prettyPrintWithTrAX(Document doc,Writer writer,boolean omitXMLDeclaration,String charset)throws TransformerException,IOException{
  179.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(doc, writer, new XMLErrorListener(),omitXMLDeclaration, charset);
  180.     }
  181.     public static void prettyPrintWithTrAX(Document doc,Writer writer,ErrorListener errorListener,boolean omitXMLDeclaration,String charset)throws TransformerException,IOException{
  182.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(doc, writer,errorListener,omitXMLDeclaration, charset);
  183.     }

  184.     public static void prettyPrintWithTrAX(Document doc,File file)throws TransformerException,IOException{
  185.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(doc, file, new XMLErrorListener(),false, null);
  186.     }
  187.     public static void prettyPrintWithTrAX(Document doc,File file,ErrorListener errorListener)throws TransformerException,IOException{
  188.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(doc, file,errorListener,false, null);
  189.     }
  190.     public static void prettyPrintWithTrAX(Document doc,File file,boolean omitXMLDeclaration)throws TransformerException,IOException{
  191.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(doc, file, new XMLErrorListener(),omitXMLDeclaration, null);
  192.     }
  193.     public static void prettyPrintWithTrAX(Document doc,File file,ErrorListener errorListener,boolean omitXMLDeclaration)throws TransformerException,IOException{
  194.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(doc, file,errorListener,omitXMLDeclaration, null);
  195.     }
  196.     public static void prettyPrintWithTrAX(Document doc,File file,String charset)throws TransformerException,IOException{
  197.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(doc, file, new XMLErrorListener(),false, charset);
  198.     }
  199.     public static void prettyPrintWithTrAX(Document doc,File file,ErrorListener errorListener,String charset)throws TransformerException,IOException{
  200.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(doc, file,errorListener,false, charset);
  201.     }
  202.     public static void prettyPrintWithTrAX(Document doc,File file,boolean omitXMLDeclaration,String charset)throws TransformerException,IOException{
  203.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(doc, file, new XMLErrorListener(),omitXMLDeclaration, charset);
  204.     }
  205.     public static void prettyPrintWithTrAX(Document doc,File file,ErrorListener errorListener,boolean omitXMLDeclaration,String charset)throws TransformerException,IOException{
  206.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(doc, file,errorListener,omitXMLDeclaration, charset);
  207.     }
  208.    
  209.     public static String prettyPrintWithTrAX(Document doc)throws TransformerException,IOException{
  210.         return PrettyPrintXMLUtils.prettyPrintWithTrAX(doc, new XMLErrorListener(),false, null);
  211.     }
  212.     public static String prettyPrintWithTrAX(Document doc,ErrorListener errorListener)throws TransformerException,IOException{
  213.         return PrettyPrintXMLUtils.prettyPrintWithTrAX(doc, errorListener,false, null);
  214.     }
  215.     public static String prettyPrintWithTrAX(Document doc,boolean omitXMLDeclaration)throws TransformerException,IOException{
  216.         return PrettyPrintXMLUtils.prettyPrintWithTrAX(doc, new XMLErrorListener(),omitXMLDeclaration, null);
  217.     }
  218.     public static String prettyPrintWithTrAX(Document doc,ErrorListener errorListener,boolean omitXMLDeclaration)throws TransformerException,IOException{
  219.         return PrettyPrintXMLUtils.prettyPrintWithTrAX(doc, errorListener,omitXMLDeclaration, null);
  220.     }
  221.     public static String prettyPrintWithTrAX(Document doc,String charset)throws TransformerException,IOException{
  222.         return PrettyPrintXMLUtils.prettyPrintWithTrAX(doc, new XMLErrorListener(),false, charset);
  223.     }
  224.     public static String prettyPrintWithTrAX(Document doc,ErrorListener errorListener,String charset)throws TransformerException,IOException{
  225.         return PrettyPrintXMLUtils.prettyPrintWithTrAX(doc, errorListener,false, charset);
  226.     }
  227.     public static String prettyPrintWithTrAX(Document doc,boolean omitXMLDeclaration,String charset)throws TransformerException,IOException{
  228.         return PrettyPrintXMLUtils.prettyPrintWithTrAX(doc, new XMLErrorListener(),omitXMLDeclaration, charset);
  229.     }
  230.     public static String prettyPrintWithTrAX(Document doc,ErrorListener errorListener,boolean omitXMLDeclaration,String charset)throws TransformerException,IOException{
  231.         ByteArrayOutputStream bout = new ByteArrayOutputStream();
  232.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(doc, bout, errorListener,omitXMLDeclaration, charset);
  233.         bout.flush();
  234.         bout.close();
  235.         return bout.toString();
  236.     }

  237.     public static void prettyPrintWithTrAX(Element element,OutputStream os)throws TransformerException,IOException{
  238.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(element, os, new XMLErrorListener(),false,null);
  239.     }
  240.     public static void prettyPrintWithTrAX(Element element,OutputStream os,ErrorListener errorListener)throws TransformerException,IOException{
  241.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(element, os,errorListener,false,null);
  242.     }
  243.     public static void prettyPrintWithTrAX(Element element,OutputStream os,boolean omitXMLDeclaration)throws TransformerException,IOException{
  244.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(element, os, new XMLErrorListener(),omitXMLDeclaration,null);
  245.     }
  246.     public static void prettyPrintWithTrAX(Element element,OutputStream os,ErrorListener errorListener,boolean omitXMLDeclaration)throws TransformerException,IOException{
  247.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(element, os,errorListener,omitXMLDeclaration,null);
  248.     }
  249.     public static void prettyPrintWithTrAX(Element element,OutputStream os,String charset)throws TransformerException,IOException{
  250.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(element, os, new XMLErrorListener(),false, charset);
  251.     }
  252.     public static void prettyPrintWithTrAX(Element element,OutputStream os,ErrorListener errorListener,String charset)throws TransformerException,IOException{
  253.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(element, os,errorListener,false, charset);
  254.     }
  255.     public static void prettyPrintWithTrAX(Element element,OutputStream os,boolean omitXMLDeclaration,String charset)throws TransformerException,IOException{
  256.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(element, os, new XMLErrorListener(),omitXMLDeclaration, charset);
  257.     }
  258.     public static void prettyPrintWithTrAX(Element element,OutputStream os,ErrorListener errorListener,boolean omitXMLDeclaration,String charset)throws TransformerException,IOException{
  259.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(element, os,errorListener,omitXMLDeclaration, charset);
  260.     }

  261.     public static void prettyPrintWithTrAX(Element element,Writer writer)throws TransformerException,IOException{
  262.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(element, writer, new XMLErrorListener(),false,null);
  263.     }
  264.     public static void prettyPrintWithTrAX(Element element,Writer writer,ErrorListener errorListener)throws TransformerException,IOException{
  265.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(element, writer,errorListener,false,null);
  266.     }
  267.     public static void prettyPrintWithTrAX(Element element,Writer writer,boolean omitXMLDeclaration)throws TransformerException,IOException{
  268.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(element, writer, new XMLErrorListener(),omitXMLDeclaration,null);
  269.     }
  270.     public static void prettyPrintWithTrAX(Element element,Writer writer,ErrorListener errorListener,boolean omitXMLDeclaration)throws TransformerException,IOException{
  271.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(element, writer,errorListener,omitXMLDeclaration,null);
  272.     }
  273.     public static void prettyPrintWithTrAX(Element element,Writer writer,String charset)throws TransformerException,IOException{
  274.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(element, writer, new XMLErrorListener(),false, charset);
  275.     }
  276.     public static void prettyPrintWithTrAX(Element element,Writer writer,ErrorListener errorListener,String charset)throws TransformerException,IOException{
  277.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(element, writer,errorListener,false, charset);
  278.     }
  279.     public static void prettyPrintWithTrAX(Element element,Writer writer,boolean omitXMLDeclaration,String charset)throws TransformerException,IOException{
  280.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(element, writer, new XMLErrorListener(),omitXMLDeclaration, charset);
  281.     }
  282.     public static void prettyPrintWithTrAX(Element element,Writer writer,ErrorListener errorListener,boolean omitXMLDeclaration,String charset)throws TransformerException,IOException{
  283.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(element, writer,errorListener,omitXMLDeclaration, charset);
  284.     }

  285.     public static void prettyPrintWithTrAX(Element element,File file)throws TransformerException,IOException{
  286.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(element, file, new XMLErrorListener(),false,null);
  287.     }
  288.     public static void prettyPrintWithTrAX(Element element,File file,ErrorListener errorListener)throws TransformerException,IOException{
  289.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(element, file,errorListener,false,null);
  290.     }
  291.     public static void prettyPrintWithTrAX(Element element,File file,boolean omitXMLDeclaration)throws TransformerException,IOException{
  292.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(element, file, new XMLErrorListener(),omitXMLDeclaration,null);
  293.     }
  294.     public static void prettyPrintWithTrAX(Element element,File file,ErrorListener errorListener,boolean omitXMLDeclaration)throws TransformerException,IOException{
  295.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(element, file,errorListener,omitXMLDeclaration,null);
  296.     }
  297.     public static void prettyPrintWithTrAX(Element element,File file,String charset)throws TransformerException,IOException{
  298.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(element, file, new XMLErrorListener(),false, charset);
  299.     }
  300.     public static void prettyPrintWithTrAX(Element element,File file,ErrorListener errorListener,String charset)throws TransformerException,IOException{
  301.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(element, file,errorListener,false, charset);
  302.     }
  303.     public static void prettyPrintWithTrAX(Element element,File file,boolean omitXMLDeclaration,String charset)throws TransformerException,IOException{
  304.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(element, file, new XMLErrorListener(),omitXMLDeclaration, charset);
  305.     }
  306.     public static void prettyPrintWithTrAX(Element element,File file,ErrorListener errorListener,boolean omitXMLDeclaration,String charset)throws TransformerException,IOException{
  307.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(element, file,errorListener,omitXMLDeclaration, charset);
  308.     }

  309.     public static String prettyPrintWithTrAX(Element element)throws TransformerException,IOException{
  310.         return PrettyPrintXMLUtils.prettyPrintWithTrAX(element, new XMLErrorListener(),false,null);
  311.     }
  312.     public static String prettyPrintWithTrAX(Element element,ErrorListener errorListener)throws TransformerException,IOException{
  313.         return PrettyPrintXMLUtils.prettyPrintWithTrAX(element, errorListener,false,null);
  314.     }
  315.     public static String prettyPrintWithTrAX(Element element,boolean omitXMLDeclaration)throws TransformerException,IOException{
  316.         return PrettyPrintXMLUtils.prettyPrintWithTrAX(element, new XMLErrorListener(),omitXMLDeclaration,null);
  317.     }
  318.     public static String prettyPrintWithTrAX(Element element,ErrorListener errorListener,boolean omitXMLDeclaration)throws TransformerException,IOException{
  319.         return PrettyPrintXMLUtils.prettyPrintWithTrAX(element, errorListener,omitXMLDeclaration,null);
  320.     }
  321.     public static String prettyPrintWithTrAX(Element element,String charset)throws TransformerException,IOException{
  322.         return PrettyPrintXMLUtils.prettyPrintWithTrAX(element, new XMLErrorListener(),false, charset);
  323.     }
  324.     public static String prettyPrintWithTrAX(Element element,ErrorListener errorListener,String charset)throws TransformerException,IOException{
  325.         return PrettyPrintXMLUtils.prettyPrintWithTrAX(element, errorListener,false, charset);
  326.     }
  327.     public static String prettyPrintWithTrAX(Element element,boolean omitXMLDeclaration,String charset)throws TransformerException,IOException{
  328.         return PrettyPrintXMLUtils.prettyPrintWithTrAX(element, new XMLErrorListener(),omitXMLDeclaration, charset);
  329.     }
  330.     public static String prettyPrintWithTrAX(Element element,ErrorListener errorListener,boolean omitXMLDeclaration,String charset)throws TransformerException,IOException{
  331.         ByteArrayOutputStream bout = new ByteArrayOutputStream();
  332.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(element, bout, errorListener,omitXMLDeclaration, charset);
  333.         bout.flush();
  334.         bout.close();
  335.         return bout.toString();
  336.     }
  337.    
  338.     public static void prettyPrintWithTrAX(Node node,OutputStream os)throws TransformerException,IOException{
  339.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(node,os, new XMLErrorListener(),false,null);
  340.     }
  341.     public static void prettyPrintWithTrAX(Node node,OutputStream os,ErrorListener errorListener)throws TransformerException,IOException{
  342.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(node,os,errorListener,false,null);
  343.     }
  344.     public static void prettyPrintWithTrAX(Node node,OutputStream os,boolean omitXMLDeclaration)throws TransformerException,IOException{
  345.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(node,os, new XMLErrorListener(),omitXMLDeclaration,null);
  346.     }
  347.     public static void prettyPrintWithTrAX(Node node,OutputStream os,ErrorListener errorListener,boolean omitXMLDeclaration)throws TransformerException,IOException{
  348.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(node,os,errorListener,omitXMLDeclaration,null);
  349.     }
  350.     public static void prettyPrintWithTrAX(Node node,OutputStream os,String charset)throws TransformerException,IOException{
  351.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(node,os, new XMLErrorListener(),false, charset);
  352.     }
  353.     public static void prettyPrintWithTrAX(Node node,OutputStream os,ErrorListener errorListener,String charset)throws TransformerException,IOException{
  354.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(node,os,errorListener,false, charset);
  355.     }
  356.     public static void prettyPrintWithTrAX(Node node,OutputStream os,boolean omitXMLDeclaration,String charset)throws TransformerException,IOException{
  357.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(node,os, new XMLErrorListener(),omitXMLDeclaration, charset);
  358.     }
  359.     public static void prettyPrintWithTrAX(Node node,OutputStream os,ErrorListener errorListener,boolean omitXMLDeclaration,String charset)throws TransformerException,IOException{
  360.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(node,os,errorListener,omitXMLDeclaration, charset);
  361.     }

  362.     public static void prettyPrintWithTrAX(Node node,Writer writer)throws TransformerException,IOException{
  363.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(node,writer, new XMLErrorListener(),false,null);
  364.     }
  365.     public static void prettyPrintWithTrAX(Node node,Writer writer,ErrorListener errorListener)throws TransformerException,IOException{
  366.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(node,writer,errorListener,false,null);
  367.     }
  368.     public static void prettyPrintWithTrAX(Node node,Writer writer,boolean omitXMLDeclaration)throws TransformerException,IOException{
  369.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(node,writer, new XMLErrorListener(),omitXMLDeclaration,null);
  370.     }
  371.     public static void prettyPrintWithTrAX(Node node,Writer writer,ErrorListener errorListener,boolean omitXMLDeclaration)throws TransformerException,IOException{
  372.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(node,writer,errorListener,omitXMLDeclaration,null);
  373.     }
  374.     public static void prettyPrintWithTrAX(Node node,Writer writer,String charset)throws TransformerException,IOException{
  375.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(node,writer, new XMLErrorListener(),false, charset);
  376.     }
  377.     public static void prettyPrintWithTrAX(Node node,Writer writer,ErrorListener errorListener,String charset)throws TransformerException,IOException{
  378.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(node,writer,errorListener,false, charset);
  379.     }
  380.     public static void prettyPrintWithTrAX(Node node,Writer writer,boolean omitXMLDeclaration,String charset)throws TransformerException,IOException{
  381.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(node,writer, new XMLErrorListener(),omitXMLDeclaration, charset);
  382.     }
  383.     public static void prettyPrintWithTrAX(Node node,Writer writer,ErrorListener errorListener,boolean omitXMLDeclaration,String charset)throws TransformerException,IOException{
  384.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(node,writer,errorListener,omitXMLDeclaration, charset);
  385.     }

  386.     public static void prettyPrintWithTrAX(Node node,File file)throws TransformerException,IOException{
  387.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(node,file, new XMLErrorListener(),false,null);
  388.     }
  389.     public static void prettyPrintWithTrAX(Node node,File file,ErrorListener errorListener)throws TransformerException,IOException{
  390.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(node,file,errorListener,false,null);
  391.     }
  392.     public static void prettyPrintWithTrAX(Node node,File file,boolean omitXMLDeclaration)throws TransformerException,IOException{
  393.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(node,file, new XMLErrorListener(),omitXMLDeclaration,null);
  394.     }
  395.     public static void prettyPrintWithTrAX(Node node,File file,ErrorListener errorListener,boolean omitXMLDeclaration)throws TransformerException,IOException{
  396.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(node,file,errorListener,omitXMLDeclaration,null);
  397.     }
  398.     public static void prettyPrintWithTrAX(Node node,File file,String charset)throws TransformerException,IOException{
  399.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(node,file, new XMLErrorListener(),false, charset);
  400.     }
  401.     public static void prettyPrintWithTrAX(Node node,File file,ErrorListener errorListener,String charset)throws TransformerException,IOException{
  402.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(node,file,errorListener,false, charset);
  403.     }
  404.     public static void prettyPrintWithTrAX(Node node,File file,boolean omitXMLDeclaration,String charset)throws TransformerException,IOException{
  405.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(node,file, new XMLErrorListener(),omitXMLDeclaration, charset);
  406.     }
  407.     public static void prettyPrintWithTrAX(Node node,File file,ErrorListener errorListener,boolean omitXMLDeclaration,String charset)throws TransformerException,IOException{
  408.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(node,file,errorListener,omitXMLDeclaration, charset);
  409.     }
  410.    
  411.     public static String prettyPrintWithTrAX(Node node)throws TransformerException,IOException{
  412.         return PrettyPrintXMLUtils.prettyPrintWithTrAX(node, new XMLErrorListener(),false,null);
  413.     }
  414.     public static String prettyPrintWithTrAX(Node node,ErrorListener errorListener)throws TransformerException,IOException{
  415.         return PrettyPrintXMLUtils.prettyPrintWithTrAX(node, errorListener,false,null);
  416.     }
  417.     public static String prettyPrintWithTrAX(Node node,boolean omitXMLDeclaration)throws TransformerException,IOException{
  418.         return PrettyPrintXMLUtils.prettyPrintWithTrAX(node, new XMLErrorListener(),omitXMLDeclaration,null);
  419.     }
  420.     public static String prettyPrintWithTrAX(Node node,ErrorListener errorListener,boolean omitXMLDeclaration)throws TransformerException,IOException{
  421.         return PrettyPrintXMLUtils.prettyPrintWithTrAX(node, errorListener,omitXMLDeclaration,null);
  422.     }
  423.     public static String prettyPrintWithTrAX(Node node,String charset)throws TransformerException,IOException{
  424.         return PrettyPrintXMLUtils.prettyPrintWithTrAX(node, new XMLErrorListener(),false,charset);
  425.     }
  426.     public static String prettyPrintWithTrAX(Node node,ErrorListener errorListener,String charset)throws TransformerException,IOException{
  427.         return PrettyPrintXMLUtils.prettyPrintWithTrAX(node, errorListener,false,charset);
  428.     }
  429.     public static String prettyPrintWithTrAX(Node node,boolean omitXMLDeclaration,String charset)throws TransformerException,IOException{
  430.         return PrettyPrintXMLUtils.prettyPrintWithTrAX(node, new XMLErrorListener(),omitXMLDeclaration,charset);
  431.     }
  432.     public static String prettyPrintWithTrAX(Node node,ErrorListener errorListener,boolean omitXMLDeclaration,String charset)throws TransformerException,IOException{
  433.         ByteArrayOutputStream bout = new ByteArrayOutputStream();
  434.         PrettyPrintXMLUtils.prettyPrintWithTrAX_engine(node, bout, errorListener,omitXMLDeclaration,charset);
  435.         bout.flush();
  436.         bout.close();
  437.         return bout.toString();
  438.     }
  439.    
  440.     private static InputStream readPrettyPrintXslt() throws IOException{
  441.         return PrettyPrintXMLUtils.class.getResourceAsStream("/org/openspcoop2/utils/xml/pretty-print.xsl");
  442.     }
  443.    
  444.     private static void prettyPrintWithTrAX_engine(Node doc,OutputStream os,ErrorListener errorListener,boolean omitXMLDeclaration, String charset)throws TransformerException,IOException{
  445.         // Pretty-prints a DOM document to XML using TrAX.
  446.         // Note that a stylesheet is needed to make formatting reliable.
  447.         InputStream is = null;
  448.         try{
  449.             is = PrettyPrintXMLUtils.readPrettyPrintXslt();
  450.             Source source = new DOMSource(doc);
  451.             StreamResult result = new StreamResult(os);
  452.             Transformer transformer = null;
  453.             try{
  454.                 transformer = PrettyPrintXMLUtils.xmlUtils.getTransformerFactory().newTransformer(new StreamSource(is));
  455.             }catch(Exception e){
  456.                 throw new TransformerException(e.getMessage(),e);
  457.             }
  458.             if(omitXMLDeclaration)
  459.                 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,"yes");
  460.             if(charset!=null) {
  461.                 transformer.setOutputProperty(OutputKeys.ENCODING, charset);
  462.             }
  463.             transformer.setErrorListener(errorListener);
  464.             transformer.transform(source, result);
  465.             os.flush();
  466.         }finally{
  467.             try{
  468.                 if(is!=null){
  469.                     is.close();
  470.                 }
  471.             }catch(Exception eClose){
  472.                 // close
  473.             }
  474.         }
  475.     }
  476.     private static void prettyPrintWithTrAX_engine(Node doc,Writer writer,ErrorListener errorListener,boolean omitXMLDeclaration, String charset)throws TransformerException,IOException{
  477.         // Pretty-prints a DOM document to XML using TrAX.
  478.         // Note that a stylesheet is needed to make formatting reliable.
  479.         InputStream is = null;
  480.         try{
  481.             is = PrettyPrintXMLUtils.readPrettyPrintXslt();
  482.             Source source = new DOMSource(doc);
  483.             StreamResult result = new StreamResult(writer);
  484.             Transformer transformer = null;
  485.             try{
  486.                 transformer = PrettyPrintXMLUtils.xmlUtils.getTransformerFactory().newTransformer(new StreamSource(is));
  487.             }catch(Exception e){
  488.                 throw new TransformerException(e.getMessage(),e);
  489.             }
  490.             if(omitXMLDeclaration)
  491.                 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,"yes");
  492.             if(charset!=null) {
  493.                 transformer.setOutputProperty(OutputKeys.ENCODING, charset);
  494.             }
  495.             transformer.setErrorListener(errorListener);
  496.             transformer.transform(source, result);
  497.             writer.flush();
  498.         }finally{
  499.             try{
  500.                 if(is!=null){
  501.                     is.close();
  502.                 }
  503.             }catch(Exception eClose){
  504.                 // close
  505.             }
  506.         }
  507.     }
  508.     private static void prettyPrintWithTrAX_engine(Node doc,File file,ErrorListener errorListener,boolean omitXMLDeclaration, String charset)throws TransformerException,IOException{
  509.         // Pretty-prints a DOM document to XML using TrAX.
  510.         // Note that a stylesheet is needed to make formatting reliable.
  511.         InputStream is = null;
  512.         try{
  513.             is = PrettyPrintXMLUtils.readPrettyPrintXslt();
  514.             Source source = new DOMSource(doc);
  515.             StreamResult result = new StreamResult(file);
  516.             Transformer transformer = null;
  517.             try{
  518.                 transformer = PrettyPrintXMLUtils.xmlUtils.getTransformerFactory().newTransformer(new StreamSource(is));
  519.             }catch(Exception e){
  520.                 throw new TransformerException(e.getMessage(),e);
  521.             }
  522.             if(omitXMLDeclaration)
  523.                 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,"yes");
  524.             if(charset!=null) {
  525.                 transformer.setOutputProperty(OutputKeys.ENCODING, charset);
  526.             }
  527.             transformer.setErrorListener(errorListener);
  528.             transformer.transform(source, result);
  529.         }finally{
  530.             try{
  531.                 if(is!=null){
  532.                     is.close();
  533.                 }
  534.             }catch(Exception eClose){
  535.                 // close
  536.             }
  537.         }
  538.     }
  539.        
  540.    

  541.    
  542.    
  543.    
  544.     // *********** prettyPrintWithDOM3LS **************
  545.    
  546.     /*
  547.      * DOM Level 3 Load and Save (LS) is the new API for pretty-printing XML.  
  548.      * Support for LS is included with JAXP 1.4 which is bundled as part of Sun’s JDK 1.5,
  549.      * but unfortunately pretty-print formatting only works in JDK 1.6 or better.
  550.      */
  551.    
  552.     public static void prettyPrintWithDOM3LS(Document document,OutputStream out){
  553.         DOM3LS_XMLUtils.serialize(document, out);
  554.     }
  555.    
  556.     public static void prettyPrintWithDOM3LS(Document document,Writer writer){
  557.         DOM3LS_XMLUtils.serialize(document, writer);
  558.     }
  559.    
  560.     public static void prettyPrintWithDOM3LS(Document document,File file) throws FileNotFoundException{
  561.         DOM3LS_XMLUtils.serialize(document, file);
  562.     }
  563.    
  564.     public static String prettyPrintWithDOM3LS(Document document){
  565.         return DOM3LS_XMLUtils.toString(document);
  566.     }
  567.    
  568. }