You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@stanbol.apache.org by rw...@apache.org on 2012/04/08 17:12:46 UTC
svn commit: r1311019 [3/5] - in /incubator/stanbol/trunk: ./
contenthub/search/featured/src/main/java/org/apache/stanbol/contenthub/search/featured/
contenthub/search/related/src/main/java/org/apache/stanbol/contenthub/search/related/
contenthub/store/...
Added: incubator/stanbol/trunk/enhancer/generic/core/src/main/java/org/apache/stanbol/enhancer/contentitem/inmemory/InMemoryContentItemFactory.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/enhancer/generic/core/src/main/java/org/apache/stanbol/enhancer/contentitem/inmemory/InMemoryContentItemFactory.java?rev=1311019&view=auto
==============================================================================
--- incubator/stanbol/trunk/enhancer/generic/core/src/main/java/org/apache/stanbol/enhancer/contentitem/inmemory/InMemoryContentItemFactory.java (added)
+++ incubator/stanbol/trunk/enhancer/generic/core/src/main/java/org/apache/stanbol/enhancer/contentitem/inmemory/InMemoryContentItemFactory.java Sun Apr 8 15:12:40 2012
@@ -0,0 +1,94 @@
+package org.apache.stanbol.enhancer.contentitem.inmemory;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+
+import org.apache.clerezza.rdf.core.MGraph;
+import org.apache.clerezza.rdf.core.UriRef;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Properties;
+import org.apache.felix.scr.annotations.Property;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.stanbol.enhancer.servicesapi.Blob;
+import org.apache.stanbol.enhancer.servicesapi.ContentItem;
+import org.apache.stanbol.enhancer.servicesapi.ContentItemFactory;
+import org.apache.stanbol.enhancer.servicesapi.ContentSink;
+import org.apache.stanbol.enhancer.servicesapi.ContentSource;
+import org.apache.stanbol.enhancer.servicesapi.helper.ContentItemHelper;
+import org.apache.stanbol.enhancer.servicesapi.impl.AbstractContentItemFactory;
+import org.osgi.framework.Constants;
+@Component(inherit=true)
+@Service(value=ContentItemFactory.class)
+@Properties(value={
+ //set service ranking to an positive integer so that others do not accitently
+ //override the default
+ @Property(name=Constants.SERVICE_RANKING,intValue=100)
+})
+public class InMemoryContentItemFactory extends AbstractContentItemFactory implements ContentItemFactory {
+
+ private static InMemoryContentItemFactory instance;
+ /**
+ * Getter for the singleton instance of this factory. Within an OSGI
+ * environment this should not be used as this Factory is also registered
+ * as OSGI service.
+ * @return the singleton instance
+ */
+ public static InMemoryContentItemFactory getInstance(){
+ if(instance == null){
+ instance = new InMemoryContentItemFactory();
+ }
+ return instance;
+ }
+
+ public InMemoryContentItemFactory() {
+ super(true); //lazy initialisation makes a lot of sense for in-memory implementations
+ }
+
+ @Override
+ protected ContentItem createContentItem(UriRef id, Blob blob, MGraph metadata) {
+ return new InMemoryContentItem(id, blob, metadata);
+ }
+
+ @Override
+ protected ContentItem createContentItem(String prefix, Blob blob, MGraph metadata) {
+ return new InMemoryContentItem(ContentItemHelper.makeDefaultUri(prefix, blob), blob, metadata);
+ }
+
+ @Override
+ public Blob createBlob(ContentSource source) throws IOException {
+ if(source == null){
+ throw new IllegalArgumentException("The parsed ContentSource MUST NOT be NULL!");
+ }
+ //use source.getData to avoid making copies of byte arrays
+ return new InMemoryBlob(source.getData(), source.getMediaType(),null);
+ }
+
+ @Override
+ public ContentSink createContentSink(String mediaType) throws IOException {
+ return new InMemoryContentSink(mediaType);
+ }
+
+
+ protected static class InMemoryContentSink implements ContentSink {
+
+ private final ByteArrayOutputStream out;
+ private final InMemoryBlob blob;
+
+ protected InMemoryContentSink(String mt){
+ out = new ByteArrayOutputStream();
+ blob = new InMemoryBlob(out, mt, null);
+ }
+
+ @Override
+ public OutputStream getOutputStream() {
+ return out;
+ }
+
+ @Override
+ public Blob getBlob() {
+ return blob;
+ }
+
+ }
+}
Added: incubator/stanbol/trunk/enhancer/generic/core/src/main/java/org/apache/stanbol/enhancer/enginemanager/impl/EnhancementEngineManagerImpl.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/enhancer/generic/core/src/main/java/org/apache/stanbol/enhancer/enginemanager/impl/EnhancementEngineManagerImpl.java?rev=1311019&view=auto
==============================================================================
--- incubator/stanbol/trunk/enhancer/generic/core/src/main/java/org/apache/stanbol/enhancer/enginemanager/impl/EnhancementEngineManagerImpl.java (added)
+++ incubator/stanbol/trunk/enhancer/generic/core/src/main/java/org/apache/stanbol/enhancer/enginemanager/impl/EnhancementEngineManagerImpl.java Sun Apr 8 15:12:40 2012
@@ -0,0 +1,52 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements. See the NOTICE file distributed with
+* this work for additional information regarding copyright ownership.
+* The ASF licenses this file to You under the Apache License, Version 2.0
+* (the "License"); you may not use this file except in compliance with
+* the License. You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package org.apache.stanbol.enhancer.enginemanager.impl;
+
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.stanbol.enhancer.servicesapi.EnhancementEngineManager;
+import org.apache.stanbol.enhancer.servicesapi.impl.EnginesTracker;
+import org.osgi.service.component.ComponentContext;
+
+/**
+ * Implementation of the {@link EnhancementEngineManager} interface as OSGI
+ * component based on {@link EnginesTracker}.
+ *
+ * @author Rupert Westenthaler
+ *
+ */
+@Component(immediate=true,enabled=true)
+@Service(value=EnhancementEngineManager.class)
+public class EnhancementEngineManagerImpl extends EnginesTracker implements EnhancementEngineManager {
+
+ public EnhancementEngineManagerImpl() {
+ super();
+ }
+
+ @Activate
+ public void activate(ComponentContext ctx){
+ initEngineTracker(ctx.getBundleContext(), null, null);
+ open();
+ }
+ @Deactivate
+ public void deactivate(ComponentContext ctx){
+ close();
+ }
+
+}
Added: incubator/stanbol/trunk/enhancer/generic/core/src/test/java/org/apache/stanbol/enhancer/contentitem/file/CustomDirFileContentItemFactoryTest.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/enhancer/generic/core/src/test/java/org/apache/stanbol/enhancer/contentitem/file/CustomDirFileContentItemFactoryTest.java?rev=1311019&view=auto
==============================================================================
--- incubator/stanbol/trunk/enhancer/generic/core/src/test/java/org/apache/stanbol/enhancer/contentitem/file/CustomDirFileContentItemFactoryTest.java (added)
+++ incubator/stanbol/trunk/enhancer/generic/core/src/test/java/org/apache/stanbol/enhancer/contentitem/file/CustomDirFileContentItemFactoryTest.java Sun Apr 8 15:12:40 2012
@@ -0,0 +1,53 @@
+package org.apache.stanbol.enhancer.contentitem.file;
+
+import static junit.framework.Assert.assertNotNull;
+import static junit.framework.Assert.assertTrue;
+
+import java.io.File;
+import java.io.IOException;
+
+import junit.framework.Assert;
+
+import org.apache.stanbol.enhancer.servicesapi.Blob;
+import org.apache.stanbol.enhancer.servicesapi.ContentItemFactory;
+import org.apache.stanbol.enhancer.servicesapi.impl.StringSource;
+import org.apache.stanbol.enhancer.test.ContentItemFactoryTest;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+public class CustomDirFileContentItemFactoryTest extends ContentItemFactoryTest {
+
+ private static File customDir;
+ private static ContentItemFactory factory;
+
+ @BeforeClass
+ public static void init(){
+ String prefix = System.getProperty("basedir",".");
+ File targetDir = new File(prefix,"target");
+ customDir = new File(targetDir,"fileContentItem");
+ }
+
+ @Override
+ protected ContentItemFactory createContentItemFactory() throws IOException {
+ if(factory == null){
+ factory = new FileContentItemFactory(customDir);
+ }
+ return factory;
+ }
+
+ /**
+ * Tests that the specified directory is actually used!
+ */
+ @Test
+ public void testCustomDir() throws IOException {
+ assertTrue("The custom dir '"+customDir+"'MUST exist",
+ customDir.exists());
+ assertTrue("The custom dir '"+customDir+"'MUST be an directory",
+ customDir.isDirectory());
+ int numFiles = customDir.list().length;
+ Blob blob = contentItemFactory.createBlob(new StringSource("ensure a file exist"));
+ assertNotNull(blob);
+ Assert.assertEquals("Creating a new Blob has not increased the " +
+ "number of files by one!",numFiles, customDir.list().length-1);
+ }
+}
Added: incubator/stanbol/trunk/enhancer/generic/core/src/test/java/org/apache/stanbol/enhancer/contentitem/file/DefaultFileContentItemFactoryTest.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/enhancer/generic/core/src/test/java/org/apache/stanbol/enhancer/contentitem/file/DefaultFileContentItemFactoryTest.java?rev=1311019&view=auto
==============================================================================
--- incubator/stanbol/trunk/enhancer/generic/core/src/test/java/org/apache/stanbol/enhancer/contentitem/file/DefaultFileContentItemFactoryTest.java (added)
+++ incubator/stanbol/trunk/enhancer/generic/core/src/test/java/org/apache/stanbol/enhancer/contentitem/file/DefaultFileContentItemFactoryTest.java Sun Apr 8 15:12:40 2012
@@ -0,0 +1,13 @@
+package org.apache.stanbol.enhancer.contentitem.file;
+
+import org.apache.stanbol.enhancer.servicesapi.ContentItemFactory;
+import org.apache.stanbol.enhancer.test.ContentItemFactoryTest;
+
+public class DefaultFileContentItemFactoryTest extends ContentItemFactoryTest {
+
+ @Override
+ protected ContentItemFactory createContentItemFactory() {
+ return FileContentItemFactory.getInstance();
+ }
+
+}
Added: incubator/stanbol/trunk/enhancer/generic/core/src/test/java/org/apache/stanbol/enhancer/contentitem/file/FileBlobTest.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/enhancer/generic/core/src/test/java/org/apache/stanbol/enhancer/contentitem/file/FileBlobTest.java?rev=1311019&view=auto
==============================================================================
--- incubator/stanbol/trunk/enhancer/generic/core/src/test/java/org/apache/stanbol/enhancer/contentitem/file/FileBlobTest.java (added)
+++ incubator/stanbol/trunk/enhancer/generic/core/src/test/java/org/apache/stanbol/enhancer/contentitem/file/FileBlobTest.java Sun Apr 8 15:12:40 2012
@@ -0,0 +1,19 @@
+package org.apache.stanbol.enhancer.contentitem.file;
+
+import java.io.IOException;
+
+import org.apache.stanbol.enhancer.servicesapi.Blob;
+import org.apache.stanbol.enhancer.servicesapi.ContentItemFactory;
+import org.apache.stanbol.enhancer.servicesapi.ContentSource;
+import org.apache.stanbol.enhancer.test.BlobTest;
+
+public class FileBlobTest extends BlobTest {
+
+ private ContentItemFactory factory = FileContentItemFactory.getInstance();
+
+ @Override
+ protected Blob createBlob(ContentSource cs) throws IOException {
+ return factory.createBlob(cs);
+ }
+
+}
Added: incubator/stanbol/trunk/enhancer/generic/core/src/test/java/org/apache/stanbol/enhancer/contentitem/file/FileContentItemTest.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/enhancer/generic/core/src/test/java/org/apache/stanbol/enhancer/contentitem/file/FileContentItemTest.java?rev=1311019&view=auto
==============================================================================
--- incubator/stanbol/trunk/enhancer/generic/core/src/test/java/org/apache/stanbol/enhancer/contentitem/file/FileContentItemTest.java (added)
+++ incubator/stanbol/trunk/enhancer/generic/core/src/test/java/org/apache/stanbol/enhancer/contentitem/file/FileContentItemTest.java Sun Apr 8 15:12:40 2012
@@ -0,0 +1,25 @@
+package org.apache.stanbol.enhancer.contentitem.file;
+
+import java.io.IOException;
+
+import org.apache.stanbol.enhancer.servicesapi.Blob;
+import org.apache.stanbol.enhancer.servicesapi.ContentItem;
+import org.apache.stanbol.enhancer.servicesapi.ContentItemFactory;
+import org.apache.stanbol.enhancer.servicesapi.ContentSource;
+import org.apache.stanbol.enhancer.test.ContentItemTest;
+
+public class FileContentItemTest extends ContentItemTest {
+
+ private ContentItemFactory factory = FileContentItemFactory.getInstance();
+
+ @Override
+ protected ContentItem createContentItem(ContentSource source) throws IOException {
+ return factory.createContentItem(source);
+ }
+
+ @Override
+ protected Blob createBlob(ContentSource source) throws IOException {
+ return factory.createBlob(source);
+ }
+
+}
Propchange: incubator/stanbol/trunk/enhancer/generic/rdfentities/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Sun Apr 8 15:12:40 2012
@@ -0,0 +1,7 @@
+.settings
+
+.project
+
+.classpath
+
+target
Added: incubator/stanbol/trunk/enhancer/generic/rdfentities/pom.xml
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/enhancer/generic/rdfentities/pom.xml?rev=1311019&view=auto
==============================================================================
--- incubator/stanbol/trunk/enhancer/generic/rdfentities/pom.xml (added)
+++ incubator/stanbol/trunk/enhancer/generic/rdfentities/pom.xml Sun Apr 8 15:12:40 2012
@@ -0,0 +1,108 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<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/maven-v4_0_0.xsd">
+
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <groupId>org.apache.stanbol</groupId>
+ <artifactId>org.apache.stanbol.enhancer.parent</artifactId>
+ <version>0.9.0-incubating-SNAPSHOT</version>
+ <relativePath>../../parent</relativePath>
+ </parent>
+
+ <groupId>org.apache.stanbol</groupId>
+ <artifactId>org.apache.stanbol.enhancer.rdfentities</artifactId>
+ <packaging>bundle</packaging>
+
+ <name>Apache Stanbol Enhancer RDF entities</name>
+ <description>Simple RDF facading utility using Java InvocationHandler</description>
+
+ <inceptionYear>2010</inceptionYear>
+
+ <scm>
+ <connection>
+ scm:svn:http://svn.apache.org/repos/asf/incubator/stanbol/trunk/enhancer/generic/rdfentities/
+ </connection>
+ <developerConnection>
+ scm:svn:https://svn.apache.org/repos/asf/incubator/stanbol/trunk/enhancer/generic/rdfentities/
+ </developerConnection>
+ <url>http://incubator.apache.org/stanbol/</url>
+ </scm>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>maven-bundle-plugin</artifactId>
+ <extensions>true</extensions>
+ <configuration>
+ <instructions>
+ <Export-Package>
+ org.apache.stanbol.enhancer.rdfentities;version=${project.version},
+ org.apache.stanbol.enhancer.rdfentities.fise;version=${project.version}
+ </Export-Package>
+ <Private-Package>
+ org.apache.stanbol.enhancer.rdfentities.impl;version=${project.version}
+ </Private-Package>
+ </instructions>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.stanbol</groupId>
+ <artifactId>org.apache.stanbol.enhancer.servicesapi</artifactId>
+ </dependency>
+
+ <dependency>
+ <groupId>org.apache.clerezza</groupId>
+ <artifactId>rdf.core</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>javax.servlet</groupId>
+ <artifactId>servlet-api</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>commons-io</groupId>
+ <artifactId>commons-io</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.stanbol</groupId>
+ <artifactId>org.apache.stanbol.commons.indexedgraph</artifactId>
+ </dependency>
+ <!-- for tests -->
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-simple</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.stanbol</groupId>
+ <artifactId>org.apache.stanbol.enhancer.core</artifactId>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+
+</project>
Propchange: incubator/stanbol/trunk/enhancer/generic/rdfentities/pom.xml
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: incubator/stanbol/trunk/enhancer/generic/rdfentities/src/main/java/org/apache/stanbol/enhancer/rdfentities/fise/Enhancement.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/enhancer/generic/rdfentities/src/main/java/org/apache/stanbol/enhancer/rdfentities/fise/Enhancement.java?rev=1311019&view=auto
==============================================================================
--- incubator/stanbol/trunk/enhancer/generic/rdfentities/src/main/java/org/apache/stanbol/enhancer/rdfentities/fise/Enhancement.java (added)
+++ incubator/stanbol/trunk/enhancer/generic/rdfentities/src/main/java/org/apache/stanbol/enhancer/rdfentities/fise/Enhancement.java Sun Apr 8 15:12:40 2012
@@ -0,0 +1,82 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements. See the NOTICE file distributed with
+* this work for additional information regarding copyright ownership.
+* The ASF licenses this file to You under the Apache License, Version 2.0
+* (the "License"); you may not use this file except in compliance with
+* the License. You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package org.apache.stanbol.enhancer.rdfentities.fise;
+
+import java.util.Collection;
+import java.util.Date;
+
+import org.apache.clerezza.rdf.core.UriRef;
+import org.apache.stanbol.enhancer.rdfentities.Rdf;
+import org.apache.stanbol.enhancer.rdfentities.RdfEntity;
+
+
+/**
+ * This Interface represents a Stanbol Enhancer enhancement.
+ * <p>
+ * To create an instance of this interface use the following code
+ * <code><pre>
+ * ContentItem ci;
+ * MGraph graph = ci.getMetadata();
+ * RdfEntityFactory factory = RdfEntityFactory.createInstance(graph);
+ * String enhancementId = "http://wwww.example.com/iks-project/enhancer/example-enhancement";
+ * UriRef enhancementNode = new UriRef(enhancementId);
+ * Enhancement enhancement = factory.getProxy(enhancementNode, Enhancement.class);
+ * enhancement.setCreator("Rupert Westenthaler");
+ * enhancement.setCreated(new Date());
+ * ...
+ * </pre></code>
+ *
+ * @author Rupert Westenthaler
+ */
+@Rdf(id="http://fise.iks-project.eu/ontology/Enhancement")
+public interface Enhancement extends RdfEntity{
+
+ @Rdf(id="http://purl.org/dc/terms/creator")
+ UriRef getCreator();
+ @Rdf(id="http://purl.org/dc/terms/creator")
+ void setCreator(UriRef creator);
+
+ @Rdf(id="http://purl.org/dc/terms/created")
+ void setCreated(Date date);
+ @Rdf(id="http://purl.org/dc/terms/created")
+ Date getCreated();
+
+// @Rdf(id="http://purl.org/dc/terms/type")
+// void setDcType(Collection<URI> types);
+ @Rdf(id="http://purl.org/dc/terms/type")
+ Collection<UriRef> getDcType();
+
+ @Rdf(id="http://fise.iks-project.eu/ontology/confidence")
+ Double getConfidence();
+ @Rdf(id="http://fise.iks-project.eu/ontology/confidence")
+ void setConfidence(Double value);
+
+ @Rdf(id="http://fise.iks-project.eu/ontology/extracted-from")
+ UriRef getExtractedFrom();
+ @Rdf(id="http://fise.iks-project.eu/ontology/extracted-from")
+ void setExtractedFrom(UriRef contentItem);
+
+ @Rdf(id="http://purl.org/dc/terms/requires")
+ Collection<Enhancement> getRequires();
+// @Rdf(id="http://purl.org/dc/terms/requires")
+// void setRequires(Collection<Enhancement> required);
+
+ @Rdf(id="http://purl.org/dc/terms/relation")
+ Collection<Enhancement> getRelations();
+// @Rdf(id="http://purl.org/dc/terms/relation")
+// void setRelation(Collection<Enhancement> related);
+}
Propchange: incubator/stanbol/trunk/enhancer/generic/rdfentities/src/main/java/org/apache/stanbol/enhancer/rdfentities/fise/Enhancement.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: incubator/stanbol/trunk/enhancer/generic/rdfentities/src/main/java/org/apache/stanbol/enhancer/rdfentities/fise/EntityAnnotation.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/enhancer/generic/rdfentities/src/main/java/org/apache/stanbol/enhancer/rdfentities/fise/EntityAnnotation.java?rev=1311019&view=auto
==============================================================================
--- incubator/stanbol/trunk/enhancer/generic/rdfentities/src/main/java/org/apache/stanbol/enhancer/rdfentities/fise/EntityAnnotation.java (added)
+++ incubator/stanbol/trunk/enhancer/generic/rdfentities/src/main/java/org/apache/stanbol/enhancer/rdfentities/fise/EntityAnnotation.java Sun Apr 8 15:12:40 2012
@@ -0,0 +1,40 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements. See the NOTICE file distributed with
+* this work for additional information regarding copyright ownership.
+* The ASF licenses this file to You under the Apache License, Version 2.0
+* (the "License"); you may not use this file except in compliance with
+* the License. You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package org.apache.stanbol.enhancer.rdfentities.fise;
+
+import java.util.Collection;
+
+import org.apache.clerezza.rdf.core.UriRef;
+import org.apache.stanbol.enhancer.rdfentities.Rdf;
+
+
+@Rdf(id="http://fise.iks-project.eu/ontology/EntityAnnotation")
+public interface EntityAnnotation extends Enhancement {
+
+ @Rdf(id="http://fise.iks-project.eu/ontology/entity-reference")
+ UriRef getEntityReference();
+ @Rdf(id="http://fise.iks-project.eu/ontology/entity-reference")
+ void setEntityReference(UriRef reference);
+
+ @Rdf(id="http://fise.iks-project.eu/ontology/entity-label")
+ String getEntityLabel();
+ @Rdf(id="http://fise.iks-project.eu/ontology/entity-label")
+ void setEntityLabel(String label);
+
+ @Rdf(id="http://fise.iks-project.eu/ontology/entity-type")
+ Collection<UriRef> getEntityTypes();
+}
Propchange: incubator/stanbol/trunk/enhancer/generic/rdfentities/src/main/java/org/apache/stanbol/enhancer/rdfentities/fise/EntityAnnotation.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: incubator/stanbol/trunk/enhancer/generic/rdfentities/src/main/java/org/apache/stanbol/enhancer/rdfentities/fise/TextAnnotation.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/enhancer/generic/rdfentities/src/main/java/org/apache/stanbol/enhancer/rdfentities/fise/TextAnnotation.java?rev=1311019&view=auto
==============================================================================
--- incubator/stanbol/trunk/enhancer/generic/rdfentities/src/main/java/org/apache/stanbol/enhancer/rdfentities/fise/TextAnnotation.java (added)
+++ incubator/stanbol/trunk/enhancer/generic/rdfentities/src/main/java/org/apache/stanbol/enhancer/rdfentities/fise/TextAnnotation.java Sun Apr 8 15:12:40 2012
@@ -0,0 +1,44 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements. See the NOTICE file distributed with
+* this work for additional information regarding copyright ownership.
+* The ASF licenses this file to You under the Apache License, Version 2.0
+* (the "License"); you may not use this file except in compliance with
+* the License. You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package org.apache.stanbol.enhancer.rdfentities.fise;
+
+import org.apache.stanbol.enhancer.rdfentities.Rdf;
+
+
+@Rdf(id="http://fise.iks-project.eu/ontology/TextAnnotation")
+public interface TextAnnotation extends Enhancement {
+
+ @Rdf(id="http://fise.iks-project.eu/ontology/start")
+ Integer getStart();
+ @Rdf(id="http://fise.iks-project.eu/ontology/start")
+ void setStart(Integer start);
+
+ @Rdf(id="http://fise.iks-project.eu/ontology/end")
+ Integer getEnd();
+ @Rdf(id="http://fise.iks-project.eu/ontology/end")
+ void setEnd(Integer end);
+
+ @Rdf(id="http://fise.iks-project.eu/ontology/selected-text")
+ String getSelectedText();
+ @Rdf(id="http://fise.iks-project.eu/ontology/selected-text")
+ void setSelectedText(String selectedText);
+
+ @Rdf(id="http://fise.iks-project.eu/ontology/selection-context")
+ String getSelectionContext();
+ @Rdf(id="http://fise.iks-project.eu/ontology/selection-context")
+ void setSelectionContext(String selectionContext);
+}
Propchange: incubator/stanbol/trunk/enhancer/generic/rdfentities/src/main/java/org/apache/stanbol/enhancer/rdfentities/fise/TextAnnotation.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: incubator/stanbol/trunk/enhancer/generic/rdfentities/src/main/java/org/apache/stanbol/enhancer/rdfentities/impl/RdfProxyInvocationHandler.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/enhancer/generic/rdfentities/src/main/java/org/apache/stanbol/enhancer/rdfentities/impl/RdfProxyInvocationHandler.java?rev=1311019&view=auto
==============================================================================
--- incubator/stanbol/trunk/enhancer/generic/rdfentities/src/main/java/org/apache/stanbol/enhancer/rdfentities/impl/RdfProxyInvocationHandler.java (added)
+++ incubator/stanbol/trunk/enhancer/generic/rdfentities/src/main/java/org/apache/stanbol/enhancer/rdfentities/impl/RdfProxyInvocationHandler.java Sun Apr 8 15:12:40 2012
@@ -0,0 +1,444 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements. See the NOTICE file distributed with
+* this work for additional information regarding copyright ownership.
+* The ASF licenses this file to You under the Apache License, Version 2.0
+* (the "License"); you may not use this file except in compliance with
+* the License. You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+/**
+ *
+ */
+package org.apache.stanbol.enhancer.rdfentities.impl;
+
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
+import java.net.MalformedURLException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.util.AbstractCollection;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Set;
+
+import org.apache.clerezza.rdf.core.LiteralFactory;
+import org.apache.clerezza.rdf.core.NoConvertorException;
+import org.apache.clerezza.rdf.core.NonLiteral;
+import org.apache.clerezza.rdf.core.Resource;
+import org.apache.clerezza.rdf.core.Triple;
+import org.apache.clerezza.rdf.core.TypedLiteral;
+import org.apache.clerezza.rdf.core.UriRef;
+import org.apache.clerezza.rdf.core.impl.TripleImpl;
+import org.apache.stanbol.enhancer.rdfentities.Rdf;
+import org.apache.stanbol.enhancer.rdfentities.RdfEntity;
+import org.apache.stanbol.enhancer.servicesapi.rdf.Properties;
+
+
+public class RdfProxyInvocationHandler implements InvocationHandler {
+
+ /**
+ * The getID method of the RdfEntity Interface
+ */
+ protected static final Method getIDMethod;
+
+ /**
+ * The toString Method of {@link Object}
+ */
+ protected static final Method toString;
+
+ /**
+ * The equals Method of {@link Object}
+ */
+ protected static final Method equals;
+
+ /**
+ * The hashCode Method of {@link Object}
+ */
+ protected static final Method hashCode;
+
+ static {
+ try {
+ getIDMethod = RdfEntity.class.getMethod("getId", new Class<?>[]{});
+ toString = Object.class.getMethod("toString", new Class<?>[]{});
+ equals = Object.class.getMethod("equals", new Class<?>[]{Object.class});
+ hashCode = Object.class.getMethod("hashCode", new Class<?>[]{});
+ } catch (SecurityException e) {
+ throw new IllegalStateException("Unable to access getId Method in the "+RdfEntity.class+" Interface",e);
+ } catch (NoSuchMethodException e) {
+ throw new IllegalStateException("Unable to find getId Method in the "+RdfEntity.class+" Interface",e);
+ }
+ }
+
+ /**
+ * The logger TODO: Question: How to get the dependencies for logging working with maven :(
+ */
+// private static final Logger log = LoggerFactory.getLogger(RdfProxyInvocationHandler.class);
+
+ protected SimpleRdfEntityFactory factory;
+ protected LiteralFactory literalFactory;
+ protected NonLiteral rdfNode;
+ private final Set<Class<?>> interfaces;
+ public RdfProxyInvocationHandler(SimpleRdfEntityFactory factory, NonLiteral rdfNode, Class<?>[] parsedInterfaces, LiteralFactory literalFactory){
+ this.rdfNode = rdfNode;
+ this.factory = factory;
+ this.literalFactory = literalFactory;
+ //TODO If slow implement this by directly using the MGraph Interface!
+ Collection<UriRef> nodeTypes = getValues(Properties.RDF_TYPE, UriRef.class);
+ Set<Class<?>> interfaceSet = new HashSet<Class<?>>();
+ for (Class<?> clazz : parsedInterfaces){
+ if(!clazz.isInterface()){
+ throw new IllegalStateException("Parsed Class "+clazz+" is not an interface!");
+ }
+ interfaceSet.add(clazz);
+ getSuperInterfaces(clazz, interfaceSet);
+ }
+ this.interfaces = Collections.unmodifiableSet(interfaceSet); //nobody should be able to change this!
+ for (Class<?> clazz : this.interfaces){
+ Rdf classAnnotation = clazz.getAnnotation(Rdf.class);
+ if(classAnnotation == null){
+ } else { //check of the type statement is present
+ UriRef typeRef = new UriRef(classAnnotation.id());
+ if(!nodeTypes.contains(typeRef)){
+ //TODO: Question: How to get the dependencies for logging working with maven :(
+ //log.debug("add type "+typeRef+" for interface "+clazz+" to node "+rdfNode);
+ addValue(Properties.RDF_TYPE, typeRef); //add the missing type!
+ } else {
+ // else the type is already present ... nothing to do
+ //TODO: Question: How to get the dependencies for logging working with maven :(
+ //log.debug("type "+typeRef+" for interface "+clazz+" is already present for node "+rdfNode);
+ }
+ }
+ }
+ }
+
+ private static void getSuperInterfaces(Class<?> interfaze, Collection<Class<?>> interfaces){
+ for (Class<?> superInterface : interfaze.getInterfaces()){
+ if(superInterface != null){
+ interfaces.add(superInterface);
+ getSuperInterfaces(superInterface, interfaces); //recursive
+ }
+ }
+ }
+
+ @Override
+ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
+ //RdfEntity rdfEntity;
+ if(!(proxy instanceof RdfEntity)){
+ throw new IllegalArgumentException("Parsed proxy instance is not of type "+RdfEntity.class
+ +". This RdfWrapperInvocationHandler implementations only work for proxies implementing this interface!");
+ }
+ //First check for Methods defined in RDFEntity and java.lang.Object
+ //implementation of the RffEntity Interface method!
+ if(method.equals(getIDMethod)){
+ return rdfNode;
+ }
+ //implement toString
+ if(method.equals(equals)){
+ return args[0] != null && args[0] instanceof RdfEntity && ((RdfEntity)args[0]).getId().equals(rdfNode);
+ }
+ //implement hashCode
+ if(method.equals(hashCode)){
+ return rdfNode.toString().hashCode();
+ }
+ //implement toString
+ if(method.equals(toString)){
+ return "Proxy for Node "+rdfNode+" and interfaces "+interfaces;
+ }
+ Rdf rdf = method.getAnnotation(Rdf.class);
+ if(rdf == null){
+ throw new IllegalStateException("Invoked Method does not have an Rdf annotation!");
+ }
+ UriRef property;
+ if(rdf.id().startsWith("http://") || rdf.id().startsWith("urn:")){
+ property = new UriRef(rdf.id());
+ } else {
+ throw new IllegalStateException("The id=\""+rdf.id()+"\"provided by the rdf annotation is not an valid URI");
+ }
+ //check for Write (Setter) Method
+ if(method.getReturnType().equals(void.class)){
+ Type[] parameterTypes = method.getGenericParameterTypes();
+ //check the parameter types to improve error messages!
+ //Only methods with a single parameter are supported
+ if(parameterTypes.length!=1){
+ throw new IllegalStateException("Unsupported parameters for Method "+method.toString()
+ +"! Only setter methodes with a singe parameter are supported.");
+ }
+ final Type parameterType = parameterTypes[0];
+ //now check if args != null and has an element
+ if(args == null){
+ throw new IllegalArgumentException(
+ "NULL parsed as \"Object[] args\". An array with a single value is expected when calling "+method.toString()+"!");
+ }
+ if(args.length<1){
+ throw new IllegalArgumentException(
+ "An empty array was parsed as \"Object[] args\". An array with a single value is expected when calling method "+method.toString()+"!");
+ }
+ final Object value = args[0];
+ //Handle Arrays
+ if(parameterType instanceof Class<?> && ((Class<?>)parameterType).isArray()){
+ throw new IllegalStateException("No support for Arrays right now. Use "+Collection.class+" instead");
+ }
+ //if null is parsed as value we need to delete all values
+ if (value == null){
+ removeValues(property);
+ return null; //setter methods are void -> return null
+ }
+ //if a collection is parsed we need to check the generic type
+ if (Collection.class.isAssignableFrom(value.getClass())){
+ Type genericType = null;
+ if (parameterTypes[0] instanceof ParameterizedType){
+ for(Type typeArgument : ((ParameterizedType)parameterTypes[0]).getActualTypeArguments()){
+ if (genericType == null){
+ genericType = typeArgument;
+ } else {
+ //TODO: replace with a warning but for testing start with an exception
+ throw new IllegalStateException(
+ "Multiple generic type definition for method "+method.toString()
+ +" (generic types: "+((ParameterizedType) parameterTypes[0]).getActualTypeArguments()+")");
+ }
+ }
+ }
+ setValues(property, (Collection<?>) value);
+ return null;
+ } else {
+ setValue(property, value);
+ return null;
+ }
+ } else { //assume an read (getter) method
+ Class<?> returnType = method.getReturnType();
+ if (Collection.class.isAssignableFrom(returnType)){
+ Type genericType = null;
+ Type genericReturnType = method.getGenericReturnType();
+ if (genericReturnType instanceof ParameterizedType){
+ ParameterizedType type = (ParameterizedType) genericReturnType;
+ for (Type typeArgument : type.getActualTypeArguments()){
+ if (genericType == null){
+ genericType = typeArgument;
+ } else {
+ //TODO: replace with a warning but for testing start with an exception
+ throw new IllegalStateException(
+ "Multiple generic type definition for method "+method.toString()
+ +" (generic types: "+type.getActualTypeArguments()+")");
+ }
+ }
+ }
+ if (genericType == null){
+ throw new IllegalStateException(
+ "Generic Type not defined for Collection in Method "+method.toString()
+ +" (generic type is needed to correctly map rdf values for property "+property);
+ }
+ return getValues(property, (Class<?>)genericType);
+ } else {
+ return getValue(property, returnType);
+ }
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ private <T> T getValue(UriRef property, Class<T> type){
+ Iterator<Triple> results = factory.getGraph().filter(rdfNode, property, null);
+ if (results.hasNext()){
+ Resource result = results.next().getObject();
+ if (result instanceof NonLiteral){
+ if (RdfEntity.class.isAssignableFrom(type)){
+ return (T)factory.getProxy((NonLiteral)result, (Class<? extends RdfEntity>)type);
+ } else { //check result for UriRef and types UriRef, URI or URL
+ if(result instanceof UriRef){
+ if (UriRef.class.isAssignableFrom(type)){
+ return (T)result;
+ } else if (URI.class.isAssignableFrom(type)){
+ try {
+ return (T)new URI(((UriRef)result).getUnicodeString());
+ } catch (URISyntaxException e) {
+ throw new IllegalStateException("Unable to parse "+URI.class
+ +" for "+UriRef.class+" value="+((UriRef)result).getUnicodeString());
+ }
+ } else if (URL.class.isAssignableFrom(type)){
+ try {
+ return (T)new URL(((UriRef)result).getUnicodeString());
+ } catch (MalformedURLException e) {
+ throw new IllegalStateException("Unable to parse "+URL.class
+ +" for "+UriRef.class+" value="+((UriRef)result).getUnicodeString());
+ }
+ } else {
+ throw new IllegalArgumentException("Parsed Type "+type
+ +" is not compatible for result type "+result.getClass()
+ +" (value "+result+") of node "+rdfNode+" and property "+property
+ +"! (Subclass of RdfEntity, UriRef, URI or URL is expected for NonLiteral Values)");
+ }
+ } else {
+ throw new IllegalArgumentException("Parsed Type "+type
+ +" is not compatible for result type "+result.getClass()
+ +" (value "+result+") of node "+rdfNode+" and property "+property
+ +"! (Subclass of RdfEntity expected as type for NonLiteral values that are no instanceof UriRef)");
+ }
+ }
+ } else {
+ return literalFactory.createObject(type,(TypedLiteral) result);
+ }
+ } else {
+ return null;
+ }
+ }
+ private <T> Collection<T> getValues(UriRef property, Class<T> type){
+ return new RdfProxyPropertyCollection<T>(property, type);
+ }
+ private void setValue(UriRef property, Object value){
+ removeValues(property);
+ addValue(property, value);
+ }
+ private void setValues(UriRef property, Collection<?> values){
+ removeValues(property);
+ for(Object value : values){
+ addValue(property, value);
+ }
+ }
+ protected Resource getRdfResource(Object value) throws NoConvertorException{
+ if(value instanceof Resource){
+ //if the parsed object is already a Resource
+ return (Resource) value; //return it
+ } else if(value instanceof RdfEntity){ //check for other proxies
+ return ((RdfEntity)value).getId();
+ } else if(value instanceof URI){ //or URI links
+ return new UriRef(value.toString());
+ } else if(value instanceof URL){ //or URL links
+ return new UriRef(value.toString());
+ } else { //nothing of that
+ //try to make an Literal (Clarezza internal Adapters)
+ return literalFactory.createTypedLiteral(value);
+ }
+ }
+ private boolean addValue(UriRef property, Object value){
+ Resource rdfValue;
+ try {
+ rdfValue = getRdfResource(value);
+ return factory.getGraph().add(new TripleImpl(rdfNode, property, rdfValue));
+ } catch (NoConvertorException e){
+ throw new IllegalArgumentException("Unable to transform "+value.getClass()
+ +" to an RDF Node. Only "+RdfEntity.class+" and RDF Literal Types are supported");
+ }
+ }
+ private boolean removeValue(UriRef property, Object value){
+ Resource rdfValue;
+ try {
+ rdfValue = getRdfResource(value);
+ return factory.getGraph().remove(new TripleImpl(rdfNode, property, rdfValue));
+ } catch (NoConvertorException e){
+ throw new IllegalArgumentException("Unable to transform "+value.getClass()
+ +" to an RDF Node. Only "+RdfEntity.class+" and RDF Literal Types are supported");
+ }
+ }
+ private void removeValues(UriRef proptery){
+ Iterator<Triple> toRemove = factory.getGraph().filter(rdfNode, proptery, null);
+ while(toRemove.hasNext()){
+ factory.getGraph().remove(toRemove.next());
+ }
+ }
+
+ /**
+ * We need this class to apply changes in the collection to the MGraph.
+ * This collection implementation is a stateless wrapper over the
+ * triples selected by the subject,property pair over the MGraph!<br>
+ * Default implementation of {@link AbstractCollection} are very poor
+ * performance. Because of that this class overrides some methods
+ * already implemented by its abstract super class.
+ * @author westei
+ *
+ * @param <T>
+ */
+ private class RdfProxyPropertyCollection<T> extends AbstractCollection<T> {
+
+ //private final NonLiteral resource;
+ private final UriRef property;
+ private final Class<T> genericType;
+ private final boolean entity;
+ private final boolean uri;
+ private final boolean url;
+ private final boolean uriRef;
+
+ private RdfProxyPropertyCollection(UriRef property,Class<T> genericType) {
+ this.property = property;
+ this.genericType = genericType;
+ entity = RdfEntity.class.isAssignableFrom(genericType);
+ uri = URI.class.isAssignableFrom(genericType);
+ url = URL.class.isAssignableFrom(genericType);
+ uriRef = UriRef.class.isAssignableFrom(genericType);
+ }
+
+ @Override
+ public Iterator<T> iterator() {
+ return new Iterator<T>() {
+ Iterator<Triple> results = factory.getGraph().filter(rdfNode, property, null);
+ @Override
+ public boolean hasNext() {
+ return results.hasNext();
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public T next() {
+ Resource value = results.next().getObject();
+ if (entity){
+ //type checks are done within the constructor
+ return (T) factory.getProxy((NonLiteral)value, (Class<? extends RdfEntity>)genericType);
+ } else if(uri){
+ try {
+ return (T)new URI(((UriRef)value).getUnicodeString());
+ } catch (URISyntaxException e) {
+ throw new IllegalStateException("Unable to parse "+URI.class+" for "+UriRef.class+" value="+((UriRef)value).getUnicodeString());
+ }
+ } else if(url){
+ try {
+ return (T)new URL(((UriRef)value).getUnicodeString());
+ } catch (MalformedURLException e) {
+ throw new IllegalStateException("Unable to parse "+URL.class+" for "+UriRef.class+" value="+((UriRef)value).getUnicodeString());
+ }
+ } else if(uriRef){
+ return (T)value;
+ } else {
+ return literalFactory.createObject(genericType, (TypedLiteral)value);
+ }
+ }
+
+ @Override
+ public void remove() {
+ results.remove(); //no Idea if Clerezza implements that ^
+ }
+ };
+ }
+
+ @Override
+ public int size() {
+ Iterator<Triple> results = factory.getGraph().filter(rdfNode, property, null);
+ int size = 0;
+ for (;results.hasNext();size++){
+ results.next();
+ }
+ return size;
+ }
+ public boolean add(T value) {
+ return addValue(property, value);
+ }
+ @Override
+ public boolean remove(Object value) {
+ return removeValue(property,value);
+ }
+ @Override
+ public boolean isEmpty() {
+ return !factory.getGraph().filter(rdfNode, property, null).hasNext();
+ }
+ }
+}
Propchange: incubator/stanbol/trunk/enhancer/generic/rdfentities/src/main/java/org/apache/stanbol/enhancer/rdfentities/impl/RdfProxyInvocationHandler.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: incubator/stanbol/trunk/enhancer/generic/rdfentities/src/main/java/org/apache/stanbol/enhancer/rdfentities/impl/SimpleRdfEntityFactory.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/enhancer/generic/rdfentities/src/main/java/org/apache/stanbol/enhancer/rdfentities/impl/SimpleRdfEntityFactory.java?rev=1311019&view=auto
==============================================================================
--- incubator/stanbol/trunk/enhancer/generic/rdfentities/src/main/java/org/apache/stanbol/enhancer/rdfentities/impl/SimpleRdfEntityFactory.java (added)
+++ incubator/stanbol/trunk/enhancer/generic/rdfentities/src/main/java/org/apache/stanbol/enhancer/rdfentities/impl/SimpleRdfEntityFactory.java Sun Apr 8 15:12:40 2012
@@ -0,0 +1,58 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements. See the NOTICE file distributed with
+* this work for additional information regarding copyright ownership.
+* The ASF licenses this file to You under the Apache License, Version 2.0
+* (the "License"); you may not use this file except in compliance with
+* the License. You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package org.apache.stanbol.enhancer.rdfentities.impl;
+
+import java.lang.reflect.Proxy;
+
+import org.apache.clerezza.rdf.core.LiteralFactory;
+import org.apache.clerezza.rdf.core.MGraph;
+import org.apache.clerezza.rdf.core.NonLiteral;
+import org.apache.stanbol.enhancer.rdfentities.RdfEntity;
+import org.apache.stanbol.enhancer.rdfentities.RdfEntityFactory;
+
+
+public class SimpleRdfEntityFactory extends RdfEntityFactory {
+
+ private final MGraph graph;
+ private final LiteralFactory literalFactory;
+
+ public SimpleRdfEntityFactory(MGraph graph) {
+ if (graph == null){
+ throw new IllegalArgumentException("The MGraph parsed as parameter MUST NOT be NULL!");
+ }
+ this.graph = graph;
+ literalFactory = LiteralFactory.getInstance();
+ }
+
+ @SuppressWarnings("unchecked")
+ public <T extends RdfEntity> T getProxy(NonLiteral rdfNode, Class<T> type,Class<?>...additionalInterfaces) {
+ Class<?>[] interfaces = new Class<?>[additionalInterfaces.length+1];
+ interfaces[0] = type;
+ System.arraycopy(additionalInterfaces, 0, interfaces, 1, additionalInterfaces.length);
+ //Class<?> proxy = Proxy.getProxyClass(WrapperFactory.class.getClassLoader(), interfaces);
+ Object instance = Proxy.newProxyInstance(
+ SimpleRdfEntityFactory.class.getClassLoader(),
+ interfaces,
+ new RdfProxyInvocationHandler(this, rdfNode, interfaces, literalFactory));
+ return (T)instance;
+ }
+
+ protected MGraph getGraph() {
+ return graph;
+ }
+
+}
Propchange: incubator/stanbol/trunk/enhancer/generic/rdfentities/src/main/java/org/apache/stanbol/enhancer/rdfentities/impl/SimpleRdfEntityFactory.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: incubator/stanbol/trunk/enhancer/generic/rdfentities/src/test/java/org/apache/stanbol/enhancer/rdfentities/RdfEntityFactoryTest.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/enhancer/generic/rdfentities/src/test/java/org/apache/stanbol/enhancer/rdfentities/RdfEntityFactoryTest.java?rev=1311019&view=auto
==============================================================================
--- incubator/stanbol/trunk/enhancer/generic/rdfentities/src/test/java/org/apache/stanbol/enhancer/rdfentities/RdfEntityFactoryTest.java (added)
+++ incubator/stanbol/trunk/enhancer/generic/rdfentities/src/test/java/org/apache/stanbol/enhancer/rdfentities/RdfEntityFactoryTest.java Sun Apr 8 15:12:40 2012
@@ -0,0 +1,372 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements. See the NOTICE file distributed with
+* this work for additional information regarding copyright ownership.
+* The ASF licenses this file to You under the Apache License, Version 2.0
+* (the "License"); you may not use this file except in compliance with
+* the License. You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package org.apache.stanbol.enhancer.rdfentities;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.net.URI;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Calendar;
+import java.util.Collection;
+import java.util.Date;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Set;
+
+import org.apache.clerezza.rdf.core.MGraph;
+import org.apache.clerezza.rdf.core.Resource;
+import org.apache.clerezza.rdf.core.Triple;
+import org.apache.clerezza.rdf.core.UriRef;
+import org.apache.clerezza.rdf.core.impl.SimpleMGraph;
+import org.apache.stanbol.enhancer.rdfentities.Rdf;
+import org.apache.stanbol.enhancer.rdfentities.RdfEntity;
+import org.apache.stanbol.enhancer.rdfentities.RdfEntityFactory;
+import org.apache.stanbol.enhancer.servicesapi.rdf.Properties;
+import org.junit.Test;
+
+
+/**
+ * Tests the Factory, basic RdfEntity Methods and all features supported for
+ * Interfaces.
+ *
+ * @author Rupert Westenthaler
+ */
+public class RdfEntityFactoryTest {
+
+ @Test
+ public void testRdfEntity() throws Exception {
+ MGraph graph = new SimpleMGraph();
+ RdfEntityFactory factory = RdfEntityFactory.createInstance(graph);
+ String testUri = "urn:RdfEntityFactoryTest:TestEntity";
+ UriRef node = new UriRef(testUri);
+ RdfEntity rdfEntity = factory.getProxy(node, RdfEntity.class);
+ //TODO: Test type statement
+ //TODO: test getID Method
+ assertEquals(rdfEntity.getId(), node);
+ //TODO: Test equals
+ RdfEntity rdfEntity2 = factory.getProxy(node,RdfEntity.class);
+ assertEquals(rdfEntity, rdfEntity2);
+ //TODO: Test hashCode
+ assertEquals(rdfEntity.hashCode(), rdfEntity2.hashCode());
+ }
+ @Test
+ public void testPrimitiveDataTypes() throws Exception {
+ MGraph graph = new SimpleMGraph();
+ RdfEntityFactory factory = RdfEntityFactory.createInstance(graph);
+ String testUri = "urn:RdfEntityFactoryTest:TestEntity";
+ UriRef node = new UriRef(testUri);
+ TestRdfEntity testEntity = factory.getProxy(node, TestRdfEntity.class);
+
+ testEntity.setBoolean(true);
+ assertTrue(testEntity.getBoolean());
+
+ testEntity.setInteger(10);
+ assertEquals(new Integer(10), testEntity.getInteger());
+
+ testEntity.setLong(20l);
+ assertEquals(new Long(20), testEntity.getLong());
+
+ //TODO: Not supported by org.apache.clerezza.rdf.core.impl.SimpleLiteralFactory!
+ //testEntity.setFloat(0.1f);
+ //assertTrue(new Float(0.1f).equals(testEntity.getFloat()));
+
+ testEntity.setDouble(0.2);
+ assertEquals(new Double(0.2), testEntity.getDouble());
+
+ testEntity.setString("Test!");
+ assertEquals("Test!", testEntity.getString());
+
+ Date currentDate = new Date();
+ testEntity.setDate(currentDate);
+ assertEquals(currentDate, testEntity.getDate());
+
+ testEntity.setIntegers(Arrays.asList(new Integer(1),new Integer(2), new Integer(3)));
+ Collection<Integer> integers = testEntity.getIntegers();
+ assertTrue(integers.contains(new Integer(1)));
+ assertTrue(integers.contains(new Integer(2)));
+ assertTrue(integers.contains(new Integer(3)));
+
+ //test Remove
+ integers.remove(new Integer(2));
+ assertTrue(integers.contains(new Integer(1)));
+ assertTrue(!integers.contains(new Integer(2)));
+ assertTrue(integers.contains(new Integer(3)));
+
+ //get an new Collection and repeat the test
+ integers = testEntity.getIntegers();
+ assertTrue(integers.contains(new Integer(1)));
+ assertTrue(!integers.contains(new Integer(2)));
+ assertTrue(integers.contains(new Integer(3)));
+
+ //test Add
+ integers.add(new Integer(-1));
+ assertTrue(integers.contains(new Integer(-1)));
+ assertTrue(integers.contains(new Integer(1)));
+ assertTrue(integers.contains(new Integer(3)));
+
+ //again get a new collection
+ Collection<Integer>integers2 = testEntity.getIntegers();
+ assertTrue(integers2.contains(new Integer(-1)));
+ assertTrue(integers2.contains(new Integer(1)));
+ assertTrue(integers2.contains(new Integer(3)));
+
+ //remove/add an value in integers and test in integers2
+ integers.remove(new Integer(3));
+ integers.add(new Integer(0));
+ assertTrue(integers2.contains(new Integer(-1)));
+ assertTrue(integers2.contains(new Integer(0)));
+ assertTrue(integers2.contains(new Integer(1)));
+ assertTrue(!integers2.contains(new Integer(2)));
+ assertTrue(!integers2.contains(new Integer(3)));
+ }
+
+ @Test
+ public void testTypeStatements() throws Exception {
+ MGraph graph = new SimpleMGraph();
+ RdfEntityFactory factory = RdfEntityFactory.createInstance(graph);
+ String testUri = "urn:RdfEntityFactoryTest:TestEntity";
+ UriRef node = new UriRef(testUri);
+ TestRdfEntity entity = factory.getProxy(node, TestRdfEntity.class, new Class[]{TestRdfEntity2.class});
+ // test the if the proxy implements both interfaces
+ assertTrue(entity instanceof TestRdfEntity);
+ assertTrue(entity instanceof TestRdfEntity2);
+
+ Set<String> typeStrings = getRdfTypes(graph, node);
+ assertTrue(typeStrings.contains(TestRdfEntity.class.getAnnotation(Rdf.class).id()));
+ assertTrue(typeStrings.contains(TestRdfEntity2.class.getAnnotation(Rdf.class).id()));
+ }
+
+ @Test
+ public void testObjectProperties() throws Exception {
+ MGraph graph = new SimpleMGraph();
+ RdfEntityFactory factory = RdfEntityFactory.createInstance(graph);
+ String testUri = "urn:RdfEntityFactoryTest:TestEntity";
+ String testUri2 = "urn:RdfEntityFactoryTest:TestEntity2";
+ UriRef node = new UriRef(testUri);
+ UriRef node2 = new UriRef(testUri2);
+ TestRdfEntity entity = factory.getProxy(node, TestRdfEntity.class);
+ TestRdfEntity2 entity2 = factory.getProxy(node2, TestRdfEntity2.class);
+
+ URI testURI = new URI("urn:test:URI");
+ entity.setURI(testURI);
+ assertEquals(testURI, entity.getURI());
+
+ URL testURL = new URL("http://www.iks-project.eu");
+ entity.setURL(testURL);
+ assertEquals(testURL, entity.getURL());
+
+ entity.setUriRef(node2);
+ assertEquals(node2, entity.getUriRef());
+
+ entity2.setTestEntity(entity);
+ assertEquals(entity, entity2.getTestEntity());
+
+ Collection<TestRdfEntity> testEntities = entity2.getTestEntities();
+ assertTrue(testEntities.isEmpty()); //check that entity is not in the collection
+ Set<UriRef> testUriRefs = new HashSet<UriRef>();
+ int NUM = 10;
+ for (int i=0;i<NUM;i++){
+ UriRef testNode = new UriRef(testUri+':'+'_'+i);
+ testUriRefs.add(testNode);
+ testEntities.add(factory.getProxy(testNode, TestRdfEntity.class));
+ }
+ //now get a new collection and test if the added entities are there
+ Collection<UriRef> resultUriRefs = new ArrayList<UriRef>(); //add to a list to check for duplicates
+ for (TestRdfEntity e : entity2.getTestEntities()){
+ assertTrue(e.getId() instanceof UriRef); //I used UriRefs for the generation ...
+ resultUriRefs.add((UriRef)e.getId());
+ }
+ //now cross check
+ assertTrue(testUriRefs.containsAll(resultUriRefs));
+ assertTrue(resultUriRefs.containsAll(testUriRefs));
+ //now one could try to remove some Elements ...
+ // ... but things like that are already tested for Integers in testPrimitiveDataTypes
+ }
+
+ @Test
+ public void testInterfaceHierarchies() throws Exception {
+ MGraph graph = new SimpleMGraph();
+ RdfEntityFactory factory = RdfEntityFactory.createInstance(graph);
+ String testUri = "urn:RdfEntityFactoryTest:SubTestEntity";
+ String testUri2 = "urn:RdfEntityFactoryTest:TestEntity2";
+ String testUri3 = "urn:RdfEntityFactoryTest:TestEntity";
+ UriRef node = new UriRef(testUri);
+ UriRef node2 = new UriRef(testUri2);
+ UriRef node3 = new UriRef(testUri3);
+ SubTestRdfEntity entity = factory.getProxy(node, SubTestRdfEntity.class);
+ TestRdfEntity entity2 = factory.getProxy(node2, TestRdfEntity.class,new Class<?>[]{SubTestRdfEntity.class,TestRdfEntity2.class});
+ TestRdfEntity entity3 = factory.getProxy(node3, TestRdfEntity.class);
+
+ //Start with checking the types for entity2
+ //first type cast to the hierarchy
+ assertTrue(entity instanceof TestRdfEntity);
+ assertTrue(entity instanceof RdfEntity);
+
+ // test if the rdf:type triples are present in the MGraph
+ Set<String> typeStrings = getRdfTypes(graph, node);
+ assertTrue(typeStrings.contains(SubTestRdfEntity.class.getAnnotation(Rdf.class).id()));
+ assertTrue(typeStrings.contains(TestRdfEntity.class.getAnnotation(Rdf.class).id()));
+
+ typeStrings = null;
+ //now the same for entity2
+ //first type cast to the hierarchy
+ assertTrue(entity2 instanceof SubTestRdfEntity);
+ assertTrue(entity2 instanceof TestRdfEntity2);
+ assertTrue(entity2 instanceof RdfEntity);
+
+ // test if the rdf:type triples are present in the MGraph
+ typeStrings = getRdfTypes(graph, node2);
+ assertTrue(typeStrings.contains(SubTestRdfEntity.class.getAnnotation(Rdf.class).id()));
+ assertTrue(typeStrings.contains(TestRdfEntity.class.getAnnotation(Rdf.class).id()));
+ assertTrue(typeStrings.contains(TestRdfEntity2.class.getAnnotation(Rdf.class).id()));
+
+ typeStrings = null;
+ //Now check Entity3
+ assertTrue(!(entity3 instanceof SubTestRdfEntity));
+ assertTrue(entity3 instanceof TestRdfEntity);
+
+ //Now create an new Entity for the same Node that implements SubEntity2
+ SubTestRdfEntity entity4 = factory.getProxy(node3, SubTestRdfEntity.class);
+ //check if entity4 implements SubTestRefEntity
+ assertTrue(entity4 instanceof SubTestRdfEntity);
+
+ //now check if the additional type was added to node3
+ typeStrings = getRdfTypes(graph, node3);
+ assertTrue(typeStrings.contains(SubTestRdfEntity.class.getAnnotation(Rdf.class).id()));
+ assertTrue(typeStrings.contains(TestRdfEntity.class.getAnnotation(Rdf.class).id()));
+ //and that entity3 still dose not implement SubTestEntity
+ // ... because adding/removing rdf:type triples in the graph can not affect existing proxy instances!
+ assertTrue(!(entity3 instanceof SubTestRdfEntity));
+ }
+
+ private static Set<String> getRdfTypes(MGraph graph, UriRef node) {
+ Iterator<Triple> typeStatements = graph.filter(node, Properties.RDF_TYPE, null);
+ Set<String> typeStrings = new HashSet<String>();
+ while(typeStatements.hasNext()){
+ Resource type = typeStatements.next().getObject();
+ assertTrue(type instanceof UriRef);
+ typeStrings.add(((UriRef)type).getUnicodeString());
+ }
+ return typeStrings;
+ }
+
+ /**
+ * Interface to test primitive Datatypes and Uri links.
+ *
+ * @author westei
+ */
+ @Rdf(id="urn:test:TestRdfEntity")
+ public interface TestRdfEntity extends RdfEntity{
+ @Rdf(id="urn:test:Integer")
+ Integer getInteger();
+ @Rdf(id="urn:test:Integer")
+ void setInteger(Integer i);
+
+ @Rdf(id="urn:test:Integers")
+ Collection<Integer> getIntegers();
+ @Rdf(id="urn:test:Integers")
+ void setIntegers(Collection<Integer> is);
+
+ @Rdf(id="urn:test:Long")
+ Long getLong();
+ @Rdf(id="urn:test:Long")
+ void setLong(Long l);
+
+ @Rdf(id="urn:test:Float")
+ Float getFloat();
+ @Rdf(id="urn:test:Float")
+ void setFloat(Float f);
+
+ @Rdf(id="urn:test:Double")
+ Double getDouble();
+ @Rdf(id="urn:test:Double")
+ void setDouble(Double d);
+
+ @Rdf(id="urn:test:Boolean")
+ Boolean getBoolean();
+ @Rdf(id="urn:test:Boolean")
+ void setBoolean(Boolean b);
+
+ @Rdf(id="urn:test:Date")
+ Date getDate();
+ @Rdf(id="urn:test:Date")
+ void setDate(Date d);
+
+ @Rdf(id="urn:test:String")
+ String getString();
+ @Rdf(id="urn:test:String")
+ void setString(String string);
+
+ @Rdf(id="urn:test:Calendar")
+ Calendar getCalendar();
+ @Rdf(id="urn:test:Calendar")
+ void setCalendar(Calendar d);
+
+ @Rdf(id="urn:test:URI")
+ URI getURI();
+ @Rdf(id="urn:test:URI")
+ void setURI(URI uri);
+
+ @Rdf(id="urn:test:URL")
+ URL getURL();
+ @Rdf(id="urn:test:URL")
+ void setURL(URL uri);
+
+ @Rdf(id="urn:test:UriRef")
+ UriRef getUriRef();
+ @Rdf(id="urn:test:UriRef")
+ void setUriRef(UriRef uriRef);
+ }
+
+ /**
+ * Interface to test relations to other RdfEntities.
+ *
+ * @author westei
+ */
+ @Rdf(id="urn:test:TestRdfEntity2")
+ public interface TestRdfEntity2 extends RdfEntity {
+ @Rdf(id="urn:test:RdfEntity")
+ TestRdfEntity getTestEntity();
+ @Rdf(id="urn:test:RdfEntity")
+ void setTestEntity(TestRdfEntity testRdfEntity);
+
+ @Rdf(id="urn:test:RdfEntities")
+ Collection<TestRdfEntity> getTestEntities();
+ @Rdf(id="urn:test:RdfEntities")
+ void setTestEntities(Collection<TestRdfEntity> entities);
+ }
+
+ /**
+ * Interface to test extends relations between Interfaces.
+ *
+ * @author westei
+ */
+ @Rdf(id="urn:test:SubTestRdfEntity")
+ public interface SubTestRdfEntity extends TestRdfEntity {
+ @Rdf(id="urn:test:RdfEntity2")
+ TestRdfEntity2 getTestEntity2();
+ @Rdf(id="urn:test:RdfEntity2")
+ void setTestEntity2(TestRdfEntity2 entity2);
+ }
+
+// public static void main(String[] args) throws Exception{
+// new RdfEntityFactoryTest().testTypeStatements();
+// }
+}
Propchange: incubator/stanbol/trunk/enhancer/generic/rdfentities/src/test/java/org/apache/stanbol/enhancer/rdfentities/RdfEntityFactoryTest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: incubator/stanbol/trunk/enhancer/generic/servicesapi/pom.xml
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/enhancer/generic/servicesapi/pom.xml?rev=1311019&r1=1311018&r2=1311019&view=diff
==============================================================================
--- incubator/stanbol/trunk/enhancer/generic/servicesapi/pom.xml (original)
+++ incubator/stanbol/trunk/enhancer/generic/servicesapi/pom.xml Sun Apr 8 15:12:40 2012
@@ -57,10 +57,10 @@
<Export-Package>
org.apache.stanbol.enhancer.servicesapi;version=${project.version},
org.apache.stanbol.enhancer.servicesapi.helper;version=${project.version},
+ org.apache.stanbol.enhancer.servicesapi.impl;version=${project.version},
org.apache.stanbol.enhancer.servicesapi.rdf;version=${project.version}
</Export-Package>
<Private-Package>
- org.apache.stanbol.enhancer.servicesapi.helper.impl;version=${project.version}
</Private-Package>
</instructions>
</configuration>
Modified: incubator/stanbol/trunk/enhancer/generic/servicesapi/src/main/java/org/apache/stanbol/enhancer/servicesapi/Blob.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/enhancer/generic/servicesapi/src/main/java/org/apache/stanbol/enhancer/servicesapi/Blob.java?rev=1311019&r1=1311018&r2=1311019&view=diff
==============================================================================
--- incubator/stanbol/trunk/enhancer/generic/servicesapi/src/main/java/org/apache/stanbol/enhancer/servicesapi/Blob.java (original)
+++ incubator/stanbol/trunk/enhancer/generic/servicesapi/src/main/java/org/apache/stanbol/enhancer/servicesapi/Blob.java Sun Apr 8 15:12:40 2012
@@ -32,6 +32,8 @@ public interface Blob {
String getMimeType();
/**
+ * Getter for the data of this blob in form of an {@link InputStream}.
+ * Multiple calls need to return multiple instances of InputStreams
* @return a stream of the data of this blog
*/
InputStream getStream();
Added: incubator/stanbol/trunk/enhancer/generic/servicesapi/src/main/java/org/apache/stanbol/enhancer/servicesapi/ContentItemFactory.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/enhancer/generic/servicesapi/src/main/java/org/apache/stanbol/enhancer/servicesapi/ContentItemFactory.java?rev=1311019&view=auto
==============================================================================
--- incubator/stanbol/trunk/enhancer/generic/servicesapi/src/main/java/org/apache/stanbol/enhancer/servicesapi/ContentItemFactory.java (added)
+++ incubator/stanbol/trunk/enhancer/generic/servicesapi/src/main/java/org/apache/stanbol/enhancer/servicesapi/ContentItemFactory.java Sun Apr 8 15:12:40 2012
@@ -0,0 +1,192 @@
+package org.apache.stanbol.enhancer.servicesapi;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.clerezza.rdf.core.MGraph;
+import org.apache.clerezza.rdf.core.Triple;
+import org.apache.clerezza.rdf.core.UriRef;
+import org.apache.stanbol.enhancer.servicesapi.helper.ContentItemHelper;
+
+/**
+ * OSGI service to be used to create {@link ContentItem}s and Blobs.
+ *
+ * @since 0.9.1-incubating
+ */
+public interface ContentItemFactory {
+
+ /**
+ * Creates a new ContentItem for the passed {@link ContentSource} and
+ * generates as unique ID from the passed content.
+ * Implementors might want to use
+ * {@link ContentItemHelper#streamDigest(InputStream, java.io.OutputStream, String)
+ * for generating an ID while reading the data from the ContentSource.
+ * @param source The content source
+ * @return the {@link ContentItem} with a generated id and the passed
+ * content as content-part of type {@link Blob} at index <code>0</code>
+ * @throws IllegalArgumentException if <code>null</code> is passed as content
+ * source or the passed content source is already consumed.
+ * @throws IOException on any error while reading the content from the
+ * content source.
+ */
+ ContentItem createContentItem(ContentSource source) throws IOException;
+ /**
+ * Creates a new ContentItem for the passed content source and an
+ * ID relative to the passed prefix.
+ * @param prefix the URI prefix used generate the URI of the content item.
+ * Note the only a generated ID will be added to the passed prefix. So passed
+ * values should typically end with an separator char (e.g. '/', '#', ':').
+ * Implementors might want to use
+ * {@link ContentItemHelper#streamDigest(InputStream, java.io.OutputStream, String)
+ * for generating an ID while reading the data from the ContentSource.
+ * @param source The content source
+ * @return the {@link ContentItem} with a generated id and the passed
+ * content as content-part of type {@link Blob} at index <code>0</code>
+ * @throws IllegalArgumentException if <code>null</code> is passed as content
+ * source, the content source is already consumed or the passed prefix is
+ * <code>null</code>
+ * @throws IOException on any error while reading the content from the
+ * content source.
+ */
+ ContentItem createContentItem(String prefix, ContentSource source) throws IOException;
+ /**
+ * Creates a new ContentItem for the passed id and content source.
+ * @param id the id for the ContentItem or <code>null</code> to generate an id.
+ * If <code>null</code> is passed as ID, implementors might want to use
+ * {@link ContentItemHelper#streamDigest(InputStream, java.io.OutputStream, String)
+ * for generating an ID while reading the data from the ContentSource.
+ * @param source The content source
+ * @return the {@link ContentItem} with a passed/generated id and the passed
+ * content as content-part of type {@link Blob} at index <code>0</code>
+ * @throws IllegalArgumentException if <code>null</code> is passed as content
+ * source, the content source is already consumed or the
+ * passed id is not <code>null</code> but empty.
+ * @throws IOException on any error while reading the content from the
+ * content source.
+ */
+ ContentItem createContentItem(UriRef id, ContentSource source) throws IOException;
+ /**
+ * Creates a new ContentItem for the passed id and content source.
+ * @param prefix the URI prefix used generate the URI of the content item.
+ * Note the only a generated ID will be added to the passed prefix. So passed
+ * values should typically end with an separator char (e.g. '/', '#', ':').
+ * Implementors might want to use
+ * {@link ContentItemHelper#streamDigest(InputStream, java.io.OutputStream, String)
+ * for generating an ID while reading the data from the ContentSource
+ * @param source The content source
+ * @param metadata an {@link MGraph} with the metadata or <code>null</code>
+ * if none. Implementation are free to use the passed instance or to generate
+ * a new one. However they MUST ensure that all {@link Triple}s contained by
+ * the passed graph are also added to the {@link ContentItem#getMetadata()
+ * metadata} of the returned ContentItem.
+ * @return the {@link ContentItem} with a passed/generated id and the passed
+ * content as content-part of type {@link Blob} at index <code>0</code>
+ * @throws IllegalArgumentException if <code>null</code> is passed as content
+ * source, the content source is already consumed or the
+ * passed prefix is <code>null</code>.
+ * @throws IOException on any error while reading the content from the
+ * content source.
+ */
+ ContentItem createContentItem(String prefix, ContentSource source, MGraph metadata) throws IOException;
+ /**
+ * Creates a new ContentItem for the passed id and content source.
+ * @param id the id for the ContentItem or <code>null</code> to generate an id.
+ * If <code>null</code> is passed as ID, implementors might want to use
+ * {@link ContentItemHelper#streamDigest(InputStream, java.io.OutputStream, String)
+ * for generating an ID while reading the data from the ContentSource.
+ * @param source The content source
+ * @param metadata an {@link MGraph} with the metadata or <code>null</code>
+ * if none. Implementation are free to use the passed instance or to generate
+ * a new one. However they MUST ensure that all {@link Triple}s contained by
+ * the passed graph are also added to the {@link ContentItem#getMetadata()
+ * metadata} of the returned ContentItem.
+ * @return the {@link ContentItem} with a passed/generated id and the passed
+ * content as content-part of type {@link Blob} at index <code>0</code>
+ * @throws IllegalArgumentException if <code>null</code> is passed as content
+ * source, the content source is already consumed or the
+ * passed id is not <code>null</code> but empty.
+ * @throws IOException on any error while reading the content from the
+ * content source.
+ */
+ ContentItem createContentItem(UriRef id, ContentSource source, MGraph metadata) throws IOException;
+ /**
+ * Creates a new ContentItem for the passed {@link ContentReference}. The
+ * {@link ContentReference#getReference()} is used as ID for the content
+ * item. Implementations might choose to {@link ContentReference#dereference()
+ * dereference}
+ * the reference at creation if needed.
+ * @param reference the reference to the content
+ * @return the {@link ContentItem} with the {@link ContentReference#getReference()}
+ * as ID.
+ * @throws IOException if the implementation {@link ContentReference#dereference()
+ * dereferences} the {@link ContentReference} during creation and this action
+ * fails.
+ * @throws IllegalArgumentException if the passed {@link ContentReference}
+ * is <code>null</code>.
+ */
+ ContentItem createContentItem(ContentReference reference) throws IOException;
+ /**
+ * Creates a new ContentItem for the passed {@link ContentReference}. The
+ * {@link ContentReference#getReference()} is used as ID for the content
+ * item. Implementations might choose to {@link ContentReference#dereference()
+ * dereference}
+ * the reference at creation if needed.
+ * @param reference the reference to the content
+ * @param metadata an {@link MGraph} with the metadata or <code>null</code>
+ * if none. Implementation are free to use the passed instance or to generate
+ * a new one. However they MUST ensure that all {@link Triple}s contained by
+ * the passed graph are also added to the {@link ContentItem#getMetadata()
+ * metadata} of the returned ContentItem.
+ * @return the {@link ContentItem} with the {@link ContentReference#getReference()}
+ * as ID.
+ * @throws IOException if the implementation {@link ContentReference#dereference()
+ * dereferences} the {@link ContentReference} during creation and this action
+ * fails.
+ * @throws IllegalArgumentException if the passed {@link ContentReference}
+ * is <code>null</code>.
+ */
+ ContentItem createContentItem(ContentReference reference, MGraph metadata) throws IOException;
+ /**
+ * Creates a new Blob based on the passed {@link ContentSource}
+ * @param source The source
+ * @return the Blob
+ * @throws IllegalArgumentException of the passed source is <code>null</code>
+ * @throws IllegalStateException if the passed source is already consumed
+ * @throws IOException on any error while reading the content from the source.
+ */
+ Blob createBlob(ContentSource source) throws IOException;
+ /**
+ * Creates a new Blob based on the passed {@link ContentReference}. If the
+ * content reference is {@link ContentReference#dereference() dereferenced}
+ * during the construction or at an later point in time is implementation
+ * dependent.
+ * @param reference The reference to the content
+ * @return the Blob
+ * @throws IllegalArgumentException of the passed reference is <code>null</code>
+ * @throws IOException on any error while dereferencing the passed reference.
+ */
+ Blob createBlob(ContentReference reference) throws IOException;
+
+ /**
+ * Creates a {@link ContentSink} that allows to "stream" content to an
+ * newly created {@link Blob}. This is intended to be used by
+ * {@link EnhancementEngine}s that transform the parsed content and want
+ * to store "stream" the transformation result to a new {@link Blob} that
+ * can be later added to the {@link ContentItem}. <p>
+ * <b>IMPORTANT NOTE:</b> Do not parse the {@link Blob} of a {@link ContentSink}
+ * to any other components until all the data are written to the
+ * {@link OutputStream} (see {@link ContentSink} for details).
+ * @param mediaType the mediaType for the created blob. "application/octet-stream" is
+ * used as default if <code>null</code>. For textual content the charset should be
+ * added as parameter (e.g. "text/plain; charset=UTF-8"). If no charset is
+ * present Stanbol will assume that the text is encoded using <code>UTF-8</code>.
+ * @return The {@link ContentSink} providing both the {@link Blob} and an
+ * {@link OutputStream} allowing to write the data for this {@link Blob}.
+ * @throws IOException On any error while creating the Blob.
+ */
+ ContentSink createContentSink(String mediaType) throws IOException;
+
+}
Propchange: incubator/stanbol/trunk/enhancer/generic/servicesapi/src/main/java/org/apache/stanbol/enhancer/servicesapi/ContentItemFactory.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: incubator/stanbol/trunk/enhancer/generic/servicesapi/src/main/java/org/apache/stanbol/enhancer/servicesapi/ContentReference.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/enhancer/generic/servicesapi/src/main/java/org/apache/stanbol/enhancer/servicesapi/ContentReference.java?rev=1311019&view=auto
==============================================================================
--- incubator/stanbol/trunk/enhancer/generic/servicesapi/src/main/java/org/apache/stanbol/enhancer/servicesapi/ContentReference.java (added)
+++ incubator/stanbol/trunk/enhancer/generic/servicesapi/src/main/java/org/apache/stanbol/enhancer/servicesapi/ContentReference.java Sun Apr 8 15:12:40 2012
@@ -0,0 +1,22 @@
+package org.apache.stanbol.enhancer.servicesapi;
+
+import java.io.IOException;
+
+
+/**
+ * A reference to the content. This allows to {@link #dereference()} the
+ * content when it is used.
+ */
+public interface ContentReference {
+ /**
+ * The String representation of this reference.
+ * @return the reference string
+ */
+ String getReference();
+ /**
+ * Dereferences this content reference
+ * @return the referenced {@link ContentSource}
+ * @throws IOException on any error while dereferencing the source
+ */
+ ContentSource dereference() throws IOException;
+}
\ No newline at end of file
Propchange: incubator/stanbol/trunk/enhancer/generic/servicesapi/src/main/java/org/apache/stanbol/enhancer/servicesapi/ContentReference.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: incubator/stanbol/trunk/enhancer/generic/servicesapi/src/main/java/org/apache/stanbol/enhancer/servicesapi/ContentSink.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/enhancer/generic/servicesapi/src/main/java/org/apache/stanbol/enhancer/servicesapi/ContentSink.java?rev=1311019&view=auto
==============================================================================
--- incubator/stanbol/trunk/enhancer/generic/servicesapi/src/main/java/org/apache/stanbol/enhancer/servicesapi/ContentSink.java (added)
+++ incubator/stanbol/trunk/enhancer/generic/servicesapi/src/main/java/org/apache/stanbol/enhancer/servicesapi/ContentSink.java Sun Apr 8 15:12:40 2012
@@ -0,0 +1,39 @@
+package org.apache.stanbol.enhancer.servicesapi;
+
+import java.io.OutputStream;
+
+/**
+ * A {@link ContentSink} allows to stream content to a {@link Blob}. This
+ * allows to "stream" content that is created by the Stanbol Enhancer (e.g.
+ * the plain text extracted from an PDF) directly to the Blob and therefore
+ * greatly reduces the memory footprint if dealing with very large
+ * content.<p>
+ * <b>IMPORTANT NOTE:</b> Do not parse the {@link Blob} of a {@link ContentSink}
+ * to any other components until all the data are written to the
+ * {@link OutputStream}, because this may cause that other components to read
+ * partial data when calling {@link Blob#getStream()}!<br>
+ * This feature is intended to reduce the memory footprint and not to support
+ * concurrent writing and reading of data as supported by pipes. However
+ * if you can come-up with a nice use case that would require this ability
+ * let us know on the stanbol-dev mailing list or by creating an
+ * <a href=https://issues.apache.org/jira/browse/STANBOL>issue</a>.
+ */
+public interface ContentSink {
+ /**
+ * The output stream - sink - for the content. Multiple calls to
+ * this method will return the same {@link OutputStream}.<p>
+ * Users need to ensure that the provided stream is correctly closed
+ * after all the content was written to it.
+ * @return the output stream used to stream the content to the
+ * {@link Blob}
+ */
+ OutputStream getOutputStream();
+ /**
+ * The - initially empty - Blob. if this Blob is shared with other
+ * components (e.g. added to an {@link ContentItem}) before all data are
+ * written to the sink, than other engines will be able to access it
+ * even while data are still added to blob.
+ * @return The blob.
+ */
+ Blob getBlob();
+}
\ No newline at end of file
Propchange: incubator/stanbol/trunk/enhancer/generic/servicesapi/src/main/java/org/apache/stanbol/enhancer/servicesapi/ContentSink.java
------------------------------------------------------------------------------
svn:mime-type = text/plain