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

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import javax.microedition.io.file.FileConnection;

import org.javarosa.core.model.FormDef;
import org.javarosa.xform.parse.XFormParser;

/**
 * Static Utility methods pertaining to XForms.
 *  
 * @author <NAME>
 *
 */
public class XFormUtils {
	public static FormDef getFormFromResource (String resource) {
		InputStream is = System.class.getResourceAsStream(resource);
		return getFormFromInputStream(is);
	}
	
	// 18 Aug 2008 added by <NAME>
	// #if app.usefileconnections
	public static FormDef getFormFromFile(FileConnection fc) {
		InputStream fis = null;
		try {
			fis = fc.openInputStream();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return XFormUtils.getFormFromInputStream(fis);
	}
	// #endif
	
	private static FormDef getFormFromInputStream(InputStream is) {
		FormDef returnForm = null;
		InputStreamReader isr = new InputStreamReader(is);
		if(isr != null) {
			returnForm = XFormParser.getFormDef(isr);
		}
		try {
			isr.close();
		}
		catch(IOException e) {
			System.err.println("IO Exception while closing stream.");
			e.printStackTrace();
		}
		return returnForm;
	}
}<commit_msg>[r1085] [branches/dev] add the getformFromSerializedResouce method<commit_after>package org.javarosa.xform.util;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import javax.microedition.io.file.FileConnection;

import org.javarosa.core.model.FormDef;
import org.javarosa.core.model.QuestionDef;
import org.javarosa.core.model.instance.DataModelTree;
import org.javarosa.core.model.utils.PrototypeFactory;
import org.javarosa.core.util.UnavailableExternalizerException;
import org.javarosa.xform.parse.XFormParser;

/**
 * Static Utility methods pertaining to XForms.
 *
 * @author <NAME>
 *
 */
public class XFormUtils {
	public static FormDef getFormFromResource (String resource) {
		InputStream is = System.class.getResourceAsStream(resource);
		return getFormFromInputStream(is);
	}

	// 18 Aug 2008 added by <NAME>
	// #if app.usefileconnections
	public static FormDef getFormFromFile(FileConnection fc) {
		InputStream fis = null;
		try {
			fis = fc.openInputStream();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return XFormUtils.getFormFromInputStream(fis);
	}
	// #endif

	private static FormDef getFormFromInputStream(InputStream is) {
		FormDef returnForm = null;
		InputStreamReader isr = new InputStreamReader(is);
		if(isr != null) {
			returnForm = XFormParser.getFormDef(isr);
		}
		try {
			isr.close();
		}
		catch(IOException e) {
			System.err.println("IO Exception while closing stream.");
			e.printStackTrace();
		}
		return returnForm;
	}

	public static FormDef getFormFromSerializedResource(String resource) {
		FormDef returnForm = new FormDef();
		QuestionDef handy = new QuestionDef();
		PrototypeFactory modelFactory = new PrototypeFactory();
		modelFactory.addNewPrototype(new DataModelTree().getClass().getName(), new DataModelTree().getClass());
		returnForm.setModelFactory(modelFactory);
		InputStream is = System.class.getResourceAsStream(resource);
		try {
			if(is != null) {
				DataInputStream dis = new DataInputStream(is);
				returnForm.readExternal(dis);
				dis.close();
				is.close();
			}else{
				//#if debug.output==verbose
				System.out.println("ResourceStream NULL");
				//#endif
			}
		}
		catch(IOException e) {
			System.err.println("IO Exception while closing stream.");
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnavailableExternalizerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return returnForm;

	}
}
{'fraction_non_alphanumeric': 0.11191245958716738, 'fraction_numerical': 0.003979109674210396, 'mean_word_length': 4.313077939233818, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '4891765', 'n_tokens_mistral': 1369, 'n_tokens_neox': 1224, 'n_words': 278}