2 * Copyright (C) 2001, 2002 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 * 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.
30 package mircoders.localizer.basic;
33 import java.util.ArrayList;
34 import java.util.HashMap;
35 import java.util.Iterator;
36 import java.util.List;
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;
54 public class MirBasicProducerLocalizer implements MirProducerLocalizer {
55 private Map producerRecipes;
56 private List producerRecipeNames;
58 protected EntityAdapterModel model;
59 protected Generator.Library generatorLibrary;
60 protected WriterEngine writerEngine;
62 protected LoggerWrapper logger;
64 public MirBasicProducerLocalizer() {
66 logger = new LoggerWrapper("Localizer.Basic.Producer");
68 producerRecipes = new HashMap();
69 producerRecipeNames = new ArrayList();
71 String[] recipes = MirGlobal.config().getStringArray("Mir.Localizer.Producer.ProducerRecipes");
72 for (int i = 0; i<recipes.length; i++) {
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);
82 throw new Exception("'=' expected");
86 logger.error("Error while processing producer recipe '" + recipes[i] + "': " + t.toString());
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));
95 if (!producerRecipeNames.contains("allnew")) {
96 producerRecipeNames.add("allnew");
100 model = MirGlobal.localizer().dataModel().adapterModel();
101 generatorLibrary = MirGlobal.localizer().generators().makeProducerGeneratorLibrary();
102 writerEngine = MirGlobal.localizer().generators().makeWriterEngine();
104 catch (Throwable t) {
105 logger.error("MirBasicProducerLocalizer(): Exception "+t.getMessage());
106 model = new EntityAdapterModel();
110 public List getRecipeNames() throws MirLocalizerExc, MirLocalizerFailure {
111 return producerRecipeNames;
114 public void produceRecipe(String aName) throws MirLocalizerExc, MirLocalizerFailure {
115 if (producerRecipes.containsKey(aName)) {
116 Iterator i = ((List) producerRecipes.get(aName)).iterator();
118 while (i.hasNext()) {
119 ProducerEngine.ProducerTask task = (ProducerEngine.ProducerTask) i.next();
122 MirGlobal.getProducerEngine().addTask(task);
124 catch (Throwable t) {
125 logger.error("Error recipe "+aName+" tasks "+
126 task.getProducer()+"::"+task.getVerb()+": " + t.toString());
131 throw new MirLocalizerExc("Unknown recipe name: " + aName);
134 public void beforeProducerTask(String aFactory, String aVerb) {
137 public void afterProducerTask(String aFactory, String aVerb) {
141 * Loads factories from a file with a {@link ProducerConfigReader}
143 public List loadFactories() throws MirLocalizerExc {
145 List producers = new ArrayList();
146 ProducerConfigReader reader;
147 ProducerNodeBuilderLibrary library = new ProducerNodeBuilderLibrary();
148 setupProducerNodeBuilderLibrary(library);
149 reader = new ProducerConfigReader();
151 MirGlobal.config().getFile("Mir.Localizer.ProducerConfigFile");
152 reader.parse(inputFile, library, producers);
154 logger.info("MirBasicProducerLocalizer.loadFactories(): successfully loaded factories");
158 catch (Throwable t) {
159 logger.error("MirBasicProducerLocalizer.loadFactories(): Unable to load factories: "+
161 throw new MirLocalizerFailure(t);
166 * Sets up a {@link ProducerNodeBuilderLibrary} for use by the producer
167 * definition reader. Can be overridden by subclasses to tweak the standard
170 protected void setupProducerNodeBuilderLibrary(ProducerNodeBuilderLibrary aLibrary) throws MirLocalizerFailure {
172 DefaultProducerNodeBuilders.registerBuilders(
173 aLibrary, model, generatorLibrary, writerEngine,
174 MirGlobal.config().getHome(), MirGlobal.config().getFile("Producer.StorageRoot"));
175 SupplementalProducerNodeBuilders.registerBuilders(aLibrary, MirGlobal.config().getHome());
177 catch (Throwable t) {
178 throw new MirLocalizerFailure(t.getMessage(), t);