/*
 * Copyright 
 *   2005 axYus - www.axyus.com
 *   2005 C.Marchand - christophe.marchand@axyus.com
 * 
 * This file is part of XEMELIOS.
 * 
 * XEMELIOS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * XEMELIOS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with XEMELIOS; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package fr.gouv.finances.cp.utils;

import org.apache.commons.lang.StringEscapeUtils;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import fr.gouv.finances.cp.xemelios.utils.TextWriter;


/**
 * Fournit des methodes utilitaires
 * @author chm
 * @deprecated
 */
public class XmlUtils {
//    private static Logger logger = Logger.getLogger(XmlUtils.class);

    /**
     * Reconstruit le XML a partir du DOM
     * @param node
     * @return
     * @deprecated
     */
    @Deprecated
	public static String getXmlData(Node node) {
/*        StringBuffer sb = new StringBuffer();
        switch(node.getNodeType()) {
            case Node.COMMENT_NODE:
            case Node.ENTITY_NODE:
            case Node.ENTITY_REFERENCE_NODE:
            case Node.NOTATION_NODE:
            case Node.PROCESSING_INSTRUCTION_NODE: break;
            case Node.DOCUMENT_NODE:
            case Node.DOCUMENT_FRAGMENT_NODE:
            case Node.ELEMENT_NODE: {
                sb.append("<").append(node.getNodeName());
                StringBuffer attrs = new StringBuffer();
                StringBuffer children = new StringBuffer();
                NamedNodeMap nnm = node.getAttributes();
                if(nnm!=null) {
                    for(int i=0;i<nnm.getLength();i++) {
                        Node attr = nnm.item(i);
                        attrs.append(" ").append(getXmlData(attr));
                    }
                }
                NodeList nl = node.getChildNodes();
                if(nl!=null) {
                    for(int i=0;i<nl.getLength();i++) {
                        Node child = nl.item(i);
                        if(child.getNodeType()==Node.ATTRIBUTE_NODE) {
                            attrs.append(" ").append(getXmlData(child));
                        } else {
                            children.append(getXmlData(child));
                        }
                    }
                }
                sb.append(attrs.toString());
                if(children.length()>0) {
                    sb.append(">").append(children.toString()).append("</").append(node.getNodeName()).append(">");
                } else {
                    sb.append("/>");
                }
                break;
            }
            case Node.ATTRIBUTE_NODE: {
                sb.append(node.getNodeName()).append("=\"").append(StringEscapeUtils.escapeXml(node.getNodeValue())).append("\"");
                break;
            }
            case Node.CDATA_SECTION_NODE: {
                sb.append("<![CDATA[").append(StringEscapeUtils.escapeXml(node.getNodeValue())).append("]]>");
            }
        }
        return sb.toString(); */
        return TextWriter.toString(node);
    }

    /**
     * Reconstruit le XML a partir du DOM
     * @param node
     * @return
     * @deprecated
     */
    @Deprecated
	public static String getXmlDataSubstituteNode(Node node,String substituteWith, String substituteInWhat) {
        StringBuffer sb = new StringBuffer();
        switch(node.getNodeType()) {
            case Node.COMMENT_NODE:
            case Node.ENTITY_NODE:
            case Node.ENTITY_REFERENCE_NODE:
            case Node.NOTATION_NODE:
            case Node.PROCESSING_INSTRUCTION_NODE: break;
            case Node.DOCUMENT_NODE: 
            case Node.DOCUMENT_FRAGMENT_NODE:
            case Node.ELEMENT_NODE: {
//                logger.debug("element");
                String nodeName = node.getNodeName();
//                sb.append("<").append(nodeName);
                if(!substituteInWhat.equals(nodeName)) {
                    sb.append("<").append(nodeName);
	                StringBuffer attrs = new StringBuffer();
	                StringBuffer children = new StringBuffer();
	                NamedNodeMap nnm = node.getAttributes();
	                if(nnm!=null) {
	                    for(int i=0;i<nnm.getLength();i++) {
	                        Node attr = nnm.item(i);
	                        attrs.append(" ").append(getXmlDataSubstituteNode(attr,substituteWith,substituteInWhat));
	                    }
	                }
	                NodeList nl = node.getChildNodes();
	                if(nl!=null) {
	                    for(int i=0;i<nl.getLength();i++) {
	                        Node child = nl.item(i);
	                        if(child.getNodeType()==Node.ATTRIBUTE_NODE) {
	                            attrs.append(" ").append(getXmlDataSubstituteNode(child,substituteWith,substituteInWhat));
	                        } else {
	                            children.append(getXmlDataSubstituteNode(child,substituteWith,substituteInWhat));
	                        }
	                    }
	                }
	                sb.append(attrs.toString());
	                if(children.length()>0) {
	                    sb.append(">").append(children.toString()).append("</").append(nodeName).append(">");
	                } else {
	                    sb.append("/>");
	                }
                } else {
//                    sb.append(">").append(substituteWith).append("</").append(nodeName).append(">");
                    sb.append(substituteWith);
                }
                break;
            }
            case Node.ATTRIBUTE_NODE: {
//                logger.debug("attribut");
                sb.append(node.getNodeName()).append("=\"").append(StringEscapeUtils.escapeXml(node.getNodeValue())).append("\"");
                break;
            }
            case Node.CDATA_SECTION_NODE: {
                sb.append("<![CDATA[").append(StringEscapeUtils.escapeXml(node.getNodeValue())).append("]]>");
            }
            case Node.TEXT_NODE: {
                sb.append(StringEscapeUtils.escapeXml(node.getNodeValue()));
            }
        }
        return sb.toString();
    }
}
