merged 1.1 branch into head
[mir.git] / source / mir / producer / reader / ScriptedProducerNode.java
index 047f27a..ed8df73 100755 (executable)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2001, 2002 The Mir-coders group
+ * Copyright (C) 2001-2006 The Mir-coders group
  *
  * This file is part of Mir.
  *
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  *
  * In addition, as a special exception, The Mir-coders gives permission to link
- * the code of this program with  any library licensed under the Apache Software License, 
- * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library 
- * (or with modified versions of the above that use the same license as the above), 
- * and distribute linked combinations including the two.  You must obey the 
- * GNU General Public License in all respects for all of the code used other than 
- * the above mentioned libraries.  If you modify this file, you may extend this 
- * exception to your version of the file, but you are not obligated to do so.  
+ * the code of this program with  any library licensed under the Apache Software License,
+ * and distribute linked combinations including the two.  You must obey the
+ * GNU General Public License in all respects for all of the code used other than
+ * the above mentioned libraries.  If you modify this file, you may extend this
+ * exception to your version of the file, but you are not obligated to do so.
  * If you do not wish to do so, delete this exception statement from your version.
  */
 package mir.producer.reader;
 
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.Map;
-
-import mir.log.LoggerWrapper;
 import mir.producer.ProducerExc;
 import mir.producer.ProducerFailure;
 import mir.producer.ProducerNode;
+import mir.producer.ProductionContext;
 import mir.util.ParameterExpander;
 
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
+/**
+ * <p> A  ScriptedProducerNode is a  ProducerNode which is  defined in
+ * producers.xml file by the &lt;nodedefinition&gt; statement.  </p>
+ *
+ * <p>It  Acts as  a  "function"  (or "macro")  that  can be  "called"
+ * elsewhere in a  producer.  More precisely, it is a  way to define a
+ * new producer  node type inside the producers.xml  file.  (check the
+ * Language node in that file for an example).</p>
+ */
 public class ScriptedProducerNode implements ProducerNode {
   private ScriptedProducerNodeDefinition definition;
   private Map integerParameterValues;
@@ -55,11 +62,13 @@ public class ScriptedProducerNode implements ProducerNode {
     nodeParameterValues.putAll(aNodeParameterValues);
   }
 
-  public void produce(Map aValues, String aVerb, LoggerWrapper aLogger) throws ProducerFailure, ProducerExc {
+  public void produce(ProductionContext aProductionContext) throws ProducerFailure, ProducerExc {
     try {
       Map oldValues = new HashMap();
-      ScriptedProducerNodeTool.saveMapValues(oldValues, aValues, definition.getStringParameters().keySet());
-      ScriptedProducerNodeTool.saveMapValues(oldValues, aValues, definition.getIntegerParameters().keySet());
+      ScriptedProducerNodeTool.saveMapValues(oldValues,
+          aProductionContext.getValueSet(), definition.getStringParameters().keySet());
+      ScriptedProducerNodeTool.saveMapValues(oldValues,
+          aProductionContext.getValueSet(), definition.getIntegerParameters().keySet());
       try {
         Iterator i = stringParameterValues.entrySet().iterator();
 
@@ -67,7 +76,9 @@ public class ScriptedProducerNode implements ProducerNode {
           Map.Entry entry = (Map.Entry) i.next();
 
           if (entry.getValue() instanceof String) {
-            aValues.put(entry.getKey(), ParameterExpander.expandExpression(aValues, (String) entry.getValue()));
+            aProductionContext.getValueSet().put(entry.getKey(),
+                ParameterExpander.expandExpression(aProductionContext.getValueSet(),
+                    (String) entry.getValue()));
           }
         }
 
@@ -77,25 +88,27 @@ public class ScriptedProducerNode implements ProducerNode {
           Map.Entry entry = (Map.Entry) i.next();
 
           if (entry.getValue() instanceof String) {
-            aValues.put(entry.getKey(), ParameterExpander.evaluateExpression(aValues, (String) entry.getValue()));
+            aProductionContext.getValueSet().put(entry.getKey(),
+                ParameterExpander.evaluateExpression(aProductionContext.getValueSet(), (String) entry.getValue()));
           }
         }
 
-        ScriptedProducerNodeTool.pushNodeParameterValues(aValues, definition.getName(), nodeParameterValues);
+        ScriptedProducerNodeTool.pushNodeParameterValues(aProductionContext.getValueSet(),
+            definition.getName(), nodeParameterValues);
         try {
-          definition.getBody().produce(aValues, aVerb, aLogger);
+          definition.getBody().produce(aProductionContext);
         }
         finally {
-          ScriptedProducerNodeTool.popNodeParameterValues(aValues, definition.getName());
+          ScriptedProducerNodeTool.popNodeParameterValues(aProductionContext.getValueSet(), definition.getName());
         }
       }
       finally {
-        ScriptedProducerNodeTool.restoreMapValues(aValues, definition.getIntegerParameters().keySet(), oldValues);
-        ScriptedProducerNodeTool.restoreMapValues(aValues, definition.getStringParameters().keySet(), oldValues);
+        ScriptedProducerNodeTool.restoreMapValues(aProductionContext.getValueSet(), definition.getIntegerParameters().keySet(), oldValues);
+        ScriptedProducerNodeTool.restoreMapValues(aProductionContext.getValueSet(), definition.getStringParameters().keySet(), oldValues);
       }
     }
     catch (Exception e) {
-      aLogger.error("Scripted producer node " + definition.getName() + " caused an exception: " + e.getMessage());
+      aProductionContext.getLogger().error("Scripted producer node " + definition.getName() + " caused an exception", e);
     }
   }