Author: rwesten
Date: Mon Sep  5 07:08:36 2016
New Revision: 1759201

URL: http://svn.apache.org/viewvc?rev=1759201&view=rev
Log:
implementation for STANBOL-1452

Added:
    
stanbol/trunk/commons/web/base/src/main/java/org/apache/stanbol/commons/web/base/writers/TextRdfNtProvider.java

Added: 
stanbol/trunk/commons/web/base/src/main/java/org/apache/stanbol/commons/web/base/writers/TextRdfNtProvider.java
URL: 
http://svn.apache.org/viewvc/stanbol/trunk/commons/web/base/src/main/java/org/apache/stanbol/commons/web/base/writers/TextRdfNtProvider.java?rev=1759201&view=auto
==============================================================================
--- 
stanbol/trunk/commons/web/base/src/main/java/org/apache/stanbol/commons/web/base/writers/TextRdfNtProvider.java
 (added)
+++ 
stanbol/trunk/commons/web/base/src/main/java/org/apache/stanbol/commons/web/base/writers/TextRdfNtProvider.java
 Mon Sep  5 07:08:36 2016
@@ -0,0 +1,73 @@
+package org.apache.stanbol.commons.web.base.writers;
+
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.Locale;
+
+import org.apache.clerezza.commons.rdf.Graph;
+import org.apache.clerezza.commons.rdf.IRI;
+import org.apache.clerezza.rdf.core.serializedform.Parser;
+import org.apache.clerezza.rdf.core.serializedform.ParsingProvider;
+import org.apache.clerezza.rdf.core.serializedform.Serializer;
+import org.apache.clerezza.rdf.core.serializedform.SerializingProvider;
+import org.apache.clerezza.rdf.core.serializedform.SupportedFormat;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Property;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.Service;
+
+
+/**
+ * With Clerezza 1.0 the deprecated <code>text/rdf+nt</code> content type is 
no longer
+ * support. This provider allows the usage of this content type by forwarding 
the
+ * request to the provider registered for the new {@link 
SupportedFormat#N_TRIPLE} content type
+ * @author Rupert Westenthaler
+ *
+ */
+@Component(immediate=true)
+@Service(SerializingProvider.class)
+@Property(name="supportedFormat", value={SupportedFormat.TEXT_RDF_NT})
+@SupportedFormat({SupportedFormat.TEXT_RDF_NT})
+public class TextRdfNtProvider implements SerializingProvider, ParsingProvider 
{
+
+    @Reference
+    Serializer serializer;
+    
+    @Reference
+    Parser parser;
+    
+    
+    @Override
+    public void serialize(OutputStream outputStream, Graph tc, String format) {
+        String formatIdentifier = cleanFormat(format);
+        if(SupportedFormat.TEXT_RDF_NT.equals(formatIdentifier)){
+            serializer.serialize(outputStream, tc, SupportedFormat.N_TRIPLE);
+        } else {
+            throw new IllegalArgumentException("This serializer only supports 
"+ SupportedFormat.TEXT_RDF_NT + 
+                    "(parsed: " + format +" | format: " + formatIdentifier + 
")!");
+        }
+    }
+
+
+    @Override
+    public void parse(Graph target, InputStream serializedGraph, String 
format, IRI baseUri) {
+        String formatIdentifier = cleanFormat(format);
+        if(SupportedFormat.TEXT_RDF_NT.equals(formatIdentifier)){
+            parser.parse(target, serializedGraph, SupportedFormat.N_TRIPLE, 
baseUri);
+        } else {
+            throw new IllegalArgumentException("This serializer only supports 
"+ SupportedFormat.TEXT_RDF_NT + 
+                    "(parsed: " + format +" | format: " + formatIdentifier + 
")!");
+        }
+    }
+    /**
+     * Used to strip parameters; ensure lower case and trim the media type
+     * @param formatIdentifier
+     * @return
+     */
+    private static String cleanFormat(String formatIdentifier){
+        return formatIdentifier == null ? null :  formatIdentifier.split(";", 
2)[0].trim().toLowerCase(Locale.ROOT);
+    }
+    public static void main(String[] args) {
+        System.out.println(cleanFormat("text/rdf+nt;charset=UTF-8"));
+    }
+}


Reply via email to