is-collector/src/org/gcube/informationsystem/collector/impl/xmlstorage/exist/XQuery.java

130 lines
3.7 KiB
Java
Executable File

package org.gcube.informationsystem.collector.impl.xmlstorage.exist;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import javax.xml.transform.OutputKeys;
import org.exist.xmldb.XQueryService;
import org.xmldb.api.base.Collection;
import org.xmldb.api.base.CompiledExpression;
import org.xmldb.api.base.ResourceSet;
import org.gcube.informationsystem.collector.impl.xmlstorage.exist.State;
import org.gcube.informationsystem.collector.impl.xmlstorage.exist.XQuery;
import org.globus.wsrf.config.ContainerConfig;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Reads an XQuery file or string and executes it.
*
*/
public class XQuery {
private String query_string = null;
private static Log logger = LogFactory.getLog(XQuery.class.getName());
//testsuite constructor: TO REMOVE
public XQuery() throws IOException {
try {
this.query_string = this.getStaticQuery();
} catch (Exception e) {
logger.error(State.logPrefix + "Unable to read the XQuery file");
throw new IOException("Unable to read the XQuery file "
+ e.getStackTrace()[0].toString());
}
}
public XQuery(String xquery) {
this.query_string = xquery;
}
public XQuery(FileReader file) throws IOException {
try {
this.query_string = this.readFile(file);
} catch (IOException ioe) {
logger.error(State.logPrefix + "Unable to read the XQuery file");
throw new IOException("Unable to read the XQuery file "
+ ioe.getStackTrace()[0].toString());
}
}
/**
* Returns the xquery string
*/
public String toString() {
return this.query_string;
}
/**
* Executes the query on the given base collection
*
* @param col
*/
public ResourceSet execute(Collection col) throws Exception {
logger.info(State.logPrefix + "executing query on collection " + col.getName());
XQueryService service = (XQueryService) col.getService("XQueryService", "1.0");
// set pretty-printing on
service.setProperty(OutputKeys.INDENT, "yes");
service.setProperty(OutputKeys.ENCODING, "UTF-8");
CompiledExpression compiled = service.compile(this.query_string);
long start = System.currentTimeMillis();
// execute query and get results in ResourceSet
ResourceSet result = service.execute(compiled);
long qtime = System.currentTimeMillis() - start;
start = System.currentTimeMillis();
/*Properties outputProperties = new Properties();
outputProperties.setProperty(OutputKeys.INDENT, "yes");
SAXSerializer serializer = (SAXSerializer) SerializerPool.getInstance().borrowObject(SAXSerializer.class);
serializer.setOutput(new OutputStreamWriter(System.out), outputProperties);
SerializerPool.getInstance().returnObject(serializer); */
//long rtime = System.currentTimeMillis() - start;
logger.info(State.logPrefix + "hits: " + result.getSize());
logger.info(State.logPrefix + "query time: " + qtime + "ms");
//logger.info(State.logPrefix + "retrieve time: " + rtime);
return result;
}
/**
* Reads the xquery file and return as string.
*/
protected String readFile(FileReader file) throws IOException {
BufferedReader f = new BufferedReader(file);
String line;
StringBuffer xml = new StringBuffer();
while ((line = f.readLine()) != null)
xml.append(line + " ");
f.close();
return xml.toString();
}
/**
* Retrives the query from a local file
*
* @return the query
*/
private String getStaticQuery() throws Exception {
String file = ContainerConfig.getBaseDirectory() + "/etc/org_diligentproject_informationservice_disic/query-example.xq";
return this.readFile(new FileReader(file));
}
}