Skip to content

Commit 54bd7a9

Browse files
committed
metafacture-json/ (main): Fix Checkstyle violations.
1 parent 859d478 commit 54bd7a9

File tree

4 files changed

+82
-49
lines changed

4 files changed

+82
-49
lines changed

metafacture-json/src/main/java/org/metafacture/json/JsonDecoder.java

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,8 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.metafacture.json;
1716

18-
import com.fasterxml.jackson.core.JsonFactory;
19-
import com.fasterxml.jackson.core.JsonParser;
20-
import com.fasterxml.jackson.core.JsonProcessingException;
21-
import com.fasterxml.jackson.core.JsonToken;
22-
import com.fasterxml.jackson.databind.ObjectMapper;
23-
import com.jayway.jsonpath.JsonPath;
17+
package org.metafacture.json;
2418

2519
import org.metafacture.framework.FluxCommand;
2620
import org.metafacture.framework.MetafactureException;
@@ -30,6 +24,13 @@
3024
import org.metafacture.framework.annotations.Out;
3125
import org.metafacture.framework.helpers.DefaultObjectPipe;
3226

27+
import com.fasterxml.jackson.core.JsonFactory;
28+
import com.fasterxml.jackson.core.JsonParser;
29+
import com.fasterxml.jackson.core.JsonProcessingException;
30+
import com.fasterxml.jackson.core.JsonToken;
31+
import com.fasterxml.jackson.databind.ObjectMapper;
32+
import com.jayway.jsonpath.JsonPath;
33+
3334
import java.io.IOException;
3435
import java.util.Arrays;
3536
import java.util.List;
@@ -41,8 +42,8 @@
4142
* @author Jens Wille
4243
*
4344
*/
44-
@Description("Decodes JSON to metadata events. The \'recordPath\' option can be used to set a JsonPath "
45-
+ "to extract a path as JSON - or to split the data into multiple JSON documents.")
45+
@Description("Decodes JSON to metadata events. The \'recordPath\' option can be used to set a JsonPath " +
46+
"to extract a path as JSON - or to split the data into multiple JSON documents.")
4647
@In(String.class)
4748
@Out(StreamReceiver.class)
4849
@FluxCommand("decode-json")
@@ -67,8 +68,6 @@ public final class JsonDecoder extends DefaultObjectPipe<String, StreamReceiver>
6768
private String recordPath;
6869

