You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@any23.apache.org by an...@apache.org on 2013/02/22 05:01:49 UTC

svn commit: r1448888 - in /any23/trunk/core/src: main/java/org/apache/any23/cli/VocabPrinter.java main/java/org/apache/any23/vocab/RDFSchemaUtils.java test/java/org/apache/any23/vocab/RDFSchemaUtilsTest.java

Author: ansell
Date: Fri Feb 22 04:01:49 2013
New Revision: 1448888

URL: http://svn.apache.org/r1448888
Log:
ANY23-83 : Support arbitrary RDFFormats for VocabPrinter without being manually added to the list

Modified:
    any23/trunk/core/src/main/java/org/apache/any23/cli/VocabPrinter.java
    any23/trunk/core/src/main/java/org/apache/any23/vocab/RDFSchemaUtils.java
    any23/trunk/core/src/test/java/org/apache/any23/vocab/RDFSchemaUtilsTest.java

Modified: any23/trunk/core/src/main/java/org/apache/any23/cli/VocabPrinter.java
URL: http://svn.apache.org/viewvc/any23/trunk/core/src/main/java/org/apache/any23/cli/VocabPrinter.java?rev=1448888&r1=1448887&r2=1448888&view=diff
==============================================================================
--- any23/trunk/core/src/main/java/org/apache/any23/cli/VocabPrinter.java (original)
+++ any23/trunk/core/src/main/java/org/apache/any23/cli/VocabPrinter.java Fri Feb 22 04:01:49 2013
@@ -18,8 +18,8 @@
 package org.apache.any23.cli;
 
 import org.apache.any23.vocab.RDFSchemaUtils;
-import org.apache.any23.vocab.RDFSchemaUtils.VocabularyFormat;
 import org.kohsuke.MetaInfServices;
+import org.openrdf.rio.RDFFormat;
 
 import com.beust.jcommander.IStringConverter;
 import com.beust.jcommander.Parameter;
