From df3205ee0fef65557eba542b6339d531dd368bb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=CC=88rg=20Prante?= Date: Sun, 13 Nov 2016 19:58:03 +0100 Subject: [PATCH] re-enable yield() method, add TreeMultiMap --- build.gradle | 4 +- .../org/xbib/content/settings/Settings.java | 2 +- .../rdf/io/rdfxml/RdfXmlContentParser.java | 52 +++++----- .../rdf/io/xml/AbstractXmlHandler.java | 4 + .../xbib/content/rdf/util/TreeMultiMap.java | 97 +++++++++++++++++++ .../xbib/content/rdf/util/MultiMapTest.java | 43 ++++++++ .../xbib/content/rdf/util/package-info.java | 4 + gradle/publish.gradle | 4 +- 8 files changed, 183 insertions(+), 27 deletions(-) create mode 100644 content-rdf/src/main/java/org/xbib/content/rdf/util/TreeMultiMap.java create mode 100644 content-rdf/src/test/java/org/xbib/content/rdf/util/MultiMapTest.java create mode 100644 content-rdf/src/test/java/org/xbib/content/rdf/util/package-info.java diff --git a/build.gradle b/build.gradle index c25da96..0d98775 100644 --- a/build.gradle +++ b/build.gradle @@ -1,7 +1,7 @@ plugins { id "org.sonarqube" version "2.2" id "org.ajoberstar.github-pages" version "1.6.0-rc.1" - id "org.xbib.gradle.plugin.jbake" version "1.1.0" + id "org.xbib.gradle.plugin.jbake" version "1.2.1" } @@ -14,7 +14,7 @@ ext { allprojects { group = 'org.xbib' - version = '1.0.3' + version = '1.0.4' apply plugin: 'java' apply plugin: 'maven' diff --git a/content-core/src/main/java/org/xbib/content/settings/Settings.java b/content-core/src/main/java/org/xbib/content/settings/Settings.java index fd1b080..482a874 100644 --- a/content-core/src/main/java/org/xbib/content/settings/Settings.java +++ b/content-core/src/main/java/org/xbib/content/settings/Settings.java @@ -661,7 +661,7 @@ public class Settings { */ public Builder loadFromSystemProperties() { for (Map.Entry entry : System.getProperties().entrySet()) { - put((String)entry.getKey(), (String)entry.getValue()); + put((String) entry.getKey(), (String) entry.getValue()); } return this; } diff --git a/content-rdf/src/main/java/org/xbib/content/rdf/io/rdfxml/RdfXmlContentParser.java b/content-rdf/src/main/java/org/xbib/content/rdf/io/rdfxml/RdfXmlContentParser.java index 27c54a4..cb95be1 100644 --- a/content-rdf/src/main/java/org/xbib/content/rdf/io/rdfxml/RdfXmlContentParser.java +++ b/content-rdf/src/main/java/org/xbib/content/rdf/io/rdfxml/RdfXmlContentParser.java @@ -127,6 +127,10 @@ public class RdfXmlContentParser implements RdfConst return this; } + public RdfContentBuilder getBuilder() { + return builder; + } + public XmlHandler getHandler() { return xmlHandler; } @@ -359,6 +363,32 @@ public class RdfXmlContentParser implements RdfConst return l; } + /** + * Allow to override this method to control triple stream generation. + * @param triple a triple + * @throws IOException if yield does not work + */ + protected void yield(Triple triple) throws IOException { + if (builder != null) { + builder.receive(triple); + } + } + + private void yield(Object s, Object p, Object o) throws IOException { + yield(new DefaultTriple(resource.newSubject(s), resource.newPredicate(p), resource.newObject(o))); + } + + // produce a (possibly) reified triple + private void yield(Object s, IRI p, Object o, IRI reified) throws IOException { + yield(s, p, o); + if (reified != null) { + yield(reified, RDF_TYPE, RDF_STATEMENT); + yield(reified, RDF_SUBJECT, s); + yield(reified, RDF_PREDICATE, p); + yield(reified, RDF_OBJECT, o); + } + } + private static class Frame { IRI node = null; // the subject/object String lang = null; // the language tag @@ -638,27 +668,6 @@ public class RdfXmlContentParser implements RdfConst return b; } - // produce a (possibly) reified triple - private void yield(Object s, IRI p, Object o, IRI reified) throws IOException { - yield(s, p, o); - if (reified != null) { - yield(reified, RDF_TYPE, RDF_STATEMENT); - yield(reified, RDF_SUBJECT, s); - yield(reified, RDF_PREDICATE, p); - yield(reified, RDF_OBJECT, o); - } - } - - private void yield(Object s, Object p, Object o) throws IOException { - yield(new DefaultTriple(resource.newSubject(s), resource.newPredicate(p), resource.newObject(o))); - } - - private void yield(Triple triple) throws IOException { - if (builder != null) { - builder.receive(triple); - } - } - // if we're looking at a subject, is it an item in a Collection? private boolean isCollectionItem(Deque stack) throws SAXException { if (inPredicate(stack)) { @@ -668,6 +677,5 @@ public class RdfXmlContentParser implements RdfConst return false; } } - } } diff --git a/content-rdf/src/main/java/org/xbib/content/rdf/io/xml/AbstractXmlHandler.java b/content-rdf/src/main/java/org/xbib/content/rdf/io/xml/AbstractXmlHandler.java index 5397d6e..6f38534 100644 --- a/content-rdf/src/main/java/org/xbib/content/rdf/io/xml/AbstractXmlHandler.java +++ b/content-rdf/src/main/java/org/xbib/content/rdf/io/xml/AbstractXmlHandler.java @@ -53,6 +53,10 @@ public abstract class AbstractXmlHandler

return resource; } + public LinkedList getParents() { + return parents; + } + @Override public AbstractXmlHandler

