git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/branches/data-access/spd-having-engine/1.0@74499 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
commit
1260382072
|
@ -0,0 +1,36 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<classpath>
|
||||||
|
<classpathentry kind="src" output="target/classes" path="src/main/java">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="optional" value="true"/>
|
||||||
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="optional" value="true"/>
|
||||||
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="maven.pomderived" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry kind="output" path="target/classes"/>
|
||||||
|
</classpath>
|
|
@ -0,0 +1,23 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<projectDescription>
|
||||||
|
<name>having-engine</name>
|
||||||
|
<comment></comment>
|
||||||
|
<projects>
|
||||||
|
</projects>
|
||||||
|
<buildSpec>
|
||||||
|
<buildCommand>
|
||||||
|
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
<buildCommand>
|
||||||
|
<name>org.eclipse.m2e.core.maven2Builder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
</buildSpec>
|
||||||
|
<natures>
|
||||||
|
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||||
|
<nature>org.eclipse.m2e.core.maven2Nature</nature>
|
||||||
|
</natures>
|
||||||
|
</projectDescription>
|
|
@ -0,0 +1,6 @@
|
||||||
|
eclipse.preferences.version=1
|
||||||
|
encoding//src/main/java=UTF-8
|
||||||
|
encoding//src/main/resources=UTF-8
|
||||||
|
encoding//src/test/java=UTF-8
|
||||||
|
encoding//src/test/resources=UTF-8
|
||||||
|
encoding/<project>=UTF-8
|
|
@ -0,0 +1,5 @@
|
||||||
|
eclipse.preferences.version=1
|
||||||
|
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
|
||||||
|
org.eclipse.jdt.core.compiler.compliance=1.6
|
||||||
|
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
|
||||||
|
org.eclipse.jdt.core.compiler.source=1.6
|
|
@ -0,0 +1,4 @@
|
||||||
|
activeProfiles=
|
||||||
|
eclipse.preferences.version=1
|
||||||
|
resolveWorkspaceProjects=true
|
||||||
|
version=1
|
|
@ -0,0 +1,6 @@
|
||||||
|
gCube System - License
|
||||||
|
------------------------------------------------------------
|
||||||
|
|
||||||
|
The gCube/gCore software is licensed as Free Open Source software conveying to the EUPL (http://ec.europa.eu/idabc/eupl).
|
||||||
|
The software and documentation is provided by its authors/distributors "as is" and no expressed or
|
||||||
|
implied warranty is given for its use, quality or fitness for a particular case.
|
|
@ -0,0 +1 @@
|
||||||
|
Lucio lelii (lucio.lelii@isti.cnr.it), CNR Pisa, Istituto di Scienza e Tecnologie dell'Informazione "A. Faedo".
|
|
@ -0,0 +1,41 @@
|
||||||
|
The gCube System - Having engine
|
||||||
|
------------------------------------------------------------
|
||||||
|
|
||||||
|
This work has been partially supported by the following European projects: DILIGENT (FP6-2003-IST-2),
|
||||||
|
D4Science (FP7-INFRA-2007-1.2.2), D4Science-II (FP7-INFRA-2008-1.2.2), iMarine (FP7-INFRASTRUCTURES-2011-2),
|
||||||
|
and EUBrazilOpenBio (FP7-ICT-2011-EU-Brazil).
|
||||||
|
|
||||||
|
|
||||||
|
Authors
|
||||||
|
-------
|
||||||
|
|
||||||
|
* Federico De Faveri (federico.defaveri@isti.cnr.it), CNR Pisa, Istituto di Scienza e Tecnologie dell'Informazione "A. Faedo"
|
||||||
|
|
||||||
|
Version and Release Date
|
||||||
|
------------------------
|
||||||
|
|
||||||
|
v. 1.0.0 (19-04-2013)
|
||||||
|
* First release
|
||||||
|
|
||||||
|
Description
|
||||||
|
-----------
|
||||||
|
Having engine
|
||||||
|
|
||||||
|
|
||||||
|
Download information
|
||||||
|
--------------------
|
||||||
|
https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-access/having-engine
|
||||||
|
|
||||||
|
|
||||||
|
Documentation
|
||||||
|
-------------
|
||||||
|
https://gcube.wiki.gcube-system.org/gcube/index.php
|
||||||
|
|
||||||
|
|
||||||
|
Licensing
|
||||||
|
---------
|
||||||
|
|
||||||
|
This software is licensed under the terms you may find in the file named "LICENSE" in this directory.
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
<ReleaseNotes>
|
||||||
|
<Changeset component="org.gcube.dataaccess.having-engine.1-0-0" date="2013-04-19">
|
||||||
|
<Change>First release</Change>
|
||||||
|
</Changeset>
|
||||||
|
</ReleaseNotes>
|
|
@ -0,0 +1,42 @@
|
||||||
|
<assembly
|
||||||
|
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
|
||||||
|
<id>servicearchive</id>
|
||||||
|
<formats>
|
||||||
|
<format>tar.gz</format>
|
||||||
|
</formats>
|
||||||
|
<baseDirectory>/</baseDirectory>
|
||||||
|
<fileSets>
|
||||||
|
<fileSet>
|
||||||
|
<directory>${distroDirectory}</directory>
|
||||||
|
<outputDirectory>/</outputDirectory>
|
||||||
|
<useDefaultExcludes>true</useDefaultExcludes>
|
||||||
|
<includes>
|
||||||
|
<include>README</include>
|
||||||
|
<include>LICENSE</include>
|
||||||
|
<include>INSTALL</include>
|
||||||
|
<include>MAINTAINERS</include>
|
||||||
|
<include>changelog.xml</include>
|
||||||
|
</includes>
|
||||||
|
<fileMode>755</fileMode>
|
||||||
|
<filtered>true</filtered>
|
||||||
|
</fileSet>
|
||||||
|
</fileSets>
|
||||||
|
<files>
|
||||||
|
<file>
|
||||||
|
<source>${distroDirectory}/profile.xml</source>
|
||||||
|
<outputDirectory>/</outputDirectory>
|
||||||
|
<filtered>true</filtered>
|
||||||
|
</file>
|
||||||
|
<file>
|
||||||
|
<source>target/${build.finalName}.jar</source>
|
||||||
|
<outputDirectory>/${artifactId}</outputDirectory>
|
||||||
|
</file>
|
||||||
|
<file>
|
||||||
|
<source>${distroDirectory}/svnpath.txt</source>
|
||||||
|
<outputDirectory>/${artifactId}</outputDirectory>
|
||||||
|
<filtered>true</filtered>
|
||||||
|
</file>
|
||||||
|
</files>
|
||||||
|
</assembly>
|
|
@ -0,0 +1,28 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<Resource>
|
||||||
|
<ID></ID>
|
||||||
|
<Type>Service</Type>
|
||||||
|
<Profile>
|
||||||
|
<Description>${description}</Description>
|
||||||
|
<Class>DataAccess</Class>
|
||||||
|
<Name>${artifactId}</Name>
|
||||||
|
<Version>1.0.0</Version>
|
||||||
|
<Packages>
|
||||||
|
<Software>
|
||||||
|
<Description>${description}</Description>
|
||||||
|
<Name>${artifactId}</Name>
|
||||||
|
<Version>${version}</Version>
|
||||||
|
<MavenCoordinates>
|
||||||
|
<groupId>${groupId}</groupId>
|
||||||
|
<artifactId>${artifactId}</artifactId>
|
||||||
|
<version>${version}</version>
|
||||||
|
</MavenCoordinates>
|
||||||
|
<Type>library</Type>
|
||||||
|
<Files>
|
||||||
|
<File>${build.finalName}.jar</File>
|
||||||
|
</Files>
|
||||||
|
</Software>
|
||||||
|
</Packages>
|
||||||
|
</Profile>
|
||||||
|
</Resource>
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-access/having-engine
|
|
@ -0,0 +1,98 @@
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>org.gcube.data.spd</groupId>
|
||||||
|
<artifactId>having-engine</artifactId>
|
||||||
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<artifactId>maven-parent</artifactId>
|
||||||
|
<groupId>org.gcube.tools</groupId>
|
||||||
|
<version>1.0.0</version>
|
||||||
|
<relativePath />
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<distroDirectory>${project.basedir}/distro</distroDirectory>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-jexl</artifactId>
|
||||||
|
<version>2.1.1</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.thoughtworks.xstream</groupId>
|
||||||
|
<artifactId>xstream</artifactId>
|
||||||
|
<version>1.4.4</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.slf4j</groupId>
|
||||||
|
<artifactId>slf4j-api</artifactId>
|
||||||
|
<version>1.6.4</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.slf4j</groupId>
|
||||||
|
<artifactId>slf4j-log4j12</artifactId>
|
||||||
|
<version>1.7.5</version>
|
||||||
|
<scope>runtime</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
|
||||||
|
<build>
|
||||||
|
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-resources-plugin</artifactId>
|
||||||
|
<version>2.5</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>copy-profile</id>
|
||||||
|
<phase>install</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>copy-resources</goal>
|
||||||
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<outputDirectory>target</outputDirectory>
|
||||||
|
<resources>
|
||||||
|
<resource>
|
||||||
|
<directory>${distroDirectory}</directory>
|
||||||
|
<filtering>true</filtering>
|
||||||
|
<includes>
|
||||||
|
<include>profile.xml</include>
|
||||||
|
</includes>
|
||||||
|
</resource>
|
||||||
|
</resources>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-assembly-plugin</artifactId>
|
||||||
|
<version>2.2</version>
|
||||||
|
<configuration>
|
||||||
|
<descriptors>
|
||||||
|
<descriptor>${distroDirectory}/descriptor.xml</descriptor>
|
||||||
|
</descriptors>
|
||||||
|
</configuration>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>servicearchive</id>
|
||||||
|
<phase>install</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>single</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
|
@ -0,0 +1,20 @@
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package org.gcube.dataaccess.spd.havingengine;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author "Federico De Faveri defaveri@isti.cnr.it"
|
||||||
|
*
|
||||||
|
* @param <T> the type of the element checked.
|
||||||
|
*/
|
||||||
|
public interface HavingStatement<T> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if the passed element can be accepted.
|
||||||
|
* @param element the element to check.
|
||||||
|
* @return <code>true</code> if it is accepted, <code>false</code> otherwise.
|
||||||
|
*/
|
||||||
|
public boolean accept(T element);
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,21 @@
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package org.gcube.dataaccess.spd.havingengine;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@link HavingStatement} factory.
|
||||||
|
* @author "Federico De Faveri defaveri@isti.cnr.it"
|
||||||
|
*/
|
||||||
|
public interface HavingStatementFactory {
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Compile the passed expression for the target class.
|
||||||
|
* @param expression the expression to compile.
|
||||||
|
* @return the compiled statement.
|
||||||
|
* @throws Exception if an error occurs during the compilation.
|
||||||
|
*/
|
||||||
|
public abstract <T> HavingStatement<T> compile(String expression) throws Exception;
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,126 @@
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package org.gcube.dataaccess.spd.havingengine.exl;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
|
||||||
|
import javax.xml.parsers.DocumentBuilder;
|
||||||
|
import javax.xml.parsers.DocumentBuilderFactory;
|
||||||
|
import javax.xml.parsers.ParserConfigurationException;
|
||||||
|
|
||||||
|
import org.apache.commons.jexl2.JexlContext;
|
||||||
|
import org.apache.commons.jexl2.JexlEngine;
|
||||||
|
import org.apache.commons.jexl2.ObjectContext;
|
||||||
|
import org.apache.commons.jexl2.ReadonlyContext;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.w3c.dom.Document;
|
||||||
|
|
||||||
|
import com.thoughtworks.xstream.XStream;
|
||||||
|
import com.thoughtworks.xstream.mapper.MapperWrapper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Enhanced {@link JexlContext} that let retrieve XML version of the object and the original object.
|
||||||
|
* @author "Federico De Faveri defaveri@isti.cnr.it"
|
||||||
|
*/
|
||||||
|
public class HavingContext<T> implements JexlContext {
|
||||||
|
|
||||||
|
protected static Logger logger = LoggerFactory.getLogger(HavingContext.class);
|
||||||
|
|
||||||
|
protected static XStream XSTREAM = new XStream() {
|
||||||
|
protected MapperWrapper wrapMapper(MapperWrapper next) {
|
||||||
|
return new HavingMapper(next);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
protected static DocumentBuilder builder;
|
||||||
|
|
||||||
|
static {
|
||||||
|
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
||||||
|
try {
|
||||||
|
builder = factory.newDocumentBuilder();
|
||||||
|
} catch (ParserConfigurationException e) {
|
||||||
|
throw new RuntimeException("Error initializing the builder", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected JexlContext context;
|
||||||
|
protected JexlEngine engine;
|
||||||
|
protected T wrapped;
|
||||||
|
protected Document doc;
|
||||||
|
|
||||||
|
public HavingContext(JexlEngine engine, T wrapped)
|
||||||
|
{
|
||||||
|
context = new ReadonlyContext(new ObjectContext<T>(engine, wrapped));
|
||||||
|
this.wrapped = wrapped;
|
||||||
|
this.engine = engine;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the engine
|
||||||
|
*/
|
||||||
|
public JexlEngine getEngine() {
|
||||||
|
return engine;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the wrapped
|
||||||
|
*/
|
||||||
|
public T getWrapped() {
|
||||||
|
return wrapped;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public Document getDocument() {
|
||||||
|
if (doc == null) doc = buildDocument();
|
||||||
|
return doc;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build XML document and parse it.
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
protected Document buildDocument()
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
String xml = XSTREAM.toXML(wrapped);
|
||||||
|
logger.trace("xml {}", xml);
|
||||||
|
ByteArrayInputStream stream = new ByteArrayInputStream(xml.getBytes());
|
||||||
|
Document doc = builder.parse(stream);
|
||||||
|
return doc;
|
||||||
|
} catch(Exception e)
|
||||||
|
{
|
||||||
|
logger.error("Error converting item to XML", e);
|
||||||
|
throw new RuntimeException("An error occurred building the document", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param name
|
||||||
|
* @return
|
||||||
|
* @see org.apache.commons.jexl2.ObjectContext#get(java.lang.String)
|
||||||
|
*/
|
||||||
|
public Object get(String name) {
|
||||||
|
return context.get(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param name
|
||||||
|
* @param value
|
||||||
|
* @see org.apache.commons.jexl2.ObjectContext#set(java.lang.String, java.lang.Object)
|
||||||
|
*/
|
||||||
|
public void set(String name, Object value) {
|
||||||
|
context.set(name, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param name
|
||||||
|
* @return
|
||||||
|
* @see org.apache.commons.jexl2.ObjectContext#has(java.lang.String)
|
||||||
|
*/
|
||||||
|
public boolean has(String name) {
|
||||||
|
return context.has(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,123 @@
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package org.gcube.dataaccess.spd.havingengine.exl;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import javax.xml.parsers.ParserConfigurationException;
|
||||||
|
import javax.xml.xpath.XPath;
|
||||||
|
import javax.xml.xpath.XPathConstants;
|
||||||
|
import javax.xml.xpath.XPathExpression;
|
||||||
|
import javax.xml.xpath.XPathExpressionException;
|
||||||
|
import javax.xml.xpath.XPathFactory;
|
||||||
|
|
||||||
|
import org.apache.commons.jexl2.Expression;
|
||||||
|
import org.apache.commons.jexl2.JexlContext;
|
||||||
|
import org.apache.commons.jexl2.JexlEngine;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.w3c.dom.Document;
|
||||||
|
import org.xml.sax.SAXException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author "Federico De Faveri defaveri@isti.cnr.it"
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class HavingFunctions {
|
||||||
|
|
||||||
|
protected static Logger logger = LoggerFactory.getLogger(HavingFunctions.class);
|
||||||
|
|
||||||
|
protected static Map<String, XPathExpression> expression_cache;
|
||||||
|
|
||||||
|
static {
|
||||||
|
expression_cache = new HashMap<String, XPathExpression>();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected HavingContext<?> context;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param context
|
||||||
|
* @throws ParserConfigurationException
|
||||||
|
*/
|
||||||
|
public HavingFunctions(JexlContext context) {
|
||||||
|
this.context = (HavingContext<?>) context;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Xpath function.
|
||||||
|
* @param xpath the xpath expression to evaluate.
|
||||||
|
* @return <code>true</code> if the expression is evaluated <code>true</code>, <code>false</code> otherwise.
|
||||||
|
* @throws SAXException if an error occurs.
|
||||||
|
* @throws IOException if an error occurs.
|
||||||
|
* @throws XPathExpressionException if an error occurs.
|
||||||
|
*/
|
||||||
|
public boolean xpath(String xpath) throws SAXException, IOException, XPathExpressionException
|
||||||
|
{
|
||||||
|
logger.debug("xpath {} ", xpath);
|
||||||
|
Document doc = context.getDocument();
|
||||||
|
XPathExpression expression = getExpression(xpath);
|
||||||
|
boolean eval = (Boolean) expression.evaluate(doc, XPathConstants.BOOLEAN);
|
||||||
|
logger.trace("xpath eval: {}",eval);
|
||||||
|
return eval;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieved the compiled expression.
|
||||||
|
* @param xpathExpression
|
||||||
|
* @return
|
||||||
|
* @throws XPathExpressionException
|
||||||
|
*/
|
||||||
|
protected XPathExpression getExpression(String xpathExpression) throws XPathExpressionException
|
||||||
|
{
|
||||||
|
logger.trace("getExpression xpathExpression {}", xpathExpression);
|
||||||
|
XPathExpression expression = expression_cache.get(xpathExpression);
|
||||||
|
if (expression == null) {
|
||||||
|
logger.trace("Building xpath expression");
|
||||||
|
expression = buildExpression(xpathExpression);
|
||||||
|
expression_cache.put(xpathExpression, expression);
|
||||||
|
} else logger.trace("XPath expression already cached");
|
||||||
|
return expression;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Compiles the passed expression
|
||||||
|
* @param xpathExpression
|
||||||
|
* @return
|
||||||
|
* @throws XPathExpressionException
|
||||||
|
*/
|
||||||
|
protected XPathExpression buildExpression(String xpathExpression) throws XPathExpressionException
|
||||||
|
{
|
||||||
|
XPathFactory xPathfactory = XPathFactory.newInstance();
|
||||||
|
XPath xpath = xPathfactory.newXPath();
|
||||||
|
XPathExpression expr = xpath.compile(xpathExpression);
|
||||||
|
return expr;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* EXL function.
|
||||||
|
* @param exlExpression the exl expression to evaluate.
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public boolean exl(String exlExpression) {
|
||||||
|
logger.debug("exl exlExpression: {}", exlExpression);
|
||||||
|
|
||||||
|
JexlEngine engine = context.getEngine();
|
||||||
|
Expression expression = engine.createExpression(exlExpression);
|
||||||
|
boolean eval = (Boolean) expression.evaluate(context);
|
||||||
|
logger.trace("xpath eval: {}",eval);
|
||||||
|
return eval;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean lucio(String expression) {
|
||||||
|
logger.debug("lucio expression: {}", expression);
|
||||||
|
boolean eval = ("oh".equalsIgnoreCase(expression)) || ("fro".equalsIgnoreCase(expression));
|
||||||
|
logger.trace("xpath eval: {}",eval);
|
||||||
|
return eval;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,30 @@
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package org.gcube.dataaccess.spd.havingengine.exl;
|
||||||
|
|
||||||
|
import com.thoughtworks.xstream.mapper.Mapper;
|
||||||
|
import com.thoughtworks.xstream.mapper.MapperWrapper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@link MapperWrapper} that generare short class names and in lower case.
|
||||||
|
* @author "Federico De Faveri defaveri@isti.cnr.it"
|
||||||
|
*/
|
||||||
|
public class HavingMapper extends MapperWrapper {
|
||||||
|
|
||||||
|
public HavingMapper(Mapper wrapped) {
|
||||||
|
super(wrapped);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("rawtypes")
|
||||||
|
@Override
|
||||||
|
public String serializedClass(Class type) {
|
||||||
|
return type.getSimpleName().toLowerCase();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,78 @@
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package org.gcube.dataaccess.spd.havingengine.exl;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.apache.commons.jexl2.Expression;
|
||||||
|
import org.apache.commons.jexl2.JexlContext;
|
||||||
|
import org.apache.commons.jexl2.JexlEngine;
|
||||||
|
import org.gcube.dataaccess.spd.havingengine.HavingStatement;
|
||||||
|
import org.gcube.dataaccess.spd.havingengine.HavingStatementFactory;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author "Federico De Faveri defaveri@isti.cnr.it"
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class HavingStatementFactoryEXL implements HavingStatementFactory {
|
||||||
|
|
||||||
|
protected Logger logger = LoggerFactory.getLogger(HavingStatementFactoryEXL.class);
|
||||||
|
|
||||||
|
protected JexlEngine engine;
|
||||||
|
|
||||||
|
public HavingStatementFactoryEXL()
|
||||||
|
{
|
||||||
|
engine = new JexlEngine();
|
||||||
|
Map<String, Object> functions = new HashMap<String, Object>();
|
||||||
|
functions.put(null, HavingFunctions.class);
|
||||||
|
engine.setFunctions(functions);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public <T> HavingStatement<T> compile(String expression) throws Exception {
|
||||||
|
logger.debug("compile {}",expression);
|
||||||
|
Expression expr = engine.createExpression(expression);
|
||||||
|
return new HavingStatementJEXL<T>(engine, expr);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected class HavingStatementJEXL<T> implements HavingStatement<T> {
|
||||||
|
|
||||||
|
protected Logger logger = LoggerFactory.getLogger(HavingStatementJEXL.class);
|
||||||
|
|
||||||
|
protected JexlEngine engine;
|
||||||
|
protected Expression expression;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param engine
|
||||||
|
* @param expression
|
||||||
|
*/
|
||||||
|
private HavingStatementJEXL(JexlEngine engine, Expression expression) {
|
||||||
|
this.engine = engine;
|
||||||
|
this.expression = expression;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean accept(T element) {
|
||||||
|
try {
|
||||||
|
JexlContext context = new HavingContext<T>(engine, element);
|
||||||
|
return (Boolean)expression.evaluate(context);
|
||||||
|
} catch (Exception e)
|
||||||
|
{
|
||||||
|
logger.debug("Error evaluating expression", e);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,48 @@
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package org.gcube.dataaccess.spd.havingengine.test;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Calendar;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.gcube.dataaccess.spd.havingengine.HavingStatement;
|
||||||
|
import org.gcube.dataaccess.spd.havingengine.HavingStatementFactory;
|
||||||
|
import org.gcube.dataaccess.spd.havingengine.exl.HavingStatementFactoryEXL;
|
||||||
|
import org.gcube.dataaccess.spd.havingengine.test.model.Person;
|
||||||
|
import org.gcube.dataaccess.spd.havingengine.test.model.Product;
|
||||||
|
import org.gcube.dataaccess.spd.havingengine.test.model.Product.Type;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author "Federico De Faveri defaveri@isti.cnr.it"
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class TestHavingEngine {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param args
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
|
||||||
|
List<Person> persons = new ArrayList<Person>();
|
||||||
|
persons.add(new Person("Federico", "De Faveri", 31, Calendar.getInstance(), new ArrayList<Product>(Arrays.asList(new Product(Type.TAXON, 12), new Product(Type.OCCURRENCE, 13)))));
|
||||||
|
persons.add(new Person("Valentina", "Marioli", 27, Calendar.getInstance(), new ArrayList<Product>(Arrays.asList(new Product(Type.OCCURRENCE, 13)))));
|
||||||
|
persons.add(new Person("Roberto", "Cirillo", 31, Calendar.getInstance(), new ArrayList<Product>(Arrays.asList(new Product(Type.OCCURRENCE, 0)))));
|
||||||
|
persons.add(new Person("Lucio", "Lelii", 33, Calendar.getInstance(), new ArrayList<Product>(Arrays.asList(new Product(Type.TAXON, 0)))));
|
||||||
|
|
||||||
|
HavingStatementFactory factory = new HavingStatementFactoryEXL();
|
||||||
|
HavingStatement<Person> personFilter = factory.compile("xpath(\"//product[type='TAXON' and counter>0]\")");
|
||||||
|
|
||||||
|
for (Person person:persons) {
|
||||||
|
System.out.println("Evaluating "+person);
|
||||||
|
boolean accept = personFilter.accept(person);
|
||||||
|
System.out.println(accept?"ACCEPTED":"NOT ACCEPTED");
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,49 @@
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package org.gcube.dataaccess.spd.havingengine.test;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Calendar;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.gcube.dataaccess.spd.havingengine.HavingStatement;
|
||||||
|
import org.gcube.dataaccess.spd.havingengine.HavingStatementFactory;
|
||||||
|
import org.gcube.dataaccess.spd.havingengine.exl.HavingStatementFactoryEXL;
|
||||||
|
import org.gcube.dataaccess.spd.havingengine.test.model.Person;
|
||||||
|
import org.gcube.dataaccess.spd.havingengine.test.model.Product;
|
||||||
|
import org.gcube.dataaccess.spd.havingengine.test.model.Product.Type;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author "Federico De Faveri defaveri@isti.cnr.it"
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class TestHavingEngineSpeed {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param args
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
List<Person> persons = new ArrayList<Person>();
|
||||||
|
|
||||||
|
for (int i = 0; i<100000; i++) persons.add(new Person("name"+i, "surname"+i, i, Calendar.getInstance(), new ArrayList<Product>(Arrays.asList(new Product(Type.OCCURRENCE, 11+i)))));
|
||||||
|
|
||||||
|
long start = System.currentTimeMillis();
|
||||||
|
HavingStatementFactory factory = new HavingStatementFactoryEXL();
|
||||||
|
HavingStatement<Person> personFilter = factory.compile("age<31 || xpath(\"/Person/surname = 'Marioli'\")");
|
||||||
|
long time = System.currentTimeMillis()-start;
|
||||||
|
System.out.println("setup in "+time);
|
||||||
|
|
||||||
|
start = System.currentTimeMillis();
|
||||||
|
long accepted = 0;
|
||||||
|
for (Person person:persons) {
|
||||||
|
boolean accept = personFilter.accept(person);
|
||||||
|
if (accept) accepted++;
|
||||||
|
}
|
||||||
|
time = System.currentTimeMillis()-start;
|
||||||
|
System.out.println("accepted "+accepted+" in "+time+" over "+persons.size()+" elements");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,109 @@
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package org.gcube.dataaccess.spd.havingengine.test.model;
|
||||||
|
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Calendar;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Federico De Faveri defaveri@gmail.com
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class Person {
|
||||||
|
|
||||||
|
protected static final SimpleDateFormat spd = new SimpleDateFormat("yyyy.MM.dd G 'at' HH:mm:ss z");
|
||||||
|
|
||||||
|
protected String name;
|
||||||
|
protected String surname;
|
||||||
|
protected int age;
|
||||||
|
protected Calendar birthDate;
|
||||||
|
|
||||||
|
protected List<Product> products;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param name
|
||||||
|
* @param surname
|
||||||
|
* @param age
|
||||||
|
* @param birthDate
|
||||||
|
*/
|
||||||
|
public Person(String name, String surname, int age, Calendar birthDate, List<Product> products) {
|
||||||
|
this.name = name;
|
||||||
|
this.surname = surname;
|
||||||
|
this.age = age;
|
||||||
|
this.birthDate = birthDate;
|
||||||
|
this.products = products;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* @return the name
|
||||||
|
*/
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* @param name the name to set
|
||||||
|
*/
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* @return the surname
|
||||||
|
*/
|
||||||
|
public String getSurname() {
|
||||||
|
return surname;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* @param surname the surname to set
|
||||||
|
*/
|
||||||
|
public void setSurname(String surname) {
|
||||||
|
this.surname = surname;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* @return the age
|
||||||
|
*/
|
||||||
|
public int getAge() {
|
||||||
|
return age;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* @param age the age to set
|
||||||
|
*/
|
||||||
|
public void setAge(int age) {
|
||||||
|
this.age = age;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* @return the birthDate
|
||||||
|
*/
|
||||||
|
public Calendar getBirthDate() {
|
||||||
|
return birthDate;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* @param birthDate the birthDate to set
|
||||||
|
*/
|
||||||
|
public void setBirthDate(Calendar birthDate) {
|
||||||
|
this.birthDate = birthDate;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
StringBuilder builder = new StringBuilder();
|
||||||
|
builder.append("Person [name=");
|
||||||
|
builder.append(name);
|
||||||
|
builder.append(", surname=");
|
||||||
|
builder.append(surname);
|
||||||
|
builder.append(", age=");
|
||||||
|
builder.append(age);
|
||||||
|
builder.append(", birthDate=");
|
||||||
|
if (birthDate!=null) builder.append(spd.format(birthDate.getTime()));
|
||||||
|
else builder.append(birthDate);
|
||||||
|
builder.append("]");
|
||||||
|
return builder.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,27 @@
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package org.gcube.dataaccess.spd.havingengine.test.model;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author "Federico De Faveri defaveri@isti.cnr.it"
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class Product {
|
||||||
|
public enum Type {TAXON, OCCURRENCE}
|
||||||
|
|
||||||
|
protected Type type;
|
||||||
|
protected int counter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param type
|
||||||
|
* @param counter
|
||||||
|
*/
|
||||||
|
public Product(Type type, int counter) {
|
||||||
|
this.type = type;
|
||||||
|
this.counter = counter;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,8 @@
|
||||||
|
# Root logger option
|
||||||
|
log4j.rootLogger=TRACE, stdout
|
||||||
|
|
||||||
|
# Direct log messages to stdout
|
||||||
|
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
|
||||||
|
log4j.appender.stdout.Target=System.out
|
||||||
|
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
|
||||||
|
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
|
Loading…
Reference in New Issue