<commit_before>/***************************************************************************
 *   Copyright (C) 2008 by <NAME>                                *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Library General Public License as       *
 *   published by the Free Software Foundation; either version 2 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 General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU Library General Public     *
 *   License along with this program; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 *                                                                         *
 *   For details about the authors of this software, see the AUTHORS file. *
 ***************************************************************************/

package jolie.xml;

import com.sun.xml.xsom.XSAttributeUse;
import com.sun.xml.xsom.XSComplexType;
import com.sun.xml.xsom.XSContentType;
import com.sun.xml.xsom.XSElementDecl;
import com.sun.xml.xsom.XSModelGroup;
import com.sun.xml.xsom.XSModelGroupDecl;
import com.sun.xml.xsom.XSParticle;
import com.sun.xml.xsom.XSTerm;
import com.sun.xml.xsom.XSType;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import jolie.lang.Constants;
import jolie.runtime.Value;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import jolie.runtime.ValueVector;

/**
 * Utilities for interactions and transformations with XML.
 * @author <NAME>
 */
public class XmlUtils
{
	/**
	 * Transforms a jolie.Value object to an XML Document instance.
	 * @see Document
	 * @param value the source Value
	 * @param rootNodeName the name to give to the root node of the document
	 * @param document the XML document receiving the transformation
	 */
	public static void valueToDocument( Value value, String rootNodeName, Document document )
	{
		Element root = document.createElement( rootNodeName );
		document.appendChild( root );
		_valueToDocument( value, root, document );
	}

	/**
	 * Transforms a jolie.Value object to an XML Document instance following a given XML Type Definition.
	 * @see Document
	 * @param value the source Value
	 * @param rootNodeName the name to give to the root node of the document.
	 * @param document the XML document receiving the transformation.
	 * @param type the XML type definition to follow in writing the XML document.
	 */
	public static void valueToDocument( Value value, String rootNodeName, Document document, XSType type )
	{
		Element root = document.createElement( rootNodeName );
		document.appendChild( root );
		_valueToDocument( value, root, document, type );
	}

	private static void _valueToDocument( Value value, Element element, Document doc, XSModelGroup modelGroup )
	{
		String name;
		XSModelGroup.Compositor compositor = modelGroup.getCompositor();
		if ( compositor.equals( XSModelGroup.SEQUENCE ) ) {
			XSParticle[] children = modelGroup.getChildren();
			XSTerm currTerm;
			XSElementDecl currElementDecl;
			Value v;
			ValueVector vec;
			for( int i = 0; i < children.length; i++ ) {
				currTerm = children[i].getTerm();
				if ( currTerm.isElementDecl() ) {
					currElementDecl = currTerm.asElementDecl();
					name = currElementDecl.getName();
					Element childElement = null;
					if ( (vec=value.children().get( name )) != null ) {
						childElement = doc.createElement( name );
						element.appendChild( childElement );
						v = vec.remove( 0 );
						_valueToDocument( v, childElement, doc, currElementDecl.getType() );
					} else if ( children[i].getMinOccurs() > 0 ) {
						// TODO throw some error here
					}<commit_msg>Value to Typed XML array handling fix.<commit_after>/***************************************************************************
 *   Copyright (C) 2008 by <NAME>                                *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Library General Public License as       *
 *   published by the Free Software Foundation; either version 2 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 General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU Library General Public     *
 *   License along with this program; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 *                                                                         *
 *   For details about the authors of this software, see the AUTHORS file. *
 ***************************************************************************/

package jolie.xml;

import com.sun.xml.xsom.XSAttributeUse;
import com.sun.xml.xsom.XSComplexType;
import com.sun.xml.xsom.XSContentType;
import com.sun.xml.xsom.XSElementDecl;
import com.sun.xml.xsom.XSModelGroup;
import com.sun.xml.xsom.XSModelGroupDecl;
import com.sun.xml.xsom.XSParticle;
import com.sun.xml.xsom.XSTerm;
import com.sun.xml.xsom.XSType;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import jolie.lang.Constants;
import jolie.runtime.Value;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import jolie.runtime.ValueVector;

/**
 * Utilities for interactions and transformations with XML.
 * @author <NAME>
 */
public class XmlUtils
{
	/**
	 * Transforms a jolie.Value object to an XML Document instance.
	 * @see Document
	 * @param value the source Value
	 * @param rootNodeName the name to give to the root node of the document
	 * @param document the XML document receiving the transformation
	 */
	public static void valueToDocument( Value value, String rootNodeName, Document document )
	{
		Element root = document.createElement( rootNodeName );
		document.appendChild( root );
		_valueToDocument( value, root, document );
	}

	/**
	 * Transforms a jolie.Value object to an XML Document instance following a given XML Type Definition.
	 * @see Document
	 * @param value the source Value
	 * @param rootNodeName the name to give to the root node of the document.
	 * @param document the XML document receiving the transformation.
	 * @param type the XML type definition to follow in writing the XML document.
	 */
	public static void valueToDocument( Value value, String rootNodeName, Document document, XSType type )
	{
		Element root = document.createElement( rootNodeName );
		document.appendChild( root );
		_valueToDocument( value, root, document, type );
	}

	private static void _valueToDocument( Value value, Element element, Document doc, XSModelGroup modelGroup )
	{
		String name;
		XSModelGroup.Compositor compositor = modelGroup.getCompositor();
		if ( compositor.equals( XSModelGroup.SEQUENCE ) ) {
			XSParticle[] children = modelGroup.getChildren();
			XSTerm currTerm;
			XSElementDecl currElementDecl;
			Value v;
			ValueVector vec;
			for( int i = 0; i < children.length; i++ ) {
				currTerm = children[i].getTerm();
				if ( currTerm.isElementDecl() ) {
					currElementDecl = currTerm.asElementDecl();
					name = currElementDecl.getName();
					Element childElement = null;
					if ( (vec=value.children().get( name )) != null ) {
						int k = 0;
						while(
							vec.isEmpty() == false &&
							(children[i].getMaxOccurs() == XSParticle.UNBOUNDED ||
								children[i].getMaxOccurs() > k)
						) {
							childElement = doc.createElement( name );
							element.appendChild( childElement );
							v = vec.remove( 0 );
							_valueToDocument( v, childElement, doc, currElementDecl.getType() );
							k++;
						}
					} else if ( children[i].getMinOccurs() > 0 ) {
						// TODO throw some error here
					}
{'fraction_non_alphanumeric': 0.11596254355400697, 'fraction_numerical': 0.0059886759581881535, 'mean_word_length': 2.449117536612843, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 20}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 26, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '239500', 'n_tokens_mistral': 2661, 'n_tokens_neox': 2377, 'n_words': 817}