<commit_before>/**
 * http://surenpi.com
 */
package org.suren.autotest.web.framework.data;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.List;

import org.apache.poi.util.IOUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.VisitorSupport;
import org.dom4j.io.SAXReader;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.suren.autotest.web.framework.core.ui.Text;
import org.suren.autotest.web.framework.page.Page;

/**
 * @author suren
 * @date Jul 17, 2016 8:56:51 AM
 */
@Component("xml_data_source")
public class XmlDataSource implements DataSource
{
	private Page page;

	public boolean loadData(DataResource resource, Page page)
	{
		this.page = page;
		URL url = resource.getUrl();
		if(url == null)
		{
			return false;
		}
		
		InputStream inputStream = null;
		try
		{
			inputStream = url.openStream();
			
			parse(inputStream);
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		catch (DocumentException e)
		{
			e.printStackTrace();
		}
		finally
		{
			IOUtils.closeQuietly(inputStream);
		}
		
		return false;
	}

	/**
	 * @param inputStream
	 * @throws DocumentException 
	 */
	private void parse(InputStream inputStream) throws DocumentException
	{
		Document document = new SAXReader().read(inputStream);

		parse(document);
	}
	
	private void parse(Document doc)
	{
		String pageClass = page.getClass().getName();
		List<Element> dataSourceList = doc.selectNodes(
				String.format("/dataSources/dataSource[@pageClass='%s']/page", pageClass));
		if (dataSourceList == null)
		{
			throw new RuntimeException("can not found datasource config.");
		}
		
		for(Element dataSource : dataSourceList)
		{
			dataSource.accept(new VisitorSupport()
			{

				@Override
				public void visit(Element node)
				{
					if(!"field".equals(node.getName()))
					{
						return;
					}
					
					String fieldName = node.attributeValue("name");
					String value = node.attributeValue("data");

					Method getterMethod = BeanUtils.findMethod(page.getClass(),<commit_msg>数据源解析修改<commit_after>/**
 * http://surenpi.com
 */
package org.suren.autotest.web.framework.data;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.List;

import org.apache.poi.util.IOUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.VisitorSupport;
import org.dom4j.XPath;
import org.dom4j.io.SAXReader;
import org.dom4j.xpath.DefaultXPath;
import org.jaxen.SimpleNamespaceContext;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.suren.autotest.web.framework.core.ui.Text;
import org.suren.autotest.web.framework.page.Page;

/**
 * @author suren
 * @date Jul 17, 2016 8:56:51 AM
 */
@Component("xml_data_source")
public class XmlDataSource implements DataSource
{
	private Page page;

	public boolean loadData(DataResource resource, Page page)
	{
		this.page = page;
		URL url = resource.getUrl();
		if(url == null)
		{
			return false;
		}
		
		InputStream inputStream = null;
		try
		{
			inputStream = url.openStream();
			
			parse(inputStream);
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		catch (DocumentException e)
		{
			e.printStackTrace();
		}
		finally
		{
			IOUtils.closeQuietly(inputStream);
		}
		
		return false;
	}

	/**
	 * @param inputStream
	 * @throws DocumentException 
	 */
	private void parse(InputStream inputStream) throws DocumentException
	{
		Document document = new SAXReader().read(inputStream);

		parse(document);
	}
	
	private void parse(Document doc)
	{
		String pageClass = page.getClass().getName();
		SimpleNamespaceContext simpleNamespaceContext = new SimpleNamespaceContext();
		simpleNamespaceContext.addNamespace("ns", "http://surenpi.com");
		
		XPath xpath = new DefaultXPath(String.format("/ns:dataSources/ns:dataSource[@pageClass='%s']/ns:page", pageClass));
		xpath.setNamespaceContext(simpleNamespaceContext);
		List<Element> dataSourceList = xpath.selectNodes(doc);
		if (dataSourceList == null)
		{
			throw new RuntimeException("can not found datasource config.");
		}
		
		for(Element dataSource : dataSourceList)
		{
			dataSource.accept(new VisitorSupport()
			{

				@Override
				public void visit(Element node)
				{
					if(!"field".equals(node.getName()))
					{
						return;
					}
					
					String fieldName = node.attributeValue("name");
					String value = node.attributeValue("data");

					Method getterMethod = BeanUtils.findMethod(page.getClass(),
{'fraction_non_alphanumeric': 0.1214759535655058, 'fraction_numerical': 0.007048092868988391, 'mean_word_length': 4.036534446764092, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '4231534', 'n_tokens_mistral': 1765, 'n_tokens_neox': 1521, 'n_words': 305}