setDefaultNamespace(String prefix, String namespaceURI) { this.defaultPrefix = prefix; diff --git a/content-rdf/src/main/java/org/xbib/content/rdf/util/TreeMultiMap.java b/content-rdf/src/main/java/org/xbib/content/rdf/util/TreeMultiMap.java new file mode 100644 index 0000000..545dedc --- /dev/null +++ b/content-rdf/src/main/java/org/xbib/content/rdf/util/TreeMultiMap.java @@ -0,0 +1,97 @@ +package org.xbib.content.rdf.util; + +import java.util.Collection; +import java.util.LinkedHashSet; +import java.util.Map; +import java.util.Set; +import java.util.TreeMap; + +/** + * A {@link TreeMap} based multi map. The keys ore ordered. + * @param te key type + * @param the value type + */ +public class TreeMultiMap implements MultiMap { + + private final Map> map = new TreeMap<>(); + + @Override + public int size() { + return map.size(); + } + + @Override + public void clear() { + map.clear(); + } + + @Override + public boolean isEmpty() { + return map.isEmpty(); + } + + @Override + public boolean containsKey(K key) { + return map.containsKey(key); + } + + @Override + public Set keySet() { + return map.keySet(); + } + + @Override + public boolean put(K key, V value) { + Set set = map.get(key); + if (set == null) { + set = new LinkedHashSet<>(); + set.add(value); + map.put(key, set); + return true; + } else { + set.add(value); + return false; + } + } + + @Override + public void putAll(K key, Collection values) { + Set set = map.get(key); + if (set == null) { + set = new LinkedHashSet<>(); + map.put(key, set); + } + set.addAll(values); + } + + @Override + public Collection get(K key) { + return map.get(key); + } + + @Override + public Set remove(K key) { + return map.remove(key); + } + + @Override + public boolean remove(K key, V value) { + Set set = map.get(key); + return set != null && set.remove(value); + } + + @Override + public boolean equals(Object obj) { + return obj != null && obj instanceof TreeMultiMap && map.equals(((TreeMultiMap) obj).map); + } + + @Override + public int hashCode() { + return map.hashCode(); + } + + @Override + public String toString() { + return map.toString(); + } +} diff --git a/content-rdf/src/test/java/org/xbib/content/rdf/util/MultiMapTest.java b/content-rdf/src/test/java/org/xbib/content/rdf/util/MultiMapTest.java new file mode 100644 index 0000000..a6a6af0 --- /dev/null +++ b/content-rdf/src/test/java/org/xbib/content/rdf/util/MultiMapTest.java @@ -0,0 +1,43 @@ +package org.xbib.content.rdf.util; + +import org.junit.Test; + +import java.util.Arrays; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +/** + * + */ +public class MultiMapTest { + + @Test + public void testLinkedHashMultiMap() { + LinkedHashMultiMap map = new LinkedHashMultiMap<>(); + map.put("a", "b"); + map.put("b", "c"); + map.put("a", "c"); + assertTrue(map.containsKey("a")); + assertTrue(map.containsKey("b")); + assertEquals("[b, c]", map.get("a").toString()); + assertEquals("[c]", map.get("b").toString()); + map.putAll("a", Arrays.asList("d", "e")); + assertEquals("[b, c, d, e]", map.get("a").toString()); + } + + @Test + public void testTreeMultiMap() { + TreeMultiMap map = new TreeMultiMap<>(); + map.put("a", "b"); + map.put("b", "c"); + map.put("a", "c"); + assertTrue(map.containsKey("a")); + assertTrue(map.containsKey("b")); + assertEquals("[a, b]", map.keySet().toString()); + assertEquals("[b, c]", map.get("a").toString()); + assertEquals("[c]", map.get("b").toString()); + map.putAll("a", Arrays.asList("d", "e")); + assertEquals("[b, c, d, e]", map.get("a").toString()); + } +} diff --git a/content-rdf/src/test/java/org/xbib/content/rdf/util/package-info.java b/content-rdf/src/test/java/org/xbib/content/rdf/util/package-info.java new file mode 100644 index 0000000..6f0bc4f --- /dev/null +++ b/content-rdf/src/test/java/org/xbib/content/rdf/util/package-info.java @@ -0,0 +1,4 @@ +/** + * Classes for testing multi maps. + */ +package org.xbib.content.rdf.util; diff --git a/gradle/publish.gradle b/gradle/publish.gradle index 9d43d7b..caf0531 100644 --- a/gradle/publish.gradle +++ b/gradle/publish.gradle @@ -1,5 +1,5 @@ -task xbibUpload(type: Upload) { +task xbibUpload(type: Upload, dependsOn: build) { configuration = configurations.archives uploadDescriptor = true repositories { @@ -14,7 +14,7 @@ task xbibUpload(type: Upload) { } } -task sonatypeUpload(type: Upload) { +task sonatypeUpload(type: Upload, dependsOn: build) { configuration = configurations.archives uploadDescriptor = true repositories {