<commit_before>/**
 * 
 */
package org.commcare.xml;

import java.io.IOException;
import java.util.Stack;
import java.util.Vector;

import org.commcare.suite.model.Detail;
import org.commcare.suite.model.Filter;
import org.commcare.suite.model.Text;
import org.commcare.xml.util.InvalidStructureException;
import org.javarosa.core.model.instance.FormInstance;
import org.javarosa.core.model.instance.TreeElement;
import org.kxml2.io.KXmlParser;
import org.xmlpull.v1.XmlPullParserException;

/**
 * @author ctsims
 *
 */
public class DetailParser extends ElementParser<Detail> {

	public DetailParser(KXmlParser parser) {
		super(parser);
	}

	public Detail parse() throws InvalidStructureException, IOException, XmlPullParserException {
		checkNode("detail");
		
			String id = parser.getAttributeValue(null,"id");
			
			//First fetch the title
			getNextTagInBlock("detail");
			//inside title, should be a text node as the child
			checkNode("title");
			getNextTagInBlock("title");
			Text title = new TextParser(parser).parse();
			
			getNextTagInBlock("detail");
			
			Filter filter = Filter.EmptyFilter();
			
			if(parser.getName().toLowerCase().equals("filter")) {
				filter = new FilterParser(parser).parse();
				getNextTagInBlock("detail");
			}
			
			//Now the model
			FormInstance model = parseModel();
			
			//Now get the headers and templates.
			Vector<Text> headers = new Vector<Text>();
			Vector<Text> templates = new Vector<Text>();
			Vector<Integer> headerHints = new Vector<Integer>();
			Vector<Integer> templateHints = new Vector<Integer>();
			Vector<String> headerForms = new Vector<String>();
			Vector<String> templateForms = new Vector<String>();
			
			while(nextTagInBlock("detail")) {
				checkNode("field");
				//Get the fields
				if(nextTagInBlock("field")) {
					//Header
					checkNode("header");
					
					headerHints.addElement(new Integer(getWidth()));
					
					String form = parser.getAttributeValue(null, "form");
					headerForms.addElement(form == null ? "" : form);
					
					parser.nextTag();
					checkNode("text");
					Text header = new TextParser(parser).parse();
					headers.addElement(header);
				}
				if(nextTagInBlock("field")) {
					//Template
					checkNode("template");
					
					templateHints.addElement(new Integer(getWidth()));
					
					String form = parser.getAttributeValue(null, "form");
					templateForms.addElement(form == null ? "" : form);
					
					parser.nextTag();
					checkNode("text");
					Text template = new TextParser(parser).parse();
					templates.addElement(template);
				}
			}
		
		
		
		Detail d = new Detail(id, title, model, headers, templates,filter, toArray(headerHints), toArray(templateHints), toArray(headerForms), toArray(templateForms));
		return d;
	}
	
	private int getWidth() throws InvalidStructureException {
		String width = parser.getAttributeValue(null,"width");
		if(width == null) { return -1; };
		
		//Remove the trailing % sign if any
		if(width.indexOf("%") != -1) {
			width = width.substring(0,width.indexOf("%"));
		}
		return this.parseInt(width);
	}
	
	private int[] toArray(Vector<Integer> vector) {
		int[] ret = new int[vector.size()];
		for(int i = 0; i < ret.length ; ++i) {
			ret[i] = vector.elementAt(i).intValue();
		}
		return ret;
	}
	
	private String[] toArray(Vector<String> vector) {
		String[] ret = new String[vector.size()];
		for(int i = 0; i < ret.length ; ++i) {
			if(vector.elementAt(i).equals("")) {
				ret[i] = null;
			} else {
				ret[i] = vector.elementAt(i);
			}
		}
		return ret;
	}
	
