any23-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ans...@apache.org
Subject [17/25] any23 git commit: ANY23-302 : Autoclose JSONWriter rather than relying on endDocument being called
Date Fri, 13 Jan 2017 22:25:56 GMT
ANY23-302 : Autoclose JSONWriter rather than relying on endDocument being called

Signed-off-by: Peter Ansell <p_ansell@yahoo.com>


Project: http://git-wip-us.apache.org/repos/asf/any23/repo
Commit: http://git-wip-us.apache.org/repos/asf/any23/commit/b5b8b585
Tree: http://git-wip-us.apache.org/repos/asf/any23/tree/b5b8b585
Diff: http://git-wip-us.apache.org/repos/asf/any23/diff/b5b8b585

Branch: refs/heads/master
Commit: b5b8b5859b94a8a73c68aab0011668be3ff76958
Parents: 8ccf456
Author: Peter Ansell <p_ansell@yahoo.com>
Authored: Thu Jan 12 10:03:54 2017 +1100
Committer: Peter Ansell <p_ansell@yahoo.com>
Committed: Thu Jan 12 10:03:54 2017 +1100

----------------------------------------------------------------------
 .../org/apache/any23/writer/TripleHandler.java  |  2 +-
 .../extractor/microdata/MicrodataParser.java    |  8 +--
 .../org/apache/any23/writer/JSONWriter.java     | 11 ++++
 .../microdata/MicrodataParserTest.java          |  1 -
 .../org/apache/any23/writer/JSONWriterTest.java | 54 ++++++++++----------
 5 files changed, 42 insertions(+), 34 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/any23/blob/b5b8b585/api/src/main/java/org/apache/any23/writer/TripleHandler.java
----------------------------------------------------------------------
diff --git a/api/src/main/java/org/apache/any23/writer/TripleHandler.java b/api/src/main/java/org/apache/any23/writer/TripleHandler.java
index 6d88ed3..e74436f 100644
--- a/api/src/main/java/org/apache/any23/writer/TripleHandler.java
+++ b/api/src/main/java/org/apache/any23/writer/TripleHandler.java
@@ -25,7 +25,7 @@ import org.eclipse.rdf4j.model.Value;
 /**
  * Defines a document based triple handler.
  */
