package com.c2kernel.lifecycle.instance.predefined;
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.xml.serialize.Method;
import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;
import org.w3c.dom.CDATASection;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.xml.sax.InputSource;
import com.c2kernel.lifecycle.instance.Activity;
import com.c2kernel.utils.Logger;
/***********************************************************************************************************************************************************************************************************************************************************************************************************
* @author $Author: sgaspard $ $Date: 2004/09/21 10:32:17 $
* @version $Revision: 1.14 $
**********************************************************************************************************************************************************************************************************************************************************************************************************/
public class PredefinedStep extends Activity
{
/*******************************************************************************************************************************************************************************************************************************************************************************************************
* predefined Steps are always Active, and have only one transition subclasses could override this method (if necessary)
******************************************************************************************************************************************************************************************************************************************************************************************************/
private boolean isPredefined = false;
public boolean getActive()
{
if (isPredefined)
return true;
else
return super.getActive();
}
public String getTransitions()
{
if (isPredefined)
return "done";
else
return super.getTransitions();
}
public String getErrors()
{
if (isPredefined)
return getName();
else
return super.getErrors();
}
public boolean verify()
{
if (isPredefined)
return true;
else
return super.verify();
}
/**
* Returns the isPredefined.
*
* @return boolean
*/
public boolean getIsPredefined()
{
return isPredefined;
}
/**
* Sets the isPredefined.
*
* @param isPredefined
* The isPredefined to set
*/
public void setIsPredefined(boolean isPredefined)
{
this.isPredefined = isPredefined;
}
public String getType()
{
return getName();
}
// generic bundling of parameters
static public String bundleData(String[] data)
{
try
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document dom = builder.newDocument();
Element root = dom.createElement("PredefinedStepOutcome");
dom.appendChild(root);
for (int i = 0; i < data.length; i++)
{
Element param = dom.createElement("param");
Text t = dom.createTextNode(data[i]);
param.appendChild(t);
root.appendChild(param);
}
// xalan method - use internal xerces one instead
// TransformerFactory transFactory = TransformerFactory.newInstance();
// Transformer transformer = transFactory.newTransformer();
// StringWriter stringOut = new StringWriter();
// transformer.transform(new DOMSource(dom), new StreamResult(stringOut));
// return stringOut.toString();
OutputFormat format = new OutputFormat(Method.XML, null, false);
StringWriter stringOut = new StringWriter();
XMLSerializer serial = new XMLSerializer(stringOut, format);
serial.asDOMSerializer();
serial.serialize(dom);
return stringOut.toString();
}
catch (Exception e)
{
Logger.error(e);
StringBuffer xmlData = new StringBuffer().append("");
for (int i = 0; i < data.length; i++)
xmlData.append("");
xmlData.append("");
return xmlData.toString();
}
}
// generic bundling of single parameter
static public String bundleData(String data)
{
return "";
}
static public String[] getDataList(String xmlData)
{
try
{
Document scriptDoc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(new StringReader(xmlData)));
NodeList nodeList = scriptDoc.getElementsByTagName("param");
String[] result = new String[nodeList.getLength()];
for (int i = 0; i < nodeList.getLength(); i++)
{
Node n = nodeList.item(i).getFirstChild();
if (n instanceof CDATASection)
result[i] = ((CDATASection) n).getData();
else if (n instanceof Text)
result[i] = ((Text) n).getData();
}
return result;
}
catch (Exception ex)
{
Logger.error("Exception::PredefinedStep::getDataList()");
Logger.error(ex);
}
return null;
}
}