<commit_before>/**
 * The content of this file is subject to the caAdapter Software License (the "License").  
 * A copy of the License is available at:
 * [caAdapter CVS home directory]\etc\license\caAdapter_license.txt. or at:
 * http://ncicb.nci.nih.gov/infrastructure/cacore_overview/caadapter/indexContent
 * /docs/caAdapter_License
 */
package gov.nih.nci.cbiit.cmts.transform;

import java.util.Properties;

import javax.xml.xquery.XQConnection;
import javax.xml.xquery.XQException;
import javax.xml.xquery.XQItemType;
import javax.xml.xquery.XQPreparedExpression;
import javax.xml.xquery.XQResultSequence;

import javax.xml.namespace.QName;

import net.sf.saxon.xqj.SaxonXQDataSource;

/**
 * This class performs the transformation using XQuery
 *
 * @author <NAME>
 * @author LAST UPDATE $Author: linc $
 * @since     CMTS v1.0
 * @version    $Revision: 1.3 $
 * @date       $Date: 2008-10-22 19:01:17 $
 *
 */
public class XQueryTransformer {
	// Filename for XML document to query
	private String filename;
	
	// Data Source for querying
	private SaxonXQDataSource dataSource;

	// Connection for querying
	private XQConnection conn;

	// Query String
	private String queryString;
	
	// Prepared Query
	private XQPreparedExpression exp;
	
	/**
	 * constructor
	 * @throws XQException
	 */
	public XQueryTransformer() throws XQException {
		dataSource = new SaxonXQDataSource();
		conn = dataSource.getConnection();
	}

	/**
	 * @return the filename
	 */
	public final String getFilename() {
		return filename;
	}

	/**
	 * @param filename the filename to set
	 */
	public final void setFilename(String filename) {
		this.filename = filename;
	}

	/**
	 * @return the dataSource
	 */
	public final SaxonXQDataSource getDataSource() {
		return dataSource;
	}

	/**
	 * @return the conn
	 */
	public final XQConnection getConn() {
		return conn;
	}

	/**
	 * set XQuery
	 * @param queryString
	 * @throws XQException
	 */
	public void setQuery(String queryString) throws XQException {
		exp = conn.prepareExpression(queryString);
		this.queryString = queryString;
	}
	
	/**
	 * execute XQuery
	 * @return
	 * @throws XQException
	 */
	public String executeQuery() throws XQException {
		exp.bindString(new QName("docName"), filename,
				conn.createAtomicType(XQItemType.XQBASETYPE_STRING));
		XQResultSequence result = exp.executeQuery();
		return result.getSequenceAsString(new Properties());
	}

