org.metafacture.formatting.PreambleEpilogueAdder Maven / Gradle / Ivy
Show all versions of metafacture-formatting Show documentation
/*
* Copyright 2016 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.metafacture.formatting;
import org.metafacture.framework.FluxCommand;
import org.metafacture.framework.ObjectReceiver;
import org.metafacture.framework.annotations.Description;
import org.metafacture.framework.annotations.In;
import org.metafacture.framework.annotations.Out;
import org.metafacture.framework.helpers.DefaultObjectPipe;
/**
* Emits a preamble string before the first string object in the
* stream and an epilogue string before the end of the stream.
*
* The preamble and epilogue strings are only emitted if an object is received.
* If the preamble or epilogue string is empty, the respective string is not
* emitted.
*
* @author Markus Geipel
* @author Christoph Böhme
*
*/
@Description("Adds a String preamle and/or epilogue to the stream")
@In(String.class)
@Out(String.class)
@FluxCommand("add-preamble-epilogue")
public final class PreambleEpilogueAdder extends DefaultObjectPipe> {
private String preamble = "";
private String epilogue = "";
private boolean objectsReceived;
/**
* Creates an instance of {@link PreambleEpilogueAdder}.
*/
public PreambleEpilogueAdder() {
}
/**
* Sets the preamble string which is emitted before the first object.
*
* The default preamble is an empty string. That means by default no
* preamble is emitted.
*
* The parameter may only be changed before the first object is processed
* otherwise the change has no effect.
*
* @param preamble the preamble string
*/
public void setPreamble(final String preamble) {
this.preamble = preamble;
}
/**
* Gets the preamble.
*
* @return the preamble
*/
public String getPreamble() {
return preamble;
}
/**
* Sets the epilogue string which is emitted after the last object.
*
* The default epilogue string is an empty string. That means by default no
* epilogue is emitted.
*
* The parameter may be changed at any time. Its becomes effective when a
* close-stream event is received.
*
* @param epilogue the epilogue string
*/
public void setEpilogue(final String epilogue) {
this.epilogue = epilogue;
}
/**
* Gets the epilogue.
*
* @return the epilogue
*/
public String getEpilogue() {
return epilogue;
}
@Override
public void process(final String obj) {
if (!objectsReceived && !preamble.isEmpty()) {
getReceiver().process(preamble);
}
objectsReceived = true;
getReceiver().process(obj);
}
@Override
protected void onCloseStream() {
if (objectsReceived && !epilogue.isEmpty()) {
getReceiver().process(epilogue);
}
}
@Override
protected void onResetStream() {
objectsReceived = false;
}
}