2 * Copyright (C) 2001-2006 The Mir-coders group
4 * This file is part of Mir.
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.
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.
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
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.
28 package mir.producer.reader;
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;
36 import java.util.HashMap;
37 import java.util.Iterator;
41 * <p> A ScriptedProducerNode is a ProducerNode which is defined in
42 * producers.xml file by the <nodedefinition> statement. </p>
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>
49 public class ScriptedProducerNode implements ProducerNode {
50 private ScriptedProducerNodeDefinition definition;
51 private Map integerParameterValues;
52 private Map stringParameterValues;
53 private Map nodeParameterValues;
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);
65 public void produce(ProductionContext aProductionContext) throws ProducerFailure, ProducerExc {
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());
73 Iterator i = stringParameterValues.entrySet().iterator();
76 Map.Entry entry = (Map.Entry) i.next();
78 if (entry.getValue() instanceof String) {
79 aProductionContext.getValueSet().put(entry.getKey(),
80 ParameterExpander.expandExpression(aProductionContext.getValueSet(),
81 (String) entry.getValue()));
85 i = integerParameterValues.entrySet().iterator();
88 Map.Entry entry = (Map.Entry) i.next();
90 if (entry.getValue() instanceof String) {
91 aProductionContext.getValueSet().put(entry.getKey(),
92 ParameterExpander.evaluateExpression(aProductionContext.getValueSet(), (String) entry.getValue()));
96 ScriptedProducerNodeTool.pushNodeParameterValues(aProductionContext.getValueSet(),
97 definition.getName(), nodeParameterValues);
99 definition.getBody().produce(aProductionContext);
102 ScriptedProducerNodeTool.popNodeParameterValues(aProductionContext.getValueSet(), definition.getName());
106 ScriptedProducerNodeTool.restoreMapValues(aProductionContext.getValueSet(), definition.getIntegerParameters().keySet(), oldValues);
107 ScriptedProducerNodeTool.restoreMapValues(aProductionContext.getValueSet(), definition.getStringParameters().keySet(), oldValues);
110 catch (Exception e) {
111 aProductionContext.getLogger().error("Scripted producer node " + definition.getName() + " caused an exception", e);