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;
32 import java.io.ByteArrayInputStream;
33 import java.io.IOException;
34 import java.io.StringWriter;
35 import java.util.GregorianCalendar;
36 import java.util.HashMap;
37 import java.util.Iterator;
38 import java.util.List;
41 import mir.config.MirPropertiesConfiguration;
42 import mir.entity.adapter.EntityAdapter;
43 import mir.entity.adapter.EntityIteratorAdapter;
44 import mir.generator.Generator;
45 import mir.generator.GeneratorExc;
46 import mir.generator.GeneratorFailure;
47 import mir.log.LoggerWrapper;
48 import mir.misc.StringUtil;
49 import mir.util.GeneratorDateTimeFunctions;
50 import mir.util.GeneratorFormatAdapters;
51 import mir.util.generator.ReflectionGeneratorFunctionsAdapter;
52 import mircoders.global.MirGlobal;
53 import mircoders.localizer.MirLocalizerExc;
54 import mircoders.localizer.MirLocalizerFailure;
55 import mircoders.localizer.MirProducerAssistantLocalizer;
57 import org.w3c.dom.Document;
58 import org.w3c.dom.NamedNodeMap;
59 import org.w3c.dom.Node;
60 import org.w3c.dom.NodeList;
61 import org.w3c.tidy.Configuration;
62 import org.w3c.tidy.Tidy;
64 public class MirBasicProducerAssistantLocalizer implements MirProducerAssistantLocalizer {
65 protected LoggerWrapper logger;
67 public void initializeGenerationValueSet(Map aValueSet) throws MirLocalizerExc, MirLocalizerFailure {
71 Map configMap = new HashMap();
73 logger = new LoggerWrapper("Localizer.ProducerAssistant");
76 configMap.put("producerDocRoot", MirGlobal.config().getString("Producer.DocRoot"));
77 configMap.put("storageRoot", MirGlobal.config().getString("Producer.StorageRoot"));
78 configMap.put("productionHost", MirGlobal.config().getString("Producer.ProductionHost"));
79 configMap.put("openAction", MirGlobal.config().getString("Producer.OpenAction"));
80 configMap.put("docRoot", MirGlobal.config().getString("RootUri"));
81 configMap.put("actionRoot", MirGlobal.config().getString("RootUri") + "/servlet/Mir");
82 configMap.put("now", new GeneratorFormatAdapters.DateFormatAdapter(new GregorianCalendar().getTime(), MirGlobal.config().getString("Mir.DefaultTimezone")));
83 configMap.put("videoHost", MirGlobal.config().getString("Producer.Video.Host"));
84 configMap.put("audioHost", MirGlobal.config().getString("Producer.Audio.Host"));
85 configMap.put("imageHost", MirGlobal.config().getString("Producer.Image.Host"));
86 configMap.put("imagePath", MirGlobal.config().getString("Producer.Image.Path"));
87 configMap.put("mirVersion", MirGlobal.config().getString("Mir.Version"));
88 configMap.put("defEncoding", MirGlobal.config().getString("Mir.DefaultEncoding"));
91 configMap.putAll(MirPropertiesConfiguration.instance().allSettings());
93 aValueSet.put("config", configMap);
95 aValueSet.put("utility", new Utility());
97 aValueSet.put("languages",
98 new EntityIteratorAdapter("", "", 20, MirGlobal.localizer().dataModel().adapterModel(), "language"));
100 aValueSet.put("topics",
101 new EntityIteratorAdapter("", "", 20, MirGlobal.localizer().dataModel().adapterModel(), "topic"));
103 Map articleTypeMap = new HashMap();
104 articleTypeMap.put("openposting", "0");
105 articleTypeMap.put("newswire", "1");
106 articleTypeMap.put("feature", "2");
107 articleTypeMap.put("topicspecial", "3");
108 articleTypeMap.put("startspecial", "4");
110 i = new EntityIteratorAdapter("", "", 20, MirGlobal.localizer().dataModel().adapterModel(), "articleType");
111 while (i.hasNext()) {
112 EntityAdapter articleType = (EntityAdapter) i.next();
114 articleTypeMap.put(articleType.get("name"), articleType.get("id"));
116 aValueSet.put("articletype", articleTypeMap);
118 Map commentStatusMap = new HashMap();
119 i = new EntityIteratorAdapter("", "", 20, MirGlobal.localizer().dataModel().adapterModel(), "commentStatus");
120 while (i.hasNext()) {
121 EntityAdapter commentStatus = (EntityAdapter) i.next();
123 commentStatusMap.put(commentStatus.get("name"), commentStatus.get("id"));
125 aValueSet.put("commentstatus", commentStatusMap);
126 aValueSet.put("languageCodeToId", new getLanguageIdFunction());
128 catch (Throwable t) {
129 logger.error("initializeGenerationValueSet: Exception while collecting comment statuses" + t.getMessage());
131 throw new MirLocalizerFailure(t);
136 public static class getLanguageIdFunction implements Generator.Function {
137 private Map languageCodeToId;
138 private String otherLanguageId;
139 private LoggerWrapper logger = new LoggerWrapper("Localizer.Earth.getLanguageIdFunction");
141 public getLanguageIdFunction() throws MirLocalizerFailure {
143 otherLanguageId = "";
144 languageCodeToId = new HashMap();
146 Iterator i = new EntityIteratorAdapter("", "", 20, MirGlobal.localizer().dataModel().adapterModel(), "language");
147 while (i.hasNext()) {
148 EntityAdapter language = (EntityAdapter) i.next();
149 if (language.get("code").equals("ot"))
150 otherLanguageId = (String) language.get("id");
152 languageCodeToId.put(language.get("code"), language.get("id"));
155 catch (Throwable t) {
156 logger.error(t.toString());
158 throw new MirLocalizerFailure(t);
162 public Object perform(List aParameters) throws GeneratorExc, GeneratorFailure {
164 if (aParameters.size() != 1)
165 throw new GeneratorExc("getLanguageIdFunction: 1 parameter expected: language-code");
167 String result = (String) languageCodeToId.get(aParameters.get(0));
169 result = otherLanguageId;
173 catch (GeneratorExc e) {
176 catch (Throwable t) {
177 throw new GeneratorFailure("getLanguageIdFunction: " + t.getMessage(), t);
183 public String filterNonHTMLText(String aText) {
185 logger.debug("about to filter non HTML Text of length " + aText.length());
188 StringUtil.createHTML(
189 StringUtil.removeHTMLTags(aText),
190 MirGlobal.config().getString("Producer.ImageRoot"),
191 MirGlobal.config().getString("Producer.MailLinkName"),
192 MirGlobal.config().getString("Producer.ExtLinkName"),
193 MirGlobal.config().getString("Producer.IntLinkName")
195 logger.debug("done filtering non-HTML text ");
198 catch (Throwable t) {
199 logger.error("error while filtering non-HTML text: " + t.toString());
201 throw new RuntimeException(t.toString());
204 public String filterHTMLText(String aText) {
206 StringWriter out = new StringWriter();
207 Tidy tidy = new Tidy();
208 ByteArrayInputStream in = new ByteArrayInputStream(aText.getBytes("UTF8"));
209 tidy.setMakeClean(true);
210 tidy.setCharEncoding(Configuration.UTF8);
211 tidy.setErrout(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
212 print(tidy.parseDOM(in, null), out);
214 return out.toString();
216 catch (IOException e) {
217 return e.getMessage();
221 private boolean checkAttr(String attrName) {
222 if (attrName.equals("onLoad") || attrName.equals("onClick") || attrName.equals("onFocus") || attrName.equals("onBlur") || attrName.equals("onMouseOver") || attrName.equals("onMouseOut") || attrName.equals("style") || attrName.equals("STYLE") || attrName.equals("height") || attrName.equals("width") || attrName.equals("HEIGHT") || attrName.equals("WIDTH"))
228 private boolean checkNode(String nodeName) {
229 if (nodeName.equals("a") ||
230 nodeName.equals("img") ||
231 nodeName.equals("h1") ||
232 nodeName.equals("h2") ||
233 nodeName.equals("h3") ||
234 nodeName.equals("h4") ||
235 nodeName.equals("h5") ||
236 nodeName.equals("h6") ||
237 nodeName.equals("br") ||
238 nodeName.equals("form") ||
239 nodeName.equals("input") ||
240 nodeName.equals("hr") ||
241 nodeName.equals("strong") ||
242 nodeName.equals("font") ||
243 nodeName.equals("b") ||
244 nodeName.equals("i") ||
245 nodeName.equals("em") ||
246 nodeName.equals("p") ||
247 nodeName.equals("table") ||
248 nodeName.equals("tr") ||
249 nodeName.equals("td") ||
250 nodeName.equals("th") ||
251 nodeName.equals("ul") ||
252 nodeName.equals("ol") ||
253 nodeName.equals("li")
260 private void print(Node node, StringWriter out) throws IOException {
264 int type = node.getNodeType();
265 boolean canOutput = checkNode(node.getNodeName());
269 case Node.DOCUMENT_NODE:
271 print(((Document) node).getDocumentElement(), out);
275 case Node.ELEMENT_NODE:
279 out.write(node.getNodeName());
280 NamedNodeMap attrs = node.getAttributes();
282 for (int i = 0; i < attrs.getLength(); i++) {
283 String attrName = attrs.item(i).getNodeName();
284 if (checkAttr(attrName)) {
286 out.write(attrs.item(i).getNodeName());
289 out.write(attrs.item(i).getNodeValue());
294 if (node.getChildNodes()==null || node.getChildNodes().getLength()==0) {
299 NodeList children = node.getChildNodes();
300 if (children != null) {
301 int len = children.getLength();
302 for (int i = 0; i < len; i++) {
303 print(children.item(i), out);
309 out.write(node.getNodeValue());
314 if (type == Node.ELEMENT_NODE && canOutput && node.getChildNodes()!=null && node.getChildNodes().getLength()>0) {
316 out.write(node.getNodeName());
323 public static class Utility extends ReflectionGeneratorFunctionsAdapter {
325 super(new MirBasicUtilityFunctions());
327 public Object getDatetime() {
328 return new GeneratorDateTimeFunctions.DateTimeFunctions(
329 MirPropertiesConfiguration.instance().getString("Mir.DefaultTimezone"));
332 public Object getCompressWhitespace() {
333 return new freemarker.template.utility.CompressWhitespace();