Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 16 additions & 15 deletions src/main/java/org/culturegraph/mf/stream/converter/JsonEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ public final class JsonEncoder extends
public JsonEncoder() {
try {
jsonGenerator = new JsonFactory().createGenerator(writer);
} catch (IOException e) {
jsonGenerator.setRootValueSeparator(null);
} catch (final IOException e) {
throw new MetafactureException(e);
}
}
Expand All @@ -70,8 +71,8 @@ public void endRecord() {
endGroup();
try {
jsonGenerator.flush();
} catch (IOException e) {
throw new MetafactureException(e);
} catch (final IOException e) {
throw new MetafactureException(e);
}
getReceiver().process(writer.toString());
}
Expand All @@ -82,7 +83,7 @@ public void startEntity(final String name) {
}

@Override
public void endEntity() {
public void endEntity() {
endGroup();
}

Expand All @@ -98,10 +99,10 @@ public void literal(final String name, final String value) {
} else {
jsonGenerator.writeString(value);
}
} catch (JsonGenerationException e) {
throw new MetafactureException(e);
} catch (final JsonGenerationException e) {
throw new MetafactureException(e);
}
catch (IOException e) {
catch (final IOException e) {
throw new MetafactureException(e);
}
}
Expand All @@ -113,17 +114,17 @@ private void startGroup(final String name) {
if (ctx.inObject()) {
jsonGenerator.writeFieldName(name.substring(0, name.length() - ARRAY_MARKER.length()));
}
jsonGenerator.writeStartArray();
} else {
jsonGenerator.writeStartArray();
} else {
if (ctx.inObject()) {
jsonGenerator.writeFieldName(name);
}
jsonGenerator.writeStartObject();
}
} catch (JsonGenerationException e) {
throw new MetafactureException(e);
} catch (final JsonGenerationException e) {
throw new MetafactureException(e);
}
catch (IOException e) {
catch (final IOException e) {
throw new MetafactureException(e);
}
}
Expand All @@ -136,10 +137,10 @@ private void endGroup() {
} else if (ctx.inArray()) {
jsonGenerator.writeEndArray();
}
} catch (JsonGenerationException e) {
throw new MetafactureException(e);
} catch (final JsonGenerationException e) {
throw new MetafactureException(e);
}
catch (IOException e) {
catch (final IOException e) {
throw new MetafactureException(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 2013 Christoph Böhme
*
* Licensed under the Apache License, Version 2.0 the "License";
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.culturegraph.mf.stream.sink;

/**
* Common functions for object writers.
*
* @author Christoph Böhme
*
* @param <T>
* object type
*/
public abstract class AbstractObjectWriter<T> implements ConfigurableObjectWriter<T> {

private String header = DEFAULT_HEADER;
private String footer = DEFAULT_FOOTER;
private String separator = DEFAULT_SEPARATOR;

@Override
public final String getHeader() {
return header;
}

@Override
public final void setHeader(final String header) {
this.header = header;
}

@Override
public final String getFooter() {
return footer;
}

@Override
public final void setFooter(final String footer) {
this.footer = footer;
}

@Override
public final String getSeparator() {
return separator;
}

@Override
public final void setSeparator(final String separator) {
this.separator = separator;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* Copyright 2013 Christoph Böhme
*
* Licensed under the Apache License, Version 2.0 the "License";
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.culturegraph.mf.stream.sink;

import org.culturegraph.mf.framework.ObjectReceiver;
import org.culturegraph.mf.util.FileCompression;

/**
* Back end implementations for {@link ObjectWriter} should offer
* a default set of configuration options. These are defined by
* this interface.
*
* @author Christoph Böhme
*
* @param <T> object type
*/
public interface ConfigurableObjectWriter<T> extends ObjectReceiver<T> {

String DEFAULT_HEADER = "";
String DEFAULT_FOOTER = "\n";
String DEFAULT_SEPARATOR = "\n";

/**
* Returns the encoding used by the underlying writer.
*
* @return current encoding
*/
String getEncoding();

/**
* Sets the encoding used by the underlying writer.
*
* @param encoding
* name of the encoding
*/
void setEncoding(String encoding);

/**
* Returns the compression mode.
*
* @return current compression mode
*/
FileCompression getCompression();

/**
* Sets the compression mode.
*
* @param compression
*/
void setCompression(final FileCompression compression);

/**
* Sets the compression mode.
*
* @param compression
*/
void setCompression(final String compression);

/**
* Returns the header which is output before the first object.
*
* @return header string
*/
String getHeader();

/**
* Sets the header which is output before the first object.
*
* @param header new header string
*/
void setHeader(final String header);

/**
* Returns the footer which is output after the last object.
*
* @return footer string
*/
String getFooter();

/**
* Sets the footer which is output after the last object.
*
* @param footer new footer string
*/
void setFooter(final String footer);

/**
* Returns the separator which is output between objects.
*
* @return separator string
*/
String getSeparator();

/**
* Sets the separator which is output between objects.
*
* @param separator new separator string
*/
void setSeparator(final String separator);

}
Loading