DOM3LS_XMLUtils.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.OutputStream;
  26. import java.io.StringWriter;
  27. import java.io.Writer;

  28. import org.w3c.dom.DOMConfiguration;
  29. import org.w3c.dom.DOMImplementation;
  30. import org.w3c.dom.Document;
  31. import org.w3c.dom.Element;
  32. import org.w3c.dom.Node;
  33. import org.w3c.dom.ls.DOMImplementationLS;
  34. import org.w3c.dom.ls.LSOutput;
  35. import org.w3c.dom.ls.LSSerializer;


  36. /**
  37.  * Classe utilizzabile per ottenere documenti xml
  38.  *
  39.  *
  40.  * @author Poli Andrea (apoli@link.it)
  41.  * @author $Author$
  42.  * @version $Rev$, $Date$
  43.  */

  44. public class DOM3LS_XMLUtils {

  45.     public static AbstractXMLUtils xmlUtils = XMLUtils.getInstance();
  46.     public static synchronized void setXMLUtils(AbstractXMLUtils xmlUtilsParam) {
  47.         DOM3LS_XMLUtils.xmlUtils = xmlUtilsParam;
  48.     }

  49.    
  50.    
  51.     // *********** prettyPrintWithDOM3LS **************
  52.    
  53.     /*
  54.      * DOM Level 3 Load and Save (LS) is the new API for pretty-printing XML.  
  55.      * Support for LS is included with JAXP 1.4 which is bundled as part of Sun’s JDK 1.5,
  56.      * but unfortunately pretty-print formatting only works in JDK 1.6 or better.
  57.      */
  58.    
  59.     private static void _serialize(Document documentImplementation,Node n, Object out, boolean prettyPrint) {
  60.         // Pretty-prints a DOM document to XML using DOM Load and Save's LSSerializer.
  61.         // Note that the "format-pretty-print" DOM configuration parameter can only be set in JDK 1.6+.
  62.         DOMImplementation domImplementation = documentImplementation.getImplementation();
  63.         if (domImplementation.hasFeature("LS", "3.0") && domImplementation.hasFeature("Core", "2.0")) {
  64.             DOMImplementationLS domImplementationLS = (DOMImplementationLS) domImplementation.getFeature("LS", "3.0");
  65.             LSSerializer lsSerializer = domImplementationLS.createLSSerializer();
  66.             DOMConfiguration domConfiguration = lsSerializer.getDomConfig();
  67.             if(prettyPrint) {
  68.                 if (domConfiguration.canSetParameter("format-pretty-print", Boolean.TRUE)) {
  69.                     lsSerializer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE);
  70.                 } else {
  71.                     throw new RuntimeException("DOMConfiguration 'format-pretty-print' parameter isn't settable.");
  72.                 }
  73.             }
  74.             LSOutput lsOutput = domImplementationLS.createLSOutput();
  75.             lsOutput.setEncoding("UTF-8");
  76.             if(out instanceof OutputStream) {
  77.                 lsOutput.setByteStream((OutputStream)out);
  78.             }
  79.             else if(out instanceof Writer) {
  80.                 lsOutput.setCharacterStream((Writer) out);
  81.             }
  82.             lsSerializer.write(n, lsOutput);
  83.         } else {
  84.             throw new RuntimeException("DOM 3.0 LS and/or DOM 2.0 Core not supported.");
  85.         }
  86.     }
  87.    
  88.    
  89.     // OUTPUT STREAM
  90.    
  91.     public static void serialize(Document document,OutputStream out, boolean prettyPrint){
  92.         DOM3LS_XMLUtils.serialize(document, document, out, prettyPrint);
  93.     }
  94.     public static void serialize(Document document,OutputStream out){
  95.         DOM3LS_XMLUtils.serialize(document, document, out, false);
  96.     }
  97.     public static void serialize(Element element,OutputStream out, boolean prettyPrint){
  98.         DOM3LS_XMLUtils.serialize(element.getOwnerDocument(), element, out, prettyPrint);
  99.     }
  100.     public static void serialize(Element element,OutputStream out){
  101.         DOM3LS_XMLUtils.serialize(element.getOwnerDocument(), element, out, false);
  102.     }
  103.     public static void serialize(Node node,OutputStream out, boolean prettyPrint){
  104.         DOM3LS_XMLUtils.serialize(node.getOwnerDocument(), node, out, prettyPrint);
  105.     }
  106.     public static void serialize(Node node,OutputStream out){
  107.         DOM3LS_XMLUtils.serialize(node.getOwnerDocument(), node, out, false);
  108.     }
  109.     public static void serialize(Document documentImplementation,Node n, OutputStream out, boolean prettyPrint){
  110.         DOM3LS_XMLUtils._serialize(documentImplementation, n, out, prettyPrint);
  111.     }
  112.    
  113.    
  114.     // WRITER
  115.    
  116.     public static void serialize(Document document,Writer writer, boolean prettyPrint){
  117.         DOM3LS_XMLUtils.serialize(document, document, writer, prettyPrint);
  118.     }
  119.     public static void serialize(Document document,Writer writer){
  120.         DOM3LS_XMLUtils.serialize(document, document, writer, false);
  121.     }
  122.     public static void serialize(Element element,Writer writer, boolean prettyPrint){
  123.         DOM3LS_XMLUtils.serialize(element.getOwnerDocument(), element, writer, prettyPrint);
  124.     }
  125.     public static void serialize(Element element,Writer writer){
  126.         DOM3LS_XMLUtils.serialize(element.getOwnerDocument(), element, writer, false);
  127.     }
  128.     public static void serialize(Node node,Writer writer, boolean prettyPrint){
  129.         DOM3LS_XMLUtils.serialize(node.getOwnerDocument(), node, writer, prettyPrint);
  130.     }
  131.     public static void serialize(Node node,Writer writer){
  132.         DOM3LS_XMLUtils.serialize(node.getOwnerDocument(), node, writer, false);
  133.     }
  134.     public static void serialize(Document documentImplementation,Node n, Writer writer, boolean prettyPrint){
  135.         DOM3LS_XMLUtils._serialize(documentImplementation, n, writer, prettyPrint);
  136.     }
  137.    
  138.    
  139.     // FILE
  140.    
  141.     public static void serialize(Document document,File file, boolean prettyPrint) throws FileNotFoundException{
  142.         DOM3LS_XMLUtils.serialize(document, document, file, prettyPrint);
  143.     }
  144.     public static void serialize(Document document,File file) throws FileNotFoundException{
  145.         DOM3LS_XMLUtils.serialize(document, document, file, false);
  146.     }
  147.     public static void serialize(Element element,File file, boolean prettyPrint) throws FileNotFoundException{
  148.         DOM3LS_XMLUtils.serialize(element.getOwnerDocument(), element, file, prettyPrint);
  149.     }
  150.     public static void serialize(Element element,File file) throws FileNotFoundException{
  151.         DOM3LS_XMLUtils.serialize(element.getOwnerDocument(), element, file, false);
  152.     }
  153.     public static void serialize(Node node,File file, boolean prettyPrint) throws FileNotFoundException{
  154.         DOM3LS_XMLUtils.serialize(node.getOwnerDocument(), node, file, prettyPrint);
  155.     }
  156.     public static void serialize(Node node,File file) throws FileNotFoundException{
  157.         DOM3LS_XMLUtils.serialize(node.getOwnerDocument(), node, file, false);
  158.     }
  159.     public static void serialize(Document documentImplementation,Node n, File file, boolean prettyPrint) throws FileNotFoundException{
  160.         DOM3LS_XMLUtils._serialize(documentImplementation, n, file, prettyPrint);
  161.     }
  162.    
  163.     public static void _serialize(Document documentImplementation,Node n, File file, boolean prettyPrint) throws FileNotFoundException{
  164.         FileOutputStream fout = null;
  165.         try{
  166.             fout = new FileOutputStream(file);
  167.             DOM3LS_XMLUtils._serialize(documentImplementation,n,fout,prettyPrint);
  168.         }finally{
  169.             try{
  170.                 if(fout!=null) {
  171.                     fout.flush();
  172.                 }
  173.             }catch(Exception eClose){
  174.                 // close
  175.             }
  176.             try{
  177.                 if(fout!=null) {
  178.                     fout.close();
  179.                 }
  180.             }catch(Exception eClose){
  181.                 // close
  182.             }
  183.         }
  184.     }
  185.    
  186.    
  187.     // TO STRING
  188.    
  189.     public static String toString(Document document, boolean prettyPrint){
  190.         return DOM3LS_XMLUtils.toString(document, document, prettyPrint);
  191.     }
  192.     public static String toString(Document document){
  193.         return DOM3LS_XMLUtils.toString(document, document, false);
  194.     }
  195.     public static String toString(Element element, boolean prettyPrint){
  196.         return DOM3LS_XMLUtils.toString(element.getOwnerDocument(), element, prettyPrint);
  197.     }
  198.     public static String toString(Element element){
  199.         return DOM3LS_XMLUtils.toString(element.getOwnerDocument(), element, false);
  200.     }
  201.     public static String toString(Node node, boolean prettyPrint){
  202.         return DOM3LS_XMLUtils.toString(node.getOwnerDocument(), node, prettyPrint);
  203.     }
  204.     public static String toString(Node node){
  205.         return DOM3LS_XMLUtils.toString(node.getOwnerDocument(), node, false);
  206.     }
  207.     public static String toString(Document documentImplementation,Node n, boolean prettyPrint){
  208.         return DOM3LS_XMLUtils._toString(documentImplementation, n, prettyPrint);
  209.     }
  210.    
  211.     public static String _toString(Document documentImplementation,Node n, boolean prettyPrint){
  212.         StringWriter stringWriter = new StringWriter();
  213.         DOM3LS_XMLUtils._serialize(documentImplementation,n,stringWriter,prettyPrint);
  214.         return stringWriter.toString();
  215.     }
  216.    
  217.    
  218.    
  219.     // TO BYTE ARRAY
  220.    
  221.     public static byte[] toByteArray(Document document, boolean prettyPrint){
  222.         return DOM3LS_XMLUtils.toByteArray(document, document, prettyPrint);
  223.     }
  224.     public static byte[] toByteArray(Document document){
  225.         return DOM3LS_XMLUtils.toByteArray(document, document, false);
  226.     }
  227.     public static byte[] toByteArray(Element element, boolean prettyPrint){
  228.         return DOM3LS_XMLUtils.toByteArray(element.getOwnerDocument(), element, prettyPrint);
  229.     }
  230.     public static byte[] toByteArray(Element element){
  231.         return DOM3LS_XMLUtils.toByteArray(element.getOwnerDocument(), element, false);
  232.     }
  233.     public static byte[] toByteArray(Node node, boolean prettyPrint){
  234.         return DOM3LS_XMLUtils.toByteArray(node.getOwnerDocument(), node, prettyPrint);
  235.     }
  236.     public static byte[] toByteArray(Node node){
  237.         return DOM3LS_XMLUtils.toByteArray(node.getOwnerDocument(), node, false);
  238.     }
  239.     public static byte[] toByteArray(Document documentImplementation,Node n, boolean prettyPrint){
  240.         return DOM3LS_XMLUtils._toByteArray(documentImplementation, n, prettyPrint);
  241.     }
  242.    
  243.     public static byte[] _toByteArray(Document documentImplementation,Node n, boolean prettyPrint) {
  244.         ByteArrayOutputStream bout = null;
  245.         try{
  246.             bout = new ByteArrayOutputStream();
  247.             DOM3LS_XMLUtils._serialize(documentImplementation,n,bout,prettyPrint);
  248.         }finally{
  249.             try{
  250.                 bout.flush();
  251.             }catch(Exception eClose){}
  252.             try{
  253.                 bout.close();
  254.             }catch(Exception eClose){}
  255.         }
  256.         return bout.toByteArray();
  257.     }
  258. }