You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@any23.apache.org by mo...@apache.org on 2012/05/13 18:47:27 UTC
svn commit: r1337933 - in /incubator/any23/trunk:
core/src/main/java/org/apache/any23/io/nquads/
core/src/main/java/org/apache/any23/rdf/
core/src/test/java/org/apache/any23/cli/
core/src/test/java/org/apache/any23/rdf/ plugins/basic-crawler/src/test/j...
Author: mostarda
Date: Sun May 13 16:47:27 2012
New Revision: 1337933
URL: http://svn.apache.org/viewvc?rev=1337933&view=rev
Log:
Improved RDFUtils API, removed RDFUtils.Parser enum type. Improved compliancy of RDFUtils and NQuads support with Sesame RIO ServiceRegistry . This commit is related to issue #ANY23-83.
Added:
incubator/any23/trunk/core/src/main/java/org/apache/any23/io/nquads/NQuadsParserFactory.java
incubator/any23/trunk/core/src/main/java/org/apache/any23/io/nquads/NQuadsWriterFactory.java
Modified:
incubator/any23/trunk/core/src/main/java/org/apache/any23/rdf/RDFUtils.java
incubator/any23/trunk/core/src/test/java/org/apache/any23/cli/RoverTest.java
incubator/any23/trunk/core/src/test/java/org/apache/any23/rdf/RDFUtilsTest.java
incubator/any23/trunk/plugins/basic-crawler/src/test/java/org/apache/any23/cli/CrawlerTest.java
Added: incubator/any23/trunk/core/src/main/java/org/apache/any23/io/nquads/NQuadsParserFactory.java
URL: http://svn.apache.org/viewvc/incubator/any23/trunk/core/src/main/java/org/apache/any23/io/nquads/NQuadsParserFactory.java?rev=1337933&view=auto
==============================================================================
--- incubator/any23/trunk/core/src/main/java/org/apache/any23/io/nquads/NQuadsParserFactory.java (added)
+++ incubator/any23/trunk/core/src/main/java/org/apache/any23/io/nquads/NQuadsParserFactory.java Sun May 13 16:47:27 2012
@@ -0,0 +1,45 @@
+/*
+ * 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.any23.io.nquads;
+
+import org.kohsuke.MetaInfServices;
+import org.openrdf.rio.RDFFormat;
+import org.openrdf.rio.RDFParser;
+import org.openrdf.rio.RDFParserFactory;
+
+/**
+ * Implementation of {@link org.openrdf.rio.RDFParserFactory} for <code>NQuads</code>.
+ *
+ * @author Michele Mostarda (mostarda@fbk.eu)
+ */
+@MetaInfServices
+public class NQuadsParserFactory implements RDFParserFactory {
+
+ public NQuadsParserFactory() {}
+
+ @Override
+ public RDFFormat getRDFFormat() {
+ return NQuads.FORMAT;
+ }
+
+ @Override
+ public RDFParser getParser() {
+ return new NQuadsParser();
+ }
+
+}
Added: incubator/any23/trunk/core/src/main/java/org/apache/any23/io/nquads/NQuadsWriterFactory.java
URL: http://svn.apache.org/viewvc/incubator/any23/trunk/core/src/main/java/org/apache/any23/io/nquads/NQuadsWriterFactory.java?rev=1337933&view=auto
==============================================================================
--- incubator/any23/trunk/core/src/main/java/org/apache/any23/io/nquads/NQuadsWriterFactory.java (added)
+++ incubator/any23/trunk/core/src/main/java/org/apache/any23/io/nquads/NQuadsWriterFactory.java Sun May 13 16:47:27 2012
@@ -0,0 +1,51 @@
+/*
+ * 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.any23.io.nquads;
+
+import org.kohsuke.MetaInfServices;
+import org.openrdf.rio.RDFFormat;
+import org.openrdf.rio.RDFWriter;
+import org.openrdf.rio.RDFWriterFactory;
+
+import java.io.OutputStream;
+import java.io.Writer;
+
+/**
+ * Implementation of {@link RDFWriterFactory} for <code>NQuads</code>.
+ *
+ * @author Michele Mostarda (mostarda@fbk.eu)
+ */
+@MetaInfServices
+public class NQuadsWriterFactory implements RDFWriterFactory {
+
+ @Override
+ public RDFFormat getRDFFormat() {
+ return NQuads.FORMAT;
+ }
+
+ @Override
+ public RDFWriter getWriter(OutputStream outputStream) {
+ return new NQuadsWriter(outputStream);
+ }
+
+ @Override
+ public RDFWriter getWriter(Writer writer) {
+ return new NQuadsWriter(writer);
+ }
+
+}
Modified: incubator/any23/trunk/core/src/main/java/org/apache/any23/rdf/RDFUtils.java
URL: http://svn.apache.org/viewvc/incubator/any23/trunk/core/src/main/java/org/apache/any23/rdf/RDFUtils.java?rev=1337933&r1=1337932&r2=1337933&view=diff
==============================================================================
--- incubator/any23/trunk/core/src/main/java/org/apache/any23/rdf/RDFUtils.java (original)
+++ incubator/any23/trunk/core/src/main/java/org/apache/any23/rdf/RDFUtils.java Sun May 13 16:47:27 2012
@@ -17,7 +17,6 @@
package org.apache.any23.rdf;
-import org.apache.any23.io.nquads.NQuadsParser;
import org.apache.any23.util.MathUtils;
import org.openrdf.model.BNode;
import org.openrdf.model.Literal;
@@ -29,13 +28,13 @@ import org.openrdf.model.ValueFactory;
import org.openrdf.model.impl.URIImpl;
import org.openrdf.model.impl.ValueFactoryImpl;
import org.openrdf.model.vocabulary.RDF;
+import org.openrdf.rio.RDFFormat;
import org.openrdf.rio.RDFHandler;
import org.openrdf.rio.RDFHandlerException;
import org.openrdf.rio.RDFParseException;
import org.openrdf.rio.RDFParser;
-import org.openrdf.rio.ntriples.NTriplesParser;
-import org.openrdf.rio.rdfxml.RDFXMLParser;
-import org.openrdf.rio.turtle.TurtleParser;
+import org.openrdf.rio.RDFWriter;
+import org.openrdf.rio.Rio;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
@@ -43,10 +42,13 @@ import javax.xml.datatype.XMLGregorianCa
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.Writer;
import java.net.URISyntaxException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
+import java.util.Collection;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
@@ -54,21 +56,11 @@ import java.util.List;
/**
* Basic class providing a set of utility methods when dealing with <i>RDF</i>.
*
- * @author Davide Palmisano (dpalmisano@gmail.com)
* @author Michele Mostarda (mostarda@fbk.eu)
+ * @author Davide Palmisano (dpalmisano@gmail.com)
*/
public class RDFUtils {
- /**
- * List of supported <i>RDF</i> parsers.
- */
- public enum Parser {
- RDFXML,
- Turtle,
- NTriples,
- NQuads
- }
-
private static final ValueFactory valueFactory = ValueFactoryImpl.getInstance();
/**
@@ -336,25 +328,49 @@ public class RDFUtils {
}
/**
+ *
+ * Returns all the available {@link RDFFormat}s.
+ *
+ * @return an unmodifiable collection of formats.
+ * @see org.openrdf.rio.RDFFormat#values()
+ */
+ public static Collection<RDFFormat> getFormats() {
+ return RDFFormat.values();
+ }
+
+ /**
* Creates a new {@link RDFParser} instance.
*
- * @param p parser type.
+ * @param format parser format.
* @return parser instance.
- * @throws IllegalArgumentException if parser is unsupported.
+ * @throws IllegalArgumentException if format is not supported.
*/
- public static RDFParser getRDFParser(Parser p) {
- switch (p) {
- case RDFXML:
- return new RDFXMLParser();
- case Turtle:
- return new TurtleParser();
- case NTriples:
- return new NTriplesParser();
- case NQuads:
- return new NQuadsParser();
- default:
- throw new IllegalArgumentException();
- }
+ public static RDFParser getParser(RDFFormat format) {
+ return Rio.createParser(format);
+ }
+
+ /**
+ * Creates a new {@link RDFWriter} instance.
+ *
+ * @param format output format.
+ * @param writer data output writer.
+ * @return writer instance.
+ * @throws IllegalArgumentException if format is not supported.
+ */
+ public static RDFWriter getWriter(RDFFormat format, Writer writer) {
+ return Rio.createWriter(format, writer);
+ }
+
+ /**
+ * Creates a new {@link RDFWriter} instance.
+ *
+ * @param format output format.
+ * @param os output stream.
+ * @return writer instance.
+ * @throws IllegalArgumentException if format is not supported.
+ */
+ public static RDFWriter getWriter(RDFFormat format, OutputStream os) {
+ return Rio.createWriter(format, os);
}
/**
@@ -364,27 +380,16 @@ public class RDFUtils {
* @return parser matching the extension.
* @throws IllegalArgumentException if no extension matches.
*/
- public static Parser getParserByExtension(String ext) {
- if("rdf".equals(ext)) {
- return Parser.RDFXML;
- }
- if("ttl".equals(ext)) {
- return Parser.Turtle;
- }
- if("nt".equals(ext)) {
- return Parser.NTriples;
- }
- if("nq".equals(ext)) {
- return Parser.NQuads;
- }
- throw new IllegalArgumentException("Unknown extension : " + ext);
+ public static RDFFormat getFormatByExtension(String ext) {
+ if( ! ext.startsWith(".") ) ext = "." + ext;
+ return Rio.getParserFormatForFileName(ext);
}
/**
* Parses the content of <code>is</code> input stream with the
* specified parser <code>p</code> using <code>baseURI</code>.
*
- * @param parser parser instance.
+ * @param format input format type.
* @param is input stream containing <code>RDF</data>.
* @param baseURI base uri.
* @return list of statements detected within the input stream.
@@ -392,9 +397,10 @@ public class RDFUtils {
* @throws IOException
* @throws RDFParseException
*/
- public static Statement[] parseRDF(RDFParser parser, InputStream is, String baseURI)
+ public static Statement[] parseRDF(RDFFormat format, InputStream is, String baseURI)
throws RDFHandlerException, IOException, RDFParseException {
final BufferRDFHandler handler = new BufferRDFHandler();
+ final RDFParser parser = getParser(format);
parser.setVerifyData(true);
parser.setStopAtFirstError(true);
parser.setPreserveBNodeIDs(true);
@@ -405,52 +411,34 @@ public class RDFUtils {
/**
* Parses the content of <code>is</code> input stream with the
- * specified parser instance <code>p</code> using <code>baseURI</code>.
- *
- * @param p parser type.
- * @param is input stream containing <code>RDF</data>.
- * @param baseURI base uri.
- * @return list of statements detected within the input stream.
- * @throws RDFHandlerException
- * @throws IOException
- * @throws RDFParseException
- */
- public static Statement[] parseRDF(Parser p, InputStream is, String baseURI)
- throws RDFHandlerException, IOException, RDFParseException {
- final RDFParser parser = getRDFParser(p);
- return parseRDF(parser, is, baseURI);
- }
-
- /**
- * Parses the content of <code>is</code> input stream with the
* specified parser <code>p</code> using <code>''</code> as base URI.
*
- * @param p parser type.
+ * @param format input format type.
* @param is input stream containing <code>RDF</data>.
* @return list of statements detected within the input stream.
* @throws RDFHandlerException
* @throws IOException
* @throws RDFParseException
*/
- public static Statement[] parseRDF(Parser p, InputStream is)
+ public static Statement[] parseRDF(RDFFormat format, InputStream is)
throws RDFHandlerException, IOException, RDFParseException {
- return parseRDF(p, is, "");
+ return parseRDF(format, is, "");
}
/**
* Parses the content of <code>in</code> string with the
* specified parser <code>p</code> using <code>''</code> as base URI.
*
- * @param p parser type.
+ * @param format input format type.
* @param in input string containing <code>RDF</data>.
* @return list of statements detected within the input string.
* @throws RDFHandlerException
* @throws IOException
* @throws RDFParseException
*/
- public static Statement[] parseRDF(Parser p, String in)
+ public static Statement[] parseRDF(RDFFormat format, String in)
throws RDFHandlerException, IOException, RDFParseException {
- return parseRDF(p, new ByteArrayInputStream(in.getBytes()));
+ return parseRDF(format, new ByteArrayInputStream(in.getBytes()));
}
/**
@@ -468,7 +456,7 @@ public class RDFUtils {
if(extIndex == -1)
throw new IllegalArgumentException("Error while detecting the extension in resource name " + resource);
final String extension = resource.substring(extIndex + 1);
- return parseRDF( getParserByExtension(extension), RDFUtils.class.getResourceAsStream(resource) );
+ return parseRDF( getFormatByExtension(extension), RDFUtils.class.getResourceAsStream(resource) );
}
/**
Modified: incubator/any23/trunk/core/src/test/java/org/apache/any23/cli/RoverTest.java
URL: http://svn.apache.org/viewvc/incubator/any23/trunk/core/src/test/java/org/apache/any23/cli/RoverTest.java?rev=1337933&r1=1337932&r2=1337933&view=diff
==============================================================================
--- incubator/any23/trunk/core/src/test/java/org/apache/any23/cli/RoverTest.java (original)
+++ incubator/any23/trunk/core/src/test/java/org/apache/any23/cli/RoverTest.java Sun May 13 16:47:27 2012
@@ -17,6 +17,7 @@
package org.apache.any23.cli;
+import org.apache.any23.io.nquads.NQuads;
import org.apache.any23.rdf.RDFUtils;
import org.apache.any23.util.FileUtils;
import org.apache.any23.util.StringUtils;
@@ -120,7 +121,7 @@ public class RoverTest extends ToolTestB
);
final String outNQuads = FileUtils.readFileContent(outFile);
- final Statement[] statements = RDFUtils.parseRDF(RDFUtils.Parser.NQuads, outNQuads);
+ final Statement[] statements = RDFUtils.parseRDF(NQuads.FORMAT, outNQuads);
Assert.assertTrue("Unexpected number of statements.", statements.length > 10);
}
Modified: incubator/any23/trunk/core/src/test/java/org/apache/any23/rdf/RDFUtilsTest.java
URL: http://svn.apache.org/viewvc/incubator/any23/trunk/core/src/test/java/org/apache/any23/rdf/RDFUtilsTest.java?rev=1337933&r1=1337932&r2=1337933&view=diff
==============================================================================
--- incubator/any23/trunk/core/src/test/java/org/apache/any23/rdf/RDFUtilsTest.java (original)
+++ incubator/any23/trunk/core/src/test/java/org/apache/any23/rdf/RDFUtilsTest.java Sun May 13 16:47:27 2012
@@ -17,10 +17,14 @@
package org.apache.any23.rdf;
+import org.apache.any23.io.nquads.NQuads;
import org.junit.Assert;
import org.junit.Test;
+import org.openrdf.rio.RDFFormat;
import javax.xml.datatype.DatatypeConfigurationException;
+import java.io.ByteArrayOutputStream;
+import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.text.ParseException;
@@ -28,6 +32,7 @@ import java.text.ParseException;
/**
* Reference test class for {@link RDFUtils}.
*
+ * @author Michele Mostarda (mostarda@fbk.eu)
* @author Davide Palmisano (palmisano@gmail.com)
*/
public class RDFUtilsTest {
@@ -57,4 +62,41 @@ public class RDFUtilsTest {
);
}
+ /**
+ * Tests the extension support.
+ */
+ @Test
+ public void testGetRDFFormatByExtension() {
+ Assert.assertEquals(RDFFormat.NTRIPLES, RDFUtils.getFormatByExtension("nt"));
+ Assert.assertEquals(RDFFormat.TURTLE , RDFUtils.getFormatByExtension("ttl"));
+ Assert.assertEquals(NQuads.FORMAT, RDFUtils.getFormatByExtension("nq"));
+ Assert.assertEquals(NQuads.FORMAT, RDFUtils.getFormatByExtension(".nq"));
+ }
+
+ /**
+ * Tests the <code>NQuads</code> format support.
+ */
+ @Test
+ public void testGetNQuadsFormat() {
+ RDFUtils.getFormats().contains(NQuads.FORMAT);
+ }
+
+ /**
+ * Tests the <code>NQuads</code> parsing support.
+ */
+ @Test
+ public void testGetNQuadsParser() {
+ Assert.assertNotNull( RDFUtils.getParser(NQuads.FORMAT) );
+ }
+
+ /**
+ * Tests the <code>NQuads</code> writing support.
+ */
+ @Test
+ public void testGetNQuadsWriter() {
+ Assert.assertNotNull(
+ RDFUtils.getWriter(NQuads.FORMAT, new OutputStreamWriter(new ByteArrayOutputStream() ) )
+ );
+ }
+
}
Modified: incubator/any23/trunk/plugins/basic-crawler/src/test/java/org/apache/any23/cli/CrawlerTest.java
URL: http://svn.apache.org/viewvc/incubator/any23/trunk/plugins/basic-crawler/src/test/java/org/apache/any23/cli/CrawlerTest.java?rev=1337933&r1=1337932&r2=1337933&view=diff
==============================================================================
--- incubator/any23/trunk/plugins/basic-crawler/src/test/java/org/apache/any23/cli/CrawlerTest.java (original)
+++ incubator/any23/trunk/plugins/basic-crawler/src/test/java/org/apache/any23/cli/CrawlerTest.java Sun May 13 16:47:27 2012
@@ -18,6 +18,7 @@
package org.apache.any23.cli;
import org.apache.any23.Any23OnlineTestBase;
+import org.apache.any23.io.nquads.NQuads;
import org.apache.any23.rdf.RDFUtils;
import org.apache.any23.util.FileUtils;
import org.junit.Test;
@@ -88,7 +89,7 @@ public class CrawlerTest extends Any23On
allLinesExceptLast.append(lines[i]);
}
- final Statement[] statements = RDFUtils.parseRDF(RDFUtils.Parser.NQuads, allLinesExceptLast.toString());
+ final Statement[] statements = RDFUtils.parseRDF(NQuads.FORMAT, allLinesExceptLast.toString());
assertTrue(statements.length > 0);
}