//
// FlowStepSwitch.java - Switch statement
//

package nyangau.jmsadapter;

//...simports:0:
import java.util.Vector;
import java.util.regex.*;
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 FlowStepSwitch extends FlowStep
  {
  protected String var;
//...sclass Limb:2:
public class Limb
  {
  protected Pattern pattern;
  protected String flow;
  protected Flow f;
//...sLimb:4:
public Limb(Node eLimb)
  throws FlowProcessorException
  {
  NamedNodeMap nnmLimb = eLimb.getAttributes();
  Node aRegexp = nnmLimb.getNamedItem("regexp");
  if ( aRegexp == null )
    throw new FlowProcessorException("switch limb with missing regexp, bad jmsadapter.xsd?");
  String regexp = aRegexp.getNodeValue();
  try
    {
    pattern = Pattern.compile(regexp);
    }
  catch ( PatternSyntaxException e )
    {
    throw new FlowProcessorException("bad Java Regular Expression "+regexp+" switch limb", e);
    }
  Node aFlow = nnmLimb.getNamedItem("flow");
  if ( aFlow == null )
    throw new FlowProcessorException("unexpected missing flow in switch limb, bad jmsadapter.xsd?");
  flow = aFlow.getNodeValue();

  // Note that you might think we can lookup the callee Flow right now,
  // but note that not all of them may have been read from the configuration
  // XML at the time we are called here, so we do this lookup lazily.
  f = null;
  }
//...e
//...smatches:4:
public boolean matches(StringBuffer sb)
  {
  Matcher matcher = pattern.matcher(sb);
  return matcher.matches();
  }
//...e
//...scall:4:
public void call(FlowContext c)
  throws FlowProcessorException, JMSException
  {
  if ( f == null )
    {
    f = c.getFlowProcessor().getFlow(flow);
    if ( f == null )
      throw new FlowProcessorException("unexpected reference to non-existent flow "+flow+" in switch limb, bad jmsadapter.xsd?");
    }
  f.process(c);
  }
//...e
  }
//...e
  Vector<Limb> limbs;
//...sFlowStepSwitch:2:
public FlowStepSwitch(Node eStep)
  throws FlowProcessorException
  {
  super(eStep);
  NamedNodeMap nnmStep = eStep.getAttributes();
  Node aVar = nnmStep.getNamedItem("var");
  var = ( aVar != null ) ? aVar.getNodeValue() : "v0";

  // Build a vector of limbs
  limbs = new Vector<Limb>();
  NodeList nl = eStep.getChildNodes();
  for ( int i = 0; i < nl.getLength(); i++ )
    {
    Node eLimb = nl.item(i);
    if ( eLimb.getNodeType() == Node.ELEMENT_NODE )
      {
      Limb l = new Limb(eLimb);
      limbs.add(l);
      }
    }
  }
//...e
//...sprocess:2:
public void process(FlowContext c)
  throws FlowProcessorException, JMSException
  {
  Object o = c.getVar(var);
  if ( o == null )
    throw new FlowProcessorException(var+" undefined in step "+name);
  if ( o instanceof FlowNullValue )
    throw new FlowProcessorException(var+" is null in step "+name);
  if ( ! ( o instanceof String ) )
    throw new FlowProcessorException(var+" is not a String in step "+name);
  StringBuffer sb = new StringBuffer((String) o);
  for ( int i = 0; i < limbs.size(); i++ )
    {
    Limb l = limbs.elementAt(i);
    if ( l.matches(sb) )
      {
      l.call(c);
      break;
      }
    }
  }
//...e
  }
