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;
43 import mir.config.MirPropertiesConfiguration;
44 import mir.entity.adapter.EntityAdapter;
45 import mir.entity.adapter.EntityIteratorAdapter;
46 import mir.generator.Generator;
47 import mir.generator.GeneratorExc;
48 import mir.generator.GeneratorFailure;
49 import mir.log.LoggerWrapper;
50 import mir.misc.StringUtil;
51 import mir.util.GeneratorDateTimeFunctions;
52 import mir.util.GeneratorFormatAdapters;
53 import mir.util.generator.ReflectionGeneratorFunctionsAdapter;
54 import mir.util.StringRoutines;
55 import mircoders.global.MirGlobal;
56 import mircoders.localizer.MirLocalizerExc;
57 import mircoders.localizer.MirLocalizerFailure;
58 import mircoders.localizer.MirProducerAssistantLocalizer;
60 import org.w3c.dom.Document;
61 import org.w3c.dom.NamedNodeMap;
62 import org.w3c.dom.Node;
63 import org.w3c.dom.NodeList;
64 import org.w3c.tidy.Configuration;
65 import org.w3c.tidy.Tidy;
67 public class MirBasicProducerAssistantLocalizer implements MirProducerAssistantLocalizer {
68 protected LoggerWrapper logger;
70 private RE regularExpressionLT;
71 private RE regularExpressionGT;
73 public MirBasicProducerAssistantLocalizer() throws MirLocalizerFailure {
75 regularExpressionLT = new RE("<");
76 regularExpressionGT = new RE(">");
79 throw new MirLocalizerFailure(t);
83 public void initializeGenerationValueSet(Map aValueSet) throws MirLocalizerExc, MirLocalizerFailure {
87 Map configMap = new HashMap();
89 logger = new LoggerWrapper("Localizer.ProducerAssistant");
92 configMap.put("producerDocRoot", MirGlobal.config().getString("Producer.DocRoot"));
93 configMap.put("storageRoot", MirGlobal.config().getString("Producer.StorageRoot"));
94 configMap.put("productionHost", MirGlobal.config().getString("Producer.ProductionHost"));
95 configMap.put("openAction", MirGlobal.config().getString("Producer.OpenAction"));
96 configMap.put("docRoot", MirGlobal.config().getString("RootUri"));
97 configMap.put("actionRoot", MirGlobal.config().getString("RootUri") + "/servlet/Mir");
98 configMap.put("now", new GeneratorFormatAdapters.DateFormatAdapter(new GregorianCalendar().getTime(), MirGlobal.config().getString("Mir.DefaultTimezone")));
99 configMap.put("videoHost", MirGlobal.config().getString("Producer.Video.Host"));
100 configMap.put("audioHost", MirGlobal.config().getString("Producer.Audio.Host"));
101 configMap.put("imageHost", MirGlobal.config().getString("Producer.Image.Host"));
102 configMap.put("imagePath", MirGlobal.config().getString("Producer.Image.Path"));
103 configMap.put("mirVersion", MirGlobal.config().getString("Mir.Version"));
104 configMap.put("defEncoding", MirGlobal.config().getString("Mir.DefaultEncoding"));
107 configMap.putAll(MirPropertiesConfiguration.instance().allSettings());
109 aValueSet.put("config", configMap);
111 aValueSet.put("utility", new Utility());
113 aValueSet.put("languages",
114 new EntityIteratorAdapter("", "", 20, MirGlobal.localizer().dataModel().adapterModel(), "language"));
116 aValueSet.put("topics",
117 new EntityIteratorAdapter("", "", 20, MirGlobal.localizer().dataModel().adapterModel(), "topic"));
119 Map articleTypeMap = new HashMap();
120 articleTypeMap.put("openposting", "0");
121 articleTypeMap.put("newswire", "1");
122 articleTypeMap.put("feature", "2");
123 articleTypeMap.put("topicspecial", "3");
124 articleTypeMap.put("startspecial", "4");
126 i = new EntityIteratorAdapter("", "", 20, MirGlobal.localizer().dataModel().adapterModel(), "articleType");
127 while (i.hasNext()) {
128 EntityAdapter articleType = (EntityAdapter) i.next();
130 articleTypeMap.put(articleType.get("name"), articleType.get("id"));
132 aValueSet.put("articletype", articleTypeMap);
134 Map commentStatusMap = new HashMap();
135 i = new EntityIteratorAdapter("", "", 20, MirGlobal.localizer().dataModel().adapterModel(), "commentStatus");
136 while (i.hasNext()) {
137 EntityAdapter commentStatus = (EntityAdapter) i.next();
139 commentStatusMap.put(commentStatus.get("name"), commentStatus.get("id"));
141 aValueSet.put("commentstatus", commentStatusMap);
142 aValueSet.put("languageCodeToId", new getLanguageIdFunction());
144 catch (Throwable t) {
145 logger.error("initializeGenerationValueSet: Exception while collecting comment statuses" + t.getMessage());
147 throw new MirLocalizerFailure(t);
152 public static class getLanguageIdFunction implements Generator.Function {
153 private Map languageCodeToId;
154 private String otherLanguageId;
155 private LoggerWrapper logger = new LoggerWrapper("Localizer.Earth.getLanguageIdFunction");
157 public getLanguageIdFunction() throws MirLocalizerFailure {
159 otherLanguageId = "";
160 languageCodeToId = new HashMap();
162 Iterator i = new EntityIteratorAdapter("", "", 20, MirGlobal.localizer().dataModel().adapterModel(), "language");
163 while (i.hasNext()) {
164 EntityAdapter language = (EntityAdapter) i.next();
165 if (language.get("code").equals("ot"))
166 otherLanguageId = (String) language.get("id");
168 languageCodeToId.put(language.get("code"), language.get("id"));
171 catch (Throwable t) {
172 logger.error(t.toString());
174 throw new MirLocalizerFailure(t);
178 public Object perform(List aParameters) throws GeneratorExc, GeneratorFailure {
180 if (aParameters.size() != 1)
181 throw new GeneratorExc("getLanguageIdFunction: 1 parameter expected: language-code");
183 String result = (String) languageCodeToId.get(aParameters.get(0));
185 result = otherLanguageId;
189 catch (GeneratorExc e) {
192 catch (Throwable t) {
193 throw new GeneratorFailure("getLanguageIdFunction: " + t.getMessage(), t);
199 public String filterNonHTMLText(String aText) {
201 logger.debug("about to filter non HTML Text of length " + aText.length());
204 StringUtil.createHTML(
205 StringUtil.removeHTMLTags(aText),
206 MirGlobal.config().getString("Producer.ImageRoot"),
207 MirGlobal.config().getString("Producer.MailLinkName"),
208 MirGlobal.config().getString("Producer.ExtLinkName"),
209 MirGlobal.config().getString("Producer.IntLinkName")
211 logger.debug("done filtering non-HTML text ");
214 catch (Throwable t) {
215 logger.error("error while filtering non-HTML text: " + t.toString());
217 throw new RuntimeException(t.toString());
220 public String filterHTMLText(String aText) {
222 StringWriter out = new StringWriter();
223 Tidy tidy = new Tidy();
224 ByteArrayInputStream in = new ByteArrayInputStream(aText.getBytes("UTF8"));
225 tidy.setMakeClean(true);
226 tidy.setCharEncoding(Configuration.UTF8);
227 tidy.setErrout(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
228 print(tidy.parseDOM(in, null), out);
230 return out.toString();
232 catch (IOException e) {
233 return e.getMessage();
237 private boolean checkAttr(String attrName) {
238 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"))
244 private boolean checkNode(String nodeName) {
245 List languages = StringRoutines.splitString(MirGlobal.config().getString("Localizer.HTML.Whitelist"), ";");
247 Iterator i = languages.iterator();
248 while (i.hasNext()) {
249 if (nodeName.equals(i.next()))
255 private void print(Node node, StringWriter out) throws IOException {
259 int type = node.getNodeType();
260 boolean canOutput = checkNode(node.getNodeName());
264 case Node.DOCUMENT_NODE:
266 print(((Document) node).getDocumentElement(), out);
270 case Node.ELEMENT_NODE:
274 out.write(node.getNodeName());
275 NamedNodeMap attrs = node.getAttributes();
277 for (int i = 0; i < attrs.getLength(); i++) {
278 String attrName = attrs.item(i).getNodeName();
279 if (checkAttr(attrName)) {
281 out.write(attrs.item(i).getNodeName());
284 out.write(attrs.item(i).getNodeValue());
289 if (node.getChildNodes()==null || node.getChildNodes().getLength()==0) {
294 NodeList children = node.getChildNodes();
295 if (children != null) {
296 int len = children.getLength();
297 for (int i = 0; i < len; i++) {
298 print(children.item(i), out);
304 String value=node.getNodeValue();
306 value=regularExpressionLT.substituteAll(value, "<");
307 value=regularExpressionGT.substituteAll(value, ">");
318 if (type == Node.ELEMENT_NODE && canOutput && node.getChildNodes()!=null && node.getChildNodes().getLength()>0) {
320 out.write(node.getNodeName());
327 public static class Utility extends ReflectionGeneratorFunctionsAdapter {
329 super(new MirBasicUtilityFunctions());
332 public Object getDatetime() {
333 return new GeneratorDateTimeFunctions.DateTimeFunctions(
334 MirPropertiesConfiguration.instance().getString("Mir.DefaultTimezone"));
337 public Object getCompressWhitespace() {
338 return new freemarker.template.utility.CompressWhitespace();