a1a0982ed91dfcc4ab97f67035417a35e353c548
[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.*;
34
35 import mir.entity.adapter.EntityAdapterModel;
36 import mir.generator.Generator;
37 import mir.generator.WriterEngine;
38 import mir.log.LoggerWrapper;
39 import mir.producer.reader.DefaultProducerNodeBuilders;
40 import mir.producer.reader.ProducerConfigReader;
41 import mir.producer.reader.ProducerNodeBuilderLibrary;
42 import mir.util.StringRoutines;
43 import mircoders.global.MirGlobal;
44 import mircoders.global.ProducerEngine;
45 import mircoders.localizer.MirLocalizerExc;
46 import mircoders.localizer.MirLocalizerFailure;
47 import mircoders.localizer.MirProducerLocalizer;
48 import mircoders.producer.reader.SupplementalProducerNodeBuilders;
49
50 public class MirBasicProducerLocalizer implements MirProducerLocalizer {
51   private Map producerRecipes;
52   private List producerRecipeNames;
53
54   protected EntityAdapterModel model;
55   protected Generator.Library generatorLibrary;
56   protected WriterEngine writerEngine;
57
58   protected LoggerWrapper logger;
59
60   public MirBasicProducerLocalizer() {
61     try {
62       logger = new LoggerWrapper("Localizer.Basic.Producer");
63
64       producerRecipes = new HashMap();
65       producerRecipeNames = new ArrayList();
66
67       String[] recipes = MirGlobal.config().getStringArray("Mir.Localizer.Producer.ProducerRecipes");
68       for (int i = 0; i<recipes.length; i++) {
69         try {
70           List parts = StringRoutines.separateString(recipes[i], "=");
71           if (parts.size() == 2) {
72             String key = ((String) parts.get(0)).trim();
73             producerRecipes.put(key, ProducerEngine.ProducerTask.parseProducerTaskList(
74                 ((String) parts.get(1)).trim()));
75             producerRecipeNames.add(key);
76           }
77           else {
78             throw new Exception("'=' expected");
79           }
80         }
81         catch (Throwable t) {
82           logger.error("Error while processing producer recipe '" + recipes[i] + "': " + t.toString());
83         }
84       }
85
86       // for backward compatibility:
87       String allNewProducers = MirGlobal.config().getString("Mir.Localizer.Producer.AllNewProducers");
88       if (allNewProducers!=null && allNewProducers.length()>0) {
89         producerRecipes.put("allnew", ProducerEngine.ProducerTask.parseProducerTaskList(allNewProducers));
90
91         if (!producerRecipeNames.contains("allnew")) {
92           producerRecipeNames.add("allnew");
93         }
94       }
95
96       model = MirGlobal.localizer().dataModel().adapterModel();
97       generatorLibrary = MirGlobal.localizer().generators().makeProducerGeneratorLibrary();
98       writerEngine = MirGlobal.localizer().generators().makeWriterEngine();
99     }
100     catch (Throwable t) {
101       logger.error("MirBasicProducerLocalizer(): Exception "+t.getMessage());
102       model = new EntityAdapterModel();
103     }
104   }
105
106   public List getRecipeNames() throws MirLocalizerExc, MirLocalizerFailure {
107     return producerRecipeNames;
108   }
109
110   public void produceRecipe(String aName) throws MirLocalizerExc, MirLocalizerFailure {
111     if (producerRecipes.containsKey(aName)) {
112       Iterator i = ((List) producerRecipes.get(aName)).iterator();
113
114       while (i.hasNext()) {
115         ProducerEngine.ProducerTask task = (ProducerEngine.ProducerTask) i.next();
116
117         try {
118           MirGlobal.getProducerEngine().addTask(task);
119         }
120         catch (Throwable t) {
121           logger.error("Error recipe "+aName+" tasks "+
122               task.getProducer()+"::"+task.getVerb()+": " + t.toString());
123         }
124       }
125     }
126     else
127       throw new MirLocalizerExc("Unknown recipe name: " + aName);
128   }
129
130   /**
131    * Loads factories from a file with a {@link ProducerConfigReader}
132    */
133   public List loadFactories() throws MirLocalizerExc {
134       try {
135         List producers = new ArrayList();
136         ProducerConfigReader reader;
137         ProducerNodeBuilderLibrary library = new ProducerNodeBuilderLibrary();
138         setupProducerNodeBuilderLibrary(library);
139         reader = new ProducerConfigReader();
140         File inputFile =
141             MirGlobal.config().getFile("Mir.Localizer.ProducerConfigFile");
142         reader.parse(inputFile, library, producers);
143
144         logger.info("MirBasicProducerLocalizer.loadFactories(): successfully loaded factories");
145
146         return producers;
147       }
148       catch (Throwable t) {
149         logger.error("MirBasicProducerLocalizer.loadFactories(): Unable to load factories: "+
150             t.getMessage());
151         throw new MirLocalizerFailure(t);
152       }
153   };
154
155   /**
156    * Sets up a {@link ProducerNodeBuilderLibrary} for use by the producer
157    * definition reader. Can be overridden by subclasses to tweak the standard
158    * library.
159    */
160   protected void setupProducerNodeBuilderLibrary(ProducerNodeBuilderLibrary aLibrary) throws MirLocalizerFailure {
161     try {
162       DefaultProducerNodeBuilders.registerBuilders(
163           aLibrary, model, generatorLibrary, writerEngine,
164           MirGlobal.config().getHome(), MirGlobal.config().getFile("Producer.StorageRoot"));
165       SupplementalProducerNodeBuilders.registerBuilders(aLibrary, MirGlobal.config().getHome());
166     }
167     catch (Throwable t) {
168       throw new MirLocalizerFailure(t.getMessage(), t);
169     }
170   }
171
172 }