reintroduced StringUtil.regexpReplace
[mir.git] / source / mir / producer / reader / ScriptedProducerNode.java
1 /*
2  * Copyright (C) 2001-2006 The Mir-coders group
3  *
4  * This file is part of Mir.
5  *
6  * Mir is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * Mir is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Mir; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  * In addition, as a special exception, The Mir-coders gives permission to link
21  * the code of this program with  any library licensed under the Apache Software License,
22  * and distribute linked combinations including the two.  You must obey the
23  * GNU General Public License in all respects for all of the code used other than
24  * the above mentioned libraries.  If you modify this file, you may extend this
25  * exception to your version of the file, but you are not obligated to do so.
26  * If you do not wish to do so, delete this exception statement from your version.
27  */
28 package mir.producer.reader;
29
30 import mir.producer.ProducerExc;
31 import mir.producer.ProducerFailure;
32 import mir.producer.ProducerNode;
33 import mir.producer.ProductionContext;
34 import mir.util.ParameterExpander;
35
36 import java.util.HashMap;
37 import java.util.Iterator;
38 import java.util.Map;
39
40 /**
41  * <p> A  ScriptedProducerNode is a  ProducerNode which is  defined in
42  * producers.xml file by the &lt;nodedefinition&gt; statement.  </p>
43  *
44  * <p>It  Acts as  a  "function"  (or "macro")  that  can be  "called"
45  * elsewhere in a  producer.  More precisely, it is a  way to define a
46  * new producer  node type inside the producers.xml  file.  (check the
47  * Language node in that file for an example).</p>
48  */
49 public class ScriptedProducerNode implements ProducerNode {
50   private ScriptedProducerNodeDefinition definition;
51   private Map integerParameterValues;
52   private Map stringParameterValues;
53   private Map nodeParameterValues;
54
55   public ScriptedProducerNode(ScriptedProducerNodeDefinition aDefinition, Map aStringParameterValues, Map anIntegerParameterValues, Map aNodeParameterValues) {
56     definition = aDefinition;
57     stringParameterValues = new HashMap();
58     stringParameterValues.putAll(aStringParameterValues);
59     integerParameterValues = new HashMap();
60     integerParameterValues.putAll(anIntegerParameterValues);
61     nodeParameterValues = new HashMap();
62     nodeParameterValues.putAll(aNodeParameterValues);
63   }
64
65   public void produce(ProductionContext aProductionContext) throws ProducerFailure, ProducerExc {
66     try {
67       Map oldValues = new HashMap();
68       ScriptedProducerNodeTool.saveMapValues(oldValues,
69           aProductionContext.getValueSet(), definition.getStringParameters().keySet());
70       ScriptedProducerNodeTool.saveMapValues(oldValues,
71           aProductionContext.getValueSet(), definition.getIntegerParameters().keySet());
72       try {
73         Iterator i = stringParameterValues.entrySet().iterator();
74
75         while (i.hasNext()) {
76           Map.Entry entry = (Map.Entry) i.next();
77
78           if (entry.getValue() instanceof String) {
79             aProductionContext.getValueSet().put(entry.getKey(),
80                 ParameterExpander.expandExpression(aProductionContext.getValueSet(),
81                     (String) entry.getValue()));
82           }
83         }
84
85         i = integerParameterValues.entrySet().iterator();
86
87         while (i.hasNext()) {
88           Map.Entry entry = (Map.Entry) i.next();
89
90           if (entry.getValue() instanceof String) {
91             aProductionContext.getValueSet().put(entry.getKey(),
92                 ParameterExpander.evaluateExpression(aProductionContext.getValueSet(), (String) entry.getValue()));
93           }
94         }
95
96         ScriptedProducerNodeTool.pushNodeParameterValues(aProductionContext.getValueSet(),
97             definition.getName(), nodeParameterValues);
98         try {
99           definition.getBody().produce(aProductionContext);
100         }
101         finally {
102           ScriptedProducerNodeTool.popNodeParameterValues(aProductionContext.getValueSet(), definition.getName());
103         }
104       }
105       finally {
106         ScriptedProducerNodeTool.restoreMapValues(aProductionContext.getValueSet(), definition.getIntegerParameters().keySet(), oldValues);
107         ScriptedProducerNodeTool.restoreMapValues(aProductionContext.getValueSet(), definition.getStringParameters().keySet(), oldValues);
108       }
109     }
110     catch (Exception e) {
111       aProductionContext.getLogger().error("Scripted producer node " + definition.getName() + " caused an exception", e);
112     }
113   }
114
115 }