fr.aliasource.webmail.server.proxy.client.http.DOMUtils.java Source code

Java tutorial

Introduction

Here is the source code for fr.aliasource.webmail.server.proxy.client.http.DOMUtils.java

Source

/* ***** BEGIN LICENSE BLOCK *****
 * Version: GPL 2.0
 *
 * The contents of this file are subject to the GNU General Public
 * License Version 2 or later (the "GPL").
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Initial Developer of the Original Code is
 *   MiniG.org project members
 *
 * ***** END LICENSE BLOCK ***** */

package fr.aliasource.webmail.server.proxy.client.http;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.xml.sax.SAXException;

/**
 * Utility methods to extract data from a DOM.
 * 
 * @author Thomas Cataldo
 * 
 */
public final class DOMUtils {

    private static final Log logger = LogFactory.getLog(DOMUtils.class);

    public static String getElementText(Element root, String elementName) {
        NodeList list = root.getElementsByTagName(elementName);
        if (list.getLength() == 0) {
            if (logger.isDebugEnabled()) {
                logger.debug("No element named '" + elementName + "' under '" + root.getNodeName() + "'");
            }
            return null;
        }
        return getElementText((Element) list.item(0));
    }

    public static String getElementText(Element node) {
        Text txtElem = (Text) node.getFirstChild();
        if (txtElem == null) {
            return null;
        }
        return txtElem.getData();
    }

    public static String[] getTexts(Element root, String elementName) {
        NodeList list = root.getElementsByTagName(elementName);
        String[] ret = new String[list.getLength()];
        for (int i = 0; i < list.getLength(); i++) {
            Text txt = (Text) list.item(i).getFirstChild();
            if (txt != null) {
                ret[i] = txt.getData();
            } else {
                ret[i] = ""; //$NON-NLS-1$
            }
        }
        return ret;
    }

    /**
     * Renvoie sous la forme d'un tableau la valeur des attributs donns pour
     * toutes les occurences d'un lment donne dans le dom
     * 
     * <code>
     *  <toto>
     *   <titi id="a" val="ba"/>
     *   <titi id="b" val="bb"/>
     *  </toto>
     * </code>
     * 
     * et getAttributes(&lt;toto&gt;, "titi", { "id", "val" }) renvoie { { "a",
     * "ba" } { "b", "bb" } }
     * 
     * @param root
     * @param elementName
     * @param wantedAttributes
     * @return
     */
    public static String[][] getAttributes(Element root, String elementName, String[] wantedAttributes) {
        NodeList list = root.getElementsByTagName(elementName);
        String[][] ret = new String[list.getLength()][wantedAttributes.length];
        for (int i = 0; i < list.getLength(); i++) {
            Element elem = (Element) list.item(i);
            for (int j = 0; j < wantedAttributes.length; j++) {
                ret[i][j] = elem.getAttribute(wantedAttributes[j]);
            }
        }
        return ret;
    }

    /**
     * Renvoie la valeur de l'attribut donn, d'un lment donn qui doit tre
     * unique sous l'lment racine
     * 
     * @param root
     * @param elementName
     * @param attribute
     * @return
     */
    public static String getElementAttribute(Element root, String elementName, String attribute) {
        NodeList list = root.getElementsByTagName(elementName);
        if (list.getLength() == 0) {
            return null;
        }
        return ((Element) list.item(0)).getAttribute(attribute);
    }

    /**
     * Renvoie une lment qui doit tre unique dans le document.
     * 
     * @param root
     * @param elementName
     * @return
     */
    public static Element getUniqueElement(Element root, String elementName) {
        NodeList list = root.getElementsByTagName(elementName);
        return (Element) list.item(0);
    }

    public static Element findElementWithUniqueAttribute(Element root, String elementName, String attribute,
            String attributeValue) {
        NodeList list = root.getElementsByTagName(elementName);
        for (int i = 0; i < list.getLength(); i++) {
            Element tmp = (Element) list.item(i);
            if (tmp.getAttribute(attribute).equals(attributeValue)) {
                return tmp;
            }
        }
        return null;
    }

    public static Element createElementAndText(Element parent, String elementName, String text) {
        if (text == null) {
            throw new NullPointerException("null text");
        }
        Element el = parent.getOwnerDocument().createElement(elementName);
        parent.appendChild(el);
        Text txt = el.getOwnerDocument().createTextNode(text);
        el.appendChild(txt);
        return el;
    }

    public static Element createElement(Element parent, String elementName) {
        Element el = parent.getOwnerDocument().createElement(elementName);
        parent.appendChild(el);
        return el;
    }

    public static void serialise(Document doc, OutputStream out) throws TransformerException {
        TransformerFactory fac = TransformerFactory.newInstance();
        Transformer tf = fac.newTransformer();
        tf.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$
        Source input = new DOMSource(doc.getDocumentElement());
        Result output = new StreamResult(out);
        tf.transform(input, output);
    }

    public static void logDom(Document doc) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            serialise(doc, out);
            System.out.println("DOM log:\n" + out.toString());
        } catch (TransformerException e) {
            e.printStackTrace();
        }
    }

    public static Document parse(InputStream is)
            throws SAXException, IOException, ParserConfigurationException, FactoryConfigurationError {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        DocumentBuilder builder = dbf.newDocumentBuilder();
        return builder.parse(is);
    }

    public static Document createDoc(String namespace, String rootElement)
            throws ParserConfigurationException, FactoryConfigurationError {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        DocumentBuilder builder = dbf.newDocumentBuilder();
        DOMImplementation di = builder.getDOMImplementation();
        return di.createDocument(namespace, rootElement, null);
    }
}