	private FormInstance parseModel() throws InvalidStructureException, IOException, XmlPullParserException {
		checkNode("model");
		int startingDepth = parser.getDepth();
		int currentDepth = 1;
		Stack<TreeElement> parents = new Stack<TreeElement>();
		
		//Navigate to the first element
		nextTagInBlock("model");
		TreeElement root = new TreeElement(parser.getName());
		for(int i = 0 ; i < parser.getAttributeCount(); ++i) {<commit_msg>Fixed Generic Type erasure method dispatch problem.<commit_after>/**
 * 
 */
package org.commcare.xml;

import java.io.IOException;
import java.util.Stack;
import java.util.Vector;

import org.commcare.suite.model.Detail;
import org.commcare.suite.model.Filter;
import org.commcare.suite.model.Text;
import org.commcare.xml.util.InvalidStructureException;
import org.javarosa.core.model.instance.FormInstance;
import org.javarosa.core.model.instance.TreeElement;
import org.kxml2.io.KXmlParser;
import org.xmlpull.v1.XmlPullParserException;

/**
 * @author ctsims
 *
 */
public class DetailParser extends ElementParser<Detail> {

	public DetailParser(KXmlParser parser) {
		super(parser);
	}

	public Detail parse() throws InvalidStructureException, IOException, XmlPullParserException {
		checkNode("detail");
		
			String id = parser.getAttributeValue(null,"id");
			
			//First fetch the title
			getNextTagInBlock("detail");
			//inside title, should be a text node as the child
			checkNode("title");
			getNextTagInBlock("title");
			Text title = new TextParser(parser).parse();
			
			getNextTagInBlock("detail");
			
			Filter filter = Filter.EmptyFilter();
			
			if(parser.getName().toLowerCase().equals("filter")) {
				filter = new FilterParser(parser).parse();
				getNextTagInBlock("detail");
			}
			
			//Now the model
			FormInstance model = parseModel();
			
			//Now get the headers and templates.
			Vector<Text> headers = new Vector<Text>();
			Vector<Text> templates = new Vector<Text>();
			Vector<Integer> headerHints = new Vector<Integer>();
			Vector<Integer> templateHints = new Vector<Integer>();
			Vector<String> headerForms = new Vector<String>();
			Vector<String> templateForms = new Vector<String>();
			
			while(nextTagInBlock("detail")) {
				checkNode("field");
				//Get the fields
				if(nextTagInBlock("field")) {
					//Header
					checkNode("header");
					
					headerHints.addElement(new Integer(getWidth()));
					
					String form = parser.getAttributeValue(null, "form");
					headerForms.addElement(form == null ? "" : form);
					
					parser.nextTag();
					checkNode("text");
					Text header = new TextParser(parser).parse();
					headers.addElement(header);
				}
				if(nextTagInBlock("field")) {
					//Template
					checkNode("template");
					
					templateHints.addElement(new Integer(getWidth()));
					
					String form = parser.getAttributeValue(null, "form");
					templateForms.addElement(form == null ? "" : form);
					
					parser.nextTag();
					checkNode("text");
					Text template = new TextParser(parser).parse();
					templates.addElement(template);
				}
			}
		
		
		
		Detail d = new Detail(id, title, model, headers, templates,filter, toIntArray(headerHints), toIntArray(templateHints), toStringArray(headerForms), toStringArray(templateForms));
		return d;
	}
	
	private int getWidth() throws InvalidStructureException {
		String width = parser.getAttributeValue(null,"width");
		if(width == null) { return -1; };
		
		//Remove the trailing % sign if any
		if(width.indexOf("%") != -1) {
			width = width.substring(0,width.indexOf("%"));
		}
		return this.parseInt(width);
	}
	
	private int[] toIntArray(Vector<Integer> vector) {
		int[] ret = new int[vector.size()];
		for(int i = 0; i < ret.length ; ++i) {
			ret[i] = vector.elementAt(i).intValue();
		}
		return ret;
	}
	
	private String[] toStringArray(Vector<String> vector) {
		String[] ret = new String[vector.size()];
		for(int i = 0; i < ret.length ; ++i) {
			if(vector.elementAt(i).equals("")) {
				ret[i] = null;
			} else {
				ret[i] = vector.elementAt(i);
			}
		}
		return ret;
	}
	
	private FormInstance parseModel() throws InvalidStructureException, IOException, XmlPullParserException {
		checkNode("model");
		int startingDepth = parser.getDepth();
		int currentDepth = 1;
		Stack<TreeElement> parents = new Stack<TreeElement>();
		
		//Navigate to the first element
		nextTagInBlock("model");
		TreeElement root = new TreeElement(parser.getName());
		for(int i = 0 ; i < parser.getAttributeCount(); ++i) {
{'fraction_non_alphanumeric': 0.1430346713060768, 'fraction_numerical': 0.002236858456567665, 'mean_word_length': 3.8307322929171668, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, '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': '1879076', 'n_tokens_mistral': 2923, 'n_tokens_neox': 2403, 'n_words': 542}