<commit_before>/**
 *  Copyright (C) 2004 Orbeon, Inc.
 *
 *  This program is free software; you can redistribute it and/or modify it under the terms of the
 *  GNU Lesser General Public License as published by the Free Software Foundation; either version
 *  2.1 of the License, or (at your option) any later version.
 *
 *  This program 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 Lesser General Public License for more details.
 *
 *  The full text of the license is available at http://www.gnu.org/copyleft/lesser.html
 */
package org.orbeon.oxf.xml.dom4j;

import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.Attribute;
import org.dom4j.io.SAXReader;
import org.dom4j.util.UserDataElement;
import org.dom4j.util.UserDataAttribute;
import org.orbeon.oxf.xml.XMLUtils;
import org.orbeon.oxf.transformer.xupdate.statement.Text;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import java.util.Iterator;
import java.util.Map;

import javax.xml.parsers.SAXParser;

public class Dom4jUtils {

    /**
     * Clone a node, making sure that we copy all the declared namespace of
     * the source.
     */
    public static Node cloneNode(Node node) {
        if (node instanceof Element) {
            Element originalElement = (Element) node;
            Map namespaceContext = XMLUtils.getNamespaceContext(originalElement);
            Element resultElement = (Element) cloneNodeWorker(originalElement);
            for (Iterator i = namespaceContext.keySet().iterator(); i.hasNext();) {
                String prefix = (String) i.next();
                if (resultElement.getNamespaceForPrefix(prefix) == null)
                    resultElement.addNamespace(prefix, (String) namespaceContext.get(prefix));
            }
            return resultElement;
        } else {
            return cloneNodeWorker(node);
        }
    }
    /**
     * Replacment for DocumentHelper.parseText.  DocumentHelper.parseText is creates work for GC
     * because it relies on JAXP. 
     */
    public static org.dom4j.Document parseText( final String s ) 
    throws SAXException, DocumentException {
        final SAXParser sxPrs = XMLUtils.newSAXParser();
        final XMLReader xr = sxPrs.getXMLReader();
        final SAXReader sxRdr = new SAXReader( xr );
        final java.io.StringReader strRdr = new java.io.StringReader( s );
        final org.dom4j.Document ret = sxRdr.read( strRdr );
        return ret;
    }

    private static Node cloneNodeWorker(Node node) {
        if (node instanceof UserDataElement) {
            UserDataElement current = (UserDataElement) node;
            UserDataElement clone = new UserDataElement(current.getQName());
            clone.setData(current.getData());

            // Copy attributes
            for (Iterator i = current.attributes().iterator(); i.hasNext();) {
                Attribute attribute = (Attribute) i.next();
                clone.add(cloneNodeWorker(attribute));
            }

            // Copy content
            for (Iterator i = current.content().iterator(); i.hasNext();) {
                Node child = (Node) i.next();
                clone.add(cloneNodeWorker(child));
            }
            return clone;
        } else if (node instanceof UserDataAttribute) {
            UserDataAttribute current = (UserDataAttribute) node;
            UserDataAttribute clone = new UserDataAttribute(current.getQName(), current.getText());
            clone.setData(current.getData());
            return clone;
        } else {
            return (Node) node.clone();
        }
    }

    /**
     * Removes the elements and text inside the given element, but not the attributes or
     * namespace declarations on the element.
     */
    public static void clearElementContent(Element element) {
        for (Iterator j = element.content().iterator(); j.hasNext();) {
            Node child = (Node) j.next();
            if (child instanceof Text || child instanceof Element)
                element.remove(child);
        }
    }
}<commit_msg>Fix xforms test.   Was wrong import.<commit_after>/**
 *  Copyright (C) 2004 Orbeon, Inc.
 *
 *  This program is free software; you can redistribute it and/or modify it under the terms of the
 *  GNU Lesser General Public License as published by the Free Software Foundation; either version
 *  2.1 of the License, or (at your option) any later version.
 *
 *  This program 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 Lesser General Public License for more details.
 *
 *  The full text of the license is available at http://www.gnu.org/copyleft/lesser.html
 */
package org.orbeon.oxf.xml.dom4j;

import org.dom4j.io.SAXReader;
import org.dom4j.util.UserDataElement;
import org.dom4j.util.UserDataAttribute;
import org.orbeon.oxf.xml.XMLUtils;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import java.util.Iterator;
import java.util.Map;

import javax.xml.parsers.SAXParser;

public class Dom4jUtils {

    /**
     * Clone a node, making sure that we copy all the declared namespace of
     * the source.
     */
    public static org.dom4j.Node cloneNode(org.dom4j.Node node) {
        if (node instanceof org.dom4j.Element) {
            org.dom4j.Element originalElement = (org.dom4j.Element) node;
            Map namespaceContext = XMLUtils.getNamespaceContext(originalElement);
            org.dom4j.Element resultElement = (org.dom4j.Element) cloneNodeWorker(originalElement);
            for (Iterator i = namespaceContext.keySet().iterator(); i.hasNext();) {
                String prefix = (String) i.next();
                if (resultElement.getNamespaceForPrefix(prefix) == null)
                    resultElement.addNamespace(prefix, (String) namespaceContext.get(prefix));
            }
            return resultElement;
        } else {
            return cloneNodeWorker(node);
        }
    }
    /**
     * Replacment for DocumentHelper.parseText.  DocumentHelper.parseText is creates work for GC
     * because it relies on JAXP. 
     */
    public static org.dom4j.Document parseText( final String s ) 
    throws SAXException, org.dom4j.DocumentException {
        final SAXParser sxPrs = XMLUtils.newSAXParser();
        final XMLReader xr = sxPrs.getXMLReader();
        final SAXReader sxRdr = new SAXReader( xr );
        final java.io.StringReader strRdr = new java.io.StringReader( s );
        final org.dom4j.Document ret = sxRdr.read( strRdr );
        return ret;
    }

    private static org.dom4j.Node cloneNodeWorker(org.dom4j.Node node) {
        if (node instanceof UserDataElement) {
            UserDataElement current = (UserDataElement) node;
            UserDataElement clone = new UserDataElement(current.getQName());
            clone.setData(current.getData());

            // Copy attributes
            for (Iterator i = current.attributes().iterator(); i.hasNext();) {
                org.dom4j.Attribute attribute = (org.dom4j.Attribute) i.next();
                clone.add(cloneNodeWorker(attribute));
            }

            // Copy content
            for (Iterator i = current.content().iterator(); i.hasNext();) {
                org.dom4j.Node child = (org.dom4j.Node) i.next();
                clone.add(cloneNodeWorker(child));
            }
            return clone;
        } else if (node instanceof UserDataAttribute) {
            UserDataAttribute current = (UserDataAttribute) node;
            UserDataAttribute clone = new UserDataAttribute(current.getQName(), current.getText());
            clone.setData(current.getData());
            return clone;
        } else {
            return (org.dom4j.Node) node.clone();
        }
    }

    /**
     * Removes the elements and text inside the given element, but not the attributes or
     * namespace declarations on the element.
     */
    public static void clearElementContent(org.dom4j.Element element) {
        for (final java.util.ListIterator j = element.content().listIterator(); j.hasNext();) {
            org.dom4j.Node child = (org.dom4j.Node) j.next();
            if (child instanceof org.dom4j.Text || child instanceof org.dom4j.Element)
                j.remove();
        }
    }
}
{'fraction_non_alphanumeric': 0.09655415735622722, 'fraction_numerical': 0.005880277549100318, 'mean_word_length': 2.4924024640657083, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 10}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3011889', 'n_tokens_mistral': 2346, 'n_tokens_neox': 2304, 'n_words': 680}