package org.syrup.functions;
import org.syrup.Context;
import org.syrup.Function;
import org.syrup.Result;
import org.syrup.helpers.DataImpl;
import org.syrup.helpers.NetworkImpl;
import org.syrup.helpers.NetworkParser;
import org.syrup.helpers.ResultImpl;
import org.syrup.helpers.Utils;
import org.syrup.helpers.WorkflowImpl;
import java.util.logging.Logger;
/**
* Takes the first input and splits the head of the first input to the first output and the tail to
* the second output. The UNIX line terminator is used to cut off the head element.
*
* @author Robbert van Dalen
*/
public class Split implements Function
{
static final String COPYRIGHT = "Copyright 2005 Robbert van Dalen."
+ "At your option, you may copy, distribute, or make derivative works under "
+ "the terms of The Artistic License. This License may be found at "
+ "http://www.opensource.org/licenses/artistic-license.php. "
+ "THERE IS NO WARRANTY; USE THIS PRODUCT AT YOUR OWN RISK.";
private final static Logger logger = Logger.getLogger("org.syrup.functions.Split");
private final static String replaceWorkflow = "<workflow><binding out-1='s-1' out-2='s-2'/><task name='s' functionClass='org.syrup.functions.Split' orType='true'/></workflow>";
/**
*/
public Result execute(Context context)
{
try
{
ResultImpl im = null;
if (Utils.isFull(context.in_1_link()))
{
String content = new String(context.in_1_link().content().bytes());
// [TODO: make this a parameter taken from the second input]
String split = "\n";
int i = content.indexOf(split);
int t = content.length()
- i - split.length();
if (i > 0)
{
String head = content.substring(0, i);
String tail = content.substring(i
+ split.length(), content.length());
if (tail.length() > 0)
{
return new ResultImpl(context, true, true, new DataImpl(head.getBytes()), new DataImpl(tail.getBytes()));
}
else
{
// [TODO: optimize by prefabricating the NetworkImpl]
NetworkImpl network = new NetworkParser().parse(replaceWorkflow.getBytes());
// Returns the Workflow.
return new WorkflowImpl(new ResultImpl(context, true, true, new DataImpl(head.getBytes()), null), network);
}
}
else
{
// [TODO: optimize by prefabricating the NetworkImpl]
NetworkImpl network = new NetworkParser().parse(replaceWorkflow.getBytes());
// Returns the Workflow.
return new WorkflowImpl(new ResultImpl(context, true, true, context.in_1_link().content(), null), network);
}
}
else
{
throw new Exception("first input needs to be full");
}
}
catch (Throwable e1)
{
return new ResultImpl(context, true, true, null, org.syrup.helpers.Utils.manageError(logger, e1, "Concat error"));
}
}
}