-public interface TripleHandler {
+public interface TripleHandler extends AutoCloseable {
 
     void startDocument(IRI documentIRI) throws TripleHandlerException;
 

http://git-wip-us.apache.org/repos/asf/any23/blob/b5b8b585/core/src/main/java/org/apache/any23/extractor/microdata/MicrodataParser.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/any23/extractor/microdata/MicrodataParser.java
b/core/src/main/java/org/apache/any23/extractor/microdata/MicrodataParser.java
index 7bb3ea0..8ee1cc6 100644
--- a/core/src/main/java/org/apache/any23/extractor/microdata/MicrodataParser.java
+++ b/core/src/main/java/org/apache/any23/extractor/microdata/MicrodataParser.java
@@ -226,10 +226,10 @@ public class MicrodataParser {
         // Results.
         ps.append("\"result\" : [");
         for(int i = 0; i < itemScopes.length; i++) {
-            ps.print( itemScopes[i].toJSON() );
-            if( i < itemScopes.length - 1 ) {
+            if (i > 0) {
                 ps.print(", ");
             }
+            ps.print( itemScopes[i].toJSON() );
         }
         ps.append("] ");
 
@@ -238,10 +238,10 @@ public class MicrodataParser {
             ps.append(", ");
             ps.append("\"errors\" : [");
             for (int i = 0; i < errors.length; i++) {
-                ps.print( errors[i].toJSON() );
-                if (i < itemScopes.length - 1) {
+                if (i > 0) {
                     ps.print(", ");
                 }
+                ps.print( errors[i].toJSON() );
             }
             ps.append("] ");
         }

http://git-wip-us.apache.org/repos/asf/any23/blob/b5b8b585/core/src/main/java/org/apache/any23/writer/JSONWriter.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/any23/writer/JSONWriter.java b/core/src/main/java/org/apache/any23/writer/JSONWriter.java
index b4a11da..331a65b 100644
--- a/core/src/main/java/org/apache/any23/writer/JSONWriter.java
+++ b/core/src/main/java/org/apache/any23/writer/JSONWriter.java
@@ -50,6 +50,7 @@ public class JSONWriter implements FormatWriter {
         this.ps = new PrintStream(new BufferedOutputStream(os));
     }
 
+    @Override
     public void startDocument(IRI documentIRI) throws TripleHandlerException {
         if(documentStarted) {
             throw new IllegalStateException("Document already started.");
@@ -60,10 +61,12 @@ public class JSONWriter implements FormatWriter {
         ps.print("{ \"quads\" : [");
     }
 
+    @Override
     public void openContext(ExtractionContext context) throws TripleHandlerException {
         // Empty.
     }
 
+    @Override
     public void receiveTriple(Resource s, IRI p, Value o, IRI g, ExtractionContext context)
     throws TripleHandlerException {
         validateDocumentStarted();
@@ -98,26 +101,34 @@ public class JSONWriter implements FormatWriter {
         ps.print(']');
     }
 
+    @Override
     public void receiveNamespace(String prefix, String uri, ExtractionContext context)
     throws TripleHandlerException {
         // Empty.
     }
 
+    @Override
     public void closeContext(ExtractionContext context) throws TripleHandlerException {
         // Empty.
     }
 
+    @Override
     public void endDocument(IRI documentIRI) throws TripleHandlerException {
         validateDocumentStarted();
         ps.print("]}");
         documentStarted = false;
     }
 
+    @Override
     public void setContentLength(long contentLength) {
         // Empty.
     }
 
+    @Override
     public void close() throws TripleHandlerException {
+    	if(documentStarted) {
+    		endDocument(null);
+    	}
         ps.close();
     }
 

http://git-wip-us.apache.org/repos/asf/any23/blob/b5b8b585/core/src/test/java/org/apache/any23/extractor/microdata/MicrodataParserTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/any23/extractor/microdata/MicrodataParserTest.java
b/core/src/test/java/org/apache/any23/extractor/microdata/MicrodataParserTest.java
index ffd4e26..74ad67d 100644
--- a/core/src/test/java/org/apache/any23/extractor/microdata/MicrodataParserTest.java
+++ b/core/src/test/java/org/apache/any23/extractor/microdata/MicrodataParserTest.java
@@ -59,7 +59,6 @@ public class MicrodataParserTest {
 	
     private static final Logger logger = LoggerFactory.getLogger(MicrodataParserTest.class);
 
-    @Ignore("TODO: Determine the cause of this")
     @Test
     public void testBasicFeatures() throws IOException {
         extractItemsAndVerifyJSONSerialization(

http://git-wip-us.apache.org/repos/asf/any23/blob/b5b8b585/core/src/test/java/org/apache/any23/writer/JSONWriterTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/any23/writer/JSONWriterTest.java b/core/src/test/java/org/apache/any23/writer/JSONWriterTest.java
index 1c52228..ebe6ba3 100644
--- a/core/src/test/java/org/apache/any23/writer/JSONWriterTest.java
+++ b/core/src/test/java/org/apache/any23/writer/JSONWriterTest.java
@@ -34,37 +34,35 @@ import java.io.ByteArrayOutputStream;
  */
 public class JSONWriterTest {
 
-    private JSONWriter jsonWriter;
-
     @Test
     public void testWriting() throws TripleHandlerException {
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
-        jsonWriter = new JSONWriter(baos);
-        final IRI documentIRI = SimpleValueFactory.getInstance().createIRI("http://fake/uri");
-        jsonWriter.startDocument(documentIRI);
-        jsonWriter.receiveTriple(
-                SimpleValueFactory.getInstance().createBNode("bn1"),
-                SimpleValueFactory.getInstance().createIRI("http://pred/1"),
-                SimpleValueFactory.getInstance().createIRI("http://value/1"),
-                SimpleValueFactory.getInstance().createIRI("http://graph/1"),
-                null
-        );
-        jsonWriter.receiveTriple(
-                SimpleValueFactory.getInstance().createIRI("http://sub/2"),
-                SimpleValueFactory.getInstance().createIRI("http://pred/2"),
-                SimpleValueFactory.getInstance().createLiteral("language literal", "en"),
-                SimpleValueFactory.getInstance().createIRI("http://graph/2"),
-                null
-        );
-        jsonWriter.receiveTriple(
-                SimpleValueFactory.getInstance().createIRI("http://sub/3"),
-                SimpleValueFactory.getInstance().createIRI("http://pred/3"),
-                SimpleValueFactory.getInstance().createLiteral("123", SimpleValueFactory.getInstance().createIRI("http://datatype")),
-                null,
-                null
-        );
-        jsonWriter.endDocument(documentIRI);
-        jsonWriter.close();
+        try(JSONWriter jsonWriter = new JSONWriter(baos);) {
+	        final IRI documentIRI = SimpleValueFactory.getInstance().createIRI("http://fake/uri");
+	        jsonWriter.startDocument(documentIRI);
+	        jsonWriter.receiveTriple(
+	                SimpleValueFactory.getInstance().createBNode("bn1"),
+	                SimpleValueFactory.getInstance().createIRI("http://pred/1"),
+	                SimpleValueFactory.getInstance().createIRI("http://value/1"),
+	                SimpleValueFactory.getInstance().createIRI("http://graph/1"),
+	                null
+	        );
+	        jsonWriter.receiveTriple(
+	                SimpleValueFactory.getInstance().createIRI("http://sub/2"),
+	                SimpleValueFactory.getInstance().createIRI("http://pred/2"),
+	                SimpleValueFactory.getInstance().createLiteral("language literal", "en"),
+	                SimpleValueFactory.getInstance().createIRI("http://graph/2"),
+	                null
+	        );
+	        jsonWriter.receiveTriple(
+	                SimpleValueFactory.getInstance().createIRI("http://sub/3"),
+	                SimpleValueFactory.getInstance().createIRI("http://pred/3"),
+	                SimpleValueFactory.getInstance().createLiteral("123", SimpleValueFactory.getInstance().createIRI("http://datatype")),
+	                null,
+	                null
+	        );
+	        jsonWriter.endDocument(documentIRI);
+        }
 
         final String expected =
             "{ " +


Mime
View raw message