//
// FlowStepDebug.java - FlowStep to help with debugging
//
// This FlowStep allows the crude visualisation of the state of the
// FlowContext, at a given point in the flow.
// It can display specific named variables, or all defined variables.
// It can display the message properties and body.
// It can log to standard output, or append to a named file.
//
// However, it has some significant limitations.
// See FlowHelper.java for details.
//

package nyangau.jmsadapter;

//...simports:0:
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.io.IOException;
import javax.jms.JMSException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.Text;
//...e

public class FlowStepDebug extends FlowStep
  {
  protected String logfile;
  protected boolean logmessage;
  protected boolean logvars;
  protected String vars;
//...sFlowStepDebug:2:
public FlowStepDebug(Node eStep)
  throws FlowProcessorException
  {
  super(eStep);
  NamedNodeMap nnmStep = eStep.getAttributes();
  Node aLogFile = nnmStep.getNamedItem("logfile");
  logfile = ( aLogFile != null ) ? aLogFile.getNodeValue() : null;
  Node aLogMessage = nnmStep.getNamedItem("logmessage");
  logmessage = ( aLogMessage != null )
    ? aLogMessage.getNodeValue().equals("true")
    : false;
  Node aLogVars = nnmStep.getNamedItem("logvars");
  logvars = ( aLogVars != null )
    ? aLogVars.getNodeValue().equals("true")
    : false;
  Node aVars = nnmStep.getNamedItem("vars");
  vars = ( aVars != null ) ? aVars.getNodeValue() : null;
  }
//...e
//...sprocess:2:
public void process(FlowContext c)
  throws FlowProcessorException, JMSException
  {
  if ( logmessage || logvars )
    try
      {
      if ( logfile != null && ! logfile.equals("") )
        {
        FileOutputStream fos = new FileOutputStream(logfile, true);
        PrintStream ps = new PrintStream(fos);
        ps.println("=== FlowStep "+name);
        ps.print(FlowHelper.diagForm(c, logmessage, logvars, vars));
        ps.flush();
        fos.close();
        }
      else
        {
        PrintStream ps = new PrintStream(System.out);
        ps.println("=== FlowStep "+name);
        ps.print(FlowHelper.diagForm(c, logmessage, logvars, vars));
        }
      }
    catch ( IOException e )
      {
      // Who cares, its only debugging
      }
  }
//...e
  }
