This is an automated email from the ASF dual-hosted git repository. hasan pushed a commit to branch reunited in repository https://gitbox.apache.org/repos/asf/clerezza.git
commit 0ff3e25afdd2117e6c635d2b8a343f6cc2453459 Author: Hasan <[email protected]> AuthorDate: Wed Nov 14 03:54:16 2018 +0100 CLEREZZA-1026: Copy additional classes from rdf.core to api.impl and refactor api.impl --- .../api/impl/{ => graph}/AbstractGraph.java | 3 +- .../impl/{ => graph}/AbstractImmutableGraph.java | 2 +- .../api/impl/{ => graph}/DelayedNotificator.java | 2 +- .../api/impl/{ => graph}/LockingIterator.java | 2 +- .../api/impl/graph/PrivilegedGraphWrapper.java | 254 ++++++++++ .../PrivilegedImmuatbleGraphWrapper.java} | 42 +- .../ReadOnlyException.java} | 46 +- .../api/impl/{simple => graph}/SimpleGraph.java | 19 +- .../{simple => graph}/SimpleImmutableGraph.java | 4 +- .../api/impl/{simple => graph}/SimpleMGraph.java | 2 +- .../impl/{ => graph}/WatchableGraphWrapper.java | 24 +- .../clerezza/api/impl/graph/WriteBlockedGraph.java | 113 +++++ .../api/impl/graphmatching/GraphMatcher.java | 2 +- .../api/impl/{ => literal}/AbstractLiteral.java | 2 +- .../InvalidLiteralTypeException.java} | 40 +- .../clerezza/api/impl/literal/LiteralFactory.java | 83 +++ .../api/impl/{ => literal}/LiteralImpl.java | 2 +- .../NoConvertorException.java} | 39 +- .../api/impl/{ => literal}/PlainLiteralImpl.java | 2 +- .../api/impl/literal/SimpleLiteralFactory.java | 305 +++++++++++ .../api/impl/{ => literal}/TypedLiteralImpl.java | 2 +- .../org/apache/clerezza/api/impl/package-info.java | 3 - .../org/apache/clerezza/api/impl/util/Base64.java | 120 +++++ .../clerezza/api/impl/util/W3CDateFormat.java | 196 +++++++ .../apache/clerezza/api/impl/graph/GraphTest.java | 563 +++++++++++++++++++++ .../impl/{simple => graph}/SimpleGraphTest.java | 3 +- .../api/impl/graphmatching/GraphMatcherTest.java | 2 +- .../api/impl/graphmatching/Utils4Testing.java | 2 +- .../{simple => literal}/PlainLiteralImplTest.java | 4 +- .../impl/{simple => literal}/TripleImplTest.java | 2 +- .../{simple => literal}/TypedLiteralImplTest.java | 2 +- 31 files changed, 1716 insertions(+), 171 deletions(-) diff --git a/api.impl/src/main/java/org/apache/clerezza/api/impl/AbstractGraph.java b/api.impl/src/main/java/org/apache/clerezza/api/impl/graph/AbstractGraph.java similarity index 98% rename from api.impl/src/main/java/org/apache/clerezza/api/impl/AbstractGraph.java rename to api.impl/src/main/java/org/apache/clerezza/api/impl/graph/AbstractGraph.java index ab236b6..b4e996e 100644 --- a/api.impl/src/main/java/org/apache/clerezza/api/impl/AbstractGraph.java +++ b/api.impl/src/main/java/org/apache/clerezza/api/impl/graph/AbstractGraph.java @@ -16,7 +16,7 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.clerezza.api.impl; +package org.apache.clerezza.api.impl.graph; import java.util.AbstractCollection; import java.util.Collection; @@ -32,7 +32,6 @@ import org.apache.clerezza.api.BlankNodeOrIRI; import org.apache.clerezza.api.RDFTerm; import org.apache.clerezza.api.Triple; import org.apache.clerezza.api.ImmutableGraph; -import org.apache.clerezza.api.impl.simple.SimpleImmutableGraph; import org.apache.clerezza.api.impl.debug.ReentrantReadWriteLockTracker; /** diff --git a/api.impl/src/main/java/org/apache/clerezza/api/impl/AbstractImmutableGraph.java b/api.impl/src/main/java/org/apache/clerezza/api/impl/graph/AbstractImmutableGraph.java similarity index 98% rename from api.impl/src/main/java/org/apache/clerezza/api/impl/AbstractImmutableGraph.java rename to api.impl/src/main/java/org/apache/clerezza/api/impl/graph/AbstractImmutableGraph.java index c46f0fc..a9c35b6 100644 --- a/api.impl/src/main/java/org/apache/clerezza/api/impl/AbstractImmutableGraph.java +++ b/api.impl/src/main/java/org/apache/clerezza/api/impl/graph/AbstractImmutableGraph.java @@ -16,7 +16,7 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.clerezza.api.impl; +package org.apache.clerezza.api.impl.graph; import java.util.Collection; import java.util.Iterator; diff --git a/api.impl/src/main/java/org/apache/clerezza/api/impl/DelayedNotificator.java b/api.impl/src/main/java/org/apache/clerezza/api/impl/graph/DelayedNotificator.java similarity index 98% rename from api.impl/src/main/java/org/apache/clerezza/api/impl/DelayedNotificator.java rename to api.impl/src/main/java/org/apache/clerezza/api/impl/graph/DelayedNotificator.java index 39d52f5..a3512e2 100644 --- a/api.impl/src/main/java/org/apache/clerezza/api/impl/DelayedNotificator.java +++ b/api.impl/src/main/java/org/apache/clerezza/api/impl/graph/DelayedNotificator.java @@ -16,7 +16,7 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.clerezza.api.impl; +package org.apache.clerezza.api.impl.graph; import java.lang.ref.WeakReference; import java.util.*; diff --git a/api.impl/src/main/java/org/apache/clerezza/api/impl/LockingIterator.java b/api.impl/src/main/java/org/apache/clerezza/api/impl/graph/LockingIterator.java similarity index 97% rename from api.impl/src/main/java/org/apache/clerezza/api/impl/LockingIterator.java rename to api.impl/src/main/java/org/apache/clerezza/api/impl/graph/LockingIterator.java index 84b73de..4db0d85 100644 --- a/api.impl/src/main/java/org/apache/clerezza/api/impl/LockingIterator.java +++ b/api.impl/src/main/java/org/apache/clerezza/api/impl/graph/LockingIterator.java @@ -16,7 +16,7 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.clerezza.api.impl; +package org.apache.clerezza.api.impl.graph; import java.util.Iterator; import java.util.concurrent.locks.Lock; diff --git a/api.impl/src/main/java/org/apache/clerezza/api/impl/graph/PrivilegedGraphWrapper.java b/api.impl/src/main/java/org/apache/clerezza/api/impl/graph/PrivilegedGraphWrapper.java new file mode 100644 index 0000000..ce682b0 --- /dev/null +++ b/api.impl/src/main/java/org/apache/clerezza/api/impl/graph/PrivilegedGraphWrapper.java @@ -0,0 +1,254 @@ +/* + * 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.clerezza.api.impl.graph; + +import org.apache.clerezza.api.*; + +import java.security.AccessController; +import java.security.PrivilegedAction; +import java.util.Collection; +import java.util.Iterator; +import java.util.concurrent.locks.ReadWriteLock; + +/** + * Calls the methods of the wrapped <code>Graph</code> as privileged + * code, because they may need permissions like writing to disk or accessing + * network. + * + * @author mir + */ +public class PrivilegedGraphWrapper implements Graph { + + private Graph graph; + + public PrivilegedGraphWrapper(Graph Graph) { + this.graph = Graph; + } + + @Override + public Iterator<Triple> filter(final BlankNodeOrIRI subject, final IRI predicate, + final RDFTerm object) { + return AccessController.doPrivileged(new PrivilegedAction<Iterator<Triple>>() { + + @Override + public Iterator<Triple> run() { + return graph.filter(subject, predicate, object); + } + }); + } + + @Override + public int size() { + return AccessController.doPrivileged(new PrivilegedAction<Integer>() { + + @Override + public Integer run() { + return graph.size(); + } + }); + } + + @Override + public boolean isEmpty() { + return AccessController.doPrivileged(new PrivilegedAction<Boolean>() { + + @Override + public Boolean run() { + return graph.isEmpty(); + } + }); + } + + @Override + public boolean contains(final Object o) { + return AccessController.doPrivileged(new PrivilegedAction<Boolean>() { + + @Override + public Boolean run() { + return graph.contains(o); + } + }); + } + + @Override + public Iterator<Triple> iterator() { + return AccessController.doPrivileged(new PrivilegedAction<Iterator<Triple>>() { + + @Override + public Iterator<Triple> run() { + return graph.iterator(); + } + }); + } + + @Override + public Object[] toArray() { + return AccessController.doPrivileged(new PrivilegedAction<Object[]>() { + + @Override + public Object[] run() { + return graph.toArray(); + } + }); + } + + @Override + public <T> T[] toArray(final T[] a) { + return AccessController.doPrivileged(new PrivilegedAction<T[]>() { + + @Override + public T[] run() { + return graph.toArray(a); + } + }); + } + + @Override + public boolean add(final Triple triple) { + return AccessController.doPrivileged(new PrivilegedAction<Boolean>() { + + @Override + public Boolean run() { + return graph.add(triple); + } + }); + } + + @Override + public boolean remove(final Object o) { + return AccessController.doPrivileged(new PrivilegedAction<Boolean>() { + + @Override + public Boolean run() { + return graph.remove(o); + } + }); + } + + @Override + public boolean containsAll(final Collection<?> c) { + return AccessController.doPrivileged(new PrivilegedAction<Boolean>() { + + @Override + public Boolean run() { + return graph.containsAll(c); + } + }); + } + + @Override + public boolean addAll(final Collection<? extends Triple> c) { + return AccessController.doPrivileged(new PrivilegedAction<Boolean>() { + + @Override + public Boolean run() { + return graph.addAll(c); + } + }); + } + + @Override + public boolean removeAll(final Collection<?> c) { + return AccessController.doPrivileged(new PrivilegedAction<Boolean>() { + + @Override + public Boolean run() { + return graph.removeAll(c); + } + }); + } + + @Override + public boolean retainAll(final Collection<?> c) { + return AccessController.doPrivileged(new PrivilegedAction<Boolean>() { + + @Override + public Boolean run() { + return graph.retainAll(c); + } + }); + } + + @Override + public void clear() { + AccessController.doPrivileged(new PrivilegedAction<Object>() { + + @Override + public Object run() { + graph.clear(); + return null; + } + }); + } + + @Override + public ReadWriteLock getLock() { + return graph.getLock(); + } + + private static class PriviledgedTripleIterator implements Iterator<Triple> { + + private final Iterator<Triple> wrappedIterator; + + public PriviledgedTripleIterator(Iterator<Triple> wrappedIterator) { + this.wrappedIterator = wrappedIterator; + } + + @Override + public boolean hasNext() { + return AccessController.doPrivileged(new PrivilegedAction<Boolean>() { + + @Override + public Boolean run() { + return wrappedIterator.hasNext(); + } + }); + } + + @Override + public Triple next() { + return AccessController.doPrivileged(new PrivilegedAction<Triple>() { + + @Override + public Triple run() { + return wrappedIterator.next(); + } + }); + } + + @Override + public void remove() { + AccessController.doPrivileged(new PrivilegedAction<Object>() { + + @Override + public Object run() { + wrappedIterator.remove(); + return null; + } + }); + } + } + + + + @Override + public ImmutableGraph getImmutableGraph() { + return new SimpleImmutableGraph(this); + } +} diff --git a/api.impl/src/main/java/org/apache/clerezza/api/impl/simple/SimpleMGraph.java b/api.impl/src/main/java/org/apache/clerezza/api/impl/graph/PrivilegedImmuatbleGraphWrapper.java similarity index 54% copy from api.impl/src/main/java/org/apache/clerezza/api/impl/simple/SimpleMGraph.java copy to api.impl/src/main/java/org/apache/clerezza/api/impl/graph/PrivilegedImmuatbleGraphWrapper.java index da961c2..f9367ac 100644 --- a/api.impl/src/main/java/org/apache/clerezza/api/impl/simple/SimpleMGraph.java +++ b/api.impl/src/main/java/org/apache/clerezza/api/impl/graph/PrivilegedImmuatbleGraphWrapper.java @@ -16,41 +16,21 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.clerezza.api.impl.simple; +package org.apache.clerezza.api.impl.graph; -import java.util.Collection; -import java.util.Iterator; -import java.util.Set; - -import org.apache.clerezza.api.Graph; -import org.apache.clerezza.api.Triple; +import org.apache.clerezza.api.ImmutableGraph; /** - * - * @deprecated Use SimpleGraph - * @author reto + * Calls the methods of the wrapped <code>ImmutableGraph</code> as privileged + * code, because they may need permissions like writing to disk or accessing + * network. + * + * @author mir */ -@Deprecated -public class SimpleMGraph extends SimpleGraph implements Graph { - - /** - * Creates an empty SimpleMGraph - */ - public SimpleMGraph() { - } - - public SimpleMGraph(Set<Triple> baseSet) { - super(baseSet); - } - - public SimpleMGraph(Collection<Triple> baseCollection) { - super(baseCollection); - } +public class PrivilegedImmuatbleGraphWrapper extends PrivilegedGraphWrapper + implements ImmutableGraph { - public SimpleMGraph(Iterator<Triple> iterator) { - super(iterator); + public PrivilegedImmuatbleGraphWrapper(ImmutableGraph ImmutableGraph) { + super(ImmutableGraph); } - } - - \ No newline at end of file diff --git a/api.impl/src/main/java/org/apache/clerezza/api/impl/simple/SimpleMGraph.java b/api.impl/src/main/java/org/apache/clerezza/api/impl/graph/ReadOnlyException.java similarity index 55% copy from api.impl/src/main/java/org/apache/clerezza/api/impl/simple/SimpleMGraph.java copy to api.impl/src/main/java/org/apache/clerezza/api/impl/graph/ReadOnlyException.java index da961c2..ea5456f 100644 --- a/api.impl/src/main/java/org/apache/clerezza/api/impl/simple/SimpleMGraph.java +++ b/api.impl/src/main/java/org/apache/clerezza/api/impl/graph/ReadOnlyException.java @@ -16,41 +16,35 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.clerezza.api.impl.simple; +package org.apache.clerezza.api.impl.graph; -import java.util.Collection; -import java.util.Iterator; -import java.util.Set; - -import org.apache.clerezza.api.Graph; -import org.apache.clerezza.api.Triple; +import java.security.AccessControlException; /** + * is thrown on an attempt to add or remove triples to a + * read-only mutable ImmutableGraph * - * @deprecated Use SimpleGraph - * @author reto + * @author tsuy */ -@Deprecated -public class SimpleMGraph extends SimpleGraph implements Graph { +public class ReadOnlyException extends AccessControlException { + + private String action; /** - * Creates an empty SimpleMGraph + * creates an exception indicating that the mutable ImmutableGraph is read-only + * + * @param action */ - public SimpleMGraph() { - } - - public SimpleMGraph(Set<Triple> baseSet) { - super(baseSet); - } - - public SimpleMGraph(Collection<Triple> baseCollection) { - super(baseCollection); + public ReadOnlyException(String action) { + super("read only mutable ImmutableGraph, not allowed to "+action); + this.action = action; } - public SimpleMGraph(Iterator<Triple> iterator) { - super(iterator); + /** + * + * @return the name of the action + */ + public String getAction() { + return action; } - } - - \ No newline at end of file diff --git a/api.impl/src/main/java/org/apache/clerezza/api/impl/simple/SimpleGraph.java b/api.impl/src/main/java/org/apache/clerezza/api/impl/graph/SimpleGraph.java similarity index 92% rename from api.impl/src/main/java/org/apache/clerezza/api/impl/simple/SimpleGraph.java rename to api.impl/src/main/java/org/apache/clerezza/api/impl/graph/SimpleGraph.java index 7220532..0209c28 100644 --- a/api.impl/src/main/java/org/apache/clerezza/api/impl/simple/SimpleGraph.java +++ b/api.impl/src/main/java/org/apache/clerezza/api/impl/graph/SimpleGraph.java @@ -16,25 +16,12 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.clerezza.api.impl.simple; +package org.apache.clerezza.api.impl.graph; -import org.apache.clerezza.api.IRI; -import org.apache.clerezza.api.impl.AbstractGraph; +import org.apache.clerezza.api.*; import java.lang.ref.SoftReference; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.ConcurrentModificationException; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; -import java.util.Set; - -import org.apache.clerezza.api.BlankNodeOrIRI; -import org.apache.clerezza.api.ImmutableGraph; -import org.apache.clerezza.api.RDFTerm; -import org.apache.clerezza.api.Triple; +import java.util.*; /** * For now this is a minimalistic implementation, without any indexes or other diff --git a/api.impl/src/main/java/org/apache/clerezza/api/impl/simple/SimpleImmutableGraph.java b/api.impl/src/main/java/org/apache/clerezza/api/impl/graph/SimpleImmutableGraph.java similarity index 96% rename from api.impl/src/main/java/org/apache/clerezza/api/impl/simple/SimpleImmutableGraph.java rename to api.impl/src/main/java/org/apache/clerezza/api/impl/graph/SimpleImmutableGraph.java index d48fe5c..46b95ad 100644 --- a/api.impl/src/main/java/org/apache/clerezza/api/impl/simple/SimpleImmutableGraph.java +++ b/api.impl/src/main/java/org/apache/clerezza/api/impl/graph/SimpleImmutableGraph.java @@ -16,9 +16,7 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.clerezza.api.impl.simple; - -import org.apache.clerezza.api.impl.AbstractImmutableGraph; +package org.apache.clerezza.api.impl.graph; import java.util.Iterator; diff --git a/api.impl/src/main/java/org/apache/clerezza/api/impl/simple/SimpleMGraph.java b/api.impl/src/main/java/org/apache/clerezza/api/impl/graph/SimpleMGraph.java similarity index 96% copy from api.impl/src/main/java/org/apache/clerezza/api/impl/simple/SimpleMGraph.java copy to api.impl/src/main/java/org/apache/clerezza/api/impl/graph/SimpleMGraph.java index da961c2..94cd03a 100644 --- a/api.impl/src/main/java/org/apache/clerezza/api/impl/simple/SimpleMGraph.java +++ b/api.impl/src/main/java/org/apache/clerezza/api/impl/graph/SimpleMGraph.java @@ -16,7 +16,7 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.clerezza.api.impl.simple; +package org.apache.clerezza.api.impl.graph; import java.util.Collection; import java.util.Iterator; diff --git a/api.impl/src/main/java/org/apache/clerezza/api/impl/WatchableGraphWrapper.java b/api.impl/src/main/java/org/apache/clerezza/api/impl/graph/WatchableGraphWrapper.java similarity index 91% rename from api.impl/src/main/java/org/apache/clerezza/api/impl/WatchableGraphWrapper.java rename to api.impl/src/main/java/org/apache/clerezza/api/impl/graph/WatchableGraphWrapper.java index 5533c9b..c86156d 100644 --- a/api.impl/src/main/java/org/apache/clerezza/api/impl/WatchableGraphWrapper.java +++ b/api.impl/src/main/java/org/apache/clerezza/api/impl/graph/WatchableGraphWrapper.java @@ -13,29 +13,15 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.clerezza.api.impl; +package org.apache.clerezza.api.impl.graph; + +import org.apache.clerezza.api.*; +import org.apache.clerezza.api.event.*; import java.lang.ref.WeakReference; -import java.util.Collection; -import java.util.Collections; -import java.util.HashSet; -import java.util.Iterator; -import java.util.Set; +import java.util.*; import java.util.concurrent.locks.ReadWriteLock; -import org.apache.clerezza.api.BlankNodeOrIRI; -import org.apache.clerezza.api.Graph; -import org.apache.clerezza.api.ImmutableGraph; -import org.apache.clerezza.api.IRI; -import org.apache.clerezza.api.RDFTerm; -import org.apache.clerezza.api.Triple; -import org.apache.clerezza.api.WatchableGraph; -import org.apache.clerezza.api.event.AddEvent; -import org.apache.clerezza.api.event.FilterTriple; -import org.apache.clerezza.api.event.GraphEvent; -import org.apache.clerezza.api.event.GraphListener; -import org.apache.clerezza.api.event.RemoveEvent; - /** * * @author developer diff --git a/api.impl/src/main/java/org/apache/clerezza/api/impl/graph/WriteBlockedGraph.java b/api.impl/src/main/java/org/apache/clerezza/api/impl/graph/WriteBlockedGraph.java new file mode 100644 index 0000000..71fce87 --- /dev/null +++ b/api.impl/src/main/java/org/apache/clerezza/api/impl/graph/WriteBlockedGraph.java @@ -0,0 +1,113 @@ +/* + * 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.clerezza.api.impl.graph; + +import org.apache.clerezza.api.*; + +import java.util.Collection; +import java.util.Iterator; + +/** + * + * This is a wrapper object for <code>Graph</code>. If <code>SecurityManger</code> + * is not <code>null</code> <code>TcManager</code> checks the <code>TcPermission</code>. + * If read-only permissions are set this wrapper is used instead of <code>Graph</code> + * and throws exceptions when add or remove methods are called. + * + * @author tsuy + */ +public class WriteBlockedGraph extends AbstractGraph + implements Graph { + + private Graph triples; + + public WriteBlockedGraph(Graph triples) { + this.triples = triples; + } + + @Override + protected int performSize() { + return triples.size(); + } + + @Override + protected Iterator<Triple> performFilter(BlankNodeOrIRI subject, IRI predicate, RDFTerm object) { + final Iterator<Triple> baseIter = triples.filter(subject, predicate, object); + return new Iterator<Triple>() { + + @Override + public boolean hasNext() { + return baseIter.hasNext(); + } + + @Override + public Triple next() { + return baseIter.next(); + } + + @Override + public void remove() { + throw new ReadOnlyException("remove"); + } + + + }; + } + + @Override + public boolean add(Triple e) { + throw new ReadOnlyException("add"); + } + + @Override + public boolean addAll(Collection<? extends Triple> c) { + throw new ReadOnlyException("add all"); + } + + @Override + public void clear() { + throw new ReadOnlyException("clear"); + } + + @Override + public boolean remove(Object o) { + throw new ReadOnlyException("remove"); + } + + @Override + public boolean removeAll(Collection<?> c) { + throw new ReadOnlyException("remove all"); + } + + @Override + public boolean retainAll(Collection<?> c) { + throw new ReadOnlyException("retain all"); + } + + @Override + public Iterator iterator() { + return filter(null, null, null); + } + + @Override + public ImmutableGraph getImmutableGraph() { + return this.triples.getImmutableGraph(); + } + +} diff --git a/api.impl/src/main/java/org/apache/clerezza/api/impl/graphmatching/GraphMatcher.java b/api.impl/src/main/java/org/apache/clerezza/api/impl/graphmatching/GraphMatcher.java index 9180ae0..754e05d 100644 --- a/api.impl/src/main/java/org/apache/clerezza/api/impl/graphmatching/GraphMatcher.java +++ b/api.impl/src/main/java/org/apache/clerezza/api/impl/graphmatching/GraphMatcher.java @@ -32,7 +32,7 @@ import org.apache.clerezza.api.Graph; import org.apache.clerezza.api.RDFTerm; import org.apache.clerezza.api.Triple; import org.apache.clerezza.api.impl.TripleImpl; -import org.apache.clerezza.api.impl.simple.SimpleMGraph; +import org.apache.clerezza.api.impl.graph.SimpleMGraph; /** * @author reto diff --git a/api.impl/src/main/java/org/apache/clerezza/api/impl/AbstractLiteral.java b/api.impl/src/main/java/org/apache/clerezza/api/impl/literal/AbstractLiteral.java similarity index 97% rename from api.impl/src/main/java/org/apache/clerezza/api/impl/AbstractLiteral.java rename to api.impl/src/main/java/org/apache/clerezza/api/impl/literal/AbstractLiteral.java index 48e3221..8444813 100644 --- a/api.impl/src/main/java/org/apache/clerezza/api/impl/AbstractLiteral.java +++ b/api.impl/src/main/java/org/apache/clerezza/api/impl/literal/AbstractLiteral.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.clerezza.api.impl; +package org.apache.clerezza.api.impl.literal; import org.apache.clerezza.api.Literal; diff --git a/api.impl/src/main/java/org/apache/clerezza/api/impl/simple/SimpleMGraph.java b/api.impl/src/main/java/org/apache/clerezza/api/impl/literal/InvalidLiteralTypeException.java similarity index 56% copy from api.impl/src/main/java/org/apache/clerezza/api/impl/simple/SimpleMGraph.java copy to api.impl/src/main/java/org/apache/clerezza/api/impl/literal/InvalidLiteralTypeException.java index da961c2..f4665db 100644 --- a/api.impl/src/main/java/org/apache/clerezza/api/impl/simple/SimpleMGraph.java +++ b/api.impl/src/main/java/org/apache/clerezza/api/impl/literal/InvalidLiteralTypeException.java @@ -16,41 +16,25 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.clerezza.api.impl.simple; +package org.apache.clerezza.api.impl.literal; -import java.util.Collection; -import java.util.Iterator; -import java.util.Set; - -import org.apache.clerezza.api.Graph; -import org.apache.clerezza.api.Triple; +import org.apache.clerezza.api.IRI; /** + * Thrown when a literal is of the wrong type for conversion to a java-type * - * @deprecated Use SimpleGraph * @author reto */ -@Deprecated -public class SimpleMGraph extends SimpleGraph implements Graph { - +public class InvalidLiteralTypeException extends RuntimeException { + /** - * Creates an empty SimpleMGraph + * Constructs the exception to be thrown when a literal cannot be + * converted to an instance of the specified class + * + * @param javaType the <code>Class</code> to convert to + * @param literalType the literalType which can't be converted */ - public SimpleMGraph() { - } - - public SimpleMGraph(Set<Triple> baseSet) { - super(baseSet); - } - - public SimpleMGraph(Collection<Triple> baseCollection) { - super(baseCollection); - } - - public SimpleMGraph(Iterator<Triple> iterator) { - super(iterator); + public InvalidLiteralTypeException(Class<?> javaType, IRI literalType) { + super("Cannot create a "+javaType+" from a literal of type "+literalType); } - } - - \ No newline at end of file diff --git a/api.impl/src/main/java/org/apache/clerezza/api/impl/literal/LiteralFactory.java b/api.impl/src/main/java/org/apache/clerezza/api/impl/literal/LiteralFactory.java new file mode 100644 index 0000000..fc971bd --- /dev/null +++ b/api.impl/src/main/java/org/apache/clerezza/api/impl/literal/LiteralFactory.java @@ -0,0 +1,83 @@ +/* + * 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.clerezza.api.impl.literal; + +import org.apache.clerezza.api.Literal; + +/** + * This class provides methods to convert java objects to typed literals and + * vice versa. While the default implementation will provide literal objects + * storing the data's lexical form in memory, other implementations may + * create literal optimized for processing within the store. + * + * Note: this class uses the notion of "Convertor" (in the Exception naming), + * but does not currently provide a mechanism to register such + * <code>Convertor</code>s. An implementation is said to provide + * <code>Convertor</code>s for the types it supports. + * + * @since 0.3 + * @author reto + */ +public abstract class LiteralFactory { + + private static LiteralFactory instance = new SimpleLiteralFactory(); + + /** + * Get a <code>LiteralFactory</code>. If this has not been set using + * setInstance it returns an instance of + * {@link org.apache.clerezza.model.impl.SimpleLiteralFactory}. + * + * @return a concrete <code>LiteralFactory</code> + */ + public static LiteralFactory getInstance() { + return instance; + } + + /** + * Set the instance returned by <code>getInstance</code>. + * + * @param instance the new default <code>LiteralFactory</code> + */ + public static void setInstance(LiteralFactory instance) { + LiteralFactory.instance = instance; + } + + /** + * Create a typed literal for the specified object + * + * @param value the value of the literal to be created + * @return a TypedLiteral representing the value + * @throws NoConvertorException thrown if <code>value</code> is of an invalid type + */ + public abstract Literal createTypedLiteral(Object value) + throws NoConvertorException; + + /** + * Converts a literal to an instance of the specified class + * + * @param <T> + * @param type the <code>Class</code> of the returned object + * @param literal the literal to be converted + * @return a java object representing the value of the literal + * @throws NoConvertorException thrown if <code>type</code> is unsupported + * @throws InvalidLiteralTypeException if the literal type doesn't match the requested java type + */ + public abstract <T> T createObject(Class<T> type, Literal literal) + throws NoConvertorException, InvalidLiteralTypeException; +} diff --git a/api.impl/src/main/java/org/apache/clerezza/api/impl/LiteralImpl.java b/api.impl/src/main/java/org/apache/clerezza/api/impl/literal/LiteralImpl.java similarity index 97% rename from api.impl/src/main/java/org/apache/clerezza/api/impl/LiteralImpl.java rename to api.impl/src/main/java/org/apache/clerezza/api/impl/literal/LiteralImpl.java index e8ac986..7d86242 100644 --- a/api.impl/src/main/java/org/apache/clerezza/api/impl/LiteralImpl.java +++ b/api.impl/src/main/java/org/apache/clerezza/api/impl/literal/LiteralImpl.java @@ -16,7 +16,7 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.clerezza.api.impl; +package org.apache.clerezza.api.impl.literal; import java.io.Serializable; diff --git a/api.impl/src/main/java/org/apache/clerezza/api/impl/simple/SimpleMGraph.java b/api.impl/src/main/java/org/apache/clerezza/api/impl/literal/NoConvertorException.java similarity index 57% rename from api.impl/src/main/java/org/apache/clerezza/api/impl/simple/SimpleMGraph.java rename to api.impl/src/main/java/org/apache/clerezza/api/impl/literal/NoConvertorException.java index da961c2..a462394 100644 --- a/api.impl/src/main/java/org/apache/clerezza/api/impl/simple/SimpleMGraph.java +++ b/api.impl/src/main/java/org/apache/clerezza/api/impl/literal/NoConvertorException.java @@ -16,41 +16,26 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.clerezza.api.impl.simple; +package org.apache.clerezza.api.impl.literal; -import java.util.Collection; -import java.util.Iterator; -import java.util.Set; - -import org.apache.clerezza.api.Graph; -import org.apache.clerezza.api.Triple; +import java.lang.reflect.Type; /** + * This exception is thrown when no convertor is available to do a required + * java-object to literal or literal to java-object conversion. * - * @deprecated Use SimpleGraph + * @since 0.3 * @author reto */ -@Deprecated -public class SimpleMGraph extends SimpleGraph implements Graph { +public class NoConvertorException extends RuntimeException { /** - * Creates an empty SimpleMGraph + * Create an instance of <code>NoConvertorException</code> + * indicating that no convertor is available for the type. + * + * @param type the type for which no convertor is available */ - public SimpleMGraph() { - } - - public SimpleMGraph(Set<Triple> baseSet) { - super(baseSet); - } - - public SimpleMGraph(Collection<Triple> baseCollection) { - super(baseCollection); + public NoConvertorException(Type type) { + super("No convertor available for type "+type); } - - public SimpleMGraph(Iterator<Triple> iterator) { - super(iterator); - } - } - - \ No newline at end of file diff --git a/api.impl/src/main/java/org/apache/clerezza/api/impl/PlainLiteralImpl.java b/api.impl/src/main/java/org/apache/clerezza/api/impl/literal/PlainLiteralImpl.java similarity index 98% rename from api.impl/src/main/java/org/apache/clerezza/api/impl/PlainLiteralImpl.java rename to api.impl/src/main/java/org/apache/clerezza/api/impl/literal/PlainLiteralImpl.java index 012aeca..aeef1ea 100644 --- a/api.impl/src/main/java/org/apache/clerezza/api/impl/PlainLiteralImpl.java +++ b/api.impl/src/main/java/org/apache/clerezza/api/impl/literal/PlainLiteralImpl.java @@ -16,7 +16,7 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.clerezza.api.impl; +package org.apache.clerezza.api.impl.literal; import java.io.Serializable; diff --git a/api.impl/src/main/java/org/apache/clerezza/api/impl/literal/SimpleLiteralFactory.java b/api.impl/src/main/java/org/apache/clerezza/api/impl/literal/SimpleLiteralFactory.java new file mode 100644 index 0000000..9bbbb1b --- /dev/null +++ b/api.impl/src/main/java/org/apache/clerezza/api/impl/literal/SimpleLiteralFactory.java @@ -0,0 +1,305 @@ +/* + * 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.clerezza.api.impl.literal; + +import org.apache.clerezza.api.IRI; +import org.apache.clerezza.api.Literal; +import org.apache.clerezza.api.impl.util.Base64; +import org.apache.clerezza.api.impl.util.W3CDateFormat; + +import java.math.BigInteger; +import java.text.DateFormat; +import java.text.ParseException; +import java.util.*; + +/** + * An implementation of literal factory currently supporting only + * byte[]/base64Binary and Java.util.Date/date + * + * @author reto + */ + +public class SimpleLiteralFactory extends LiteralFactory { + + private static final String XSD = "http://www.w3.org/2001/XMLSchema#"; + final private static IRI xsdInteger = xsd("integer"); + final private static IRI xsdInt = xsd("int"); + final private static IRI xsdShort = xsd("short"); + final private static IRI xsdByte = xsd("byte"); + final private static IRI xsdLong = xsd("long"); + + + final private static Set<IRI> decimalTypes = new HashSet<IRI>(); + + final private static Map<Class<?>, TypeConverter<?>> typeConverterMap = new HashMap<Class<?>, TypeConverter<?>>(); + final static Class<? extends byte[]> byteArrayType; + + static { + Collections.addAll(decimalTypes, xsdInteger, xsdInt, xsdByte, xsdShort, xsdLong ); + + byte[] byteArray = new byte[0]; + byteArrayType = byteArray.getClass(); + typeConverterMap.put(byteArrayType, new ByteArrayConverter()); + typeConverterMap.put(Date.class, new DateConverter()); + typeConverterMap.put(Boolean.class, new BooleanConverter()); + typeConverterMap.put(String.class, new StringConverter()); + typeConverterMap.put(Integer.class, new IntegerConverter()); + typeConverterMap.put(BigInteger.class, new BigIntegerConverter()); + typeConverterMap.put(Long.class, new LongConverter()); + typeConverterMap.put(Double.class, new DoubleConverter()); + typeConverterMap.put(Float.class, new FloatConverter()); + typeConverterMap.put(IRI.class, new UriRefConverter()); + } + + final private static IRI xsdDouble =xsd("double"); + final private static IRI xsdFloat =xsd("float"); + final private static IRI xsdAnyURI =xsd("anyURI"); + + final private static IRI xsd(String name) { + return new IRI(XSD+name); + } + + private static interface TypeConverter<T> { + Literal createLiteral(T value); + T createObject(Literal literal); + } + + private static class ByteArrayConverter implements TypeConverter<byte[]> { + + private static final IRI base64Uri =xsd("base64Binary"); + + @Override + public Literal createLiteral(byte[] value) { + return new TypedLiteralImpl(Base64.encode((byte[]) value), base64Uri); + } + + @Override + public byte[] createObject(Literal literal) { + if (!literal.getDataType().equals(base64Uri)) { + throw new InvalidLiteralTypeException(byteArrayType, literal.getDataType()); + } + return (byte[]) Base64.decode(literal.getLexicalForm()); + } + + + } + private static class DateConverter implements TypeConverter<Date> { + + private static final IRI dateTimeUri =xsd("dateTime"); + private static final DateFormat DATE_FORMAT = new W3CDateFormat(); + + @Override + public Literal createLiteral(Date value) { + return new TypedLiteralImpl(DATE_FORMAT.format(value), dateTimeUri); + } + + @Override + public Date createObject(Literal literal) { + if (!literal.getDataType().equals(dateTimeUri)) { + throw new InvalidLiteralTypeException(Date.class, literal.getDataType()); + } + try { + return DATE_FORMAT.parse(literal.getLexicalForm()); + } catch (ParseException ex) { + throw new RuntimeException("Exception parsing literal as date", ex); + } + } + + + } + + private static class BooleanConverter implements TypeConverter<Boolean> { + + private static final IRI booleanUri =xsd("boolean"); + public static final TypedLiteralImpl TRUE = new TypedLiteralImpl("true", booleanUri); + public static final TypedLiteralImpl FALSE = new TypedLiteralImpl("false", booleanUri); + + @Override + public Literal createLiteral(Boolean value) { + if (value) return TRUE; + else return FALSE; + } + + @Override + public Boolean createObject(Literal literal) { + if (literal == TRUE) return true; + else if (literal == FALSE) return false; + else if (!literal.getDataType().equals(booleanUri)) { + throw new InvalidLiteralTypeException(Boolean.class, literal.getDataType()); + } + return Boolean.valueOf(literal.getLexicalForm()); + } + } + + private static class StringConverter implements TypeConverter<String> { + + private static final IRI stringUri =xsd("string"); + + @Override + public Literal createLiteral(String value) { + return new TypedLiteralImpl(value, stringUri); + } + + @Override + public String createObject(Literal literal) { + if (!literal.getDataType().equals(stringUri)) { + throw new InvalidLiteralTypeException(String.class, literal.getDataType()); + } + return literal.getLexicalForm(); + } + } + + private static class IntegerConverter implements TypeConverter<Integer> { + + + @Override + public Literal createLiteral(Integer value) { + return new TypedLiteralImpl(value.toString(), xsdInt); + } + + @Override + public Integer createObject(Literal literal) { + if (!decimalTypes.contains(literal.getDataType())) { + throw new InvalidLiteralTypeException(Integer.class, literal.getDataType()); + } + return new Integer(literal.getLexicalForm()); + } + } + + private static class LongConverter implements TypeConverter<Long> { + + + + @Override + public Literal createLiteral(Long value) { + return new TypedLiteralImpl(value.toString(), xsdLong); + } + + @Override + public Long createObject(Literal literal) { + if (!decimalTypes.contains(literal.getDataType())) { + throw new InvalidLiteralTypeException(Long.class, literal.getDataType()); + } + return new Long(literal.getLexicalForm()); + } + } + + + private static class FloatConverter implements TypeConverter<Float> { + + @Override + public Literal createLiteral(Float value) { + return new TypedLiteralImpl(value.toString(), xsdFloat); + } + + @Override + public Float createObject(Literal literal) { + if (!literal.getDataType().equals(xsdFloat)) { + throw new InvalidLiteralTypeException(Float.class, literal.getDataType()); + } + return Float.valueOf(literal.getLexicalForm()); + } + } + + private static class DoubleConverter implements TypeConverter<Double> { + + + + @Override + public Literal createLiteral(Double value) { + return new TypedLiteralImpl(value.toString(), xsdDouble); + } + + @Override + public Double createObject(Literal literal) { + if (!literal.getDataType().equals(xsdDouble)) { + throw new InvalidLiteralTypeException(Double.class, literal.getDataType()); + } + return new Double(literal.getLexicalForm()); + } + } + + private static class BigIntegerConverter implements TypeConverter<BigInteger> { + + + + @Override + public Literal createLiteral(BigInteger value) { + return new TypedLiteralImpl(value.toString(), xsdInteger); + } + + @Override + public BigInteger createObject(Literal literal) { + if (!literal.getDataType().equals(xsdInteger)) { + throw new InvalidLiteralTypeException(Double.class, literal.getDataType()); + } + return new BigInteger(literal.getLexicalForm()); + } + } + + private static class UriRefConverter implements TypeConverter<IRI> { + + + + @Override + public Literal createLiteral(IRI value) { + return new TypedLiteralImpl(value.getUnicodeString(), xsdAnyURI); + } + + @Override + public IRI createObject(Literal literal) { + if (!literal.getDataType().equals(xsdAnyURI)) { + throw new InvalidLiteralTypeException(IRI.class, literal.getDataType()); + } + return new IRI(literal.getLexicalForm()); + } + } + + + @SuppressWarnings("unchecked") + @Override + public Literal createTypedLiteral(Object value) throws NoConvertorException { + TypeConverter converter = getConverterFor(value.getClass()); + return converter.createLiteral(value); + } + + + + @Override + public <T> T createObject(Class<T> type, Literal literal) + throws NoConvertorException, InvalidLiteralTypeException { + final TypeConverter<T> converter = getConverterFor(type); + return converter.createObject(literal); + + } + + @SuppressWarnings("unchecked") + private <T> TypeConverter<T> getConverterFor(Class<T> type) throws NoConvertorException { + TypeConverter<T> convertor = (TypeConverter<T>) typeConverterMap.get(type); + if (convertor != null) { + return convertor; + } + for (Map.Entry<Class<?>, TypeConverter<?>> converterEntry : typeConverterMap.entrySet()) { + if (type.isAssignableFrom(converterEntry.getKey())) { + return (TypeConverter<T>) converterEntry.getValue(); + } + } + throw new NoConvertorException(type); + } +} diff --git a/api.impl/src/main/java/org/apache/clerezza/api/impl/TypedLiteralImpl.java b/api.impl/src/main/java/org/apache/clerezza/api/impl/literal/TypedLiteralImpl.java similarity index 97% rename from api.impl/src/main/java/org/apache/clerezza/api/impl/TypedLiteralImpl.java rename to api.impl/src/main/java/org/apache/clerezza/api/impl/literal/TypedLiteralImpl.java index 0be7ab3..8b029d2 100644 --- a/api.impl/src/main/java/org/apache/clerezza/api/impl/TypedLiteralImpl.java +++ b/api.impl/src/main/java/org/apache/clerezza/api/impl/literal/TypedLiteralImpl.java @@ -16,7 +16,7 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.clerezza.api.impl; +package org.apache.clerezza.api.impl.literal; import java.io.Serializable; diff --git a/api.impl/src/main/java/org/apache/clerezza/api/impl/package-info.java b/api.impl/src/main/java/org/apache/clerezza/api/impl/package-info.java index 6a35ba4..80652e5 100644 --- a/api.impl/src/main/java/org/apache/clerezza/api/impl/package-info.java +++ b/api.impl/src/main/java/org/apache/clerezza/api/impl/package-info.java @@ -15,7 +15,4 @@ * limitations under the License. */ -/** - * Common RDF API Implementation utilities. - */ package org.apache.clerezza.api.impl; \ No newline at end of file diff --git a/api.impl/src/main/java/org/apache/clerezza/api/impl/util/Base64.java b/api.impl/src/main/java/org/apache/clerezza/api/impl/util/Base64.java new file mode 100644 index 0000000..28607eb --- /dev/null +++ b/api.impl/src/main/java/org/apache/clerezza/api/impl/util/Base64.java @@ -0,0 +1,120 @@ +/* + * Taken from the book: + * Jonathan Knudsen, "Java Cryptography", O'Reilly Media, Inc., 1998 + */ +package org.apache.clerezza.api.impl.util; +/* + * + * 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. + * +*/ + + +public class Base64 { + + public static String encode(byte[] raw) { + StringBuffer encoded = new StringBuffer(); + for (int i = 0; i < raw.length; i += 3) { + encoded.append(encodeBlock(raw, i)); + } + return encoded.toString(); + } + + protected static char[] encodeBlock(byte[] raw, int offset) { + int block = 0; + int slack = raw.length - offset - 1; + int end = (slack >= 2) ? 2 : slack; + for (int i = 0; i <= end; i++) { + byte b = raw[offset + i]; + int neuter = (b < 0) ? b + 256 : b; + block += neuter << (8 * (2 - i)); + } + char[] base64 = new char[4]; + for (int i = 0; i < 4; i++) { + int sixbit = (block >>> (6 * (3 - i))) & 0x3f; + base64[i] = getChar(sixbit); + } + if (slack < 1) { + base64[2] = '='; + } + if (slack < 2) { + base64[3] = '='; + } + return base64; + } + + protected static char getChar(int sixBit) { + if (sixBit >= 0 && sixBit <= 25) { + return (char) ('A' + sixBit); + } + if (sixBit >= 26 && sixBit <= 51) { + return (char) ('a' + (sixBit - 26)); + } + if (sixBit >= 52 && sixBit <= 61) { + return (char) ('0' + (sixBit - 52)); + } + if (sixBit == 62) { + return '+'; + } + if (sixBit == 63) { + return '/'; + } + return '?'; + } + + public static byte[] decode(String base64) { + int pad = 0; + for (int i = base64.length() - 1; base64.charAt(i) == '='; i--) { + pad++; + } + int length = base64.length() * 6 / 8 - pad; + byte[] raw = new byte[length]; + int rawIndex = 0; + for (int i = 0; i < base64.length(); i += 4) { + int block = (getValue(base64.charAt(i)) << 18) + (getValue(base64.charAt(i + 1)) << 12) + (getValue(base64.charAt(i + 2)) << 6) + (getValue(base64.charAt(i + 3))); + for (int j = 0; j < 3 && rawIndex + j < raw.length; j++) { + raw[rawIndex + j] = (byte) ((block >> (8 * (2 - j))) & 0xff); + } + rawIndex += 3; + } + return raw; + } + + protected static int getValue(char c) { + if (c >= 'A' && c <= 'Z') { + return c - 'A'; + } + if (c >= 'a' && c <= 'z') { + return c - 'a' + 26; + } + if (c >= '0' && c <= '9') { + return c - '0' + 52; + } + if (c == '+') { + return 62; + } + if (c == '/') { + return 63; + } + if (c == '=') { + return 0; + } + return -1; + } + +} diff --git a/api.impl/src/main/java/org/apache/clerezza/api/impl/util/W3CDateFormat.java b/api.impl/src/main/java/org/apache/clerezza/api/impl/util/W3CDateFormat.java new file mode 100644 index 0000000..a337064 --- /dev/null +++ b/api.impl/src/main/java/org/apache/clerezza/api/impl/util/W3CDateFormat.java @@ -0,0 +1,196 @@ +//taken from GVS MillisDateFormat.java, modified to support different precision + +/* + (c) Copyright 2005, 2006, Hewlett-Packard Development Company, LP + [See end of file] + $Id: W3CDateFormat.java,v 1.6 2007/05/07 18:45:22 rebach Exp $ + */ +package org.apache.clerezza.api.impl.util; +/* + * + * 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. + * +*/ + + +import java.text.DateFormat; +import java.text.FieldPosition; +import java.text.ParsePosition; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; +import java.util.GregorianCalendar; +import java.util.SimpleTimeZone; +import java.util.TimeZone; + +/** + * @author reto implements http://www.w3.org/TR/NOTE-datetime with the + * limitation that it expects exactly a three digits decimal fraction of + * seconds. if a time zone designator other than 'Z' is present it must + * contain a column + */ +public class W3CDateFormat extends DateFormat { + /** + * An instance of this class + */ + public static final W3CDateFormat instance = new W3CDateFormat(); + + private static final SimpleDateFormat dateFormatWithMillis = new SimpleDateFormat( + "yyyy-MM-dd'T'HH:mm:ss.SSSZ"); + private static final SimpleDateFormat dateFormatNoMillis = new SimpleDateFormat( + "yyyy-MM-dd'T'HH:mm:ssZ"); + + private static final long serialVersionUID = 3258407344076372025L; + + private static final TimeZone utcTZ = new SimpleTimeZone(0, "UTC"); + + static { + dateFormatWithMillis.setTimeZone(utcTZ); + dateFormatNoMillis.setTimeZone(utcTZ); + } + + @Override + public void setTimeZone(TimeZone zone) { + super.setTimeZone(zone); + } + + + /** + * @see java.text.DateFormat#format(java.util.Date, java.lang.StringBuffer, + * java.text.FieldPosition) + */ + @Override + public StringBuffer format(Date date, StringBuffer toAppendTo, + FieldPosition fieldPosition) { + + final DateFormat dateFormat = (date.getTime() % 1000) == 0 ? + dateFormatNoMillis : dateFormatWithMillis; + String string = dateFormat.format(date); + if (string.endsWith("0000")) { + StringBuffer result = new StringBuffer(string.substring(0, string.length()-5)); + result.append('Z'); + return result; + } else { + StringBuffer result = new StringBuffer(string); + result.insert(string.length() - 2, ':'); + return result; + } + } + + /** + * @see java.text.DateFormat#parse(java.lang.String, + * java.text.ParsePosition) + */ + public Date parse(String dateString, ParsePosition parsePos) { + + int position = parsePos.getIndex(); + + int y1 = dateString.charAt(position++) - '0'; + int y2 = dateString.charAt(position++) - '0'; + int y3 = dateString.charAt(position++) - '0'; + int y4 = dateString.charAt(position++) - '0'; + int year = 1000 * y1 + 100 * y2 + 10 * y3 + y4; + position++; // skip '-' + int m1 = dateString.charAt(position++) - '0'; + int m2 = dateString.charAt(position++) - '0'; + int month = 10 * m1 + m2; + position++; // skip '-' + int d1 = dateString.charAt(position++) - '0'; + int d2 = dateString.charAt(position++) - '0'; + int day = 10 * d1 + d2; + position++; // skip 'T' + int h1 = dateString.charAt(position++) - '0'; + int h2 = dateString.charAt(position++) - '0'; + int hour = 10 * h1 + h2; + position++; // skip ':' + int min1 = dateString.charAt(position++) - '0'; + int min2 = dateString.charAt(position++) - '0'; + int minutes = 10 * min1 + min2; + position++; // skip ':' + int s1 = dateString.charAt(position++) - '0'; + int s2 = dateString.charAt(position++) - '0'; + int secs = 10 * s1 + s2; + Calendar resultCalendar = new GregorianCalendar(year, month - 1, day, + hour, minutes, secs); + resultCalendar.setTimeZone(utcTZ); + char afterSecChar = dateString.charAt(position++); + int msecs = 0; + char tzd1; + if (afterSecChar == '.') { + int startPos = position; + //read decimal part, this is till there is a 'Z', a '+' or a '-' + char nextChar = dateString.charAt(position++); + while ((nextChar != 'Z') && (nextChar != '-') && (nextChar != '+')) { + msecs += (nextChar - '0')*Math.pow(10, 3+startPos-position); + nextChar = dateString.charAt(position++); + } + tzd1 = nextChar; + } else { + tzd1 = afterSecChar; + } + long timeInMillis = resultCalendar.getTimeInMillis() + msecs; + if (tzd1 != 'Z') { + int htz1 = dateString.charAt(position++) - '0'; + int htz2 = dateString.charAt(position++) - '0'; + int hourtz = 10 * htz1 + htz2; + position++; // skip ':' + int mintz1 = dateString.charAt(position++) - '0'; + int mintz2 = dateString.charAt(position++) - '0'; + int minutestz = 10 * mintz1 + mintz2; + int offSetInMillis = (hourtz * 60 + minutestz) * 60000; + if (tzd1 == '+') { + timeInMillis -= offSetInMillis; + } else { + timeInMillis += offSetInMillis; + } + } + parsePos.setIndex(position); + return new Date(timeInMillis); + + } +} + +/* + * (c) Copyright 2005, 2006 Hewlett-Packard Development Company, LP All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + diff --git a/api.impl/src/test/java/org/apache/clerezza/api/impl/graph/GraphTest.java b/api.impl/src/test/java/org/apache/clerezza/api/impl/graph/GraphTest.java new file mode 100644 index 0000000..66ad861 --- /dev/null +++ b/api.impl/src/test/java/org/apache/clerezza/api/impl/graph/GraphTest.java @@ -0,0 +1,563 @@ +/* + * 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.clerezza.api.impl.graph; + +import org.apache.clerezza.api.*; +import org.apache.clerezza.api.impl.TripleImpl; +import org.apache.clerezza.api.impl.literal.PlainLiteralImpl; +import org.apache.clerezza.api.impl.literal.TypedLiteralImpl; +import org.junit.Assert; +import org.junit.Test; + +import java.util.*; + + + +/** + * A generic abstract test class, implementations overwrite this class, + * providing an implementation of the getEmptyGraph method. + * + * @author reto, szalay, mir, hhn + */ +public abstract class GraphTest { + + private final IRI uriRef1 = + new IRI("http://example.org/ontology#res1"); + private final IRI uriRef2 = + new IRI("http://example.org/ontology#res2"); + private final IRI uriRef3 = + new IRI("http://example.org/ontology#res3"); + private final IRI uriRef4 = + new IRI("http://example.org/ontology#res4"); + private final IRI xmlLiteralType = + new IRI("http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral"); + private Literal literal1 = new PlainLiteralImpl("literal1"); + private Literal literal2 = new PlainLiteralImpl("literal2"); + private BlankNode bnode1 = new BlankNode(); + private BlankNode bnode2 = new BlankNode(); + private Triple trpl1 = new TripleImpl(uriRef2, uriRef2, literal1); + private Triple trpl2 = new TripleImpl(uriRef1, uriRef2, uriRef1); + private Triple trpl3 = new TripleImpl(bnode2, uriRef3, literal2); + private Triple trpl4 = new TripleImpl(uriRef3, uriRef4, literal2); + + /** + * Subclasses implement this method to provide implementation instances of + * Graph. This method may be called an arbitrary amount of time, + * independently whether previously returned Graph are still in use or not. + * + * @return an empty Graph of the implementation to be tested + */ + protected abstract Graph getEmptyGraph(); + + @Test + public void testAddCountAndGetTriples() { + Graph graph = getEmptyGraph(); + Assert.assertEquals(0, graph.size()); + final TripleImpl triple1 = new TripleImpl(uriRef1, uriRef2, uriRef1); + graph.add(triple1); + Assert.assertEquals(1, graph.size()); + Iterator<Triple> tripleIter = graph.filter(uriRef1, uriRef2, uriRef1); + Assert.assertTrue(tripleIter.hasNext()); + Triple tripleGot = tripleIter.next(); + Assert.assertEquals(triple1, tripleGot); + Assert.assertFalse(tripleIter.hasNext()); + BlankNode bnode = new BlankNode() {}; + graph.add(new TripleImpl(bnode, uriRef1, uriRef3)); + graph.add(new TripleImpl(bnode, uriRef1, uriRef4)); + tripleIter = graph.filter(null, uriRef1, null); + Set<BlankNodeOrIRI> subjectInMatchingTriples = new HashSet<BlankNodeOrIRI>(); + Set<RDFTerm> objectsInMatchingTriples = new HashSet<RDFTerm>(); + while (tripleIter.hasNext()) { + Triple triple = tripleIter.next(); + subjectInMatchingTriples.add(triple.getSubject()); + objectsInMatchingTriples.add(triple.getObject()); + } + Assert.assertEquals(1, subjectInMatchingTriples.size()); + Assert.assertEquals(2, objectsInMatchingTriples.size()); + Set<RDFTerm> expectedObjects = new HashSet<RDFTerm>(); + expectedObjects.add(uriRef3); + expectedObjects.add(uriRef4); + Assert.assertEquals(expectedObjects, objectsInMatchingTriples); + graph.add(new TripleImpl(bnode, uriRef4, bnode)); + tripleIter = graph.filter(null, uriRef4, null); + Assert.assertTrue(tripleIter.hasNext()); + Triple retrievedTriple = tripleIter.next(); + Assert.assertFalse(tripleIter.hasNext()); + Assert.assertEquals(retrievedTriple.getSubject(), retrievedTriple.getObject()); + tripleIter = graph.filter(uriRef1, uriRef2, null); + Assert.assertTrue(tripleIter.hasNext()); + retrievedTriple = tripleIter.next(); + Assert.assertFalse(tripleIter.hasNext()); + Assert.assertEquals(retrievedTriple.getSubject(), retrievedTriple.getObject()); + } + + @Test + public void testRemoveAllTriples() { + Graph graph = getEmptyGraph(); + Assert.assertEquals(0, graph.size()); + graph.add(new TripleImpl(uriRef1, uriRef2, uriRef3)); + graph.add(new TripleImpl(uriRef2, uriRef3, uriRef4)); + Assert.assertEquals(2, graph.size()); + graph.clear(); + Assert.assertEquals(0, graph.size()); + } + + @Test + public void testUseTypedLiterals() { + Graph graph = getEmptyGraph(); + Assert.assertEquals(0, graph.size()); + Literal value = new TypedLiteralImpl("<elem>value</elem>",xmlLiteralType); + final TripleImpl triple1 = new TripleImpl(uriRef1, uriRef2, value); + graph.add(triple1); + Iterator<Triple> tripleIter = graph.filter(uriRef1, uriRef2, null); + Assert.assertTrue(tripleIter.hasNext()); + RDFTerm gotValue = tripleIter.next().getObject(); + Assert.assertEquals(value, gotValue); + } + + @Test + public void testUseLanguageLiterals() { + Graph graph = getEmptyGraph(); + Assert.assertEquals(0, graph.size()); + Language language = new Language("it"); + Literal value = new PlainLiteralImpl("<elem>value</elem>",language); + final TripleImpl triple1 = new TripleImpl(uriRef1, uriRef2, value); + graph.add(triple1); + Iterator<Triple> tripleIter = graph.filter(uriRef1, uriRef2, null); + Assert.assertTrue(tripleIter.hasNext()); + RDFTerm gotValue = tripleIter.next().getObject(); + Assert.assertEquals(value, gotValue); + Assert.assertEquals(language, ((Literal)gotValue).getLanguage()); + } + + @Test + public void testRemoveViaIterator() { + Graph graph = getEmptyGraph(); + Assert.assertEquals(0, graph.size()); + final TripleImpl triple1 = new TripleImpl(uriRef1, uriRef2, uriRef1); + graph.add(triple1); + final TripleImpl triple2 = new TripleImpl(uriRef1, uriRef2, uriRef4); + graph.add(triple2); + Assert.assertEquals(2, graph.size()); + Iterator<Triple> iterator = graph.iterator(); + while (iterator.hasNext()) { + iterator.next(); + iterator.remove(); + } + Assert.assertEquals(0, graph.size()); + } + + @Test + public void testGetSize() throws Exception { + Graph graph = getEmptyGraph(); + // The test graph must always be empty after test fixture setup + Assert.assertEquals(0, graph.size()); + } + + + @Test + public void testAddSingleTriple() throws Exception { + Graph graph = getEmptyGraph(); + final Triple triple= createTriple( + "http://example.org/ontology/Person", + "http://example.org/ontology/hasName", + "http://example.org/people/alice"); + Assert.assertEquals(0, graph.size()); + Assert.assertTrue(graph.add(triple)); + Assert.assertEquals(1, graph.size()); + } + + + @Test + public void testAddSameTripleTwice() throws Exception { + Graph graph = getEmptyGraph(); + final Triple triple= createTriple( + "http://example.org/ontology/Person", + "http://example.org/ontology/hasName", + "http://example.org/people/alice"); + Assert.assertEquals(0, graph.size()); + Assert.assertTrue(graph.add(triple)); + Assert.assertFalse(graph.add(triple)); // ImmutableGraph does not change + Assert.assertEquals(1, graph.size()); + } + + + @Test + public void testRemoveSingleTriple() throws Exception { + Graph graph = getEmptyGraph(); + final Triple triple= createTriple( + "http://example.org/ontology/Person", + "http://example.org/ontology/hasName", + "http://example.org/people/alice"); + Assert.assertTrue(graph.add(triple)); + Assert.assertTrue(graph.remove(triple)); + Assert.assertEquals(0, graph.size()); + } + + @Test + public void testRemoveSameTripleTwice() throws Exception { + Graph graph = getEmptyGraph(); + final Triple tripleAlice= createTriple( + "http://example.org/ontology/Person", + "http://example.org/ontology/hasName", + "http://example.org/people/alice"); + final Triple tripleBob= createTriple( + "http://example.org/ontology/Person", + "http://example.org/ontology/hasName", + "http://example.org/people/bob"); + Assert.assertTrue(graph.add(tripleAlice)); + Assert.assertTrue(graph.add(tripleBob)); + Assert.assertTrue(graph.remove(tripleAlice)); + Assert.assertFalse(graph.remove(tripleAlice)); + Assert.assertEquals(1, graph.size()); + } + + @Test + public void testGetSameBlankNode() throws Exception { + Graph graph = getEmptyGraph(); + BlankNode bNode = new BlankNode(); + final IRI HAS_NAME = new IRI("http://example.org/ontology/hasName"); + final PlainLiteralImpl name = new PlainLiteralImpl("http://example.org/people/alice"); + final PlainLiteralImpl name2 = new PlainLiteralImpl("http://example.org/people/bob"); + final Triple tripleAlice = new TripleImpl(bNode, HAS_NAME, name); + final Triple tripleBob = new TripleImpl(bNode, HAS_NAME, name2); + Assert.assertTrue(graph.add(tripleAlice)); + Assert.assertTrue(graph.add(tripleBob)); + Iterator<Triple> result = graph.filter(null, HAS_NAME, name); + Assert.assertEquals(bNode, result.next().getSubject()); + } + + @Test + public void testContainsIfContained() throws Exception { + Graph graph = getEmptyGraph(); + final Triple triple= createTriple( + "http://example.org/ontology/Person", + "http://example.org/ontology/hasName", + "http://example.org/people/alice"); + Assert.assertTrue(graph.add(triple)); + Assert.assertTrue(graph.contains(triple)); + } + + + @Test + public void testContainsIfEmpty() throws Exception { + Graph graph = getEmptyGraph(); + final Triple triple= createTriple( + "http://example.org/ontology/Person", + "http://example.org/ontology/hasName", + "http://example.org/people/alice"); + Assert.assertFalse(graph.contains(triple)); + } + + + @Test + public void testContainsIfNotContained() throws Exception { + Graph graph = getEmptyGraph(); + final Triple tripleAdd= createTriple( + "http://example.org/ontology/Person", + "http://example.org/ontology/hasName", + "http://example.org/people/alice"); + final Triple tripleTest= createTriple( + "http://example.org/ontology/Person", + "http://example.org/ontology/hasName", + "http://example.org/people/bob"); + Assert.assertTrue(graph.add(tripleAdd)); + Assert.assertFalse(graph.contains(tripleTest)); + } + + + @Test + public void testFilterEmptyGraph() throws Exception { + Graph graph = getEmptyGraph(); + Iterator<Triple> i = graph.filter(null, null, null); + Assert.assertFalse(i.hasNext()); + } + + + @Test + public void testFilterSingleEntry() throws Exception { + Graph graph = getEmptyGraph(); + final Triple triple= createTriple( + "http://example.org/ontology/Person", + "http://example.org/ontology/hasName", + "http://example.org/people/alice"); + Assert.assertTrue(graph.add(triple)); + + Iterator<Triple> i = graph.filter(null, null, null); + Collection<Triple> resultSet= toCollection(i); + Assert.assertEquals(1, resultSet.size()); + Assert.assertTrue(resultSet.contains(triple)); + } + + + @Test + public void testFilterByObject() throws Exception { + Graph graph = getEmptyGraph(); + final Triple tripleAlice= createTriple( + "http://example.org/ontology/Person", + "http://example.org/ontology/hasName", + "http://example.org/people/alice"); + final Triple tripleBob= createTriple( + "http://example.org/ontology/Person", + "http://example.org/ontology/hasName", + "http://example.org/people/bob"); + Assert.assertTrue(graph.add(tripleAlice)); + Assert.assertTrue(graph.add(tripleBob)); + + Iterator<Triple> iterator; + Collection<Triple> resultSet; + + // Find bob + iterator = graph.filter(null, null, + new IRI("http://example.org/people/bob")); + resultSet= toCollection(iterator); + Assert.assertEquals(1, resultSet.size()); + Assert.assertTrue(resultSet.contains(tripleBob)); + + // Find alice + iterator = graph.filter(null, null, + new IRI("http://example.org/people/alice")); + resultSet= toCollection(iterator); + Assert.assertEquals(1, resultSet.size()); + Assert.assertTrue(resultSet.contains(tripleAlice)); + + // Find both + iterator = graph.filter(null, null, null); + resultSet= toCollection(iterator); + Assert.assertEquals(2, resultSet.size()); + Assert.assertTrue(resultSet.contains(tripleAlice)); + Assert.assertTrue(resultSet.contains(tripleBob)); + } +/* + @Test + public void graphEventTestAddRemove() { + Graph mGraph = getEmptyGraph(); + TestGraphListener listener = new TestGraphListener(); + mGraph.addGraphListener(listener, new FilterTriple(uriRef1, uriRef2, null)); + mGraph.addGraphListener(listener, new FilterTriple(bnode2, null, literal2)); + mGraph.addGraphListener(listener, new FilterTriple(null, uriRef4, literal2)); + mGraph.add(trpl1); + Assert.assertNull(listener.getEvents()); + mGraph.add(trpl2); + Assert.assertEquals(1, listener.getEvents().size()); + Assert.assertEquals(trpl2, listener.getEvents().get(0).getTriple()); + Assert.assertTrue(listener.getEvents().get(0) instanceof AddEvent); + listener.resetEvents(); + mGraph.remove(trpl2); + Assert.assertEquals(1, listener.getEvents().size()); + Assert.assertEquals(trpl2, listener.getEvents().get(0).getTriple()); + Assert.assertTrue(listener.getEvents().get(0) instanceof RemoveEvent); + listener.resetEvents(); + mGraph.add(trpl3); + Assert.assertEquals(1, listener.getEvents().size()); + Assert.assertEquals(trpl3, listener.getEvents().get(0).getTriple()); + Assert.assertTrue(listener.getEvents().get(0) instanceof AddEvent); + listener.resetEvents(); + mGraph.remove(trpl4); + Assert.assertNull(listener.getEvents()); + } + + @Test + public void graphEventTestAddAllRemoveAll() { + Graph mGraph = getEmptyGraph(); + TestGraphListener listener = new TestGraphListener(); + mGraph.addGraphListener(listener, new FilterTriple(uriRef1, uriRef2, null)); + mGraph.addGraphListener(listener, new FilterTriple(bnode2, null, literal2)); + mGraph.addGraphListener(listener, new FilterTriple(null, uriRef4, literal2)); + Graph triples = new SimpleGraph(); + triples.add(trpl1); + triples.add(trpl2); + triples.add(trpl3); + triples.add(trpl4); + mGraph.addAll(triples); + List<GraphEvent> cumulatedEvents = listener.getCumulatedEvents(); + Set<Triple> cumulatedTriples = getCumulatedTriples(cumulatedEvents); + Assert.assertEquals(3, cumulatedEvents.size()); + Assert.assertTrue(cumulatedEvents.get(0) instanceof AddEvent); + Assert.assertTrue(cumulatedTriples.contains(trpl2)); + Assert.assertTrue(cumulatedTriples.contains(trpl3)); + Assert.assertTrue(cumulatedTriples.contains(trpl4)); + listener.resetCumulatedEvents(); + mGraph.removeAll(triples); + cumulatedEvents = listener.getCumulatedEvents(); + cumulatedTriples = getCumulatedTriples(cumulatedEvents); + Assert.assertEquals(3, cumulatedEvents.size()); + Assert.assertTrue(cumulatedEvents.get(0) instanceof RemoveEvent); + Assert.assertTrue(cumulatedTriples.contains(trpl2)); + Assert.assertTrue(cumulatedTriples.contains(trpl3)); + Assert.assertTrue(cumulatedTriples.contains(trpl4)); + } + + @Test + public void graphEventTestFilterRemove() { + Graph mGraph = getEmptyGraph(); + TestGraphListener listener = new TestGraphListener(); + mGraph.addGraphListener(listener, new FilterTriple(uriRef1, uriRef2, null)); + mGraph.addGraphListener(listener, new FilterTriple(bnode2, null, literal2)); + mGraph.addGraphListener(listener, new FilterTriple(null, uriRef4, literal2)); + mGraph.add(trpl1); + mGraph.add(trpl2); + mGraph.add(trpl3); + mGraph.add(trpl4); + listener.resetCumulatedEvents(); + Iterator<Triple> result = mGraph.filter(null, uriRef2, null); + while (result.hasNext()) { + result.next(); + result.remove(); + } + List<GraphEvent> cumulatedEvents = listener.getCumulatedEvents(); + Assert.assertEquals(1, cumulatedEvents.size()); + Assert.assertTrue(cumulatedEvents.get(0) instanceof RemoveEvent); + Assert.assertEquals(trpl2, listener.getEvents().get(0).getTriple()); + } + + @Test + public void graphEventTestIteratorRemove() { + Graph mGraph = getEmptyGraph(); + TestGraphListener listener = new TestGraphListener(); + mGraph.addGraphListener(listener, new FilterTriple(uriRef1, uriRef2, null)); + mGraph.addGraphListener(listener, new FilterTriple(bnode2, null, literal2)); + mGraph.addGraphListener(listener, new FilterTriple(null, uriRef4, literal2)); + mGraph.add(trpl1); + mGraph.add(trpl2); + mGraph.add(trpl3); + mGraph.add(trpl4); + listener.resetCumulatedEvents(); + Iterator<Triple> result = mGraph.iterator(); + while (result.hasNext()) { + result.next(); + result.remove(); + } + List<GraphEvent> cumulatedEvents = listener.getCumulatedEvents(); + Set<Triple> cumulatedTriples = getCumulatedTriples(cumulatedEvents); + Assert.assertEquals(3, cumulatedEvents.size()); + Assert.assertTrue(cumulatedEvents.get(0) instanceof RemoveEvent); + Assert.assertTrue(cumulatedTriples.contains(trpl2)); + Assert.assertTrue(cumulatedTriples.contains(trpl3)); + Assert.assertTrue(cumulatedTriples.contains(trpl4)); + } + + @Test + public void graphEventTestClear() { + Graph mGraph = getEmptyGraph(); + TestGraphListener listener = new TestGraphListener(); + mGraph.addGraphListener(listener, new FilterTriple(uriRef1, uriRef2, null)); + mGraph.addGraphListener(listener, new FilterTriple(bnode2, null, literal2)); + mGraph.addGraphListener(listener, new FilterTriple(null, uriRef4, literal2)); + mGraph.add(trpl1); + mGraph.add(trpl2); + mGraph.add(trpl3); + mGraph.add(trpl4); + listener.resetCumulatedEvents(); + mGraph.clear(); + List<GraphEvent> cumulatedEvents = listener.getCumulatedEvents(); + Set<Triple> cumulatedTriples = getCumulatedTriples(cumulatedEvents); + Assert.assertEquals(3, cumulatedEvents.size()); + Assert.assertTrue(cumulatedEvents.get(0) instanceof RemoveEvent); + Assert.assertTrue(cumulatedTriples.contains(trpl2)); + Assert.assertTrue(cumulatedTriples.contains(trpl3)); + Assert.assertTrue(cumulatedTriples.contains(trpl4)); + } + + private Set<Triple> getCumulatedTriples(List<GraphEvent> cumulatedEvents) { + Set<Triple> triples = new HashSet<Triple>(); + for(GraphEvent event: cumulatedEvents) { + triples.add(event.getTriple()); + } + return triples; + } + + @Test + public void graphEventTestWithDelay() throws Exception{ + Graph mGraph = getEmptyGraph(); + TestGraphListener listener = new TestGraphListener(); + mGraph.addGraphListener(listener, new FilterTriple(uriRef1, uriRef2, null), + 1000); + + Triple triple0 = new TripleImpl(uriRef2, uriRef2, literal1); + Triple triple1 = new TripleImpl(uriRef1, uriRef2, uriRef1); + Triple triple2 = new TripleImpl(uriRef1, uriRef2, literal1); + Triple triple3 = new TripleImpl(uriRef1, uriRef2, bnode1); + mGraph.add(triple0); + mGraph.add(triple1); + mGraph.add(triple2); + mGraph.add(triple3); + Thread.sleep(1500); + Assert.assertEquals(3, listener.getEvents().size()); + Assert.assertEquals(triple1, listener.getEvents().get(0).getTriple()); + Assert.assertTrue(listener.getEvents().get(0) instanceof AddEvent); + Assert.assertEquals(triple2, listener.getEvents().get(1).getTriple()); + Assert.assertTrue(listener.getEvents().get(0) instanceof AddEvent); + Assert.assertEquals(triple3, listener.getEvents().get(2).getTriple()); + Assert.assertTrue(listener.getEvents().get(0) instanceof AddEvent); + } + + private static class TestGraphListener implements GraphListener { + private List<GraphEvent> events = null; + private List<GraphEvent> cumulatedEvents = new ArrayList<GraphEvent>(); + + @Override + public void graphChanged(List<GraphEvent> events) { + this.events = events; + Iterator<GraphEvent> eventIter = events.iterator(); + while (eventIter.hasNext()) { + GraphEvent graphEvent = eventIter.next(); + this.cumulatedEvents.add(graphEvent); + } + } + + public List<GraphEvent> getEvents() { + return events; + } + + public List<GraphEvent> getCumulatedEvents() { + return cumulatedEvents; + } + + public void resetEvents() { + events = null; + } + + public void resetCumulatedEvents() { + cumulatedEvents = new ArrayList<GraphEvent>(); + } + } +*/ + private Collection<Triple> toCollection(Iterator<Triple> iterator) { + Collection<Triple> result = new ArrayList<Triple>(); + while (iterator.hasNext()) { + result.add(iterator.next()); + } + return result; + } + + /** + * Creates a new <code>Triple</code>. + * @param subject the subject. + * @param predicate the predicate. + * @param object the object. + * @throws IllegalArgumentException If an attribute is <code>null</code>. + */ + private Triple createTriple(String subject, String predicate, + String object) { + return new TripleImpl(new IRI(subject), new IRI(predicate), + new IRI(object)); + } + +} diff --git a/api.impl/src/test/java/org/apache/clerezza/api/impl/simple/SimpleGraphTest.java b/api.impl/src/test/java/org/apache/clerezza/api/impl/graph/SimpleGraphTest.java similarity index 98% rename from api.impl/src/test/java/org/apache/clerezza/api/impl/simple/SimpleGraphTest.java rename to api.impl/src/test/java/org/apache/clerezza/api/impl/graph/SimpleGraphTest.java index ff3280d..cdfadab 100644 --- a/api.impl/src/test/java/org/apache/clerezza/api/impl/simple/SimpleGraphTest.java +++ b/api.impl/src/test/java/org/apache/clerezza/api/impl/graph/SimpleGraphTest.java @@ -16,12 +16,13 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.clerezza.api.impl.simple; +package org.apache.clerezza.api.impl.graph; import org.apache.clerezza.api.impl.TripleImpl; import java.util.ConcurrentModificationException; import java.util.Iterator; + import org.junit.Assert; import org.junit.Test; import org.apache.clerezza.api.Triple; diff --git a/api.impl/src/test/java/org/apache/clerezza/api/impl/graphmatching/GraphMatcherTest.java b/api.impl/src/test/java/org/apache/clerezza/api/impl/graphmatching/GraphMatcherTest.java index 00e02a2..4a39411 100644 --- a/api.impl/src/test/java/org/apache/clerezza/api/impl/graphmatching/GraphMatcherTest.java +++ b/api.impl/src/test/java/org/apache/clerezza/api/impl/graphmatching/GraphMatcherTest.java @@ -20,7 +20,7 @@ package org.apache.clerezza.api.impl.graphmatching; import org.apache.clerezza.api.Graph; import org.apache.clerezza.api.IRI; -import org.apache.clerezza.api.impl.simple.SimpleMGraph; +import org.apache.clerezza.api.impl.graph.SimpleMGraph; import org.apache.clerezza.api.impl.TripleImpl; import java.util.Map; diff --git a/api.impl/src/test/java/org/apache/clerezza/api/impl/graphmatching/Utils4Testing.java b/api.impl/src/test/java/org/apache/clerezza/api/impl/graphmatching/Utils4Testing.java index 85dbc20..9dc1a8f 100644 --- a/api.impl/src/test/java/org/apache/clerezza/api/impl/graphmatching/Utils4Testing.java +++ b/api.impl/src/test/java/org/apache/clerezza/api/impl/graphmatching/Utils4Testing.java @@ -22,7 +22,7 @@ package org.apache.clerezza.api.impl.graphmatching; import org.apache.clerezza.api.BlankNode; import org.apache.clerezza.api.Graph; import org.apache.clerezza.api.IRI; -import org.apache.clerezza.api.impl.simple.SimpleMGraph; +import org.apache.clerezza.api.impl.graph.SimpleMGraph; import org.apache.clerezza.api.impl.TripleImpl; import org.apache.clerezza.api.BlankNodeOrIRI; diff --git a/api.impl/src/test/java/org/apache/clerezza/api/impl/simple/PlainLiteralImplTest.java b/api.impl/src/test/java/org/apache/clerezza/api/impl/literal/PlainLiteralImplTest.java similarity index 96% rename from api.impl/src/test/java/org/apache/clerezza/api/impl/simple/PlainLiteralImplTest.java rename to api.impl/src/test/java/org/apache/clerezza/api/impl/literal/PlainLiteralImplTest.java index ff6e678..ff55388 100644 --- a/api.impl/src/test/java/org/apache/clerezza/api/impl/simple/PlainLiteralImplTest.java +++ b/api.impl/src/test/java/org/apache/clerezza/api/impl/literal/PlainLiteralImplTest.java @@ -16,14 +16,14 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.clerezza.api.impl.simple; +package org.apache.clerezza.api.impl.literal; -import org.apache.clerezza.api.impl.PlainLiteralImpl; import org.junit.Test; import org.apache.clerezza.api.Language; import org.apache.clerezza.api.Literal; import org.junit.Assert; + /** * * @author reto diff --git a/api.impl/src/test/java/org/apache/clerezza/api/impl/simple/TripleImplTest.java b/api.impl/src/test/java/org/apache/clerezza/api/impl/literal/TripleImplTest.java similarity index 96% rename from api.impl/src/test/java/org/apache/clerezza/api/impl/simple/TripleImplTest.java rename to api.impl/src/test/java/org/apache/clerezza/api/impl/literal/TripleImplTest.java index 9b38af9..99e59ce 100644 --- a/api.impl/src/test/java/org/apache/clerezza/api/impl/simple/TripleImplTest.java +++ b/api.impl/src/test/java/org/apache/clerezza/api/impl/literal/TripleImplTest.java @@ -30,7 +30,7 @@ import org.apache.clerezza.api.BlankNodeOrIRI; import org.apache.clerezza.api.IRI; import org.apache.clerezza.api.RDFTerm; import org.apache.clerezza.api.Triple; -import org.apache.clerezza.api.impl.PlainLiteralImpl; +import org.apache.clerezza.api.impl.literal.PlainLiteralImpl; import org.apache.clerezza.api.impl.TripleImpl; import org.junit.Test; import junit.framework.Assert; diff --git a/api.impl/src/test/java/org/apache/clerezza/api/impl/simple/TypedLiteralImplTest.java b/api.impl/src/test/java/org/apache/clerezza/api/impl/literal/TypedLiteralImplTest.java similarity index 97% rename from api.impl/src/test/java/org/apache/clerezza/api/impl/simple/TypedLiteralImplTest.java rename to api.impl/src/test/java/org/apache/clerezza/api/impl/literal/TypedLiteralImplTest.java index dbde617..8589c4e 100644 --- a/api.impl/src/test/java/org/apache/clerezza/api/impl/simple/TypedLiteralImplTest.java +++ b/api.impl/src/test/java/org/apache/clerezza/api/impl/literal/TypedLiteralImplTest.java @@ -19,7 +19,7 @@ package org.apache.clerezza.api.impl.simple; import org.apache.clerezza.api.IRI; -import org.apache.clerezza.api.impl.TypedLiteralImpl; +import org.apache.clerezza.api.impl.literal.TypedLiteralImpl; import org.junit.Test; import junit.framework.Assert;
