added some producer variables
[mir.git] / source / mir / producer / GeneratingProducerNode.java
1 /*\r
2  * Copyright (C) 2001, 2002 The Mir-coders group\r
3  *\r
4  * This file is part of Mir.\r
5  *\r
6  * Mir is free software; you can redistribute it and/or modify\r
7  * it under the terms of the GNU General Public License as published by\r
8  * the Free Software Foundation; either version 2 of the License, or\r
9  * (at your option) any later version.\r
10  *\r
11  * Mir is distributed in the hope that it will be useful,\r
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14  * GNU General Public License for more details.\r
15  *\r
16  * You should have received a copy of the GNU General Public License\r
17  * along with Mir; if not, write to the Free Software\r
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
19  *\r
20  * In addition, as a special exception, The Mir-coders gives permission to link\r
21  * the code of this program with  any library licensed under the Apache Software License,\r
22  * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library\r
23  * (or with modified versions of the above that use the same license as the above),\r
24  * and distribute linked combinations including the two.  You must obey the\r
25  * GNU General Public License in all respects for all of the code used other than\r
26  * the above mentioned libraries.  If you modify this file, you may extend this\r
27  * exception to your version of the file, but you are not obligated to do so.\r
28  * If you do not wish to do so, delete this exception statement from your version.\r
29  */\r
30 package mir.producer;\r
31 \r
32 import java.util.*;\r
33 \r
34 import mir.generator.Generator;\r
35 import mir.generator.WriterEngine;\r
36 import mir.log.LoggerWrapper;\r
37 import mir.util.ParameterExpander;\r
38 \r
39 public class GeneratingProducerNode implements ProducerNode {\r
40   private String generatorExpression;\r
41   private String destinationExpression;\r
42   private String parametersExpression;\r
43   private Generator.GeneratorLibrary generatorLibrary;\r
44   private WriterEngine writerEngine;\r
45 \r
46   public GeneratingProducerNode(Generator.GeneratorLibrary aGeneratorLibrary, WriterEngine aWriterEngine, String aGenerator, String aDestination, String aParameters) {\r
47     generatorExpression=aGenerator;\r
48     destinationExpression=aDestination;\r
49     parametersExpression=aParameters;\r
50     generatorLibrary = aGeneratorLibrary;\r
51     writerEngine = aWriterEngine;\r
52   }\r
53 \r
54   public GeneratingProducerNode(Generator.GeneratorLibrary aGeneratorLibrary, WriterEngine aWriterEngine, String aGenerator, String aDestination) {\r
55     this(aGeneratorLibrary, aWriterEngine, aGenerator, aDestination, "");\r
56   }\r
57 \r
58   public void produce(Map aValueMap, String aVerb, LoggerWrapper aLogger) throws ProducerFailure {\r
59     Generator generator;\r
60     Object writer;\r
61     String generatorIdentifier;\r
62     String destinationIdentifier;\r
63     String parameters;\r
64 \r
65     long startTime;\r
66     long endTime;\r
67 \r
68     startTime = System.currentTimeMillis();\r
69     try {\r
70       Map mirMap = (Map) aValueMap.get("Mir");\r
71       if (mirMap==null) {\r
72         mirMap = new HashMap();\r
73         aValueMap.put("Mir", mirMap);\r
74       }\r
75 \r
76       Object oldGenerator = mirMap.get("generator");\r
77       Object oldDestination = mirMap.get("destination");\r
78       Object oldParameters = mirMap.get("parameters");\r
79       try {\r
80         destinationIdentifier = ParameterExpander.expandExpression(aValueMap, destinationExpression);\r
81         generatorIdentifier = ParameterExpander.expandExpression(aValueMap, generatorExpression);\r
82         parameters = ParameterExpander.expandExpression(aValueMap, parametersExpression);\r
83 \r
84         mirMap.put("generator", generatorIdentifier);\r
85         mirMap.put("destination", destinationIdentifier);\r
86         mirMap.put("parameters", parameters);\r
87 \r
88         writer = writerEngine.openWriter(destinationIdentifier, parameters);\r
89         generator = generatorLibrary.makeGenerator(generatorIdentifier);\r
90         generator.generate(writer, aValueMap, aLogger);\r
91         writerEngine.closeWriter(writer);\r
92 \r
93         endTime = System.currentTimeMillis();\r
94         aLogger.info("Generated " + generatorIdentifier + " into " + destinationIdentifier + " [" + parameters + "] in " + (endTime - startTime) + " ms");\r
95       }\r
96       finally {\r
97         mirMap.put("generator", oldGenerator);\r
98         mirMap.put("destination", oldDestination);\r
99         mirMap.put("parameters", oldParameters);\r
100       }\r
101     }\r
102     catch (Throwable t) {\r
103       aLogger.error("  error while generating: " + t.getClass().getName() + ": " + t.getMessage());\r
104     }\r
105   }\r
106 }