- general maintenance
[mir.git] / source / mir / producer / reader / ScriptedProducerNodeDefinition.java
1 /*
2  * Copyright (C) 2001, 2002  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 the com.oreilly.servlet library, any library
22  * licensed under the Apache Software License, The Sun (tm) Java Advanced
23  * Imaging library (JAI), The Sun JIMI library (or with modified versions of
24  * the above that use the same license as the above), and distribute linked
25  * combinations including the two.  You must obey the GNU General Public
26  * License in all respects for all of the code used other than the above
27  * mentioned libraries.  If you modify this file, you may extend this exception
28  * to your version of the file, but you are not obligated to do so.  If you do
29  * not wish to do so, delete this exception statement from your version.
30  */
31
32 package mir.producer.reader;
33
34 import java.util.*;
35 import java.io.*;
36 import mir.producer.*;
37
38 public class ScriptedProducerNodeDefinition {
39   private Map integerParameters;               // name -> default value
40   private Map stringParameters;               // name -> default value
41   private Set nodeParameters;
42   private ProducerNode body;
43   private String name;
44
45   public static String SCRIPTED_PRODUCERNODE_RUNTIMEDATA_KEY = "$SCRIPTRUNTIMEDATA";
46   public static String SCRIPTED_PRODUCERNODE_RUNTIMESTACK_KEY = "stack";
47
48   public ScriptedProducerNodeDefinition(String aName) {
49     name = aName;
50     integerParameters = new HashMap();
51     stringParameters = new HashMap();
52     nodeParameters = new HashSet();
53     body = new CompositeProducerNode();
54   }
55
56   public void addStringParameter(String aName, String aDefaultValue) {
57     stringParameters.put(aName, aDefaultValue);
58   }
59
60   public void addIntegerParameter(String aName, String aDefaultValue) {
61     integerParameters.put(aName, aDefaultValue);
62   }
63
64   public void addNodeParameter(String aName) {
65     nodeParameters.add(aName);
66   }
67
68   public void setBody(ProducerNode aBody) {
69     body = aBody;
70   }
71
72   protected Map getStringParameters() {
73     return stringParameters;
74   }
75
76   protected Map getIntegerParameters() {
77     return integerParameters;
78   }
79
80   protected Set getNodeParameters() {
81     return nodeParameters;
82   }
83
84   protected ProducerNode getBody() {
85     return body;
86   }
87
88   public String getName() {
89     return name;
90   }
91
92   public Set getRequiredAttributes() {
93     return getAttributesSelection(true);
94   }
95
96   public Set getOptionalAttributes() {
97     return getAttributesSelection(false);
98   }
99
100   public Set getAttributesSelection(boolean aRequired) {
101     Set result = new HashSet();
102     Iterator i = stringParameters.entrySet().iterator();
103
104     while (i.hasNext()) {
105       Map.Entry entry = (Map.Entry) i.next();
106
107       if ((entry.getValue() == null) == aRequired ) {
108         result.add(entry.getKey());
109       }
110     }
111
112     i = integerParameters.entrySet().iterator();
113
114     while (i.hasNext()) {
115       Map.Entry entry = (Map.Entry) i.next();
116
117       if ((entry.getValue() == null) == aRequired ) {
118         result.add(entry.getKey());
119       }
120     }
121
122     return result;
123   }
124
125
126   protected static class NodeParameterProducerNode implements ProducerNode {
127     private String parameterName;
128     private String definitionName;
129
130     public NodeParameterProducerNode(String aDefinitionName, String aParameterName) {
131       definitionName = aDefinitionName;
132       parameterName = aParameterName;
133     }
134
135     public void produce(Map aValues, String aVerb, PrintWriter aLogger) throws ProducerExc, ProducerFailure {
136       ProducerNode producerNode;
137
138       Map runTimeData = (Map) ((Map) aValues.get(SCRIPTED_PRODUCERNODE_RUNTIMEDATA_KEY)).get(definitionName);
139       Map parameters = (Map) ((Stack) runTimeData.get( SCRIPTED_PRODUCERNODE_RUNTIMESTACK_KEY )).peek();
140
141       producerNode = (ProducerNode) parameters.get(parameterName);
142
143       if (producerNode != null)
144         producerNode.produce(aValues, aVerb, aLogger);
145     }
146
147     public Set buildVerbSet() {
148       return new HashSet();
149     }
150   }
151 }