Author: mostarda
Date: Sun May 13 15:24:31 2012
New Revision: 1337919
URL: http://svn.apache.org/viewvc?rev=1337919&view=rev
Log:
Improved util class usability. Related to #ANY23-83.
Modified:
incubator/any23/trunk/core/src/main/java/org/apache/any23/rdf/RDFUtils.java
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=1337919&r1=1337918&r2=1337919&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 15:24:31 2012
@@ -294,6 +294,18 @@ public class RDFUtils {
}
/**
+ * Creates a statement of type: <code>toValue(s), toValue(p),
toValue(o)</code>
+ *
+ * @param s subject.
+ * @param p predicate.
+ * @param o object.
+ * @return a statement instance.
+ */
+ public static Statement triple(String s, String p, String o) {
+ return valueFactory.createStatement((Resource) toValue(s), (URI)
toValue(p), toValue(o));
+ }
+
+ /**
* Creates a {@link Statement}.
*/
public static Statement quad(Resource s, URI p, Value o, Resource g) {
@@ -301,6 +313,13 @@ public class RDFUtils {
}
/**
+ * Creates a statement of type: <code>toValue(s), toValue(p), toValue(o),
toValue(g)</code>
+ */
+ public static Statement quad(String s, String p, String o, String g) {
+ return valueFactory.createStatement((Resource) toValue(s), (URI)
toValue(p), toValue(o), (Resource) toValue(g));
+ }
+
+ /**
* Creates a {@link Value}. If <code>s == 'a'</code> returns
* an {@link RDF#TYPE}. If <code> s.matches('[a-z0-9]+:.*')</code>
* expands the corresponding prefix using {@link PopularPrefixes}.
@@ -308,7 +327,7 @@ public class RDFUtils {
* @param s
* @return a value instance.
*/
- public static Value toRDF(String s) {
+ public static Value toValue(String s) {
if ("a".equals(s)) return RDF.TYPE;
if (s.matches("[a-z0-9]+:.*")) {
return PopularPrefixes.get().expand(s);
@@ -317,18 +336,6 @@ public class RDFUtils {
}
/**
- * Creates a statement of type: <code>toRDF(s), toRDF(p), toRDF(o)</code>
- *
- * @param s subject.
- * @param p predicate.
- * @param o object.
- * @return a statement instance.
- */
- public static Statement toTriple(String s, String p, String o) {
- return valueFactory.createStatement((Resource) toRDF(s), (URI)
toRDF(p), toRDF(o));
- }
-
- /**
* Creates a new {@link RDFParser} instance.
*
* @param p parser type.
@@ -357,7 +364,7 @@ public class RDFUtils {
* @return parser matching the extension.
* @throws IllegalArgumentException if no extension matches.
*/
- public static Parser getParserFromExtension(String ext) {
+ public static Parser getParserByExtension(String ext) {
if("rdf".equals(ext)) {
return Parser.RDFXML;
}
@@ -377,7 +384,7 @@ public class RDFUtils {
* Parses the content of <code>is</code> input stream with the
* specified parser <code>p</code> using <code>baseURI</code>.
*
- * @param p parser type.
+ * @param parser parser instance.
* @param is input stream containing <code>RDF</data>.
* @param baseURI base uri.
* @return list of statements detected within the input stream.
@@ -385,10 +392,9 @@ public class RDFUtils {
* @throws IOException
* @throws RDFParseException
*/
- public static Statement[] parseRDF(Parser p, InputStream is, String
baseURI)
+ public static Statement[] parseRDF(RDFParser parser, InputStream is,
String baseURI)
throws RDFHandlerException, IOException, RDFParseException {
final BufferRDFHandler handler = new BufferRDFHandler();
- final RDFParser parser = getRDFParser(p);
parser.setVerifyData(true);
parser.setStopAtFirstError(true);
parser.setPreserveBNodeIDs(true);
@@ -399,6 +405,24 @@ 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.
@@ -444,7 +468,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( getParserFromExtension(extension),
RDFUtils.class.getResourceAsStream(resource) );
+ return parseRDF( getParserByExtension(extension),
RDFUtils.class.getResourceAsStream(resource) );
}
/**
@@ -472,18 +496,14 @@ public class RDFUtils {
private final List<Statement> statements = new ArrayList<Statement>();
- private int documents = 0;
- private boolean open = false;
-
@Override
public void startRDF() throws RDFHandlerException {
- documents++;
- open = true;
+ // Empty.
}
@Override
public void endRDF() throws RDFHandlerException {
- open = false;
+ // Empty.
}
@Override