1.1 restoration
[mir.git] / source / mircoders / localizer / basic / MirBasicProducerLocalizer.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  any library licensed under the Apache Software License,
22  * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
23  * (or with modified versions of the above that use the same license as the above),
24  * and distribute linked combinations including the two.  You must obey the
25  * GNU General Public License in all respects for all of the code used other than
26  * the above mentioned libraries.  If you modify this file, you may extend this
27  * exception to your version of the file, but you are not obligated to do so.
28  * If you do not wish to do so, delete this exception statement from your version.
29  */
30 package mircoders.localizer.basic;
31
32 import java.io.File;
33 import java.util.HashMap;
34 import java.util.Iterator;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.Vector;
38
39 import mir.entity.adapter.EntityAdapterModel;
40 import mir.generator.Generator;
41 import mir.generator.WriterEngine;
42 import mir.log.LoggerWrapper;
43 import mir.producer.reader.DefaultProducerNodeBuilders;
44 import mir.producer.reader.ProducerConfigReader;
45 import mir.producer.reader.ProducerNodeBuilderLibrary;
46 import mir.util.StringRoutines;
47 import mircoders.global.MirGlobal;
48 import mircoders.global.ProducerEngine;
49 import mircoders.localizer.MirLocalizerExc;
50 import mircoders.localizer.MirLocalizerFailure;
51 import mircoders.localizer.MirProducerLocalizer;
52 import mircoders.producer.reader.SupplementalProducerNodeBuilders;
53
54 public class MirBasicProducerLocalizer implements MirProducerLocalizer {
55   private Map producerRecipes;
56   private List producerRecipeNames;
57
58   protected EntityAdapterModel model;
59   protected Generator.Library generatorLibrary;
60   protected WriterEngine writerEngine;
61
62   protected LoggerWrapper logger;
63
64   public MirBasicProducerLocalizer() {
65     try {
66       logger = new LoggerWrapper("Localizer.Basic.Producer");
67
68       producerRecipes = new HashMap();
69       producerRecipeNames = new Vector();
70
71       String[] recipes = MirGlobal.config().getStringArray("Mir.Localizer.Producer.ProducerRecipes");
72       for (int i = 0; i<recipes.length; i++) {
73         try {
74           List parts = StringRoutines.separateString(recipes[i], "=");
75           if (parts.size() == 2) {
76             String key = ((String) parts.get(0)).trim();
77             producerRecipes.put(key, ProducerEngine.ProducerTask.parseProducerTaskList(
78                 ((String) parts.get(1)).trim()));
79             producerRecipeNames.add(key);
80           }
81           else {
82             throw new Exception("'=' expected");
83           }
84         }
85         catch (Throwable t) {
86           logger.error("Error while processing producer recipe '" + recipes[i] + "': " + t.toString());
87         }
88       }
89
90       // for backward compatibility:
91       String allNewProducers = MirGlobal.config().getString("Mir.Localizer.Producer.AllNewProducers");
92       if (allNewProducers!=null && allNewProducers.length()>0) {
93         producerRecipes.put("allnew", ProducerEngine.ProducerTask.parseProducerTaskList(allNewProducers));
94
95         if (!producerRecipeNames.contains("allnew")) {
96           producerRecipeNames.add("allnew");
97         }
98       }
99
100       model = MirGlobal.localizer().dataModel().adapterModel();
101       generatorLibrary = MirGlobal.localizer().generators().makeProducerGeneratorLibrary();
102       writerEngine = MirGlobal.localizer().generators().makeWriterEngine();
103     }
104     catch (Throwable t) {
105       logger.error("MirBasicProducerLocalizer(): Exception "+t.getMessage());
106       model = new EntityAdapterModel();
107     }
108   }
109
110   public List getRecipeNames() throws MirLocalizerExc, MirLocalizerFailure {
111     return producerRecipeNames;
112   }
113
114   public void produceRecipe(String aName) throws MirLocalizerExc, MirLocalizerFailure {
115     if (producerRecipes.containsKey(aName)) {
116       Iterator i = ((List) producerRecipes.get(aName)).iterator();
117
118       while (i.hasNext()) {
119         ProducerEngine.ProducerTask task = (ProducerEngine.ProducerTask) i.next();
120
121         try {
122           MirGlobal.getProducerEngine().addTask(task);
123         }
124         catch (Throwable t) {
125           logger.error("Error recipe "+aName+" tasks "+
126               task.getProducer()+"::"+task.getVerb()+": " + t.toString());
127         }
128       }
129     }
130     else
131       throw new MirLocalizerExc("Unknown recipe name: " + aName);
132   }
133
134   /**
135    * Loads factories from a file with a {@link ProducerConfigReader}
136    */
137   public List loadFactories() throws MirLocalizerExc {
138       try {
139         List producers = new Vector();
140         ProducerConfigReader reader;
141         ProducerNodeBuilderLibrary library = new ProducerNodeBuilderLibrary();
142         setupProducerNodeBuilderLibrary(library);
143         reader = new ProducerConfigReader();
144         File inputFile =
145             MirGlobal.config().getFile("Mir.Localizer.ProducerConfigFile");
146         reader.parse(inputFile, library, producers);
147
148         logger.info("MirBasicProducerLocalizer.loadFactories(): successfully loaded factories");
149
150         return producers;
151       }
152       catch (Throwable t) {
153         logger.error("MirBasicProducerLocalizer.loadFactories(): Unable to load factories: "+
154             t.getMessage());
155         throw new MirLocalizerFailure(t);
156       }
157   };
158
159   /**
160    * Sets up a {@link ProducerNodeBuilderLibrary} for use by the producer
161    * definition reader. Can be overridden by subclasses to tweak the standard
162    * library.
163    */
164   protected void setupProducerNodeBuilderLibrary(ProducerNodeBuilderLibrary aLibrary) throws MirLocalizerFailure {
165     try {
166       DefaultProducerNodeBuilders.registerBuilders(
167           aLibrary, model, generatorLibrary, writerEngine,
168           MirGlobal.config().getHome(), MirGlobal.config().getFile("Producer.StorageRoot"));
169       SupplementalProducerNodeBuilders.registerBuilders(aLibrary, MirGlobal.config().getHome());
170     }
171     catch (Throwable t) {
172       throw new MirLocalizerFailure(t.getMessage(), t);
173     }
174   }
175
176 }