6970
public JsonDecoder() {
70-
super();
71-
7271
setArrayMarker(DEFAULT_ARRAY_MARKER);
7372
setArrayName(DEFAULT_ARRAY_NAME);
7473
setRecordId(DEFAULT_RECORD_ID);
@@ -134,30 +133,34 @@ public void process(final String json) {
134133
assert !isClosed();
135134
if (recordPath.isEmpty()) {
136135
processRecord(json);
137-
} else {
136+
}
137+
else {
138138
matches(JsonPath.read(json, recordPath)).forEach(record -> {
139139
processRecord(record);
140140
});
141141
}
142142
}
143143

144-
private void processRecord(String record) {
144+
private void processRecord(final String record) {
145145
createParser(record);
146146
try {
147147
decode();
148-
} catch (final IOException e) {
148+
}
149+
catch (final IOException e) {
149150
throw new MetafactureException(e);
150-
} finally {
151+
}
152+
finally {
151153
closeParser();
152154
}
153155
}
154156

155-
private Stream<String> matches(Object obj) {
157+
private Stream<String> matches(final Object obj) {
156158
final List<?> records = (obj instanceof List<?>) ? ((List<?>) obj) : Arrays.asList(obj);
157159
return records.stream().map(doc -> {
158160
try {
159161
return new ObjectMapper().writeValueAsString(doc);
160-
} catch (JsonProcessingException e) {
162+
}
163+
catch (final JsonProcessingException e) {
161164
e.printStackTrace();
162165
return doc.toString();
163166
}

metafacture-json/src/main/java/org/metafacture/json/JsonEncoder.java

Lines changed: 60 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.metafacture.json;
1716

18-
import java.io.IOException;
19-
import java.io.StringWriter;
17+
package org.metafacture.json;
2018

2119
import org.metafacture.framework.FluxCommand;
2220
import org.metafacture.framework.MetafactureException;
@@ -36,6 +34,9 @@
3634
import com.fasterxml.jackson.core.io.SerializedString;
3735
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
3836

37+
import java.io.IOException;
38+
import java.io.StringWriter;
39+
3940
/**
4041
* Serialises an object as JSON. Records and entities are represented
4142
* as objects unless their name ends with []. If the name ends with [],
@@ -54,14 +55,18 @@ public final class JsonEncoder extends
5455

5556
public static final String ARRAY_MARKER = "[]";
5657

58+
private static final char ESCAPE_CHAR_LOW = 0x20;
59+
private static final char ESCAPE_CHAR_HIGH = 0x7f;
60+
5761
private final JsonGenerator jsonGenerator;
5862
private final StringWriter writer = new StringWriter();
5963

6064
public JsonEncoder() {
6165
try {
6266
jsonGenerator = new JsonFactory().createGenerator(writer);
6367
jsonGenerator.setRootValueSeparator(null);
64-
} catch (final IOException e) {
68+
}
69+
catch (final IOException e) {
6570
throw new MetafactureException(e);
6671
}
6772
}
@@ -126,7 +131,8 @@ public void endRecord() {
126131
endGroup();
127132
try {
128133
jsonGenerator.flush();
129-
} catch (final IOException e) {
134+
}
135+
catch (final IOException e) {
130136
throw new MetafactureException(e);
131137
}
132138
getReceiver().process(writer.toString());
@@ -151,10 +157,12 @@ public void literal(final String name, final String value) {
151157
}
152158
if (value == null) {
153159
jsonGenerator.writeNull();
154-
} else {
160+
}
161+
else {
155162
jsonGenerator.writeString(value);
156163
}
157-
} catch (final JsonGenerationException e) {
164+
}
165+
catch (final JsonGenerationException e) {
158166
throw new MetafactureException(e);
159167
}
160168
catch (final IOException e) {
@@ -170,13 +178,15 @@ private void startGroup(final String name) {
170178
jsonGenerator.writeFieldName(name.substring(0, name.length() - ARRAY_MARKER.length()));
171179
}
172180
jsonGenerator.writeStartArray();
173-
} else {
181+
}
182+
else {
174183
if (ctx.inObject()) {
175184
jsonGenerator.writeFieldName(name);
176185
}
177186
jsonGenerator.writeStartObject();
178187
}
179-
} catch (final JsonGenerationException e) {
188+
}
189+
catch (final JsonGenerationException e) {
180190
throw new MetafactureException(e);
181191
}
182192
catch (final IOException e) {
@@ -189,45 +199,63 @@ private void endGroup() {
189199
final JsonStreamContext ctx = jsonGenerator.getOutputContext();
190200
if (ctx.inObject()) {
191201
jsonGenerator.writeEndObject();
192-
} else if (ctx.inArray()) {
202+
}
203+
else if (ctx.inArray()) {
193204
jsonGenerator.writeEndArray();
194205
}
195-
} catch (final JsonGenerationException e) {
206+
}
207+
catch (final JsonGenerationException e) {
196208
throw new MetafactureException(e);
197209
}
198210
catch (final IOException e) {
199211
throw new MetafactureException(e);
200212
}
201213
}
202214

203-
private String escapeChar(char ch) {
215+
private String escapeChar(final char ch) {
204216
final String namedEscape = namedEscape(ch);
205-
if (namedEscape != null) {
206-
return namedEscape;
207-
}
208-
if (ch < 0x20 || 0x7f < ch ) {
209-
return unicodeEscape(ch);
210-
}
211-
return Character.toString(ch);
217+
return namedEscape != null ? namedEscape : (ch < ESCAPE_CHAR_LOW || ESCAPE_CHAR_HIGH < ch) ? unicodeEscape(ch) : Character.toString(ch);
212218
}
213219

214-
private String namedEscape(char ch) {
215-
switch(ch) {
216-
case '\b': return "\\b";
217-
case '\n': return "\\n";
218-
case '\t': return "\\t";
219-
case '\f': return "\\f";
220-
case '\r': return "\\r";
221-
case '\'': return "\\'";
222-
case '\\': return "\\\\";
223-
case '"': return "\\\"";
224-
case '/': return "\\/";
220+
private String namedEscape(final char ch) {
221+
final String result;
222+
223+
switch (ch) {
224+
case '\b':
225+
result = "\\b";
226+
break;
227+
case '\n':
228+
result = "\\n";
229+
break;
230+
case '\t':
231+
result = "\\t";
232+
break;
233+
case '\f':
234+
result = "\\f";
235+
break;
236+
case '\r':
237+
result = "\\r";
238+
break;
239+
case '\'':
240+
result = "\\'";
241+
break;
242+
case '\\':
243+
result = "\\\\";
244+
break;
245+
case '"':
246+
result = "\\\"";
247+
break;
248+
case '/':
249+
result = "\\/";
250+
break;
225251
default:
226-
return null;
252+
result = null;
227253
}
254+
255+
return result;
228256
}
229257

230-
private String unicodeEscape(char ch) {
258+
private String unicodeEscape(final char ch) {
231259
return String.format("\\u%4H", ch).replace(' ', '0');
232260
}
233261

metafacture-json/src/test/java/org/metafacture/json/JsonDecoderTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
package org.metafacture.json;
1718

1819
import static org.mockito.Mockito.inOrder;

metafacture-json/src/test/java/org/metafacture/json/JsonEncoderTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
package org.metafacture.json;
1718

1819
import static org.mockito.Mockito.inOrder;

0 commit comments

Comments
 (0)