##### Coverage Report - org.apache.any23.writer.JSONWriter

Classes in this File | Line Coverage | Branch Coverage | Complexity
JSONWriter |
0% | 0/101
0% | 0/26
| 1.8421052631578947;1.842

1 |  |
     /*
2 |  |
      * Licensed to the Apache Software Foundation (ASF) under one or more
3 |  |
      * contributor license agreements.  See the NOTICE file distributed with
4 |  |
      * this work for additional information regarding copyright ownership.
5 |  |
      * The ASF licenses this file to You under the Apache License, Version 2.0
6 |  |
      * (the "License"); you may not use this file except in compliance with
7 |  |
      * the License.  You may obtain a copy of the License at
8 |  |
      *
9 |  |
      *  http://www.apache.org/licenses/LICENSE-2.0
10 |  |
      *
11 |  |
      * Unless required by applicable law or agreed to in writing, software
12 |  |
      * distributed under the License is distributed on an "AS IS" BASIS,
13 |  |
      * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 |  |
      * See the License for the specific language governing permissions and
15 |  |
      * limitations under the License.
16 |  |
      */
17 |  |
18 |  |
     package org.apache.any23.writer;
19 |  |
20 |  |
     import org.apache.any23.extractor.ExtractionContext;
21 |  |
     import org.openrdf.model.BNode;
22 |  |
     import org.openrdf.model.Literal;
23 |  |
     import org.openrdf.model.Resource;
24 |  |
     import org.openrdf.model.URI;
25 |  |
     import org.openrdf.model.Value;
26 |  |
27 |  |
     import java.io.BufferedOutputStream;
28 |  |
     import java.io.OutputStream;
29 |  |
     import java.io.PrintStream;
30 |  |
31 |  |
     /**
32 |  |
      * Implementation of <i>JSON</i> format writer.
33 |  |
      *
34 |  |
      * @author Michele Mostarda (mostarda@fbk.eu)
35 |  |
      */
36 |  |
     @Writer(identifier = "json", mimeType = "text/json" )
