Skip to content

Commit b245c80

Browse files
committed
Merge pull request #153 from cboehme/issue-152-additonal-writer-options
Solution for the issues described in #152
2 parents 10b8a58 + d84456a commit b245c80

File tree

10 files changed

+623
-77
lines changed

10 files changed

+623
-77
lines changed

src/main/java/org/culturegraph/mf/stream/converter/JsonEncoder.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ public final class JsonEncoder extends
5353
public JsonEncoder() {
5454
try {
5555
jsonGenerator = new JsonFactory().createGenerator(writer);
56-
} catch (IOException e) {
56+
jsonGenerator.setRootValueSeparator(null);
57+
} catch (final IOException e) {
5758
throw new MetafactureException(e);
5859
}
5960
}
@@ -70,8 +71,8 @@ public void endRecord() {
7071
endGroup();
7172
try {
7273
jsonGenerator.flush();
73-
} catch (IOException e) {
74-
throw new MetafactureException(e);
74+
} catch (final IOException e) {
75+
throw new MetafactureException(e);
7576
}
7677
getReceiver().process(writer.toString());
7778
}
@@ -82,7 +83,7 @@ public void startEntity(final String name) {
8283
}
8384

8485
@Override
85-
public void endEntity() {
86+
public void endEntity() {
8687
endGroup();
8788
}
8889

@@ -98,10 +99,10 @@ public void literal(final String name, final String value) {
9899
} else {
99100
jsonGenerator.writeString(value);
100101
}
101-
} catch (JsonGenerationException e) {
102-
throw new MetafactureException(e);
102+
} catch (final JsonGenerationException e) {
103+
throw new MetafactureException(e);
103104
}
104-
catch (IOException e) {
105+
catch (final IOException e) {
105106
throw new MetafactureException(e);
106107
}
107108
}
@@ -113,17 +114,17 @@ private void startGroup(final String name) {
113114
if (ctx.inObject()) {
114115
jsonGenerator.writeFieldName(name.substring(0, name.length() - ARRAY_MARKER.length()));
115116
}
116-
jsonGenerator.writeStartArray();
117-
} else {
117+
jsonGenerator.writeStartArray();
118+
} else {
118119
if (ctx.inObject()) {
119120
jsonGenerator.writeFieldName(name);
120121
}
121122
jsonGenerator.writeStartObject();
122123
}
123-
} catch (JsonGenerationException e) {
124-
throw new MetafactureException(e);
124+
} catch (final JsonGenerationException e) {
125+
throw new MetafactureException(e);
125126
}
126-
catch (IOException e) {
127+
catch (final IOException e) {
127128
throw new MetafactureException(e);
128129
}
129130
}
@@ -136,10 +137,10 @@ private void endGroup() {
136137
} else if (ctx.inArray()) {
137138
jsonGenerator.writeEndArray();
138139
}
139-
} catch (JsonGenerationException e) {
140-
throw new MetafactureException(e);
140+
} catch (final JsonGenerationException e) {
141+
throw new MetafactureException(e);
141142
}
142-
catch (IOException e) {
143+
catch (final IOException e) {
143144
throw new MetafactureException(e);
144145
}
145146
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2013 Christoph Böhme
3+
*
4+
* Licensed under the Apache License, Version 2.0 the "License";
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.culturegraph.mf.stream.sink;
17+
18+
/**
19+
* Common functions for object writers.
20+
*
21+
* @author Christoph Böhme
22+
*
23+
* @param <T>
24+
* object type
25+
*/
26+
public abstract class AbstractObjectWriter<T> implements ConfigurableObjectWriter<T> {
27+
28+
private String header = DEFAULT_HEADER;
29+
private String footer = DEFAULT_FOOTER;
30+
private String separator = DEFAULT_SEPARATOR;
31+
32+
@Override
33+
public final String getHeader() {
34+
return header;
35+
}
36+
37+
@Override
38+
public final void setHeader(final String header) {
39+
this.header = header;
40+
}
41+
42+
@Override
43+
public final String getFooter() {
44+
return footer;
45+
}
46+
47+
@Override
48+
public final void setFooter(final String footer) {
49+
this.footer = footer;
50+
}
51+
52+
@Override
53+
public final String getSeparator() {
54+
return separator;
55+
}
56+
57+
@Override
58+
public final void setSeparator(final String separator) {
59+
this.separator = separator;
60+
}
61+
62+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
* Copyright 2013 Christoph Böhme
3+
*
4+
* Licensed under the Apache License, Version 2.0 the "License";
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.culturegraph.mf.stream.sink;
17+
18+
import org.culturegraph.mf.framework.ObjectReceiver;
19+
import org.culturegraph.mf.util.FileCompression;
20+
21+
/**
22+
* Back end implementations for {@link ObjectWriter} should offer
23+
* a default set of configuration options. These are defined by
24+
* this interface.
25+
*
26+
* @author Christoph Böhme
27+
*
28+
* @param <T> object type
29+
*/
30+
public interface ConfigurableObjectWriter<T> extends ObjectReceiver<T> {
31+
32+
String DEFAULT_HEADER = "";
33+
String DEFAULT_FOOTER = "\n";
34+
String DEFAULT_SEPARATOR = "\n";
35+
36+
/**
37+
* Returns the encoding used by the underlying writer.
38+
*
39+
* @return current encoding
40+
*/
41+
String getEncoding();
42+
43+
/**
44+
* Sets the encoding used by the underlying writer.
45+
*
46+
* @param encoding
47+
* name of the encoding
48+
*/
49+
void setEncoding(String encoding);
50+
51+
/**
52+
* Returns the compression mode.
53+
*
54+
* @return current compression mode
55+
*/
56+
FileCompression getCompression();
57+
58+
/**
59+
* Sets the compression mode.
60+
*
61+
* @param compression
62+
*/
63+
void setCompression(final FileCompression compression);
64+
65+
/**
66+
* Sets the compression mode.
67+
*
68+
* @param compression
69+
*/
70+
void setCompression(final String compression);
71+
72+
/**
73+
* Returns the header which is output before the first object.
74+
*
75+
* @return header string
76+
*/
77+
String getHeader();
78+
79+
/**
80+
* Sets the header which is output before the first object.
81+
*
82+
* @param header new header string
83+
*/
84+
void setHeader(final String header);
85+
86+
/**
87+
* Returns the footer which is output after the last object.
88+
*
89+
* @return footer string
90+
*/
91+
String getFooter();
92+
93+
/**
94+
* Sets the footer which is output after the last object.
95+
*
96+
* @param footer new footer string
97+
*/
98+
void setFooter(final String footer);
99+
100+
/**
101+
* Returns the separator which is output between objects.
102+
*
103+
* @return separator string
104+
*/
105+
String getSeparator();
106+
107+
/**
108+
* Sets the separator which is output between objects.
109+
*
110+
* @param separator new separator string
111+
*/
112+
void setSeparator(final String separator);
113+
114+
}

0 commit comments

Comments
 (0)