<commit_before>package org.javarosa.model.xform;

import java.io.DataOutputStream;
import java.util.Enumeration;
import java.util.Hashtable;

import org.javarosa.core.model.DataModelTree;
import org.javarosa.core.model.IAnswerDataSerializer;
import org.javarosa.core.model.IFormDataModel;
import org.javarosa.core.model.QuestionDataElement;
import org.javarosa.core.model.QuestionDataGroup;
import org.javarosa.core.model.TreeElement;
import org.javarosa.core.model.utils.IDataModelSerializingVisitor;
import org.javarosa.core.model.utils.ITreeVisitor;
import org.javarosa.xform.util.JavaRosaXformsParser;
import org.kxml2.kdom.Document;
import org.kxml2.kdom.Element;
import org.kxml2.kdom.Node;

public class XFormSerializingVisitor implements IDataModelSerializingVisitor, ITreeVisitor {

	Document theXmlDoc;
	
	Hashtable parentList;
	
	IAnswerDataSerializer serializer;
	
	
	public void visit(DataModelTree tree) {
		theXmlDoc = new Document();
		tree.accept(this);
		parentList = new Hashtable();
		parentList.put(tree.getRootElement(), theXmlDoc);
	}

	public void visit(QuestionDataElement element) {
		
		//First create the textual element for this question data
		Element text = new Element();
		text.setName(element.getName()); 
		//(I think that the below is right. I could be very wrong, and it could
		//require us to create a new element, instead of throwing the string in)
		Object serializedAnswerData = serializer.serializeAnswerData(element.getValue());
		if(serializedAnswerData.getClass() == String.class) {
			text.addChild(Element.TEXT, serializedAnswerData);
		} else if(serializedAnswerData.getClass() == Element.class) {
			text.addChild(Element.ELEMENT, serializedAnswerData);
			
		}

		//Attach to parent
		Node parentNode = (Node)parentList.get(element);
		
		if(parentNode != null) {
			parentNode.addChild(Element.ELEMENT, text);
		}
	}

	public void visit(QuestionDataGroup element) {
		Element thisNode = new Element();
		thisNode.setName(element.getName());
		
		Node parentNode = (Node)parentList.get(element);
		
		if(parentNode != null) {
			parentNode.addChild(Element.ELEMENT, thisNode);
		}
		
		Enumeration en = element.getChildren().elements();
		while (en.hasMoreElements()){ 
			parentList.put(en.nextElement(), thisNode);
		}
	}

	public void visit(TreeElement element) {
		// TODO Auto-generated method stub
		
	}

	public DataOutputStream serializeDataModel(IFormDataModel model) {
		model.accept(this);
		if(theXmlDoc != null) {
			DataOutputStream dos = new DataOutputStream(JavaRosaXformsParser.fromDoc2Stream(theXmlDoc));
			return dos;
		}
		else {
			return null;
		}
	}

	public void visit(IFormDataModel dataModel) {
		if(dataModel.getClass() == DataModelTree.class) {
			((DataModelTree)dataModel).accept(this); 
		}
	}
	
	public void setAnswerDataSerializer(IAnswerDataSerializer ads) {
		this.serializer = ads;
	}
}
<commit_msg>[r596] [branches/dev] Documentation is best!<commit_after>package org.javarosa.model.xform;

import java.io.DataOutputStream;
import java.util.Enumeration;
import java.util.Hashtable;

import org.javarosa.core.model.DataModelTree;
import org.javarosa.core.model.IAnswerDataSerializer;
import org.javarosa.core.model.IFormDataModel;
import org.javarosa.core.model.QuestionDataElement;
import org.javarosa.core.model.QuestionDataGroup;
import org.javarosa.core.model.TreeElement;
import org.javarosa.core.model.utils.IDataModelSerializingVisitor;
import org.javarosa.core.model.utils.ITreeVisitor;
import org.javarosa.xform.util.JavaRosaXformsParser;
import org.kxml2.kdom.Document;
import org.kxml2.kdom.Element;
import org.kxml2.kdom.Node;

/**
 * A visitor class which walks a DataModelTree and constructs an XML document
 * containing its instance.
 * 
 * The XML node elements are constructed in a depth-first manner, consistent with
 * standard XML document parsing.
 * 
 * @author <NAME>
 *
 */
