You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@clerezza.apache.org by ha...@apache.org on 2018/11/14 02:55:29 UTC
[2/3] clerezza git commit: CLEREZZA-1026: Copy additional classes
from rdf.core to api.impl and refactor api.impl
http://git-wip-us.apache.org/repos/asf/clerezza/blob/829e5fa5/api.impl/src/main/java/org/apache/clerezza/api/impl/graph/WatchableGraphWrapper.java
----------------------------------------------------------------------
diff --git a/api.impl/src/main/java/org/apache/clerezza/api/impl/graph/WatchableGraphWrapper.java b/api.impl/src/main/java/org/apache/clerezza/api/impl/graph/WatchableGraphWrapper.java
new file mode 100644
index 0000000..c86156d
--- /dev/null
+++ b/api.impl/src/main/java/org/apache/clerezza/api/impl/graph/WatchableGraphWrapper.java
@@ -0,0 +1,276 @@
+/*
+ * Copyright 2015 The Apache Software Foundation.
+ *
+ * Licensed 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.event.*;
+
+import java.lang.ref.WeakReference;
+import java.util.*;
+import java.util.concurrent.locks.ReadWriteLock;
+
+/**
+ *
+ * @author developer
+ */
+public class WatchableGraphWrapper implements WatchableGraph {
+
+ final Graph wrapped;
+
+ public WatchableGraphWrapper(Graph wrapped) {
+ this.wrapped = wrapped;
+ }
+
+
+ //all listeners
+ private final Set<ListenerConfiguration> listenerConfigs = Collections.synchronizedSet(
+ new HashSet<ListenerConfiguration>());
+ private DelayedNotificator delayedNotificator = new DelayedNotificator();
+
+ @Override
+ public Iterator<Triple> iterator() {
+ return filter(null, null, null);
+ }
+
+ @Override
+ public boolean contains(Object o) {
+ if (!(o instanceof Triple)) {
+ return false;
+ }
+ Triple t = (Triple) o;
+ return filter(t.getSubject(), t.getPredicate(), t.getObject()).hasNext();
+ }
+
+ @Override
+ public Iterator<Triple> filter(BlankNodeOrIRI subject, IRI predicate,
+ RDFTerm object) {
+ final Iterator<Triple> baseIter = wrapped.filter(subject, predicate, object);
+ return new Iterator<Triple>() {
+
+ Triple currentTriple = null;
+
+ @Override
+ public boolean hasNext() {
+ return baseIter.hasNext();
+ }
+
+ @Override
+ public Triple next() {
+ currentTriple = baseIter.next();
+ return currentTriple;
+ }
+
+ @Override
+ public void remove() {
+ baseIter.remove();
+ dispatchEvent(new RemoveEvent(WatchableGraphWrapper.this, currentTriple));
+ }
+ };
+ }
+
+ @Override
+ public boolean add(Triple triple) {
+ boolean success = performAdd(triple);
+ if (success) {
+ dispatchEvent(new AddEvent(this, triple));
+ }
+ return success;
+ }
+
+ /**
+ * A subclass of <code>AbstractGraph</code> should override
+ * this method instead of <code>add</code> for Graph event support to be
+ * added.
+ *
+ * @param e The triple to be added to the triple collection
+ * @return
+ */
+ protected boolean performAdd(Triple e) {
+ return wrapped.add(e);
+ }
+
+ @Override
+ public boolean remove(Object o) {
+ Triple triple = (Triple) o;
+ boolean success = performRemove(triple);
+ if (success) {
+ dispatchEvent(new RemoveEvent(this, triple));
+ }
+ return success;
+ }
+
+ @Override
+ public boolean removeAll(Collection<?> c) {
+ boolean modified = false;
+ for (Iterator<? extends Object> it = c.iterator(); it.hasNext();) {
+ Object object = it.next();
+ if (remove(object)) {
+ modified = true;
+ }
+ }
+ return modified;
+ }
+
+ /**
+ * A subclass of <code>AbstractGraph</code> should override
+ * this method instead of <code>remove</code> for ImmutableGraph event support to be
+ * added.
+ *
+ * @param o The triple to be removed from the triple collection
+ * @return
+ */
+ protected boolean performRemove(Triple triple) {
+ Iterator<Triple> e = filter(null, null, null);
+ while (e.hasNext()) {
+ if (triple.equals(e.next())) {
+ e.remove();
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Dispatches a <code>GraphEvent</code> to all registered listeners for which
+ * the specified <code>Triple</code> matches the <code>FilterTriple</code>s
+ * of the listeners.
+ *
+ * @param triple The Triple that was modified
+ * @param type The type of modification
+ */
+ protected void dispatchEvent(GraphEvent event) {
+ synchronized(listenerConfigs) {
+ Iterator<ListenerConfiguration> iter = listenerConfigs.iterator();
+ while (iter.hasNext()) {
+ ListenerConfiguration config = iter.next();
+ GraphListener registeredListener = config.getListener();
+ if (registeredListener == null) {
+ iter.remove();
+ continue;
+ }
+ if (config.getFilter().match(event.getTriple())) {
+ delayedNotificator.sendEventToListener(registeredListener, event);
+ }
+ }
+ }
+ }
+
+ @Override
+ public void addGraphListener(GraphListener listener, FilterTriple filter) {
+ addGraphListener(listener, filter, 0);
+ }
+
+ @Override
+ public void addGraphListener(GraphListener listener, FilterTriple filter,
+ long delay) {
+ listenerConfigs.add(new ListenerConfiguration(listener, filter));
+ if (delay > 0) {
+ delayedNotificator.addDelayedListener(listener, delay);
+ }
+ }
+
+ @Override
+ public void removeGraphListener(GraphListener listener) {
+ synchronized(listenerConfigs) {
+ Iterator<ListenerConfiguration> iter = listenerConfigs.iterator();
+ while (iter.hasNext()) {
+ ListenerConfiguration listenerConfig = iter.next();
+ GraphListener registeredListener = listenerConfig.getListener();
+ if ((registeredListener == null) || (registeredListener.equals(listener))) {
+ iter.remove();
+ }
+ }
+ }
+ delayedNotificator.removeDelayedListener(listener);
+ }
+
+ @Override
+ public ImmutableGraph getImmutableGraph() {
+ throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+ }
+
+ @Override
+ public ReadWriteLock getLock() {
+ return wrapped.getLock();
+ }
+
+ @Override
+ public int size() {
+ return wrapped.size();
+ }
+
+ @Override
+ public boolean isEmpty() {
+ return wrapped.isEmpty();
+ }
+
+ @Override
+ public Object[] toArray() {
+ return wrapped.toArray();
+ }
+
+ @Override
+ public <T> T[] toArray(T[] a) {
+ return wrapped.toArray(a);
+ }
+
+ @Override
+ public boolean containsAll(Collection<?> c) {
+ return wrapped.containsAll(c);
+ }
+
+ @Override
+ public boolean addAll(Collection<? extends Triple> c) {
+ return wrapped.addAll(c);
+ }
+
+ @Override
+ public boolean retainAll(Collection<?> c) {
+ return wrapped.retainAll(c);
+ }
+
+ @Override
+ public void clear() {
+ wrapped.clear();
+ }
+
+ private static class ListenerConfiguration {
+
+ private WeakReference<GraphListener> listenerRef;
+ private FilterTriple filter;
+
+ private ListenerConfiguration(GraphListener listener, FilterTriple filter) {
+ this.listenerRef = new WeakReference<GraphListener>(listener);
+ this.filter = filter;
+ }
+
+ /**
+ * @return the listener
+ */
+ GraphListener getListener() {
+ GraphListener listener = listenerRef.get();
+ return listener;
+ }
+
+ /**
+ * @return the filter
+ */
+ FilterTriple getFilter() {
+ return filter;
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/clerezza/blob/829e5fa5/api.impl/src/main/java/org/apache/clerezza/api/impl/graph/WriteBlockedGraph.java
----------------------------------------------------------------------
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();
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/clerezza/blob/829e5fa5/api.impl/src/main/java/org/apache/clerezza/api/impl/graphmatching/GraphMatcher.java
----------------------------------------------------------------------
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
http://git-wip-us.apache.org/repos/asf/clerezza/blob/829e5fa5/api.impl/src/main/java/org/apache/clerezza/api/impl/literal/AbstractLiteral.java
----------------------------------------------------------------------
diff --git a/api.impl/src/main/java/org/apache/clerezza/api/impl/literal/AbstractLiteral.java b/api.impl/src/main/java/org/apache/clerezza/api/impl/literal/AbstractLiteral.java
new file mode 100644
index 0000000..8444813
--- /dev/null
+++ b/api.impl/src/main/java/org/apache/clerezza/api/impl/literal/AbstractLiteral.java
@@ -0,0 +1,61 @@
+/*
+ * Copyright 2015 The Apache Software Foundation.
+ *
+ * Licensed 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;
+
+/**
+ *
+ * @author developer
+ */
+public abstract class AbstractLiteral implements Literal {
+
+ @Override
+ public int hashCode() {
+ int result = 0;
+ if (getLanguage() != null) {
+ result = getLanguage().hashCode();
+ }
+ result += getLexicalForm().hashCode();
+ result += getDataType().hashCode();
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj instanceof Literal) {
+ Literal other = (Literal) obj;
+
+ if (getLanguage() == null) {
+ if (other.getLanguage() != null) {
+ return false;
+ }
+ } else {
+ if (!getLanguage().equals(other.getLanguage())) {
+ return false;
+ }
+ }
+ boolean res = getDataType().equals(other.getDataType()) && getLexicalForm().equals(other.getLexicalForm());
+ return res;
+ } else {
+ return false;
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/clerezza/blob/829e5fa5/api.impl/src/main/java/org/apache/clerezza/api/impl/literal/InvalidLiteralTypeException.java
----------------------------------------------------------------------
diff --git a/api.impl/src/main/java/org/apache/clerezza/api/impl/literal/InvalidLiteralTypeException.java b/api.impl/src/main/java/org/apache/clerezza/api/impl/literal/InvalidLiteralTypeException.java
new file mode 100644
index 0000000..f4665db
--- /dev/null
+++ b/api.impl/src/main/java/org/apache/clerezza/api/impl/literal/InvalidLiteralTypeException.java
@@ -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.clerezza.api.impl.literal;
+
+import org.apache.clerezza.api.IRI;
+
+/**
+ * Thrown when a literal is of the wrong type for conversion to a java-type
+ *
+ * @author reto
+ */
+public class InvalidLiteralTypeException extends RuntimeException {
+
+ /**
+ * 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 InvalidLiteralTypeException(Class<?> javaType, IRI literalType) {
+ super("Cannot create a "+javaType+" from a literal of type "+literalType);
+ }
+}
http://git-wip-us.apache.org/repos/asf/clerezza/blob/829e5fa5/api.impl/src/main/java/org/apache/clerezza/api/impl/literal/LiteralFactory.java
----------------------------------------------------------------------
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;
+}
http://git-wip-us.apache.org/repos/asf/clerezza/blob/829e5fa5/api.impl/src/main/java/org/apache/clerezza/api/impl/literal/LiteralImpl.java
----------------------------------------------------------------------
diff --git a/api.impl/src/main/java/org/apache/clerezza/api/impl/literal/LiteralImpl.java b/api.impl/src/main/java/org/apache/clerezza/api/impl/literal/LiteralImpl.java
new file mode 100644
index 0000000..7d86242
--- /dev/null
+++ b/api.impl/src/main/java/org/apache/clerezza/api/impl/literal/LiteralImpl.java
@@ -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.clerezza.api.impl.literal;
+
+import java.io.Serializable;
+
+import org.apache.clerezza.api.IRI;
+import org.apache.clerezza.api.Language;
+
+/**
+ *
+ * @author reto
+ */
+public class LiteralImpl extends AbstractLiteral implements Serializable {
+ private String lexicalForm;
+ private IRI dataType;
+ private int hashCode;
+ private Language language;
+
+ /**
+ * @param lexicalForm
+ * @param dataType
+ * @param Language the language of this literal
+ */
+ public LiteralImpl(String lexicalForm, IRI dataType, Language language) {
+ this.lexicalForm = lexicalForm;
+ this.dataType = dataType;
+ this.language = language;
+ this.hashCode = super.hashCode();
+ }
+
+ public IRI getDataType() {
+ return dataType;
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.clerezza.rdf.core.LiteralNode#getLexicalForm()
+ */
+ @Override
+ public String getLexicalForm() {
+ return lexicalForm;
+ }
+
+ @Override
+ public int hashCode() {
+ return hashCode;
+ }
+
+
+ @Override
+ public String toString() {
+ StringBuffer result = new StringBuffer();
+ result.append('\"');
+ result.append(getLexicalForm());
+ result.append('\"');
+ result.append("^^");
+ result.append(getDataType());
+ return result.toString();
+ }
+
+ @Override
+ public Language getLanguage() {
+ return language;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/clerezza/blob/829e5fa5/api.impl/src/main/java/org/apache/clerezza/api/impl/literal/NoConvertorException.java
----------------------------------------------------------------------
diff --git a/api.impl/src/main/java/org/apache/clerezza/api/impl/literal/NoConvertorException.java b/api.impl/src/main/java/org/apache/clerezza/api/impl/literal/NoConvertorException.java
new file mode 100644
index 0000000..a462394
--- /dev/null
+++ b/api.impl/src/main/java/org/apache/clerezza/api/impl/literal/NoConvertorException.java
@@ -0,0 +1,41 @@
+/*
+ * 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 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.
+ *
+ * @since 0.3
+ * @author reto
+ */
+public class NoConvertorException extends RuntimeException {
+
+ /**
+ * 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 NoConvertorException(Type type) {
+ super("No convertor available for type "+type);
+ }
+}
http://git-wip-us.apache.org/repos/asf/clerezza/blob/829e5fa5/api.impl/src/main/java/org/apache/clerezza/api/impl/literal/PlainLiteralImpl.java
----------------------------------------------------------------------
diff --git a/api.impl/src/main/java/org/apache/clerezza/api/impl/literal/PlainLiteralImpl.java b/api.impl/src/main/java/org/apache/clerezza/api/impl/literal/PlainLiteralImpl.java
new file mode 100644
index 0000000..aeef1ea
--- /dev/null
+++ b/api.impl/src/main/java/org/apache/clerezza/api/impl/literal/PlainLiteralImpl.java
@@ -0,0 +1,80 @@
+/*
+ * 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 java.io.Serializable;
+
+import org.apache.clerezza.api.IRI;
+import org.apache.clerezza.api.Language;
+import org.apache.clerezza.api.Literal;
+
+/**
+ *
+ * @author reto
+ */
+public class PlainLiteralImpl extends AbstractLiteral implements Literal, Serializable {
+
+ private final String lexicalForm;
+ private final Language language;
+
+ public PlainLiteralImpl(String value) {
+ this(value, null);
+ }
+
+ public PlainLiteralImpl(String value, Language language) {
+ if (value == null) {
+ throw new IllegalArgumentException("The literal string cannot be null");
+ }
+ this.lexicalForm = value;
+ this.language = language;
+ if (language == null) {
+ dataType = XSD_STRING;
+ } else {
+ dataType = RDF_LANG_STRING;
+ }
+ }
+
+ @Override
+ public String getLexicalForm() {
+ return lexicalForm;
+ }
+
+ @Override
+ public Language getLanguage() {
+ return language;
+ }
+
+ @Override
+ public String toString() {
+ final StringBuilder result = new StringBuilder();
+ result.append('\"').append(lexicalForm).append('\"');
+ if (language != null) {
+ result.append("@").append(language.toString());
+ }
+ return result.toString();
+ }
+
+ @Override
+ public IRI getDataType() {
+ return dataType;
+ }
+ private final IRI dataType;
+ private static final IRI XSD_STRING = new IRI("http://www.w3.org/2001/XMLSchema#string");
+ private static final IRI RDF_LANG_STRING = new IRI("http://www.w3.org/1999/02/22-rdf-syntax-ns#langString");
+}
http://git-wip-us.apache.org/repos/asf/clerezza/blob/829e5fa5/api.impl/src/main/java/org/apache/clerezza/api/impl/literal/SimpleLiteralFactory.java
----------------------------------------------------------------------
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);
+ }
+}
http://git-wip-us.apache.org/repos/asf/clerezza/blob/829e5fa5/api.impl/src/main/java/org/apache/clerezza/api/impl/literal/TypedLiteralImpl.java
----------------------------------------------------------------------
diff --git a/api.impl/src/main/java/org/apache/clerezza/api/impl/literal/TypedLiteralImpl.java b/api.impl/src/main/java/org/apache/clerezza/api/impl/literal/TypedLiteralImpl.java
new file mode 100644
index 0000000..8b029d2
--- /dev/null
+++ b/api.impl/src/main/java/org/apache/clerezza/api/impl/literal/TypedLiteralImpl.java
@@ -0,0 +1,79 @@
+/*
+ * 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 java.io.Serializable;
+
+import org.apache.clerezza.api.IRI;
+import org.apache.clerezza.api.Language;
+
+/**
+ *
+ * @author reto
+ */
+public class TypedLiteralImpl extends AbstractLiteral implements Serializable {
+ private String lexicalForm;
+ private IRI dataType;
+ private int hashCode;
+
+ /**
+ * @param lexicalForm
+ * @param dataType
+ */
+ public TypedLiteralImpl(String lexicalForm, IRI dataType) {
+ this.lexicalForm = lexicalForm;
+ this.dataType = dataType;
+ this.hashCode = super.hashCode();
+ }
+
+ public IRI getDataType() {
+ return dataType;
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.clerezza.rdf.core.LiteralNode#getLexicalForm()
+ */
+ @Override
+ public String getLexicalForm() {
+ return lexicalForm;
+ }
+
+ @Override
+ public int hashCode() {
+ return hashCode;
+ }
+
+
+ @Override
+ public String toString() {
+ StringBuffer result = new StringBuffer();
+ result.append('\"');
+ result.append(getLexicalForm());
+ result.append('\"');
+ result.append("^^");
+ result.append(getDataType());
+ return result.toString();
+ }
+
+ @Override
+ public Language getLanguage() {
+ return null;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/clerezza/blob/829e5fa5/api.impl/src/main/java/org/apache/clerezza/api/impl/package-info.java
----------------------------------------------------------------------
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
http://git-wip-us.apache.org/repos/asf/clerezza/blob/829e5fa5/api.impl/src/main/java/org/apache/clerezza/api/impl/simple/SimpleGraph.java
----------------------------------------------------------------------
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/simple/SimpleGraph.java
deleted file mode 100644
index 7220532..0000000
--- a/api.impl/src/main/java/org/apache/clerezza/api/impl/simple/SimpleGraph.java
+++ /dev/null
@@ -1,219 +0,0 @@
-/*
- * 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.simple;
-
-import org.apache.clerezza.api.IRI;
-import org.apache.clerezza.api.impl.AbstractGraph;
-
-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;
-
-/**
- * For now this is a minimalistic implementation, without any indexes or other
- * optimizations.
- *
- * @author reto
- */
-public class SimpleGraph extends AbstractGraph {
-
- final Set<Triple> triples;
-
- private boolean checkConcurrency = false;
-
- class SimpleIterator implements Iterator<Triple> {
-
- private Iterator<Triple> listIter;
- private boolean isValid = true;
-
- public SimpleIterator(Iterator<Triple> listIter) {
- this.listIter = listIter;
- }
- private Triple currentNext;
-
- @Override
- public boolean hasNext() {
- checkValidity();
- return listIter.hasNext();
- }
-
- @Override
- public Triple next() {
- checkValidity();
- currentNext = listIter.next();
- return currentNext;
- }
-
- @Override
- public void remove() {
- checkValidity();
- listIter.remove();
- triples.remove(currentNext);
- invalidateIterators(this);
- }
-
- private void checkValidity() throws ConcurrentModificationException {
- if (checkConcurrency && !isValid) {
- throw new ConcurrentModificationException();
- }
- }
-
- private void invalidate() {
- isValid = false;
- }
- }
-
- private final Set<SoftReference<SimpleIterator>> iterators =
- Collections.synchronizedSet(new HashSet<SoftReference<SimpleIterator>>());
-
- /**
- * Creates an empty SimpleGraph
- */
- public SimpleGraph() {
- triples = Collections.synchronizedSet(new HashSet<Triple>());
- }
-
- /**
- * Creates a SimpleGraph using the passed iterator, the iterator
- * is consumed before the constructor returns
- *
- * @param iterator
- */
- public SimpleGraph(Iterator<Triple> iterator) {
- triples = new HashSet<Triple>();
- while (iterator.hasNext()) {
- Triple triple = iterator.next();
- triples.add(triple);
- }
- }
-
- /**
- * Creates a SimpleGraph for the specified set of triples,
- * subsequent modification of baseSet do affect the created instance.
- *
- * @param baseSet
- */
- public SimpleGraph(Set<Triple> baseSet) {
- this.triples = baseSet;
- }
-
- /**
- * Creates a SimpleGraph for the specified collection of triples,
- * subsequent modification of baseSet do not affect the created instance.
- *
- * @param baseSet
- */
- public SimpleGraph(Collection<Triple> baseCollection) {
- this.triples = new HashSet<Triple>(baseCollection);
- }
-
- @Override
- public int performSize() {
- return triples.size();
- }
-
- @Override
- public Iterator<Triple> performFilter(final BlankNodeOrIRI subject, final IRI predicate, final RDFTerm object) {
- final List<Triple> tripleList = new ArrayList<Triple>();
- synchronized (triples) {
- Iterator<Triple> baseIter = triples.iterator();
- while (baseIter.hasNext()) {
- Triple triple = baseIter.next();
- if ((subject != null)
- && (!triple.getSubject().equals(subject))) {
- continue;
- }
- if ((predicate != null)
- && (!triple.getPredicate().equals(predicate))) {
- continue;
- }
- if ((object != null)
- && (!triple.getObject().equals(object))) {
- continue;
- }
- tripleList.add(triple);
- }
-
- final Iterator<Triple> listIter = tripleList.iterator();
- SimpleIterator resultIter = new SimpleIterator(listIter);
- if (checkConcurrency) {
- iterators.add(new SoftReference<SimpleIterator>(resultIter));
- }
- return resultIter;
- }
- }
-
-
- @Override
- public boolean performAdd(Triple e) {
- boolean modified = triples.add(e);
- if (modified) {
- invalidateIterators(null);
- }
- return modified;
- }
-
- private void invalidateIterators(SimpleIterator caller) {
- if (!checkConcurrency) {
- return;
- }
- Set<SoftReference> oldReferences = new HashSet<SoftReference>();
- synchronized(iterators) {
- for (SoftReference<SimpleGraph.SimpleIterator> softReference : iterators) {
- SimpleIterator simpleIterator = softReference.get();
- if (simpleIterator == null) {
- oldReferences.add(softReference);
- continue;
- }
- if (simpleIterator != caller) {
- simpleIterator.invalidate();
- }
- }
- }
- iterators.removeAll(oldReferences);
- }
-
- /**
- * Specifies whether or not to throw <code>ConcurrentModificationException</code>s,
- * if this simple triple collection is modified concurrently. Concurrency
- * check is set to false by default.
- *
- * @param bool Specifies whether or not to check concurrent modifications.
- */
- public void setCheckConcurrency(boolean bool) {
- checkConcurrency = bool;
- }
-
-
- @Override
- public ImmutableGraph getImmutableGraph() {
- return new SimpleImmutableGraph(this);
- }
-}
http://git-wip-us.apache.org/repos/asf/clerezza/blob/829e5fa5/api.impl/src/main/java/org/apache/clerezza/api/impl/simple/SimpleImmutableGraph.java
----------------------------------------------------------------------
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/simple/SimpleImmutableGraph.java
deleted file mode 100644
index d48fe5c..0000000
--- a/api.impl/src/main/java/org/apache/clerezza/api/impl/simple/SimpleImmutableGraph.java
+++ /dev/null
@@ -1,80 +0,0 @@
-/*
- * 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.simple;
-
-import org.apache.clerezza.api.impl.AbstractImmutableGraph;
-
-import java.util.Iterator;
-
-import org.apache.clerezza.api.BlankNodeOrIRI;
-import org.apache.clerezza.api.RDFTerm;
-import org.apache.clerezza.api.Triple;
-import org.apache.clerezza.api.Graph;
-import org.apache.clerezza.api.IRI;
-
-/**
- *
- * @author reto
- */
-public class SimpleImmutableGraph extends AbstractImmutableGraph {
-
- private Graph graph;
-
- /**
- * Creates a ImmutableGraph with the triples in Graph
- *
- * @param Graph the collection of triples this ImmutableGraph shall consist of
- */
- public SimpleImmutableGraph(Graph Graph) {
- this.graph = new SimpleGraph(Graph.iterator());
- }
-
- /**
- * Creates a ImmutableGraph with the triples in Graph.
- *
- * This construction allows to specify if the Graph might change
- * in future. If GraphWillNeverChange is set to true it will
- * assume that the collection never changes, in this case the collection
- * isn't copied making things more efficient.
- *
- * @param Graph the collection of triples this ImmutableGraph shall consist of
- * @param GraphWillNeverChange true if the caller promises Graph will never change
- */
- public SimpleImmutableGraph(Graph Graph, boolean GraphWillNeverChange) {
- if (!GraphWillNeverChange) {
- this.graph = new SimpleGraph(Graph.iterator());
- } else {
- this.graph = Graph;
- }
- }
-
- public SimpleImmutableGraph(Iterator<Triple> tripleIter) {
- this.graph = new SimpleGraph(tripleIter);
- }
-
- @Override
- public int performSize() {
- return graph.size();
- }
-
- @Override
- public Iterator<Triple> performFilter(BlankNodeOrIRI subject, IRI predicate, RDFTerm object) {
- return graph.filter(subject, predicate, object);
- }
-}
http://git-wip-us.apache.org/repos/asf/clerezza/blob/829e5fa5/api.impl/src/main/java/org/apache/clerezza/api/impl/simple/SimpleMGraph.java
----------------------------------------------------------------------
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/simple/SimpleMGraph.java
deleted file mode 100644
index da961c2..0000000
--- a/api.impl/src/main/java/org/apache/clerezza/api/impl/simple/SimpleMGraph.java
+++ /dev/null
@@ -1,56 +0,0 @@
-/*
- * 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.simple;
-
-import java.util.Collection;
-import java.util.Iterator;
-import java.util.Set;
-
-import org.apache.clerezza.api.Graph;
-import org.apache.clerezza.api.Triple;
-
-/**
- *
- * @deprecated Use SimpleGraph
- * @author reto
- */
-@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 SimpleMGraph(Iterator<Triple> iterator) {
- super(iterator);
- }
-
-}
-
-
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/clerezza/blob/829e5fa5/api.impl/src/main/java/org/apache/clerezza/api/impl/util/Base64.java
----------------------------------------------------------------------
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;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/clerezza/blob/829e5fa5/api.impl/src/main/java/org/apache/clerezza/api/impl/util/W3CDateFormat.java
----------------------------------------------------------------------
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.
+ */
+