//
// DataMapInline.java - Use hard coded mappings from XML configuration
//

package nyangau.jmsadapter;

//...simports:0:
import java.util.Set;
import java.util.Map;
import java.util.HashMap;
import java.util.Properties;
import java.util.Enumeration;
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 DataMapInline extends DataMap
  {
  protected Map<String,String> maplets;
//...sDataMapInline:2:
public DataMapInline(Node eDataMap)
  throws FlowProcessorException
  {
  super(eDataMap);
  maplets = new HashMap<String,String>();
  NamedNodeMap nnmDataMap = eDataMap.getAttributes();

  // Read maplets from within the XML configuration
  NodeList nlMaplets = eDataMap.getChildNodes();
  for ( int i = 0; i < nlMaplets.getLength(); i++ )
    {
    Node eMaplet = nlMaplets.item(i);
    if ( eMaplet.getNodeType() == Node.ELEMENT_NODE &&
         eMaplet.getNodeName().equals("maplet") )
      {
      NamedNodeMap nnmMaplet = eMaplet.getAttributes();
      Node aSrc = nnmMaplet.getNamedItem("src");
      if ( aSrc == null )
        throw new FlowProcessorException("unexpected missing src in maplet in data map "+name+", bad jmsadapter.xsd?");
      String src = aSrc.getNodeValue();
      Node aDest = nnmMaplet.getNamedItem("dest");
      String dest = aDest.getNodeValue();
      if ( aDest == null )
        throw new FlowProcessorException("unexpected missing dest in maplet in data map"+name+", bad jmsadapter.xsd?");
      maplets.put(src, dest);
      }
    }
  }
//...e
//...smap:2:
public void map(Set<String> unmapped, Map<String,String> mapped)
  throws FlowProcessorException
  {
  if ( unmapped.size() < maplets.size() )
    for ( String s : unmapped )
      {
      String d = maplets.get(s);
      if ( d != null )
        {
        mapped.put(s, d);
        unmapped.remove(s);
        }
      }
  else
    for ( String s : maplets.keySet() )
      if ( unmapped.contains(s) )
        {
        mapped.put(s, maplets.get(s));
        unmapped.remove(s);
        }
  }
//...e
  }