public class XFormSerializingVisitor implements IDataModelSerializingVisitor, ITreeVisitor {

	/** The XML document containing the instance that is to be returned */
	Document theXmlDoc;
	
	/** A hashtable linking TreeElements to the xml element that they should add themselves to */
	Hashtable parentList;
	
	/** The serializer to be used in constructing XML for AnswerData elements */
	IAnswerDataSerializer serializer;
	
	/*
	 * (non-Javadoc)
	 * @see org.javarosa.core.model.utils.ITreeVisitor#visit(org.javarosa.core.model.DataModelTree)
	 */
	public void visit(DataModelTree tree) {
		theXmlDoc = new Document();
		tree.accept(this);
		parentList = new Hashtable();
		parentList.put(tree.getRootElement(), theXmlDoc);
	}

	/*
	 * (non-Javadoc)
	 * @see org.javarosa.core.model.utils.ITreeVisitor#visit(org.javarosa.core.model.QuestionDataElement)
	 */
	public void visit(QuestionDataElement element) {
		
		//First create the textual element for this question data
		Element text = new Element();
		text.setName(element.getName()); 
		//(I think that the below is right. I could be very wrong, and it could
		//require us to create a new element, instead of throwing the string in)
		Object serializedAnswerData = serializer.serializeAnswerData(element.getValue());
		if(serializedAnswerData.getClass() == String.class) {
			text.addChild(Element.TEXT, serializedAnswerData);
		} else if(serializedAnswerData.getClass() == Element.class) {
			text.addChild(Element.ELEMENT, serializedAnswerData);
			
		}

		//Attach to parent
		Node parentNode = (Node)parentList.get(element);
		
		if(parentNode != null) {
			parentNode.addChild(Element.ELEMENT, text);
		}
	}

	/*
	 * (non-Javadoc)
	 * @see org.javarosa.core.model.utils.ITreeVisitor#visit(org.javarosa.core.model.QuestionDataGroup)
	 */
	public void visit(QuestionDataGroup element) {
		Element thisNode = new Element();
		thisNode.setName(element.getName());
		
		Node parentNode = (Node)parentList.get(element);
		
		if(parentNode != null) {
			parentNode.addChild(Element.ELEMENT, thisNode);
		}
		
		Enumeration en = element.getChildren().elements();
		while (en.hasMoreElements()){ 
			parentList.put(en.nextElement(), thisNode);
		}
	}

	/*
	 * (non-Javadoc)
	 * @see org.javarosa.core.model.utils.ITreeVisitor#visit(org.javarosa.core.model.TreeElement)
	 */
	public void visit(TreeElement element) {
		// TODO Auto-generated method stub
		
	}

	/*
	 * (non-Javadoc)
	 * @see org.javarosa.core.model.utils.IDataModelSerializingVisitor#serializeDataModel(org.javarosa.core.model.IFormDataModel)
	 */
	public DataOutputStream serializeDataModel(IFormDataModel model) {
		model.accept(this);
		if(theXmlDoc != null) {
			DataOutputStream dos = new DataOutputStream(JavaRosaXformsParser.fromDoc2Stream(theXmlDoc));
			return dos;
		}
		else {
			return null;
		}
	}

	/*
	 * (non-Javadoc)
	 * @see org.javarosa.core.model.utils.IDataModelVisitor#visit(org.javarosa.core.model.IFormDataModel)
	 */
	public void visit(IFormDataModel dataModel) {
		if(dataModel.getClass() == DataModelTree.class) {
			((DataModelTree)dataModel).accept(this); 
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.javarosa.core.model.utils.IDataModelSerializingVisitor#setAnswerDataSerializer(org.javarosa.core.model.IAnswerDataSerializer)
	 */
	public void setAnswerDataSerializer(IAnswerDataSerializer ads) {
		this.serializer = ads;
	}
}

{'fraction_non_alphanumeric': 0.10865217970937209, 'fraction_numerical': 0.0014664711371817091, 'mean_word_length': 3.9355263157894735, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 7}, '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': '4232829', 'n_tokens_mistral': 2566, 'n_tokens_neox': 2331, 'n_words': 464}