AbstractXMLUtils.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.ByteArrayInputStream;
  22. import java.io.ByteArrayOutputStream;
  23. import java.io.File;
  24. import java.io.IOException;
  25. import java.io.InputStream;
  26. import java.io.OutputStream;
  27. import java.io.Writer;
  28. import java.util.ArrayList;
  29. import java.util.Calendar;
  30. import java.util.Date;
  31. import java.util.GregorianCalendar;
  32. import java.util.HashMap;
  33. import java.util.List;
  34. import java.util.Map;

  35. import javax.xml.datatype.DatatypeFactory;
  36. import javax.xml.datatype.XMLGregorianCalendar;
  37. import javax.xml.parsers.DocumentBuilder;
  38. import javax.xml.parsers.DocumentBuilderFactory;
  39. import javax.xml.parsers.ParserConfigurationException;
  40. import javax.xml.transform.ErrorListener;
  41. import javax.xml.transform.OutputKeys;
  42. import javax.xml.transform.Source;
  43. import javax.xml.transform.Transformer;
  44. import javax.xml.transform.TransformerException;
  45. import javax.xml.transform.TransformerFactory;
  46. import javax.xml.transform.stream.StreamResult;
  47. import javax.xml.validation.SchemaFactory;
  48. import javax.xml.xpath.XPathFactory;

  49. import org.openspcoop2.utils.UtilsException;
  50. import org.w3c.dom.Attr;
  51. import org.w3c.dom.Comment;
  52. import org.w3c.dom.Document;
  53. import org.w3c.dom.Element;
  54. import org.w3c.dom.NamedNodeMap;
  55. import org.w3c.dom.Node;
  56. import org.w3c.dom.NodeList;
  57. import org.w3c.dom.Text;
  58. import org.xml.sax.EntityResolver;
  59. import org.xml.sax.ErrorHandler;
  60. import org.xml.sax.InputSource;
  61. import org.xml.sax.SAXException;


  62. /**
  63.  * Classe utilizzabile per ottenere documenti xml
  64.  *
  65.  *
  66.  * @author Poli Andrea (apoli@link.it)
  67.  * @author $Author$
  68.  * @version $Rev$, $Date$
  69.  */

  70. public abstract class AbstractXMLUtils {

  71.     // XERCES
  72.     protected abstract DocumentBuilderFactory newDocumentBuilderFactory() throws XMLException;
  73.     protected abstract DatatypeFactory newDatatypeFactory() throws XMLException;
  74.     protected abstract javax.xml.parsers.SAXParserFactory newSAXParserFactory() throws XMLException;
  75. //  protected abstract javax.xml.stream.XMLEventFactory newXMLEventFactory() throws XMLException;
  76.     protected abstract SchemaFactory newSchemaFactory() throws XMLException;
  77.    
  78.     // XALAN
  79.     protected abstract TransformerFactory newTransformerFactory() throws XMLException;
  80.     protected abstract XPathFactory newXPathFactory() throws XMLException;
  81.    
  82.     // XERCES
  83.     private DocumentBuilderFactory documentFactory = null;
  84.     private DatatypeFactory datatypeFactory = null;
  85.     private javax.xml.parsers.SAXParserFactory saxParserFactory = null;
  86. //  private javax.xml.stream.XMLEventFactory xmlEventFactory = null;
  87.     private SchemaFactory schemaFactory = null;
  88.    
  89.     // XALAN    
  90.     private TransformerFactory transformerFactory = null;
  91.     private XPathFactory xpathFactory = null;
  92.    
  93.     // OTHER
  94.     private GregorianCalendar gregorianCalendar = null;
  95.    
  96.    
  97.     // The safest way to prevent XXE is always to disable DTDs (External Entities) completely
  98.     public static boolean DISABLE_DTDs = true;
  99.     protected String getFeatures_disallow_doctype_decl() {
  100.         return "http://apache.org/xml/features/disallow-doctype-decl";
  101.     }
  102.     public boolean isDisabledDTDs() {
  103.         return DISABLE_DTDs;
  104.     }
  105.    
  106.     // INIT - XERCES
  107.    
  108.     public synchronized void initDocumentBuilderFactory() throws XMLException {
  109.         if(this.documentFactory==null){
  110.             try {
  111.                 this.documentFactory = newDocumentBuilderFactory();
  112.                 if(DISABLE_DTDs) {
  113.                     this.documentFactory.setFeature(getFeatures_disallow_doctype_decl(), true);
  114.                 }
  115.             } catch (Exception e) {
  116.                 throw new XMLException(e.getMessage(),e);
  117.             }
  118.             this.documentFactory.setNamespaceAware(true);
  119.         }
  120.     }
  121.    
  122.     public synchronized void initDatatypeFactory() throws XMLException {
  123.         if(this.datatypeFactory==null){
  124.             try {
  125.                 this.datatypeFactory = newDatatypeFactory();
  126.             } catch (Exception e) {
  127.                 throw new XMLException(e.getMessage(),e);
  128.             }
  129.         }
  130.     }
  131.    
  132.     public synchronized void initSAXParserFactory() throws XMLException {
  133.         if(this.saxParserFactory==null){
  134.             try {
  135.                 this.saxParserFactory = newSAXParserFactory();
  136.                 if(DISABLE_DTDs) {
  137.                     this.saxParserFactory.setFeature(getFeatures_disallow_doctype_decl(), true);
  138.                 }
  139.             } catch (Exception e) {
  140.                 throw new XMLException(e.getMessage(),e);
  141.             }
  142.         }
  143.     }
  144. //  
  145. //  public synchronized void initXMLEventFactory() throws XMLException {
  146. //      if(this.xmlEventFactory==null){
  147. //          try {
  148. //              this.xmlEventFactory = newXMLEventFactory();
  149. //          } catch (Exception e) {
  150. //              throw new XMLException(e.getMessage(),e);
  151. //          }
  152. //      }
  153. //  }
  154.    
  155.     public synchronized void initSchemaFactory() throws XMLException {
  156.         if(this.schemaFactory==null){
  157.             try {
  158.                 this.schemaFactory = newSchemaFactory();
  159.                 if(DISABLE_DTDs) {
  160.                     this.schemaFactory.setFeature(getFeatures_disallow_doctype_decl(), true);
  161.                 }
  162.             } catch (Exception e) {
  163.                 throw new XMLException(e.getMessage(),e);
  164.             }
  165.         }
  166.     }

  167.     // INIT - XALAN
  168.    
  169.     public synchronized void initTransformerFactory() throws XMLException {
  170.         if(this.transformerFactory==null){
  171.             this.transformerFactory = newTransformerFactory();
  172.             /*if(DISABLE_DTDs) {
  173.                 this.transformerFactory.setAttribute(javax.xml.XMLConstants.ACCESS_EXTERNAL_DTD, "");
  174.                 this.transformerFactory.setAttribute(javax.xml.XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "");
  175.             }*/
  176.         }
  177.     }
  178.    
  179.     public synchronized void initXPathFactory() throws XMLException {
  180.         if(this.xpathFactory==null){
  181.             this.xpathFactory = newXPathFactory();
  182.         }
  183.     }
  184.    
  185.     // INIT - OTHER
  186.    
  187.     public synchronized void initCalendarConverter() throws XMLException {
  188.         try{
  189.             this.initDatatypeFactory();
  190.             this.gregorianCalendar = (GregorianCalendar) Calendar.getInstance();
  191.         } catch (Exception e) {
  192.             throw new XMLException(e.getMessage(),e);
  193.         }
  194.        
  195.     }
  196.    
  197.    
  198.     // GET - XERCES
  199.    
  200.     public DocumentBuilderFactory getDocumentBuilderFactory() throws XMLException {
  201.         if(this.documentFactory==null){
  202.             this.initDocumentBuilderFactory();
  203.         }
  204.         return this.documentFactory;
  205.     }
  206.    
  207.     public DatatypeFactory getDatatypeFactory() throws XMLException {
  208.         if(this.datatypeFactory==null){
  209.             this.initDatatypeFactory();
  210.         }
  211.         return this.datatypeFactory;
  212.     }
  213.    
  214.     public javax.xml.parsers.SAXParserFactory getSAXParserFactory() throws XMLException {
  215.         if(this.saxParserFactory==null){
  216.             this.initSAXParserFactory();
  217.         }
  218.         return this.saxParserFactory;
  219.     }
  220. //  
  221. //  public javax.xml.stream.XMLEventFactory getXMLEventFactory() throws XMLException {
  222. //      if(this.xmlEventFactory==null){
  223. //          this.initXMLEventFactory();
  224. //      }
  225. //      return this.xmlEventFactory;
  226. //  }
  227.    
  228.     public SchemaFactory getSchemaFactory() throws XMLException {
  229.         if(this.schemaFactory==null){
  230.             this.initSchemaFactory();
  231.         }
  232.         return this.schemaFactory;
  233.     }
  234.    
  235.    
  236.     // GET - XALAN

  237.     public TransformerFactory getTransformerFactory() throws XMLException {
  238.         if(this.transformerFactory==null){
  239.             this.initTransformerFactory();
  240.         }
  241.         return this.transformerFactory;
  242.     }
  243.    
  244.     public XPathFactory getXPathFactory() throws XMLException {
  245.         if(this.xpathFactory==null){
  246.             this.initXPathFactory();
  247.         }
  248.         return this.xpathFactory;
  249.     }

  250.     // GET - OTHER
  251.    
  252.     public XMLGregorianCalendar toGregorianCalendar(Date d) throws XMLException {
  253.         if(this.datatypeFactory==null || this.gregorianCalendar==null){
  254.             this.initCalendarConverter();
  255.         }
  256.         this.gregorianCalendar.setTime(d);
  257.         return this.datatypeFactory.newXMLGregorianCalendar(this.gregorianCalendar);
  258.     }



  259.     // GET AS
  260.    
  261.     public Document getAsDocument(Element element) throws IOException, SAXException, ParserConfigurationException, TransformerException, XMLException{
  262.         return this.newDocument(this.toByteArray(element));
  263.     }
  264.     public Document getAsDocument(Element element,ErrorHandler errorHandler) throws IOException, SAXException, ParserConfigurationException, TransformerException, XMLException{
  265.         return this.newDocument(this.toByteArray(element),errorHandler);
  266.     }
  267.     public Document getAsDocument(Element element,EntityResolver entityResolver) throws IOException, SAXException, ParserConfigurationException, TransformerException, XMLException{
  268.         return this.newDocument(this.toByteArray(element),entityResolver);
  269.     }
  270.     public Document getAsDocument(Element element,ErrorHandler errorHandler,EntityResolver entityResolver) throws IOException, SAXException, ParserConfigurationException, TransformerException, XMLException{
  271.         return this.newDocument(this.toByteArray(element),errorHandler,entityResolver);
  272.     }
  273.    
  274.     public Document getAsDocument(Node Node) throws IOException, SAXException, ParserConfigurationException, TransformerException, XMLException{
  275.         return this.newDocument(this.toByteArray(Node));
  276.     }
  277.     public Document getAsDocument(Node Node,ErrorHandler errorHandler) throws IOException, SAXException, ParserConfigurationException, TransformerException, XMLException{
  278.         return this.newDocument(this.toByteArray(Node),errorHandler);
  279.     }
  280.     public Document getAsDocument(Node Node,EntityResolver entityResolver) throws IOException, SAXException, ParserConfigurationException, TransformerException, XMLException{
  281.         return this.newDocument(this.toByteArray(Node),entityResolver);
  282.     }
  283.     public Document getAsDocument(Node Node,ErrorHandler errorHandler,EntityResolver entityResolver) throws IOException, SAXException, ParserConfigurationException, TransformerException, XMLException{
  284.         return this.newDocument(this.toByteArray(Node),errorHandler,entityResolver);
  285.     }
  286.    
  287.    
  288.    

  289.     // NEW DOCUMENT

  290.     public Document newDocument() throws IOException,SAXException,ParserConfigurationException,XMLException{
  291.         return this.newDocument_engine(new XMLErrorHandler(),null);
  292.     }
  293.     public Document newDocument(ErrorHandler errorHandler) throws IOException,SAXException,ParserConfigurationException,XMLException{
  294.         return this.newDocument_engine(errorHandler,null);
  295.     }
  296.     public Document newDocument(EntityResolver entityResolver) throws IOException,SAXException,ParserConfigurationException,XMLException{
  297.         return this.newDocument_engine(new XMLErrorHandler(),entityResolver);
  298.     }
  299.     public Document newDocument(ErrorHandler errorHandler,EntityResolver entityResolver) throws IOException,SAXException,ParserConfigurationException,XMLException{
  300.         return this.newDocument_engine(errorHandler,entityResolver);
  301.     }
  302.     private Document newDocument_engine(ErrorHandler errorHandler,EntityResolver entityResolver) throws IOException,SAXException,ParserConfigurationException,XMLException{
  303.         DocumentBuilder documentBuilder = this.getDocumentBuilderFactory().newDocumentBuilder();
  304.         documentBuilder.setErrorHandler(errorHandler);
  305.         if(entityResolver!=null){
  306.             documentBuilder.setEntityResolver(entityResolver);
  307.         }
  308.         return documentBuilder.newDocument();
  309.     }
  310.    
  311.     public Document newDocument(byte[] xml) throws IOException,SAXException,ParserConfigurationException,XMLException{
  312.         return this.newDocument(xml,new XMLErrorHandler(),null);
  313.     }
  314.     public Document newDocument(byte[] xml,ErrorHandler errorHandler) throws IOException,SAXException,ParserConfigurationException,XMLException{
  315.         return this.newDocument(xml,errorHandler,null);
  316.     }
  317.     public Document newDocument(byte[] xml,EntityResolver entityResolver) throws IOException,SAXException,ParserConfigurationException,XMLException{
  318.         return this.newDocument(xml,new XMLErrorHandler(),entityResolver);
  319.     }
  320.     public Document newDocument(byte[] xml,ErrorHandler errorHandler,EntityResolver entityResolver) throws IOException,SAXException,ParserConfigurationException,XMLException{
  321.         ByteArrayInputStream bin = null;
  322.         try{
  323.             bin = new ByteArrayInputStream(xml);
  324.             return this.newDocument(bin,errorHandler,entityResolver);
  325.         }finally{
  326.             if(bin!=null){
  327.                 try{
  328.                     bin.close();
  329.                 }catch(Exception eClose){}
  330.             }
  331.         }
  332.     }

  333.     public Document newDocument(InputStream is) throws IOException,SAXException,ParserConfigurationException, XMLException{
  334.         return this.newDocument(is,new XMLErrorHandler(),null);
  335.     }
  336.     public Document newDocument(InputStream is,ErrorHandler errorHandler) throws IOException,SAXException,ParserConfigurationException, XMLException{
  337.         return this.newDocument(is,errorHandler,null);
  338.     }
  339.     public Document newDocument(InputStream is,EntityResolver entityResolver) throws IOException,SAXException,ParserConfigurationException, XMLException{
  340.         return this.newDocument(is,new XMLErrorHandler(),entityResolver);
  341.     }
  342.     public Document newDocument(InputStream is,ErrorHandler errorHandler,EntityResolver entityResolver) throws IOException,SAXException,ParserConfigurationException, XMLException{
  343.         DocumentBuilder documentBuilder = this.getDocumentBuilderFactory().newDocumentBuilder();
  344.         documentBuilder.setErrorHandler(errorHandler);
  345.         if(entityResolver!=null)
  346.             documentBuilder.setEntityResolver(entityResolver);
  347.         return documentBuilder.parse(is);
  348.     }

  349.     public Document newDocument(File f) throws IOException,SAXException,ParserConfigurationException, XMLException{
  350.         return this.newDocument(f,new XMLErrorHandler(),null);
  351.     }
  352.     public Document newDocument(File f,ErrorHandler errorHandler) throws IOException,SAXException,ParserConfigurationException, XMLException{
  353.         return this.newDocument(f,errorHandler,null);
  354.     }
  355.     public Document newDocument(File f,EntityResolver entityResolver) throws IOException,SAXException,ParserConfigurationException, XMLException{
  356.         return this.newDocument(f,new XMLErrorHandler(),entityResolver);
  357.     }
  358.     public Document newDocument(File f,ErrorHandler errorHandler,EntityResolver entityResolver) throws IOException,SAXException,ParserConfigurationException, XMLException{
  359.         DocumentBuilder documentBuilder = this.getDocumentBuilderFactory().newDocumentBuilder();
  360.         documentBuilder.setErrorHandler(errorHandler);
  361.         if(entityResolver!=null){
  362.             documentBuilder.setEntityResolver(entityResolver);
  363.         }
  364.         return documentBuilder.parse(f);
  365.     }

  366.     public Document newDocument(InputSource is) throws IOException,SAXException,ParserConfigurationException, XMLException{
  367.         return this.newDocument(is,new XMLErrorHandler(),null);
  368.     }
  369.     public Document newDocument(InputSource is,ErrorHandler errorHandler) throws IOException,SAXException,ParserConfigurationException, XMLException{
  370.         return this.newDocument(is,errorHandler,null);
  371.     }
  372.     public Document newDocument(InputSource is,EntityResolver entityResolver) throws IOException,SAXException,ParserConfigurationException, XMLException{
  373.         return this.newDocument(is,new XMLErrorHandler(),entityResolver);
  374.     }
  375.     public Document newDocument(InputSource is,ErrorHandler errorHandler,EntityResolver entityResolver) throws IOException,SAXException,ParserConfigurationException, XMLException{
  376.         DocumentBuilder documentBuilder = this.getDocumentBuilderFactory().newDocumentBuilder();
  377.         documentBuilder.setErrorHandler(errorHandler);
  378.         if(entityResolver!=null){
  379.             documentBuilder.setEntityResolver(entityResolver);
  380.         }
  381.         return documentBuilder.parse(is);
  382.     }



  383.     // NEW ELEMENT

  384.     public Element newElement() throws IOException,SAXException,ParserConfigurationException, XMLException{
  385.         return this.newElement_engine(new XMLErrorHandler(),null);
  386.     }
  387.     public Element newElement(ErrorHandler errorHandler) throws IOException,SAXException,ParserConfigurationException, XMLException{
  388.         return this.newElement_engine(errorHandler,null);
  389.     }
  390.     public Element newElement(EntityResolver entityResolver) throws IOException,SAXException,ParserConfigurationException, XMLException{
  391.         return this.newElement_engine(new XMLErrorHandler(),entityResolver);
  392.     }
  393.     public Element newElement(ErrorHandler errorHandler,EntityResolver entityResolver) throws IOException,SAXException,ParserConfigurationException, XMLException{
  394.         return this.newElement_engine(errorHandler,entityResolver);
  395.     }
  396.     private Element newElement_engine(ErrorHandler errorHandler,EntityResolver entityResolver) throws IOException,SAXException,ParserConfigurationException, XMLException{
  397.         DocumentBuilder documentBuilder = this.getDocumentBuilderFactory().newDocumentBuilder();
  398.         documentBuilder.setErrorHandler(errorHandler);
  399.         if(entityResolver!=null){
  400.             documentBuilder.setEntityResolver(entityResolver);
  401.         }
  402.         return documentBuilder.newDocument().getDocumentElement();
  403.     }
  404.    
  405.     public Element newElement(byte[] xml) throws IOException,SAXException,ParserConfigurationException, XMLException{
  406.         return this.newDocument(xml).getDocumentElement();
  407.     }
  408.     public Element newElement(byte[] xml,ErrorHandler errorHandler) throws IOException,SAXException,ParserConfigurationException, XMLException{
  409.         return this.newDocument(xml,errorHandler).getDocumentElement();
  410.     }
  411.     public Element newElement(byte[] xml,EntityResolver entityResolver) throws IOException,SAXException,ParserConfigurationException, XMLException{
  412.         return this.newDocument(xml,entityResolver).getDocumentElement();
  413.     }
  414.     public Element newElement(byte[] xml,ErrorHandler errorHandler,EntityResolver entityResolver) throws IOException,SAXException,ParserConfigurationException, XMLException{
  415.         return this.newDocument(xml,errorHandler,entityResolver).getDocumentElement();
  416.     }

  417.     public Element newElement(InputStream is) throws IOException,SAXException,ParserConfigurationException, XMLException{
  418.         return this.newDocument(is).getDocumentElement();
  419.     }
  420.     public Element newElement(InputStream is,ErrorHandler errorHandler) throws IOException,SAXException,ParserConfigurationException, XMLException{
  421.         return this.newDocument(is,errorHandler).getDocumentElement();
  422.     }
  423.     public Element newElement(InputStream is,EntityResolver entityResolver) throws IOException,SAXException,ParserConfigurationException, XMLException{
  424.         return this.newDocument(is,entityResolver).getDocumentElement();
  425.     }
  426.     public Element newElement(InputStream is,ErrorHandler errorHandler,EntityResolver entityResolver) throws IOException,SAXException,ParserConfigurationException, XMLException{
  427.         return this.newDocument(is,errorHandler,entityResolver).getDocumentElement();
  428.     }

  429.     public Element newElement(File f) throws IOException,SAXException,ParserConfigurationException, XMLException{
  430.         return this.newDocument(f).getDocumentElement();
  431.     }
  432.     public Element newElement(File f,ErrorHandler errorHandler) throws IOException,SAXException,ParserConfigurationException, XMLException{
  433.         return this.newDocument(f,errorHandler).getDocumentElement();
  434.     }
  435.     public Element newElement(File f,EntityResolver entityResolver) throws IOException,SAXException,ParserConfigurationException, XMLException{
  436.         return this.newDocument(f,entityResolver).getDocumentElement();
  437.     }
  438.     public Element newElement(File f,ErrorHandler errorHandler,EntityResolver entityResolver) throws IOException,SAXException,ParserConfigurationException, XMLException{
  439.         return this.newDocument(f,errorHandler,entityResolver).getDocumentElement();
  440.     }

  441.     public Element newElement(InputSource is) throws IOException,SAXException,ParserConfigurationException, XMLException{
  442.         return this.newDocument(is).getDocumentElement();
  443.     }
  444.     public Element newElement(InputSource is,ErrorHandler errorHandler) throws IOException,SAXException,ParserConfigurationException, XMLException{
  445.         return this.newDocument(is,errorHandler).getDocumentElement();
  446.     }
  447.     public Element newElement(InputSource is,EntityResolver entityResolver) throws IOException,SAXException,ParserConfigurationException, XMLException{
  448.         return this.newDocument(is,entityResolver).getDocumentElement();
  449.     }
  450.     public Element newElement(InputSource is,ErrorHandler errorHandler,EntityResolver entityResolver) throws IOException,SAXException,ParserConfigurationException, XMLException{
  451.         return this.newDocument(is,errorHandler,entityResolver).getDocumentElement();
  452.     }



  453.     // TO BYTE ARRAY

  454.     public byte[] toByteArray(Document doc) throws TransformerException,IOException, XMLException{
  455.         ByteArrayOutputStream bout = new ByteArrayOutputStream();
  456.         this.writeTo(doc,bout);
  457.         bout.close();
  458.         return bout.toByteArray();
  459.     }
  460.     public byte[] toByteArray(Document doc,boolean omitXMLDeclaration) throws TransformerException,IOException, XMLException{
  461.         ByteArrayOutputStream bout = new ByteArrayOutputStream();
  462.         this.writeTo(doc,bout,omitXMLDeclaration);
  463.         bout.close();
  464.         return bout.toByteArray();
  465.     }
  466.     public byte[] toByteArray(Document doc,TransformerConfig config) throws TransformerException,IOException, XMLException{
  467.         ByteArrayOutputStream bout = new ByteArrayOutputStream();
  468.         this.writeTo(doc,bout,config);
  469.         bout.close();
  470.         return bout.toByteArray();
  471.     }
  472.    
  473.     public byte[] toByteArray(Element element) throws TransformerException,IOException, XMLException{
  474.         ByteArrayOutputStream bout = new ByteArrayOutputStream();
  475.         this.writeTo(element,bout);
  476.         bout.close();
  477.         return bout.toByteArray();
  478.     }
  479.     public byte[] toByteArray(Element element,boolean omitXMLDeclaration) throws TransformerException,IOException, XMLException{
  480.         ByteArrayOutputStream bout = new ByteArrayOutputStream();
  481.         this.writeTo(element,bout,omitXMLDeclaration);
  482.         bout.close();
  483.         return bout.toByteArray();
  484.     }
  485.     public byte[] toByteArray(Element element,TransformerConfig config) throws TransformerException,IOException, XMLException{
  486.         ByteArrayOutputStream bout = new ByteArrayOutputStream();
  487.         this.writeTo(element,bout,config);
  488.         bout.close();
  489.         return bout.toByteArray();
  490.     }
  491.    
  492.     public byte[] toByteArray(Node node) throws TransformerException,IOException, XMLException{
  493.         ByteArrayOutputStream bout = new ByteArrayOutputStream();
  494.         this.writeTo(node,bout);
  495.         bout.close();
  496.         return bout.toByteArray();
  497.     }
  498.     public byte[] toByteArray(Node node,boolean omitXMLDeclaration) throws TransformerException,IOException, XMLException{
  499.         ByteArrayOutputStream bout = new ByteArrayOutputStream();
  500.         this.writeTo(node,bout,omitXMLDeclaration);
  501.         bout.close();
  502.         return bout.toByteArray();
  503.     }
  504.     public byte[] toByteArray(Node node,TransformerConfig config) throws TransformerException,IOException, XMLException{
  505.         ByteArrayOutputStream bout = new ByteArrayOutputStream();
  506.         this.writeTo(node,bout,config);
  507.         bout.close();
  508.         return bout.toByteArray();
  509.     }


  510.     // TO STRING

  511.     public String toString(Document doc) throws TransformerException,IOException, XMLException{
  512.         ByteArrayOutputStream bout = new ByteArrayOutputStream();
  513.         this.writeTo(doc,bout);
  514.         bout.close();
  515.         return bout.toString();
  516.     }
  517.     public String toString(Document doc,boolean omitXMLDeclaration) throws TransformerException,IOException, XMLException{
  518.         ByteArrayOutputStream bout = new ByteArrayOutputStream();
  519.         this.writeTo(doc,bout,omitXMLDeclaration);
  520.         bout.close();
  521.         return bout.toString();
  522.     }
  523.     public String toString(Document doc,TransformerConfig config) throws TransformerException,IOException, XMLException{
  524.         ByteArrayOutputStream bout = new ByteArrayOutputStream();
  525.         this.writeTo(doc,bout,config);
  526.         bout.close();
  527.         return bout.toString();
  528.     }
  529.    
  530.     public String toString(Element element) throws TransformerException,IOException, XMLException{
  531.         ByteArrayOutputStream bout = new ByteArrayOutputStream();
  532.         this.writeTo(element,bout);
  533.         bout.close();
  534.         return bout.toString();
  535.     }
  536.     public String toString(Element element,boolean omitXMLDeclaration) throws TransformerException,IOException, XMLException{
  537.         ByteArrayOutputStream bout = new ByteArrayOutputStream();
  538.         this.writeTo(element,bout,omitXMLDeclaration);
  539.         bout.close();
  540.         return bout.toString();
  541.     }
  542.     public String toString(Element element,TransformerConfig config) throws TransformerException,IOException, XMLException{
  543.         ByteArrayOutputStream bout = new ByteArrayOutputStream();
  544.         this.writeTo(element,bout,config);
  545.         bout.close();
  546.         return bout.toString();
  547.     }
  548.    
  549.     public String toString(Node node) throws TransformerException,IOException, XMLException{
  550.         ByteArrayOutputStream bout = new ByteArrayOutputStream();
  551.         this.writeTo(node,bout);
  552.         bout.close();
  553.         return bout.toString();
  554.     }
  555.     public String toString(Node node,boolean omitXMLDeclaration) throws TransformerException,IOException, XMLException{
  556.         ByteArrayOutputStream bout = new ByteArrayOutputStream();
  557.         this.writeTo(node,bout,omitXMLDeclaration);
  558.         bout.close();
  559.         return bout.toString();
  560.     }
  561.     public String toString(Node node,TransformerConfig config) throws TransformerException,IOException, XMLException{
  562.         ByteArrayOutputStream bout = new ByteArrayOutputStream();
  563.         this.writeTo(node,bout,config);
  564.         bout.close();
  565.         return bout.toString();
  566.     }


  567.     // WRITE TO

  568.     public void writeTo(Document doc,OutputStream os)throws TransformerException,IOException, XMLException{
  569.         this.writeNodeTo(doc, os, new XMLErrorListener(),false, null, false);
  570.     }
  571.     public void writeTo(Document doc,OutputStream os,ErrorListener errorListener)throws TransformerException,IOException, XMLException{
  572.         this.writeNodeTo(doc, os,errorListener,false, null, false);
  573.     }
  574.     public void writeTo(Document doc,OutputStream os,boolean omitXMLDeclaration)throws TransformerException,IOException, XMLException{
  575.         this.writeNodeTo(doc, os, new XMLErrorListener(),omitXMLDeclaration, null, false);
  576.     }
  577.     public void writeTo(Document doc,OutputStream os,ErrorListener errorListener,boolean omitXMLDeclaration)throws TransformerException,IOException, XMLException{
  578.         this.writeNodeTo(doc, os,errorListener,omitXMLDeclaration, null, false);
  579.     }
  580.     public void writeTo(Document doc,OutputStream os, String charset)throws TransformerException,IOException, XMLException{
  581.         this.writeNodeTo(doc, os, new XMLErrorListener(),false,  charset, false);
  582.     }
  583.     public void writeTo(Document doc,OutputStream os,ErrorListener errorListener, String charset)throws TransformerException,IOException, XMLException{
  584.         this.writeNodeTo(doc, os,errorListener,false,  charset, false);
  585.     }
  586.     public void writeTo(Document doc,OutputStream os,boolean omitXMLDeclaration, String charset)throws TransformerException,IOException, XMLException{
  587.         this.writeNodeTo(doc, os, new XMLErrorListener(),omitXMLDeclaration,  charset, false);
  588.     }
  589.     public void writeTo(Document doc,OutputStream os,ErrorListener errorListener,boolean omitXMLDeclaration, String charset)throws TransformerException,IOException, XMLException{
  590.         this.writeNodeTo(doc, os,errorListener,omitXMLDeclaration,  charset, false);
  591.     }
  592.     public void writeTo(Document doc,OutputStream os,boolean omitXMLDeclaration, String charset, boolean indent)throws TransformerException,IOException, XMLException{
  593.         this.writeNodeTo(doc, os, new XMLErrorListener(),omitXMLDeclaration,  charset, indent);
  594.     }
  595.     public void writeTo(Document doc,OutputStream os,ErrorListener errorListener,boolean omitXMLDeclaration, String charset, boolean indent)throws TransformerException,IOException, XMLException{
  596.         this.writeNodeTo(doc, os,errorListener,omitXMLDeclaration,  charset, indent);
  597.     }

  598.     public void writeTo(Document doc,Writer writer)throws TransformerException,IOException, XMLException{
  599.         this.writeNodeTo(doc, writer, new XMLErrorListener(),false, null, false);
  600.     }
  601.     public void writeTo(Document doc,Writer writer,ErrorListener errorListener)throws TransformerException,IOException, XMLException{
  602.         this.writeNodeTo(doc, writer,errorListener,false, null, false);
  603.     }
  604.     public void writeTo(Document doc,Writer writer,boolean omitXMLDeclaration)throws TransformerException,IOException, XMLException{
  605.         this.writeNodeTo(doc, writer, new XMLErrorListener(), omitXMLDeclaration, null, false);
  606.     }
  607.     public void writeTo(Document doc,Writer writer,ErrorListener errorListener,boolean omitXMLDeclaration)throws TransformerException,IOException, XMLException{
  608.         this.writeNodeTo(doc, writer,errorListener, omitXMLDeclaration, null, false);
  609.     }
  610.     public void writeTo(Document doc,Writer writer, String charset)throws TransformerException,IOException, XMLException{
  611.         this.writeNodeTo(doc, writer, new XMLErrorListener(),false, charset, false);
  612.     }
  613.     public void writeTo(Document doc,Writer writer,ErrorListener errorListener, String charset)throws TransformerException,IOException, XMLException{
  614.         this.writeNodeTo(doc, writer,errorListener,false, charset, false);
  615.     }
  616.     public void writeTo(Document doc,Writer writer,boolean omitXMLDeclaration, String charset)throws TransformerException,IOException, XMLException{
  617.         this.writeNodeTo(doc, writer, new XMLErrorListener(), omitXMLDeclaration, charset, false);
  618.     }
  619.     public void writeTo(Document doc,Writer writer,ErrorListener errorListener,boolean omitXMLDeclaration, String charset)throws TransformerException,IOException, XMLException{
  620.         this.writeNodeTo(doc, writer,errorListener, omitXMLDeclaration, charset, false);
  621.     }
  622.     public void writeTo(Document doc,Writer writer,boolean omitXMLDeclaration, String charset, boolean indent)throws TransformerException,IOException, XMLException{
  623.         this.writeNodeTo(doc, writer, new XMLErrorListener(), omitXMLDeclaration, charset, indent);
  624.     }
  625.     public void writeTo(Document doc,Writer writer,ErrorListener errorListener,boolean omitXMLDeclaration, String charset, boolean indent)throws TransformerException,IOException, XMLException{
  626.         this.writeNodeTo(doc, writer,errorListener, omitXMLDeclaration, charset, indent);
  627.     }

  628.     public void writeTo(Document doc,File file)throws TransformerException,IOException, XMLException{
  629.         this.writeNodeTo(doc, file, new XMLErrorListener(),false, null, false);
  630.     }
  631.     public void writeTo(Document doc,File file,ErrorListener errorListener)throws TransformerException,IOException, XMLException{
  632.         this.writeNodeTo(doc, file,errorListener,false, null, false);
  633.     }
  634.     public void writeTo(Document doc,File file,boolean omitXMLDeclaration)throws TransformerException,IOException, XMLException{
  635.         this.writeNodeTo(doc, file, new XMLErrorListener(),omitXMLDeclaration, null, false);
  636.     }
  637.     public void writeTo(Document doc,File file,ErrorListener errorListener,boolean omitXMLDeclaration)throws TransformerException,IOException, XMLException{
  638.         this.writeNodeTo(doc, file,errorListener,omitXMLDeclaration, null, false);
  639.     }
  640.     public void writeTo(Document doc,File file, String charset)throws TransformerException,IOException, XMLException{
  641.         this.writeNodeTo(doc, file, new XMLErrorListener(),false, charset, false);
  642.     }
  643.     public void writeTo(Document doc,File file,ErrorListener errorListener, String charset)throws TransformerException,IOException, XMLException{
  644.         this.writeNodeTo(doc, file,errorListener,false, charset, false);
  645.     }
  646.     public void writeTo(Document doc,File file,boolean omitXMLDeclaration, String charset)throws TransformerException,IOException, XMLException{
  647.         this.writeNodeTo(doc, file, new XMLErrorListener(),omitXMLDeclaration, charset, false);
  648.     }
  649.     public void writeTo(Document doc,File file,ErrorListener errorListener,boolean omitXMLDeclaration, String charset)throws TransformerException,IOException, XMLException{
  650.         this.writeNodeTo(doc, file,errorListener,omitXMLDeclaration, charset, false);
  651.     }
  652.     public void writeTo(Document doc,File file,boolean omitXMLDeclaration, String charset, boolean indent)throws TransformerException,IOException, XMLException{
  653.         this.writeNodeTo(doc, file, new XMLErrorListener(),omitXMLDeclaration, charset, indent);
  654.     }
  655.     public void writeTo(Document doc,File file,ErrorListener errorListener,boolean omitXMLDeclaration, String charset, boolean indent)throws TransformerException,IOException, XMLException{
  656.         this.writeNodeTo(doc, file,errorListener,omitXMLDeclaration, charset, indent);
  657.     }

  658.     public void writeTo(Element element,OutputStream os)throws TransformerException,IOException, XMLException{
  659.         this.writeNodeTo(element, os, new XMLErrorListener(),false, null, false);
  660.     }
  661.     public void writeTo(Element element,OutputStream os,ErrorListener errorListener)throws TransformerException,IOException, XMLException{
  662.         this.writeNodeTo(element, os,errorListener,false, null, false);
  663.     }
  664.     public void writeTo(Element element,OutputStream os,boolean omitXMLDeclaration)throws TransformerException,IOException, XMLException{
  665.         this.writeNodeTo(element, os, new XMLErrorListener(),omitXMLDeclaration, null, false);
  666.     }
  667.     public void writeTo(Element element,OutputStream os,ErrorListener errorListener,boolean omitXMLDeclaration)throws TransformerException,IOException, XMLException{
  668.         this.writeNodeTo(element, os,errorListener,omitXMLDeclaration, null, false);
  669.     }
  670.     public void writeTo(Element element,OutputStream os, String charset)throws TransformerException,IOException, XMLException{
  671.         this.writeNodeTo(element, os, new XMLErrorListener(),false, charset, false);
  672.     }
  673.     public void writeTo(Element element,OutputStream os,ErrorListener errorListener, String charset)throws TransformerException,IOException, XMLException{
  674.         this.writeNodeTo(element, os,errorListener,false, charset, false);
  675.     }
  676.     public void writeTo(Element element,OutputStream os,boolean omitXMLDeclaration, String charset)throws TransformerException,IOException, XMLException{
  677.         this.writeNodeTo(element, os, new XMLErrorListener(),omitXMLDeclaration, charset, false);
  678.     }
  679.     public void writeTo(Element element,OutputStream os,ErrorListener errorListener,boolean omitXMLDeclaration, String charset)throws TransformerException,IOException, XMLException{
  680.         this.writeNodeTo(element, os,errorListener,omitXMLDeclaration, charset, false);
  681.     }
  682.     public void writeTo(Element element,OutputStream os,boolean omitXMLDeclaration, String charset, boolean indent)throws TransformerException,IOException, XMLException{
  683.         this.writeNodeTo(element, os, new XMLErrorListener(),omitXMLDeclaration, charset, indent);
  684.     }
  685.     public void writeTo(Element element,OutputStream os,ErrorListener errorListener,boolean omitXMLDeclaration, String charset, boolean indent)throws TransformerException,IOException, XMLException{
  686.         this.writeNodeTo(element, os,errorListener,omitXMLDeclaration, charset, indent);
  687.     }

  688.     public void writeTo(Element element,Writer writer)throws TransformerException,IOException, XMLException{
  689.         this.writeNodeTo(element, writer, new XMLErrorListener(),false, null, false);
  690.     }
  691.     public void writeTo(Element element,Writer writer,ErrorListener errorListener)throws TransformerException,IOException, XMLException{
  692.         this.writeNodeTo(element, writer,errorListener,false, null, false);
  693.     }
  694.     public void writeTo(Element element,Writer writer,boolean omitXMLDeclaration)throws TransformerException,IOException, XMLException{
  695.         this.writeNodeTo(element, writer, new XMLErrorListener(), omitXMLDeclaration, null, false);
  696.     }
  697.     public void writeTo(Element element,Writer writer,ErrorListener errorListener,boolean omitXMLDeclaration)throws TransformerException,IOException, XMLException{
  698.         this.writeNodeTo(element, writer,errorListener, omitXMLDeclaration, null, false);
  699.     }
  700.     public void writeTo(Element element,Writer writer, String charset)throws TransformerException,IOException, XMLException{
  701.         this.writeNodeTo(element, writer, new XMLErrorListener(),false, charset, false);
  702.     }
  703.     public void writeTo(Element element,Writer writer,ErrorListener errorListener, String charset)throws TransformerException,IOException, XMLException{
  704.         this.writeNodeTo(element, writer,errorListener,false, charset, false);
  705.     }
  706.     public void writeTo(Element element,Writer writer,boolean omitXMLDeclaration, String charset)throws TransformerException,IOException, XMLException{
  707.         this.writeNodeTo(element, writer, new XMLErrorListener(), omitXMLDeclaration, charset, false);
  708.     }
  709.     public void writeTo(Element element,Writer writer,ErrorListener errorListener,boolean omitXMLDeclaration, String charset)throws TransformerException,IOException, XMLException{
  710.         this.writeNodeTo(element, writer,errorListener, omitXMLDeclaration, charset, false);
  711.     }
  712.     public void writeTo(Element element,Writer writer,boolean omitXMLDeclaration, String charset, boolean indent)throws TransformerException,IOException, XMLException{
  713.         this.writeNodeTo(element, writer, new XMLErrorListener(), omitXMLDeclaration, charset, indent);
  714.     }
  715.     public void writeTo(Element element,Writer writer,ErrorListener errorListener,boolean omitXMLDeclaration, String charset, boolean indent)throws TransformerException,IOException, XMLException{
  716.         this.writeNodeTo(element, writer,errorListener, omitXMLDeclaration, charset, indent);
  717.     }

  718.     public void writeTo(Element element,File file)throws TransformerException,IOException, XMLException{
  719.         this.writeNodeTo(element, file, new XMLErrorListener(),false, null, false);
  720.     }
  721.     public void writeTo(Element element,File file,ErrorListener errorListener)throws TransformerException,IOException, XMLException{
  722.         this.writeNodeTo(element, file,errorListener,false, null, false);
  723.     }
  724.     public void writeTo(Element element,File file,boolean omitXMLDeclaration)throws TransformerException,IOException, XMLException{
  725.         this.writeNodeTo(element, file, new XMLErrorListener(),omitXMLDeclaration, null, false);
  726.     }
  727.     public void writeTo(Element element,File file,ErrorListener errorListener,boolean omitXMLDeclaration)throws TransformerException,IOException, XMLException{
  728.         this.writeNodeTo(element, file,errorListener,omitXMLDeclaration, null, false);
  729.     }
  730.     public void writeTo(Element element,File file, String charset)throws TransformerException,IOException, XMLException{
  731.         this.writeNodeTo(element, file, new XMLErrorListener(),false, charset, false);
  732.     }
  733.     public void writeTo(Element element,File file,ErrorListener errorListener, String charset)throws TransformerException,IOException, XMLException{
  734.         this.writeNodeTo(element, file,errorListener,false, charset, false);
  735.     }
  736.     public void writeTo(Element element,File file,boolean omitXMLDeclaration, String charset)throws TransformerException,IOException, XMLException{
  737.         this.writeNodeTo(element, file, new XMLErrorListener(),omitXMLDeclaration, charset, false);
  738.     }
  739.     public void writeTo(Element element,File file,ErrorListener errorListener,boolean omitXMLDeclaration, String charset)throws TransformerException,IOException, XMLException{
  740.         this.writeNodeTo(element, file,errorListener,omitXMLDeclaration, charset, false);
  741.     }
  742.     public void writeTo(Element element,File file,boolean omitXMLDeclaration, String charset, boolean indent)throws TransformerException,IOException, XMLException{
  743.         this.writeNodeTo(element, file, new XMLErrorListener(),omitXMLDeclaration, charset, indent);
  744.     }
  745.     public void writeTo(Element element,File file,ErrorListener errorListener,boolean omitXMLDeclaration, String charset, boolean indent)throws TransformerException,IOException, XMLException{
  746.         this.writeNodeTo(element, file,errorListener,omitXMLDeclaration, charset, indent);
  747.     }

  748.     public void writeTo(Node node,OutputStream os)throws TransformerException,IOException, XMLException{
  749.         this.writeNodeTo(node,os, new XMLErrorListener(),false, null, false);
  750.     }
  751.     public void writeTo(Node node,OutputStream os,ErrorListener errorListener)throws TransformerException,IOException, XMLException{
  752.         this.writeNodeTo(node,os,errorListener,false, null, false);
  753.     }
  754.     public void writeTo(Node node,OutputStream os,boolean omitXMLDeclaration)throws TransformerException,IOException, XMLException{
  755.         this.writeNodeTo(node,os, new XMLErrorListener(),omitXMLDeclaration, null, false);
  756.     }
  757.     public void writeTo(Node node,OutputStream os,ErrorListener errorListener,boolean omitXMLDeclaration)throws TransformerException,IOException, XMLException{
  758.         this.writeNodeTo(node,os,errorListener,omitXMLDeclaration, null, false);
  759.     }
  760.     public void writeTo(Node node,OutputStream os, String charset)throws TransformerException,IOException, XMLException{
  761.         this.writeNodeTo(node,os, new XMLErrorListener(),false, charset, false);
  762.     }
  763.     public void writeTo(Node node,OutputStream os,ErrorListener errorListener, String charset)throws TransformerException,IOException, XMLException{
  764.         this.writeNodeTo(node,os,errorListener,false, charset, false);
  765.     }
  766.     public void writeTo(Node node,OutputStream os,boolean omitXMLDeclaration, String charset)throws TransformerException,IOException, XMLException{
  767.         this.writeNodeTo(node,os, new XMLErrorListener(),omitXMLDeclaration, charset, false);
  768.     }
  769.     public void writeTo(Node node,OutputStream os,ErrorListener errorListener,boolean omitXMLDeclaration, String charset)throws TransformerException,IOException, XMLException{
  770.         this.writeNodeTo(node,os,errorListener,omitXMLDeclaration, charset, false);
  771.     }
  772.     public void writeTo(Node node,OutputStream os,boolean omitXMLDeclaration, String charset, boolean indent)throws TransformerException,IOException, XMLException{
  773.         this.writeNodeTo(node,os, new XMLErrorListener(),omitXMLDeclaration, charset, indent);
  774.     }
  775.     public void writeTo(Node node,OutputStream os,ErrorListener errorListener,boolean omitXMLDeclaration, String charset, boolean indent)throws TransformerException,IOException, XMLException{
  776.         this.writeNodeTo(node,os,errorListener,omitXMLDeclaration, charset, indent);
  777.     }
  778.     private void writeNodeTo(Node node,OutputStream os,ErrorListener errorListener,boolean omitXMLDeclaration, String charset, boolean indent)throws TransformerException,IOException, XMLException, XMLException{
  779.         TransformerConfig config = new TransformerConfig();
  780.         config.setOmitXMLDeclaration(omitXMLDeclaration);
  781.         config.setCharset(charset);
  782.         config.setIndent(indent);
  783.         writeTo(node,os,errorListener,config);
  784.     }


  785.     public void writeTo(Node node,Writer writer)throws TransformerException,IOException, XMLException{
  786.         this.writeNodeTo(node,writer, new XMLErrorListener(),false, null, false);
  787.     }
  788.     public void writeTo(Node node,Writer writer,ErrorListener errorListener)throws TransformerException,IOException, XMLException{
  789.         this.writeNodeTo(node,writer,errorListener,false, null, false);
  790.     }
  791.     public void writeTo(Node node,Writer writer,boolean omitXMLDeclaration)throws TransformerException,IOException, XMLException{
  792.         this.writeNodeTo(node,writer, new XMLErrorListener(),omitXMLDeclaration, null, false);
  793.     }
  794.     public void writeTo(Node node,Writer writer,ErrorListener errorListener,boolean omitXMLDeclaration)throws TransformerException,IOException, XMLException{
  795.         this.writeNodeTo(node,writer,errorListener,omitXMLDeclaration, null, false);
  796.     }
  797.     public void writeTo(Node node,Writer writer, String charset)throws TransformerException,IOException, XMLException{
  798.         this.writeNodeTo(node,writer, new XMLErrorListener(),false, charset, false);
  799.     }
  800.     public void writeTo(Node node,Writer writer,ErrorListener errorListener, String charset)throws TransformerException,IOException, XMLException{
  801.         this.writeNodeTo(node,writer,errorListener,false, charset, false);
  802.     }
  803.     public void writeTo(Node node,Writer writer,boolean omitXMLDeclaration, String charset)throws TransformerException,IOException, XMLException{
  804.         this.writeNodeTo(node,writer, new XMLErrorListener(),omitXMLDeclaration, charset, false);
  805.     }
  806.     public void writeTo(Node node,Writer writer,ErrorListener errorListener,boolean omitXMLDeclaration, String charset)throws TransformerException,IOException, XMLException{
  807.         this.writeNodeTo(node,writer,errorListener,omitXMLDeclaration, charset, false);
  808.     }
  809.     public void writeTo(Node node,Writer writer,boolean omitXMLDeclaration, String charset, boolean indent)throws TransformerException,IOException, XMLException{
  810.         this.writeNodeTo(node,writer, new XMLErrorListener(),omitXMLDeclaration, charset, indent);
  811.     }
  812.     public void writeTo(Node node,Writer writer,ErrorListener errorListener,boolean omitXMLDeclaration, String charset, boolean indent)throws TransformerException,IOException, XMLException{
  813.         this.writeNodeTo(node,writer,errorListener,omitXMLDeclaration, charset, indent);
  814.     }
  815.     private void writeNodeTo(Node node,Writer writer,ErrorListener errorListener,boolean omitXMLDeclaration, String charset, boolean indent)throws TransformerException,IOException, XMLException{
  816.         TransformerConfig config = new TransformerConfig();
  817.         config.setOmitXMLDeclaration(omitXMLDeclaration);
  818.         config.setCharset(charset);
  819.         config.setIndent(indent);
  820.         writeTo(node,writer,errorListener,config);
  821.     }

  822.     public void writeTo(Node node,File file)throws TransformerException,IOException, XMLException{
  823.         this.writeNodeTo(node,file, new XMLErrorListener(),false, null, false);
  824.     }
  825.     public void writeTo(Node node,File file,ErrorListener errorListener)throws TransformerException,IOException, XMLException{
  826.         this.writeNodeTo(node,file,errorListener,false, null, false);
  827.     }
  828.     public void writeTo(Node node,File file,boolean omitXMLDeclaration)throws TransformerException,IOException, XMLException{
  829.         this.writeNodeTo(node,file, new XMLErrorListener(),omitXMLDeclaration, null, false);
  830.     }
  831.     public void writeTo(Node node,File file,ErrorListener errorListener,boolean omitXMLDeclaration)throws TransformerException,IOException, XMLException{
  832.         this.writeNodeTo(node,file,errorListener,omitXMLDeclaration, null, false);
  833.     }
  834.     public void writeTo(Node node,File file, String charset)throws TransformerException,IOException, XMLException{
  835.         this.writeNodeTo(node,file, new XMLErrorListener(),false, charset, false);
  836.     }
  837.     public void writeTo(Node node,File file,ErrorListener errorListener, String charset)throws TransformerException,IOException, XMLException{
  838.         this.writeNodeTo(node,file,errorListener,false, charset, false);
  839.     }
  840.     public void writeTo(Node node,File file,boolean omitXMLDeclaration, String charset)throws TransformerException,IOException, XMLException{
  841.         this.writeNodeTo(node,file, new XMLErrorListener(),omitXMLDeclaration, charset, false);
  842.     }
  843.     public void writeTo(Node node,File file,ErrorListener errorListener,boolean omitXMLDeclaration, String charset)throws TransformerException,IOException, XMLException{
  844.         this.writeNodeTo(node,file,errorListener,omitXMLDeclaration, charset, false);
  845.     }
  846.     public void writeTo(Node node,File file,boolean omitXMLDeclaration, String charset, boolean indent)throws TransformerException,IOException, XMLException{
  847.         this.writeNodeTo(node,file, new XMLErrorListener(),omitXMLDeclaration, charset, indent);
  848.     }
  849.     public void writeTo(Node node,File file,ErrorListener errorListener,boolean omitXMLDeclaration, String charset, boolean indent)throws TransformerException,IOException, XMLException{
  850.         this.writeNodeTo(node,file,errorListener,omitXMLDeclaration, charset, indent);
  851.     }
  852.     private void writeNodeTo(Node node,File file,ErrorListener errorListener,boolean omitXMLDeclaration, String charset, boolean indent)throws TransformerException,IOException, XMLException{
  853.         TransformerConfig config = new TransformerConfig();
  854.         config.setOmitXMLDeclaration(omitXMLDeclaration);
  855.         config.setCharset(charset);
  856.         config.setIndent(indent);
  857.         writeTo(node,file,errorListener,config);
  858.     }
  859.    
  860.     public void writeTo(Node node,OutputStream os,TransformerConfig config)throws TransformerException,IOException, XMLException, XMLException{
  861.         writeTo(node,os,new XMLErrorListener(),config);
  862.     }
  863.     public void writeTo(Node node,OutputStream os,ErrorListener errorListener,TransformerConfig config)throws TransformerException,IOException, XMLException, XMLException{
  864.         Source source = new DOMSourceFix(node);
  865.         StreamResult result = new StreamResult(os);
  866.         Transformer transformer = getTransformerFactory().newTransformer();
  867.         if(config!=null && config.isOmitXMLDeclaration()) {
  868.             transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,"yes");
  869.         }
  870.         if(config!=null && config.getCharset()!=null) {
  871.             transformer.setOutputProperty(OutputKeys.ENCODING, config.getCharset());
  872.         }
  873.         if(config!=null && config.isIndent()) {
  874.             transformer.setOutputProperty(OutputKeys.INDENT, "yes");
  875.         }
  876.         transformer.setOutputProperty(OutputKeys.METHOD, "xml");
  877.         transformer.setErrorListener(errorListener);
  878.         transformer.transform(source, result);
  879.         os.flush();
  880.     }
  881.    
  882.     public void writeTo(Node node,Writer writer,TransformerConfig config)throws TransformerException,IOException, XMLException{
  883.         writeTo(node,writer,new XMLErrorListener(),config);
  884.     }
  885.     public void writeTo(Node node,Writer writer,ErrorListener errorListener,TransformerConfig config)throws TransformerException,IOException, XMLException{
  886.         Source source = new DOMSourceFix(node);
  887.         StreamResult result = new StreamResult(writer);
  888.         Transformer transformer = getTransformerFactory().newTransformer();
  889.         if(config!=null && config.isOmitXMLDeclaration()) {
  890.             transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,"yes");
  891.         }
  892.         if(config!=null && config.getCharset()!=null) {
  893.             transformer.setOutputProperty(OutputKeys.ENCODING, config.getCharset());
  894.         }
  895.         if(config!=null && config.isIndent()) {
  896.             transformer.setOutputProperty(OutputKeys.INDENT, "yes");
  897.         }
  898.         transformer.setErrorListener(errorListener);
  899.         transformer.transform(source, result);
  900.         writer.flush();
  901.     }
  902.    
  903.     public void writeTo(Node node,File file,TransformerConfig config)throws TransformerException,IOException, XMLException{
  904.         writeTo(node,file,new XMLErrorListener(),config);
  905.     }
  906.     public void writeTo(Node node,File file,ErrorListener errorListener,TransformerConfig config)throws TransformerException,IOException, XMLException{
  907.         Source source = new DOMSourceFix(node);
  908.         StreamResult result = new StreamResult(file);
  909.         Transformer transformer = getTransformerFactory().newTransformer();
  910.         if(config!=null && config.isOmitXMLDeclaration()) {
  911.             transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,"yes");
  912.         }
  913.         if(config!=null && config.getCharset()!=null) {
  914.             transformer.setOutputProperty(OutputKeys.ENCODING, config.getCharset());
  915.         }
  916.         if(config!=null && config.isIndent()) {
  917.             transformer.setOutputProperty(OutputKeys.INDENT, "yes");
  918.         }
  919.         transformer.setErrorListener(errorListener);
  920.         transformer.transform(source, result);
  921.     }

  922.    
  923.    
  924.    
  925.     // ELIMINAZIONE DICHIARAZIONE XML
  926.    
  927.     @Deprecated
  928.     public byte[] eraserXML(byte[] xml){
  929.         return this.eraserXML(new String(xml)).getBytes();
  930.     }
  931.     @Deprecated
  932.     public String eraserXML(String xml){
  933.         String tmp = xml.trim();
  934.         if(tmp.startsWith("<?xml")){
  935.             int indexOf = tmp.indexOf(">");
  936.             return tmp.substring(indexOf+1);
  937.         }else{
  938.             return xml;
  939.         }
  940.     }
  941.    
  942.     /**
  943.      * Metodo che si occupa di effettuare l'eliminazione degli xsd:type String
  944.      *
  945.      * @deprecated  utility che elimina gli xsd type
  946.      * @param xml Xml su cui effettuare la pulizia dell'header.
  947.      * @return byte[] contenente un xml 'pulito'.
  948.      *
  949.      */
  950.     @Deprecated public byte[] eraserXsiType(byte[] xml) throws UtilsException{

  951.         ByteArrayOutputStream cleanXML = null;
  952.         try{
  953.             String eraserString = " xsi:type=\"xsd:string\"";
  954.             cleanXML = new ByteArrayOutputStream();

  955.             // Busta
  956.             for(int i=0; i<xml.length ; ){

  957.                 if(xml[i] == ' '){

  958.                     // String
  959.                     if(i+eraserString.length() < xml.length){
  960.                         StringBuilder test = new StringBuilder();
  961.                         for(int k=0 ; k < eraserString.length(); k++){
  962.                             test.append((char)xml[i+k]);
  963.                         }
  964.                         if(test.toString().equals(eraserString)){
  965.                             i = i + eraserString.length();
  966.                             continue;
  967.                         }
  968.                     }

  969.                     cleanXML.write(xml[i]);
  970.                     i++;

  971.                 }else{
  972.                     cleanXML.write(xml[i]);
  973.                     i++;
  974.                 }

  975.             }

  976.             byte [] risultato = cleanXML.toByteArray();
  977.             cleanXML.close();
  978.             return risultato;

  979.         } catch(Exception e) {
  980.             try{
  981.                 if(cleanXML!=null)
  982.                     cleanXML.close();
  983.             }catch(Exception eis){
  984.                 // close
  985.             }
  986.             throw new UtilsException("Utilities.eraserType error "+e.getMessage(),e);
  987.         }
  988.     }  
  989.    
  990.    
  991.    
  992.     // IS
  993.    
  994.     public boolean isDocument(byte[]xml){
  995.         try{
  996.             return this.newDocument(xml)!=null;
  997.         }catch(Exception e){
  998.             return false;
  999.         }
  1000.     }
  1001.     public boolean isElement(byte[]xml){
  1002.         try{
  1003.             return this.newElement(xml)!=null;
  1004.         }catch(Exception e){
  1005.             return false;
  1006.         }
  1007.     }

  1008.    

  1009.    
  1010.     // ATTRIBUTE
  1011.    
  1012.     public String getAttributeValue(Node n,String attrName){
  1013.         NamedNodeMap att = n.getAttributes();
  1014.         if(att!=null){
  1015.             Node nA = att.getNamedItem(attrName);
  1016.             if(nA!=null)
  1017.                 return nA.getNodeValue();
  1018.         }
  1019.         return null;
  1020.     }
  1021.    
  1022.     public void removeAttribute(Attr attr, Element src){
  1023.        
  1024.         // NOTA:
  1025.         //   attr.getName ritorna anche il prefisso se c'è a differenza di attr.getLocalName
  1026.        
  1027.         if("http://www.w3.org/2000/xmlns/".equals(attr.getNamespaceURI())){
  1028.             if("xmlns".equals(attr.getName())){
  1029.                 if(attr.getClass().getName().contains("org.apache.axiom.")){
  1030.                     // axiom
  1031.                     //System.out.println("REMOVE NS SPECIAL AXIOM XMLNS ["+attr.getClass().getName()+"]");
  1032.                     src.removeAttributeNS(attr.getNamespaceURI(), "");
  1033.                 }
  1034.                 else{
  1035.                     // cxf
  1036.                     //System.out.println("REMOVE NS SPECIAL CXF XMLNS ["+attr.getClass().getName()+"]");
  1037.                     src.removeAttributeNS(attr.getNamespaceURI(), attr.getName());
  1038.                 }
  1039.             }
  1040.             else if(attr.getNamespaceURI()!=null){
  1041.                 //System.out.println("REMOVE NS XMLNS");
  1042.                 src.removeAttributeNS(attr.getNamespaceURI(), attr.getLocalName()); // Deve essere usato localName per cxf
  1043.             }
  1044.         }
  1045.         else{
  1046.             //System.out.println("REMOVE NORMAL");
  1047.             src.removeAttribute(attr.getName());
  1048.         }
  1049.     }
  1050.    
  1051.     public void addAttribute(Attr attr, Element src){
  1052.        
  1053.         // NOTA:
  1054.         //   attr.getName ritorna anche il prefisso se c'è a differenza di attr.getLocalName
  1055.        
  1056.         if("http://www.w3.org/2000/xmlns/".equals(attr.getNamespaceURI())){
  1057.             if("xmlns".equals(attr.getLocalName())){
  1058.                 //System.out.println("ADD NS SPECIAL XMLNS");
  1059.                 src.setAttribute(attr.getName(), attr.getValue());
  1060.             }
  1061.             else{
  1062.                 //System.out.println("ADD NS XMLNS");
  1063.                 src.setAttributeNS(attr.getNamespaceURI(), attr.getName(), attr.getValue());
  1064.             }
  1065.         }
  1066.         else{
  1067.             //System.out.println("ADD NORMAL ATTRIBUTE");
  1068.             src.setAttribute(attr.getName(), attr.getValue());
  1069.         }
  1070.     }


  1071.    
  1072.    
  1073.    
  1074.    
  1075.    
  1076.     // NAMESPACE
  1077.    
  1078.     public Map<String, String> getNamespaceDeclaration(Node n){
  1079.         NamedNodeMap map = n.getAttributes();
  1080.         Map<String, String> namespaces = new HashMap<>();
  1081.         if(map!=null){
  1082.             for (int i = 0; i < map.getLength(); i++) {
  1083.                 Node attribute = map.item(i);
  1084.                 //System.out.println("ATTRIBUTE["+i+"] ("+attribute.getClass().getName()+") name["+attribute.getLocalName()+"] uri["+attribute.getNamespaceURI()+"] value["+attribute.getNodeValue()+"] prefix["+attribute.getPrefix()+"]");
  1085.                
  1086.                 if(attribute!=null && (attribute instanceof  Attr) ){
  1087.                     Attr a = (Attr) attribute;
  1088.                    
  1089.                     //System.out.println("ATTRIBUTE["+i+"]  INSTACE name("+a.getName()+") value("+a.getValue()+") uri("+a.getBaseURI()+") prefix("+a.getPrefix()+") localName("+a.getLocalName()+")");
  1090.                     // INSTACE name(xmlns) value(www.namespace.org)
  1091.                     // INSTACE name(xmlns:pp) value(www.namespace2.org)
  1092.                    
  1093.                     String prefix = a.getName();
  1094.                     if(prefix!=null && (prefix.startsWith("xmlns") || prefix.equals("xmlns"))){
  1095.                         if(prefix.contains(":")){
  1096.                             prefix = prefix.split(":")[1];
  1097.                         }
  1098.                         else{
  1099.                             prefix = "";
  1100.                         }
  1101.                         namespaces.put(prefix, a.getValue());
  1102.                     }
  1103.                 }
  1104.             }
  1105.         }
  1106.         return namespaces;
  1107.     }
  1108.    
  1109.     public String findNamespaceByPrefix(String prefix,Node node) {
  1110.         return this.findNamespaceByPrefix(prefix, node, node);
  1111.     }
  1112.     public String findNamespaceByPrefix(String prefix,Node node, Node parentNode) {
  1113.         if(node==null) {
  1114.             return null;
  1115.         }
  1116.        
  1117. //      System.out.println("[{"+node.getNamespaceURI()+"}"+node.getLocalName()+"] CERCO NAMESPACE ...");
  1118.        
  1119.         // cerco tra le definizioni del nodo
  1120.         NamedNodeMap nn = node.getAttributes();
  1121.         if(nn!=null && nn.getLength()>0) {
  1122.             for (int k = 0; k < nn.getLength(); k++) {
  1123.                 Node nAttr = nn.item(k);
  1124.                 if(nAttr instanceof Attr) {
  1125.                     Attr attr = (Attr) nAttr;
  1126.                     String prefixAttr = attr.getName();
  1127.                     if(prefixAttr!=null && (prefixAttr.startsWith("xmlns") || prefix.equals("prefixAttr"))){
  1128.                         if(prefixAttr.contains(":")){
  1129.                             prefixAttr = prefixAttr.split(":")[1];
  1130.                         }
  1131.                         else{
  1132.                             prefixAttr = "";
  1133.                         }
  1134.                     }
  1135. //                  System.out.println("\t CONFRONTO ["+prefix+"]==["+prefixAttr+"] ["+attr.getName()+"] ["+attr.getLocalName()+"] ["+attr.getValue()+"] ["+attr.getNamespaceURI()+"] ...");
  1136.                     if(prefix.equals(prefixAttr)) {
  1137. //                      System.out.println("TROVATO PREFIX ["+prefix+"] in node [{"+node.getNamespaceURI()+"}"+node.getLocalName()+
  1138. //                              "], namespace is ["+attr.getNamespaceURI()+"]["+attr.getValue()+"]");
  1139.                         // TROVATO PREFIX [ns10] in node [{http://www.xxxx}ElementName], namespace is [http://www.w3.org/2000/xmlns/][http://namespaceApplicativo]
  1140.                         return attr.getValue();
  1141.                     }
  1142.                 }
  1143.             }
  1144.         }
  1145.        
  1146.         // Sono al padre non salgo ulteriormente
  1147.         if(node.isSameNode(parentNode)) {
  1148. //          System.out.println("\tRicerca ["+prefix+"] RAGGIUNTO IL PADRE [{"+parentNode.getNamespaceURI()+"}"+parentNode.getLocalName()+"]");
  1149.             return null;
  1150.         }
  1151.         // cerco nel padre a meno che non ho raggiunto il parentNode
  1152.         Node p = node.getParentNode();
  1153.         if(p==null) {
  1154.             return null;
  1155.         }

  1156.         return this.findNamespaceByPrefix(prefix, p, parentNode);
  1157.     }
  1158.    
  1159.     public void addNamespaceDeclaration(Map<String, String> namespace, Element destNode){
  1160.         if(namespace!=null && namespace.size()>0){
  1161.             Map<String, String> declarationNamespacesDestNode = this.getNamespaceDeclaration(destNode);
  1162. //          if(declarationNamespacesDestNode.size()>0){
  1163. //              Enumeration<String> decSchema = declarationNamespacesDestNode.keys();
  1164. //              while (decSchema.hasMoreElements()) {
  1165. //                  String dec = (String) decSchema.nextElement();
  1166. //                  System.out.println("TROVATO ORIGINALE ["+dec+"]=["+declarationNamespacesDestNode.get(dec)+"]");
  1167. //              }
  1168. //          }
  1169.             for (String dec : namespace.keySet()) {
  1170.                 //System.out.println("TROVATO ["+dec+"]=["+namespace.get(dec)+"]");
  1171.                 if(declarationNamespacesDestNode.containsKey(dec)==false){
  1172.                     //System.out.println("AGGIUNGO IN SCHEMA");
  1173.                     String name = "xmlns:"+dec;
  1174.                     if("".equals(dec)){
  1175.                         name = "xmlns";
  1176.                     }
  1177.                     destNode.setAttributeNS("http://www.w3.org/2000/xmlns/",name,namespace.get(dec));
  1178.                 }
  1179.             }
  1180.         }
  1181.     }
  1182.    
  1183.     public void removeNamespaceDeclaration(Map<String, String> namespace, Element destNode){
  1184.        
  1185.         // TODO: Da verificare, non ancora utilizzato
  1186.         if(namespace!=null && namespace.size()>0){
  1187.             for (String dec : namespace.keySet()) {
  1188.                 //System.out.println("RIMUOVO ["+dec+"]=["+namespace.get(dec)+"]");
  1189.                 String name = "xmlns:"+dec;
  1190.                 if("".equals(dec)){
  1191.                     name = "xmlns";
  1192.                 }
  1193.                 destNode.removeAttributeNS("http://www.w3.org/2000/xmlns/", name);
  1194.             }
  1195.         }
  1196.     }
  1197.    
  1198.    
  1199.    
  1200.    
  1201.    
  1202.    
  1203.     // NAMESPACE XSI TYPE
  1204.    
  1205.     private static final String XMLSCHEMA_INSTANCE_NAMESPACE = "http://www.w3.org/2001/XMLSchema-instance";
  1206.     private static final String XMLSCHEMA_INSTANCE_LOCAL_NAME_TYPE = "type";
  1207.    
  1208.     public void addNamespaceXSITypeIfNotExists(Node node, DynamicNamespaceContext dnc, boolean deep) {
  1209.         this.addNamespaceXSITypeIfNotExists(node, dnc, deep, node);
  1210.     }
  1211.     private void addNamespaceXSITypeIfNotExists(Node node, DynamicNamespaceContext dnc, boolean deep, Node parentNode) {
  1212.        
  1213.         if(node==null) {
  1214.             return;
  1215.         }
  1216.        
  1217.        
  1218.        
  1219.         // Gestisco attributi
  1220.         if(node instanceof Element) {
  1221.             Element element = (Element) node;
  1222.             NamedNodeMap nn = element.getAttributes();
  1223.             if(nn!=null && nn.getLength()>0) {
  1224.                 List<String> prefixXSIType = new ArrayList<>();
  1225.                 for (int k = 0; k < nn.getLength(); k++) {
  1226.                     Node nAttr = nn.item(k);
  1227.                     //System.out.println("ATTR ["+nAttr.getClass().getName()+"]");
  1228.                     if(nAttr instanceof Attr) {
  1229.                         Attr attr = (Attr) nAttr;
  1230.                         //System.out.println("\t name["+attr.getName()+"] value["+attr.getValue()+"] namespace["+attr.getNamespaceURI()+"] prefix["+attr.getPrefix()+"]");
  1231.                         // name[xsi:type] value[ns10:XXXXType] namespace[http://www.w3.org/2001/XMLSchema-instance] prefix[xsi]
  1232.                         if(AbstractXMLUtils.XMLSCHEMA_INSTANCE_NAMESPACE.equals(attr.getNamespaceURI())) {
  1233.                             String prefix = attr.getPrefix();
  1234.                             String nameAtteso = prefix+":"+AbstractXMLUtils.XMLSCHEMA_INSTANCE_LOCAL_NAME_TYPE;
  1235.                             if(prefix==null || "".equals(prefix)) {
  1236.                                 nameAtteso = AbstractXMLUtils.XMLSCHEMA_INSTANCE_LOCAL_NAME_TYPE;
  1237.                             }
  1238.                             if(nameAtteso.equals(attr.getName())) {
  1239.                                 if(attr.getValue()!=null && attr.getValue().contains(":")) {
  1240.                                     prefixXSIType.add(attr.getValue());
  1241.                                 }
  1242.                             }
  1243.                         }
  1244.                     }
  1245.                 }
  1246.                 if(prefixXSIType.size()>0) {
  1247.                     for (String prefixValue : prefixXSIType) {
  1248.                         String [] tmp = prefixValue.split(":");
  1249.                         if(tmp!=null && tmp.length==2) {
  1250.                             String prefix = tmp[0];
  1251.                             //String localName = tmp[1];
  1252.                            
  1253. //                          System.out.println("\n\n\n =============================================================");
  1254. //                          System.out.println("[{"+node.getNamespaceURI()+"}"+node.getLocalName()+"] prefix ["+prefix+"]");
  1255. //                          System.out.println("=============================================================\n");
  1256.                            
  1257.                             //System.out.println("VERIFICO PRESENZA NAMESPACE PER ["+prefixValue+"] prefix["+prefix+"] localName["+localName+"]");
  1258.                             String namespaceEsistente = this.findNamespaceByPrefix(prefix, element, parentNode);
  1259.                             boolean foundNamespace = namespaceEsistente!=null;
  1260.                             if(!foundNamespace) {
  1261.                                 String namespace = dnc.getNamespaceURI(prefix);
  1262. //                              System.out.println("[{"+node.getNamespaceURI()+"}"+node.getLocalName()+"] NAMESPACE AGGIUNTO PERCHE' NON TROVATO per ["+prefix+"]=["+namespace+"]");
  1263.                                 element.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:"+prefix, namespace);
  1264.                             }
  1265. //                          else {
  1266. //                              System.out.println("[{"+node.getNamespaceURI()+"}"+node.getLocalName()+"] ESISTE NAMESPACE ["+namespaceEsistente+"] PER PREFIX: "+prefix);
  1267. //                          }
  1268.                         }
  1269.                     }
  1270.                 }
  1271.             }
  1272.         }
  1273.        
  1274.         // Vado in ricorsione sugli elementi figli
  1275.         if(deep) {
  1276.             List<Node> childList = this.getNotEmptyChildNodes(node, false);
  1277.             if(childList!=null && childList.size()>0) {
  1278.                 for (Node child : childList) {
  1279.                     this.addNamespaceXSITypeIfNotExists(child, dnc, deep, parentNode);
  1280.                 }
  1281.             }
  1282.         }
  1283.     }
  1284.    

  1285.    
  1286.    
  1287.    
  1288.    
  1289.    
  1290.    
  1291.    
  1292.     // UTILITIES
  1293.    
  1294.     public List<Node> getNotEmptyChildNodes(Node e){
  1295.         return getNotEmptyChildNodes(e, true);
  1296.     }
  1297.     public List<Node> getNotEmptyChildNodes(Node e, boolean consideraTextNotEmptyAsNode){
  1298.         if(e==null) {
  1299.             throw new RuntimeException("Param node is null");
  1300.         }
  1301.         NodeList nl = e.getChildNodes();
  1302.         List<Node> vec = new ArrayList<Node>();
  1303.         if(nl!=null){
  1304.             for(int index = 0 ; index<nl.getLength(); index++){
  1305.                 Node n = nl.item(index);
  1306.                 if(n instanceof Text){
  1307.                     if(consideraTextNotEmptyAsNode){
  1308.                         if (((Text) nl.item(index)).getData().trim().length() == 0) {
  1309.                             continue;
  1310.                         }
  1311.                     }else{
  1312.                         continue;
  1313.                     }
  1314.                 }
  1315.                 else if (n instanceof Comment) {
  1316.                     continue;
  1317.                 }
  1318.                 vec.add(nl.item(index));
  1319.             }
  1320.         }
  1321.         return vec;
  1322.     }
  1323.    
  1324.     public Node getFirstNotEmptyChildNode(Node e){
  1325.         return getFirstNotEmptyChildNode(e, true);
  1326.     }
  1327.     public Node getFirstNotEmptyChildNode(Node e, boolean consideraTextNotEmptyAsNode){
  1328.         if(e==null) {
  1329.             throw new RuntimeException("Param node is null");
  1330.         }
  1331.         NodeList nl = e.getChildNodes();
  1332.         if(nl!=null){
  1333.             for(int index = 0 ; index<nl.getLength(); index++){
  1334.                 Node n = nl.item(index);
  1335.                 if(n instanceof Text){
  1336.                     if(consideraTextNotEmptyAsNode){
  1337.                         if (((Text) nl.item(index)).getData().trim().length() == 0) {
  1338.                             continue;
  1339.                         }
  1340.                     }else{
  1341.                         continue;
  1342.                     }
  1343.                 }
  1344.                 else if (nl.item(index) instanceof Comment) {
  1345.                     continue;
  1346.                 }
  1347.                 return nl.item(index);
  1348.             }
  1349.         }
  1350.         return null;
  1351.     }
  1352.    
  1353.     public boolean matchLocalName(Node nodo,String nodeName,String prefix,String namespace){
  1354.         if(nodo==null)
  1355.             return false;
  1356.         if(nodo.getNodeName()==null)
  1357.             return false;
  1358.         // Il nodo possiede il prefisso atteso
  1359.         if(nodo.getNodeName().equals(prefix+nodeName))
  1360.             return true;
  1361.         // Il nodo puo' ridefinire il prefisso ridefinendo il namespace
  1362.         String namespaceNodo = nodo.getNamespaceURI();
  1363.         if(namespaceNodo!=null && namespaceNodo.equals(namespace)){
  1364.             String xmlns = nodo.getPrefix();
  1365.             if(xmlns == null){
  1366.                 xmlns = "";
  1367.             }else if(!xmlns.equals("")){
  1368.                 xmlns = xmlns + ":";
  1369.             }
  1370.             if(nodo.getNodeName().equals(xmlns+nodeName))
  1371.                 return true;
  1372.         }
  1373.         return false;
  1374.     }
  1375.    
  1376.     public Node getAttributeNode(Node node,String attributeName){
  1377.         if (node == null)
  1378.         {
  1379.             return null;
  1380.         }
  1381.         NamedNodeMap map = node.getAttributes();
  1382.         if(map==null || map.getLength()==0){
  1383.             return null;
  1384.         }
  1385.         else{
  1386.             return map.getNamedItem(attributeName);
  1387.         }
  1388.     }
  1389.     public Node getQualifiedAttributeNode(Node node,String attributeName,String namespace){
  1390.         if (node == null)
  1391.         {
  1392.             return null;
  1393.         }
  1394.         NamedNodeMap map = node.getAttributes();
  1395.         if(map==null || map.getLength()==0){
  1396.             return null;
  1397.         }
  1398.         else{
  1399.             return map.getNamedItemNS(namespace, attributeName);
  1400.         }
  1401.     }
  1402.    
  1403. }