c9461243f538a5aad192e3e6c15a6b6ba795d471
[mir.git] / source / mircoders / localizer / basic / MirBasicProducerAssistantLocalizer.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.util.GregorianCalendar;
33 import java.util.HashMap;
34 import java.util.Iterator;
35 import java.util.Map;
36
37 import mir.config.MirPropertiesConfiguration;
38 import mir.entity.adapter.EntityAdapter;
39 import mir.entity.adapter.EntityIteratorAdapter;
40 import mir.log.LoggerWrapper;
41 import mir.misc.StringUtil;
42 import mir.util.GeneratorDateTimeFunctions;
43 import mir.util.GeneratorExpressionFunctions;
44 import mir.util.GeneratorFormatAdapters;
45 import mir.util.GeneratorHTMLFunctions;
46 import mir.util.GeneratorIntegerFunctions;
47 import mir.util.GeneratorListFunctions;
48 import mir.util.GeneratorRegularExpressionFunctions;
49 import mir.util.GeneratorStringFunctions;
50 import mircoders.global.MirGlobal;
51 import mircoders.localizer.MirLocalizerExc;
52 import mircoders.localizer.MirLocalizerFailure;
53 import mircoders.localizer.MirProducerAssistantLocalizer;
54
55 public class MirBasicProducerAssistantLocalizer implements MirProducerAssistantLocalizer {
56   protected LoggerWrapper logger;
57
58   public void initializeGenerationValueSet(Map aValueSet) throws MirLocalizerExc, MirLocalizerFailure  {
59     try {
60       Iterator i;
61
62       Map configMap = new HashMap();
63       Map utilityMap = new HashMap();
64
65       logger = new LoggerWrapper("Localizer.ProducerAssistant");
66
67 // obsolete:
68       configMap.put("producerDocRoot", MirGlobal.config().getString("Producer.DocRoot"));
69       configMap.put("storageRoot", MirGlobal.config().getString("Producer.StorageRoot"));
70       configMap.put("productionHost", MirGlobal.config().getString("Producer.ProductionHost"));
71       configMap.put("openAction", MirGlobal.config().getString("Producer.OpenAction"));
72       configMap.put("docRoot", MirGlobal.config().getString("RootUri"));
73       configMap.put("actionRoot", MirGlobal.config().getString("RootUri") + "/servlet/Mir");
74       configMap.put("now", new GeneratorFormatAdapters.DateFormatAdapter(new GregorianCalendar().getTime(), MirGlobal.config().getString("Mir.DefaultTimezone")));
75       configMap.put("videoHost", MirGlobal.config().getString("Producer.Video.Host"));
76       configMap.put("audioHost", MirGlobal.config().getString("Producer.Audio.Host"));
77       configMap.put("imageHost", MirGlobal.config().getString("Producer.Image.Host"));
78       configMap.put("imagePath", MirGlobal.config().getString("Producer.Image.Path"));
79       configMap.put("mirVersion", MirGlobal.config().getString("Mir.Version"));
80       configMap.put("defEncoding", MirGlobal.config().getString("Mir.DefaultEncoding"));
81
82 // "new":
83       configMap.putAll(MirPropertiesConfiguration.instance().allSettings());
84
85       utilityMap.put("compressWhitespace", new freemarker.template.utility.CompressWhitespace());
86       utilityMap.put("encodeHTML", new GeneratorHTMLFunctions.encodeHTMLGeneratorFunction());
87       utilityMap.put("prettyEncodeHTML", new GeneratorHTMLFunctions.prettyEncodeHTMLGeneratorFunction());
88       utilityMap.put("encodeXML", new GeneratorHTMLFunctions.encodeXMLGeneratorFunction());
89       utilityMap.put("encodeURI", new GeneratorHTMLFunctions.encodeURIGeneratorFunction());
90       utilityMap.put("subString", new GeneratorStringFunctions.subStringFunction());
91       utilityMap.put("subList", new GeneratorListFunctions.subListFunction());
92       utilityMap.put("listSize", new GeneratorListFunctions.listSizeFunction());
93       utilityMap.put("isOdd", new GeneratorIntegerFunctions.isOddFunction());
94       utilityMap.put("increment", new GeneratorIntegerFunctions.incrementFunction());
95       utilityMap.put("evaluate", new GeneratorExpressionFunctions.evaluateExpressionFunction());
96       utilityMap.put("constructString", new GeneratorStringFunctions.constructStructuredStringFunction());
97       utilityMap.put("parseStructuredString", new GeneratorStringFunctions.structuredStringParserFunction());
98       utilityMap.put("escapeJDBCString", new GeneratorStringFunctions.jdbcStringEscapeFunction());
99       utilityMap.put("regexpreplace", new GeneratorRegularExpressionFunctions.regularExpressionReplaceFunction());
100       utilityMap.put("regexpmatch", new GeneratorRegularExpressionFunctions.regularExpressionMatchFunction());
101       utilityMap.put("datetime", new GeneratorDateTimeFunctions.DateTimeFunctions(
102           MirPropertiesConfiguration.instance().getString("Mir.DefaultTimezone")));
103
104       aValueSet.put("config", configMap);
105       aValueSet.put("utility", utilityMap);
106
107       aValueSet.put("languages",
108         new EntityIteratorAdapter("", "", 20, MirGlobal.localizer().dataModel().adapterModel(), "language"));
109
110       aValueSet.put("topics",
111         new EntityIteratorAdapter("", "", 20, MirGlobal.localizer().dataModel().adapterModel(), "topic"));
112
113       Map articleTypeMap = new HashMap();
114       articleTypeMap.put("openposting", "0");
115       articleTypeMap.put("newswire", "1");
116       articleTypeMap.put("feature", "2");
117       articleTypeMap.put("topicspecial", "3");
118       articleTypeMap.put("startspecial", "4");
119
120       i = new EntityIteratorAdapter("", "", 20, MirGlobal.localizer().dataModel().adapterModel(), "articleType");
121       while (i.hasNext()) {
122         EntityAdapter articleType = (EntityAdapter) i.next();
123
124         articleTypeMap.put(articleType.get("name"), articleType.get("id"));
125       }
126       aValueSet.put("articletype", articleTypeMap);
127
128       Map commentStatusMap = new HashMap();
129       i = new EntityIteratorAdapter("", "", 20, MirGlobal.localizer().dataModel().adapterModel(), "commentStatus");
130       while (i.hasNext()) {
131         EntityAdapter commentStatus = (EntityAdapter) i.next();
132
133         commentStatusMap.put(commentStatus.get("name"), commentStatus.get("id"));
134       }
135       aValueSet.put("commentstatus", commentStatusMap);
136     }
137     catch (Throwable t) {
138       logger.error("initializeGenerationValueSet: Exception while collecting comment statuses" + t.getMessage());
139       throw new RuntimeException(t.getMessage());
140     }
141
142   };
143
144   public String filterNonHTMLText(String aText) {
145
146     logger.debug("about to filter non HTML Text of length " + aText.length());
147     try {
148       String result =
149           StringUtil.createHTML(
150           StringUtil.removeHTMLTags(aText),
151           MirGlobal.config().getString("Producer.ImageRoot"),
152           MirGlobal.config().getString("Producer.MailLinkName"),
153           MirGlobal.config().getString("Producer.ExtLinkName"),
154           MirGlobal.config().getString("Producer.IntLinkName")
155           );
156       logger.debug("done filtering non-HTML text ");
157       return result;
158     }
159     catch (Throwable t) {
160       logger.error("error while filtering non-HTML text: " + t.toString());
161
162       throw new RuntimeException(t.toString());
163     }
164   }
165
166   public String filterHTMLText(String aText) {
167     return StringUtil.deleteForbiddenTags(aText);
168   }
169 }