	/**
	 * test method
	 * @param args
	 */
	public static void main(String[] args) {
		if (args.length != 1) {
			System.err.println("Usage: java ibm.dw.xqj.XQueryTester [XML filename]");
			System.exit(-1);
		}

		try {
			String xmlFilename = args[0];
			XQueryTransformer tester= new XQueryTransformer();
			tester.setFilename(xmlFilename);

			final String sep = System.getProperty("line.separator");
			String queryString =
				"declare variable $docName as xs:string external;" + sep +
				"      for $cd in doc($docName)/CATALOG/CD " +
				"    where $cd/YEAR > 1980 " +
				"      and $cd/COUNTRY = 'USA' " +
				" order by $cd/YEAR " +
				"   return " +
				"<cd><title>{$cd/TITLE/text()}</title>" +
				" <year>{$cd/YEAR/text()}</year></cd>";
			tester.setQuery(queryString);
			System.out.println(tester.executeQuery());
		} catch (Exception e) {
			e.printStackTrace(System.err);
			System.err.println(e.getMessage());
		}<commit_msg>clean codes<commit_after>/**
 * The content of this file is subject to the caAdapter Software License (the "License").  
 * A copy of the License is available at:
 * [caAdapter CVS home directory]\etc\license\caAdapter_license.txt. or at:
 * http://ncicb.nci.nih.gov/infrastructure/cacore_overview/caadapter/indexContent
 * /docs/caAdapter_License
 */
package gov.nih.nci.cbiit.cmts.transform;

import java.util.Properties;

import javax.xml.xquery.XQConnection;
import javax.xml.xquery.XQException;
import javax.xml.xquery.XQItemType;
import javax.xml.xquery.XQPreparedExpression;
import javax.xml.xquery.XQResultSequence;

import javax.xml.namespace.QName;

import net.sf.saxon.Configuration;
import net.sf.saxon.xqj.SaxonXQDataSource;

/**
 * This class performs the transformation using XQuery
 *
 * @author <NAME>
 * @author LAST UPDATE $Author: linc $
 * @since     CMTS v1.0
 * @version    $Revision: 1.3 $
 * @date       $Date: 2008-10-22 19:01:17 $
 *
 */
public class XQueryTransformer {
	// Filename for XML document to query
	private String sourceFileName;
	
	// Data Source for querying
//	private SaxonXQDataSource dataSource;

	// Connection for querying
	private XQConnection conn;

	// Query String
	private String queryString;
//	
//	// Prepared Query
//	private XQPreparedExpression exp;
	
	/**
	 * constructor
	 * @throws XQException
	 */
	public XQueryTransformer() throws XQException {
		Configuration  saxonConfig=new Configuration();
		SaxonXQDataSource dataSource = new SaxonXQDataSource(saxonConfig);
		conn = dataSource.getConnection();
	}

	/**
	 * @return the filename
	 */
	public final String getFilename() {
		return sourceFileName;
	}

	/**
	 * @param filename the filename to set
	 */
	public final void setSourceFileName(String filename) {
		this.sourceFileName = filename;
	}

	/**
	 * @return the conn
	 */
	public final XQConnection getConn() {
		return conn;
	}

	/**
	 * set XQuery
	 * @param queryString
	 * @throws XQException
	 */
	public void setQuery(String query) throws XQException {
		queryString=query;
	}
	
	/**
	 * execute XQuery with pre-set query and source file
	 * @see #executeQuery(String, String)
	 * @return
	 * @throws XQException
	 */
	public String executeQuery() throws XQException {
		return executeQuery(queryString, sourceFileName);
	}

	/**
	 * execute XQuery
	 * @param query query string to execute
	 * @param sourceFile name of source file
	 * @return
	 * @throws XQException
	 */
	public String executeQuery(String query, String sourceFile) throws XQException {
		XQPreparedExpression exp=conn.prepareExpression(query);
		exp.bindString(new QName("docName"), sourceFile,
				conn.createAtomicType(XQItemType.XQBASETYPE_STRING));
		XQResultSequence result = exp.executeQuery();
		return result.getSequenceAsString(new Properties());
	}
	
	/**
	 * test method
	 * @param args
	 */
	public static void main(String[] args) {
		if (args.length != 1) {
			System.err.println("Usage: java ibm.dw.xqj.XQueryTester [XML filename]");
			System.exit(-1);
		}

		try {
			String xmlFilename = args[0];
			XQueryTransformer tester= new XQueryTransformer();
			tester.setSourceFileName(xmlFilename);

			final String sep = System.getProperty("line.separator");
			String queryString =
				"declare variable $docName as xs:string external;" + sep +
				"      for $cd in doc($docName)/CATALOG/CD " +
				"    where $cd/YEAR > 1980 " +
				"      and $cd/COUNTRY = 'USA' " +
				" order by $cd/YEAR " +
				"   return " +
				"<cd><title>{$cd/TITLE/text()}</title>" +
				" <year>{$cd/YEAR/text()}</year></cd>";
			tester.setQuery(queryString);
			System.out.println(tester.executeQuery());
		} catch (Exception e) {
			e.printStackTrace(System.err);
			System.err.println(e.getMessage());
		}
{'fraction_non_alphanumeric': 0.12378538234051542, 'fraction_numerical': 0.007041261794113505, 'mean_word_length': 3.6509495743287492, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 16}, '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': '1794901', 'n_tokens_mistral': 2518, 'n_tokens_neox': 2245, 'n_words': 552}