@@ -27,32 +27,30 @@ import com.beust.jcommander.Parameters;
 
 /**
  * Prints out the vocabulary <i>RDFSchema</i> as <i>NQuads</i>.
- *
+ * 
  * @author Michele Mostarda (mostarda@fbk.eu)
  */
 @MetaInfServices
 @Parameters(commandNames = { "vocab" }, commandDescription = "Prints out the RDF Schema of the vocabularies used by Any23.")
 public class VocabPrinter implements Tool {
 
-    @Parameter(
-       names = { "-f", "--format" },
-       description = "Vocabulary output format",
-       converter = VocabularyFormatConverter.class
-    )
-    private VocabularyFormat format = RDFSchemaUtils.VocabularyFormat.NQuads;
+    @Parameter(names = { "-f", "--format" }, description = "Vocabulary output format", converter = RDFFormatConverter.class)
+    private RDFFormat format = RDFFormat.NQUADS;
 
     public void run() throws Exception {
         RDFSchemaUtils.serializeVocabularies(format, System.out);
     }
 
-    public static final class VocabularyFormatConverter implements IStringConverter<RDFSchemaUtils.VocabularyFormat> {
+    public static final class RDFFormatConverter implements
+            IStringConverter<RDFFormat> {
 
         @Override
-        public VocabularyFormat convert(String value) {
+        public RDFFormat convert(String value) {
             try {
-                return RDFSchemaUtils.VocabularyFormat.valueOf( value );
+                return RDFFormat.valueOf(value);
             } catch (Throwable t) {
-                throw new IllegalArgumentException("Unknown format [" + value + "'");
+                throw new IllegalArgumentException("Unknown format [" + value
+                        + "'");
             }
         }
 

Modified: any23/trunk/core/src/main/java/org/apache/any23/vocab/RDFSchemaUtils.java
URL: http://svn.apache.org/viewvc/any23/trunk/core/src/main/java/org/apache/any23/vocab/RDFSchemaUtils.java?rev=1448888&r1=1448887&r2=1448888&view=diff
==============================================================================
--- any23/trunk/core/src/main/java/org/apache/any23/vocab/RDFSchemaUtils.java (original)
+++ any23/trunk/core/src/main/java/org/apache/any23/vocab/RDFSchemaUtils.java Fri Feb 22 04:01:49 2013
@@ -45,15 +45,6 @@ public class RDFSchemaUtils {
     private static final String RDF_XML_SEPARATOR = StringUtils.multiply('=', 100);
     
     /**
-     * Supported formats for vocabulary serialization.
-     */
-    public enum VocabularyFormat {
-        NQuads,
-        NTriples,
-        RDFXML
-    }
-
-    /**
      * Serializes a vocabulary composed of the given <code>namespace</code>,
      * <code>resources</code> and <code>properties</code>.
      *
@@ -118,25 +109,20 @@ public class RDFSchemaUtils {
      */
     public static void serializeVocabulary(
             Vocabulary vocabulary,
-            VocabularyFormat format,
+            RDFFormat format,
             boolean willFollowAnother,
             PrintStream ps
     ) throws RDFHandlerException {
         final RDFWriter rdfWriter;
         // FIXME: Remove hardcoding for format translation
-        if(format == VocabularyFormat.RDFXML) {
+        if(format == RDFFormat.RDFXML) {
             rdfWriter = Rio.createWriter(RDFFormat.RDFXML, ps);
             if(willFollowAnother)
                 ps.print("\n");
                 ps.print(RDF_XML_SEPARATOR);
                 ps.print("\n");
-        } else if(format == VocabularyFormat.NTriples) {
-            rdfWriter = Rio.createWriter(RDFFormat.NTRIPLES, ps);
-        } else if(format == VocabularyFormat.NQuads) {
-            rdfWriter = Rio.createWriter(RDFFormat.NQUADS, ps);
-        }
-        else {
-            throw new IllegalArgumentException("Unsupported format " + format);
+        } else {
+            rdfWriter = Rio.createWriter(format, ps);
         }
         serializeVocabulary(vocabulary, rdfWriter);
     }
@@ -149,7 +135,7 @@ public class RDFSchemaUtils {
      * @return string contained serialization.
      * @throws RDFHandlerException
      */
-    public static String serializeVocabulary(Vocabulary vocabulary, VocabularyFormat format)
+    public static String serializeVocabulary(Vocabulary vocabulary, RDFFormat format)
     throws RDFHandlerException {
         final ByteArrayOutputStream baos = new ByteArrayOutputStream();
         final PrintStream ps = new PrintStream(baos);
@@ -164,7 +150,7 @@ public class RDFSchemaUtils {
      * @param format output format for vocabularies.
      * @param ps output print stream.
      */
-    public static void serializeVocabularies(VocabularyFormat format, PrintStream ps) {
+    public static void serializeVocabularies(RDFFormat format, PrintStream ps) {
         final Class vocabularyClass = Vocabulary.class;
         final List<Class> vocabularies = DiscoveryUtils.getClassesInPackage(
                 vocabularyClass.getPackage().getName(),

Modified: any23/trunk/core/src/test/java/org/apache/any23/vocab/RDFSchemaUtilsTest.java
URL: http://svn.apache.org/viewvc/any23/trunk/core/src/test/java/org/apache/any23/vocab/RDFSchemaUtilsTest.java?rev=1448888&r1=1448887&r2=1448888&view=diff
==============================================================================
--- any23/trunk/core/src/test/java/org/apache/any23/vocab/RDFSchemaUtilsTest.java (original)
+++ any23/trunk/core/src/test/java/org/apache/any23/vocab/RDFSchemaUtilsTest.java Fri Feb 22 04:01:49 2013
@@ -20,6 +20,7 @@ package org.apache.any23.vocab;
 import org.apache.any23.util.StringUtils;
 import org.junit.Assert;
 import org.junit.Test;
+import org.openrdf.rio.RDFFormat;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -42,7 +43,7 @@ public class RDFSchemaUtilsTest {
      */
     @Test
     public void testSerializeVocabulariesNTriples() {
-        serializeVocabularies(RDFSchemaUtils.VocabularyFormat.NTriples, 1622);
+        serializeVocabularies(RDFFormat.NTRIPLES, 1622);
     }
 
     /**
@@ -52,10 +53,10 @@ public class RDFSchemaUtilsTest {
      */
     @Test
     public void testSerializeVocabulariesRDFXML() {
-        serializeVocabularies(RDFSchemaUtils.VocabularyFormat.RDFXML, 4241); // Effective lines + separators.
+        serializeVocabularies(RDFFormat.RDFXML, 4241); // Effective lines + separators.
     }
 
-    private void serializeVocabularies(RDFSchemaUtils.VocabularyFormat format, int expectedLines) {
+    private void serializeVocabularies(RDFFormat format, int expectedLines) {
         final ByteArrayOutputStream baos = new ByteArrayOutputStream();
         final PrintStream ps = new PrintStream(baos);
         RDFSchemaUtils.serializeVocabularies(format, ps);