37 |  |
     public class JSONWriter implements FormatWriter {
38 |  |
39 |  |
         private final PrintStream ps;
40 |  |
41 | 0 |
         private boolean documentStarted = false;
42 |  |
43 | 0 |
         private boolean firstArrayElemWritten = false;
44 | 0 |
         private boolean firstObjectWritten    = false;
45 |  |
46 | 0 |
         public JSONWriter(OutputStream os) {
47 | 0 |
             if(os == null) {
48 | 0 |
                 throw new NullPointerException("Output stream cannot be null.");
49 |  |
             }
50 | 0 |
             this.ps = new PrintStream(new BufferedOutputStream(os));
51 | 0 |
         }
52 |  |
53 |  |
         public void startDocument(URI documentURI) throws TripleHandlerException {
54 | 0 |
             if(documentStarted) {
55 | 0 |
                 throw new IllegalStateException("Document already started.");
56 |  |
             }
57 | 0 |
             documentStarted = true;
58 |  |
59 | 0 |
             firstArrayElemWritten = false;
60 | 0 |
             ps.print("{ \"quads\" : [");
61 | 0 |
         }
62 |  |
63 |  |
         public void openContext(ExtractionContext context) throws TripleHandlerException {
64 |  |
             // Empty.
65 | 0 |
         }
66 |  |
67 |  |
         public void receiveTriple(Resource s, URI p, Value o, URI g, ExtractionContext context)
68 |  |
         throws TripleHandlerException {
69 | 0 |
             validateDocumentStarted();
70 |  |
71 | 0 |
             if(firstArrayElemWritten) {
72 | 0 |
                 ps.print(", ");
73 |  |
             } else {
74 | 0 |
                 firstArrayElemWritten = true;
75 |  |
             }
76 | 0 |
             firstObjectWritten    = false;
77 |  |
78 | 0 |
             ps.print('[');
79 |  |
80 | 0 |
             if(s instanceof URI) {
81 | 0 |
                 printExplicitURI(s.stringValue(), ps);
82 |  |
             } else {
83 | 0 |
                 printBNode(s.stringValue(), ps);
84 |  |
             }
85 |  |
86 | 0 |
             printURI(p.stringValue(), ps);
87 |  |
88 | 0 |
              if(o instanceof URI) {
89 | 0 |
                 printExplicitURI(o.stringValue(), ps);
90 | 0 |
             } else if(o instanceof BNode) {
91 | 0 |
                 printBNode(o.stringValue(), ps);
92 |  |
             } else {
93 | 0 |
                 printLiteral((Literal) o, ps);
94 |  |
             }
95 |  |
96 | 0 |
             printURI(g == null ? null : g.stringValue(), ps);
97 |  |
98 | 0 |
             ps.print(']');
99 | 0 |
         }
100 |  |
101 |  |
         public void receiveNamespace(String prefix, String uri, ExtractionContext context)
102 |  |
         throws TripleHandlerException {
103 |  |
             // Empty.
104 | 0 |
         }
105 |  |
106 |  |
         public void closeContext(ExtractionContext context) throws TripleHandlerException {
107 |  |
             // Empty.
108 | 0 |
         }
109 |  |
110 |  |
         public void endDocument(URI documentURI) throws TripleHandlerException {
111 | 0 |
             validateDocumentStarted();
112 | 0 |
             ps.print("]}");
113 | 0 |
             documentStarted = false;
114 | 0 |
         }
115 |  |
116 |  |
         public void setContentLength(long contentLength) {
117 |  |
             // Empty.
118 | 0 |
         }
119 |  |
120 |  |
         public void close() throws TripleHandlerException {
121 | 0 |
             ps.close();
122 | 0 |
         }
123 |  |
124 |  |
         private void validateDocumentStarted() {
125 | 0 |
            if(!documentStarted) {
126 | 0 |
                 throw new IllegalStateException("Document didn't start.");
127 |  |
             }
128 | 0 |
         }
129 |  |
130 |  |
         private void printURI(String uri, PrintStream ps) {
131 | 0 |
             printValue(uri, ps);
132 | 0 |
         }
133 |  |
134 |  |
         private void printExplicitURI(String uri, PrintStream ps) {
135 | 0 |
             printValue("uri", uri, ps);
136 | 0 |
         }
137 |  |
138 |  |
         private void printBNode(String bnode, PrintStream ps) {
139 | 0 |
             printValue("bnode", bnode, ps);
140 | 0 |
         }
141 |  |
142 |  |
         private void printCommaIfNeeded(PrintStream ps) {
143 | 0 |
             if(firstObjectWritten) {
144 | 0 |
                 ps.print(", ");
145 |  |
             } else {
146 | 0 |
                 firstObjectWritten = true;
147 |  |
             }
148 | 0 |
         }
149 |  |
150 |  |
         private void printLiteral(Literal literal, PrintStream ps) {
151 | 0 |
             printCommaIfNeeded(ps);
152 |  |
153 | 0 |
             ps.print('{');
154 |  |
155 | 0 |
             ps.print("\"type\" : \"literal\"");
156 |  |
157 | 0 |
             ps.print(", ");
158 |  |
159 | 0 |
             ps.print("\"value\" : ");
160 | 0 |
             ps.print('"');
161 | 0 |
             ps.print(literal.stringValue());
162 | 0 |
             ps.print('"');
163 |  |
164 | 0 |
             ps.print(", ");
165 |  |
166 | 0 |
             ps.print("\"lang\" : ");
167 | 0 |
             final String language = literal.getLanguage();
168 | 0 |
             if (language != null) {
169 | 0 |
                 ps.print('"');
170 | 0 |
                 ps.print(literal.getLanguage());
171 | 0 |
                 ps.print('"');
172 |  |
             } else {
173 | 0 |
                 ps.print("null");
174 |  |
             }
175 |  |
176 | 0 |
             ps.print(", ");
177 |  |
178 | 0 |
             ps.print("\"datatype\" : ");
179 | 0 |
             final URI datatype = literal.getDatatype();
180 | 0 |
             if(datatype != null) {
181 | 0 |
             ps.print('"');
182 | 0 |
             ps.print(datatype.stringValue());
183 | 0 |
             ps.print('"');
184 |  |
             } else {
185 | 0 |
                 ps.print("null");
186 |  |
             }
187 |  |
188 | 0 |
             ps.print('}');
189 | 0 |
         }
190 |  |
191 |  |
         private void printValue(String type, String value, PrintStream ps) {
192 | 0 |
             printCommaIfNeeded(ps);
193 |  |
194 | 0 |
             ps.print("{ \"type\" : \"");
195 | 0 |
             ps.print(type);
196 | 0 |
             ps.print("\", \"value\" : ");
197 | 0 |
             if (value != null) {
198 | 0 |
                 ps.print('"');
199 | 0 |
                 ps.print(value);
200 | 0 |
                 ps.print('"');
201 |  |
             } else {
202 | 0 |
                 ps.print("null");
203 |  |
             }
204 | 0 |
             ps.print('}');
205 | 0 |
         }
206 |  |
207 |  |
         private void printValue(String value, PrintStream ps) {
208 | 0 |
             printCommaIfNeeded(ps);
209 |  |
210 | 0 |
             if (value != null) {
211 | 0 |
                 ps.print('"');
212 | 0 |
                 ps.print(value);
213 | 0 |
                 ps.print('"');
214 |  |
             } else {
215 | 0 |
                 ps.print("null");
216 |  |
             }
217 | 0 |
         }
218 |  |
219 |  |
         @Override
220 |  |
         public boolean isAnnotated() {
221 | 0 |
             return false; // TODO: add annotation support.
222 |  |
         }
223 |  |
224 |  |
         @Override
225 |  |
         public void setAnnotated(boolean f) {
226 |  |
             // Empty.
227 | 0 |
         }
228 |  |
     }