config attribute filters via config.propertieis./
[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 gnu.regexp.RE;
33 import mir.config.MirPropertiesConfiguration;
34 import mir.entity.adapter.EntityAdapter;
35 import mir.entity.adapter.EntityIteratorAdapter;
36 import mir.generator.Generator;
37 import mir.generator.GeneratorExc;
38 import mir.generator.GeneratorFailure;
39 import mir.log.LoggerWrapper;
40 import mir.util.GeneratorDateTimeFunctions;
41 import mir.util.GeneratorFormatAdapters;
42 import mir.util.HTMLStripper;
43 import mir.util.StringRoutines;
44 import mir.util.generator.ReflectionGeneratorFunctionsAdapter;
45 import mircoders.global.MirGlobal;
46 import mircoders.localizer.MirLocalizerExc;
47 import mircoders.localizer.MirLocalizerFailure;
48 import mircoders.localizer.MirProducerAssistantLocalizer;
49 import org.w3c.dom.Document;
50 import org.w3c.dom.NamedNodeMap;
51 import org.w3c.dom.Node;
52 import org.w3c.dom.NodeList;
53 import org.w3c.tidy.Configuration;
54 import org.w3c.tidy.Tidy;
55
56 import java.io.ByteArrayInputStream;
57 import java.io.IOException;
58 import java.io.StringWriter;
59 import java.util.GregorianCalendar;
60 import java.util.HashMap;
61 import java.util.Iterator;
62 import java.util.List;
63 import java.util.Map;
64
65 public class MirBasicProducerAssistantLocalizer implements MirProducerAssistantLocalizer {
66   protected LoggerWrapper logger;
67
68   private HTMLStripper stripper;
69   private RE regularExpressionLT;
70   private RE regularExpressionGT;
71   private RE regularExpressionWhitespace;
72   private RE regularExpressionLeadingSlashes;
73     
74
75   public MirBasicProducerAssistantLocalizer() throws MirLocalizerFailure {
76     try {
77       stripper = new HTMLStripper();
78
79       regularExpressionLT = new RE("<");
80       regularExpressionGT = new RE(">");
81       regularExpressionWhitespace = new RE("\\s+|&#x0A;|&#x0D;");
82       regularExpressionLeadingSlashes = new RE("^//+");
83     }
84     catch (Throwable t) {
85       throw new MirLocalizerFailure(t);
86     }
87   }
88
89   public void initializeGenerationValueSet(Map aValueSet) throws MirLocalizerExc, MirLocalizerFailure {
90     try {
91       Iterator i;
92
93       Map configMap = new HashMap();
94
95       logger = new LoggerWrapper("Localizer.ProducerAssistant");
96
97 // obsolete:
98       configMap.put("producerDocRoot", MirGlobal.config().getString("Producer.DocRoot"));
99       configMap.put("storageRoot", MirGlobal.config().getString("Producer.StorageRoot"));
100       configMap.put("productionHost", MirGlobal.config().getString("Producer.ProductionHost"));
101       configMap.put("openAction", MirGlobal.config().getString("Producer.OpenAction"));
102       configMap.put("docRoot", MirGlobal.config().getString("RootUri"));
103       configMap.put("actionRoot", MirGlobal.config().getString("RootUri") + "/servlet/Mir");
104       configMap.put("now", new GeneratorFormatAdapters.DateFormatAdapter(new GregorianCalendar().getTime(), MirGlobal.config().getString("Mir.DefaultTimezone")));
105       configMap.put("videoHost", MirGlobal.config().getString("Producer.Video.Host"));
106       configMap.put("audioHost", MirGlobal.config().getString("Producer.Audio.Host"));
107       configMap.put("imageHost", MirGlobal.config().getString("Producer.Image.Host"));
108       configMap.put("imagePath", MirGlobal.config().getString("Producer.Image.Path"));
109       configMap.put("mirVersion", MirGlobal.config().getString("Mir.Version"));
110       configMap.put("defEncoding", MirGlobal.config().getString("Mir.DefaultEncoding"));
111
112 // "new":
113       configMap.putAll(MirPropertiesConfiguration.instance().allSettings());
114
115       aValueSet.put("config", configMap);
116
117       aValueSet.put("utility", new Utility());
118
119       aValueSet.put("languages",
120           new EntityIteratorAdapter("", "", 20, MirGlobal.localizer().dataModel().adapterModel(), "language"));
121
122       aValueSet.put("topics",
123           new EntityIteratorAdapter("", "", 20, MirGlobal.localizer().dataModel().adapterModel(), "topic"));
124
125       Map articleTypeMap = new HashMap();
126       articleTypeMap.put("openposting", "0");
127       articleTypeMap.put("newswire", "1");
128       articleTypeMap.put("feature", "2");
129       articleTypeMap.put("topicspecial", "3");
130       articleTypeMap.put("startspecial", "4");
131
132       i = new EntityIteratorAdapter("", "", 20, MirGlobal.localizer().dataModel().adapterModel(), "articleType");
133       while (i.hasNext()) {
134         EntityAdapter articleType = (EntityAdapter) i.next();
135
136         articleTypeMap.put(articleType.get("name"), articleType.get("id"));
137       }
138       aValueSet.put("articletype", articleTypeMap);
139
140       Map commentStatusMap = new HashMap();
141       i = new EntityIteratorAdapter("", "", 20, MirGlobal.localizer().dataModel().adapterModel(), "commentStatus");
142       while (i.hasNext()) {
143         EntityAdapter commentStatus = (EntityAdapter) i.next();
144
145         commentStatusMap.put(commentStatus.get("name"), commentStatus.get("id"));
146       }
147       aValueSet.put("commentstatus", commentStatusMap);
148       aValueSet.put("languageCodeToId", new getLanguageIdFunction());
149     }
150     catch (Throwable t) {
151       logger.error("initializeGenerationValueSet: Exception while collecting comment statuses" + t.getMessage());
152
153       throw new MirLocalizerFailure(t);
154     }
155
156   }
157
158   public static class getLanguageIdFunction implements Generator.Function {
159     private Map languageCodeToId;
160     private String otherLanguageId;
161     private LoggerWrapper logger = new LoggerWrapper("Localizer.Earth.getLanguageIdFunction");
162
163     public getLanguageIdFunction() throws MirLocalizerFailure {
164       try {
165         otherLanguageId = "";
166         languageCodeToId = new HashMap();
167
168         Iterator i = new EntityIteratorAdapter("", "", 20, MirGlobal.localizer().dataModel().adapterModel(), "language");
169         while (i.hasNext()) {
170           EntityAdapter language = (EntityAdapter) i.next();
171           if (language.get("code").equals("ot")) {
172             otherLanguageId = (String) language.get("id");
173           }
174
175           languageCodeToId.put(language.get("code"), language.get("id"));
176         }
177       }
178       catch (Throwable t) {
179         logger.error(t.toString());
180
181         throw new MirLocalizerFailure(t);
182       }
183     }
184
185     public Object perform(List aParameters) throws GeneratorExc, GeneratorFailure {
186       try {
187         if (aParameters.size() != 1) {
188           throw new GeneratorExc("getLanguageIdFunction: 1 parameter expected: language-code");
189         }
190
191         String result = (String) languageCodeToId.get(aParameters.get(0));
192         if (result == null) {
193           result = otherLanguageId;
194         }
195
196         return result;
197       }
198       catch (GeneratorExc e) {
199         throw e;
200       }
201       catch (Throwable t) {
202         throw new GeneratorFailure("getLanguageIdFunction: " + t.getMessage(), t);
203       }
204     }
205   }
206
207
208   public String filterNonHTMLText(String aText) {
209
210     logger.debug("about to filter non HTML Text of length " + aText.length());
211     try {
212       String result =
213           stripper.createHTML(
214               stripper.removeHTMLTags(aText),
215               MirGlobal.config().getString("Producer.ImageRoot"),
216               MirGlobal.config().getString("Producer.MailLinkName"),
217               MirGlobal.config().getString("Producer.ExtLinkName"),
218               MirGlobal.config().getString("Producer.IntLinkName"));
219       logger.debug("done filtering non-HTML text ");
220       return result;
221     }
222     catch (Throwable t) {
223       logger.error("error while filtering non-HTML text: " + t.toString());
224
225       throw new RuntimeException(t.toString());
226     }
227   }
228
229   public Generator.Interceptor createGenerationInterceptor() throws MirLocalizerExc, MirLocalizerFailure {
230
231     if (MirGlobal.config().getBoolean("Mir.Producer.UseInterceptor", true)) {
232       return new Generator.Interceptor() {
233
234         public Object intercept(Object anObject) {
235           if (anObject instanceof EntityAdapter) {
236             return new InterceptedEntityAdapter((EntityAdapter) anObject);
237           }
238
239           return anObject;
240         }
241       };
242     }
243     else {
244       return null;
245     }
246   }
247
248   public class InterceptedEntityAdapter {
249     private EntityAdapter adapter;
250
251     InterceptedEntityAdapter(EntityAdapter anEntityAdapter) {
252       adapter = anEntityAdapter;
253     }
254
255     public Object get(String aField) {
256       Object result = adapter.get(aField);
257       if (result instanceof String) {
258         return filterHTMLText((String) result);
259       }
260       else {
261         return result;
262       }
263     }
264
265     public Object getRaw() {
266       return new RawEntityAdapter(adapter);
267     }
268   }
269
270   public class RawEntityAdapter {
271     private EntityAdapter adapter;
272
273     RawEntityAdapter(EntityAdapter anEntityAdapter) {
274       adapter = anEntityAdapter;
275     }
276
277     public Object get(String aField) {
278       return adapter.get(aField);
279     }
280   }
281
282   public String filterHTMLText(String aText) {
283     try {
284       StringWriter out = new StringWriter();
285       Tidy tidy = new Tidy();
286       ByteArrayInputStream in = new ByteArrayInputStream(aText.getBytes("UTF8"));
287       tidy.setMakeClean(true);
288       tidy.setCharEncoding(Configuration.UTF8);
289       tidy.setErrout(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
290       print(tidy.parseDOM(in, null), out);
291
292       return out.toString();
293     }
294     catch (IOException e) {
295       return e.getMessage();
296     }
297   }
298
299
300
301   private boolean isBadAttr(String attrName) {
302     List badAttributes = StringRoutines.splitString(MirGlobal.config().getString("Localizer.HTML.BadAttributes"), ";");
303     Iterator i = badAttributes.iterator();
304     while (i.hasNext()) {
305       if (((String) i.next()).toLowerCase().equals(attrName.toLowerCase())) {
306         return true;
307       }
308     }
309     return false;
310   }
311
312   private String stripWhitespace(String aString) {
313     try {
314       return regularExpressionWhitespace.substituteAll(aString, "");
315     }
316     catch (Throwable t) {
317       return "";
318     }
319   }
320
321   private boolean checkAttr(String attrName) {
322     if (isBadAttr(attrName)) {
323       return false;
324     }
325     return true;
326
327   }
328
329   private boolean checkAttrValue(String attrValue) {
330       List badPrefixes = StringRoutines.splitString(MirGlobal.config().getString("Localizer.HTML.BadAttributeValuePrefixes"), ";");
331       Iterator i = badPrefixes.iterator();
332       while (i.hasNext()) {
333           if ((stripWhitespace(attrValue.toLowerCase())).startsWith(((String) i.next()).toLowerCase() + ":")) {
334         return false;
335       }
336     }
337     return true;
338   }
339
340
341   private boolean checkNode(String nodeName) {
342     List acceptableNodes = StringRoutines.splitString(MirGlobal.config().getString("Localizer.HTML.Whitelist"), ";");
343
344     Iterator i = acceptableNodes.iterator();
345     while (i.hasNext()) {
346       if (nodeName.equals(i.next())) {
347         return true;
348       }
349     }
350     return false;
351   }
352
353   private void print(Node node, StringWriter out) throws IOException {
354     if (node == null) {
355       return;
356     }
357     int type = node.getNodeType();
358     boolean canOutput = checkNode(node.getNodeName());
359
360     switch (type) {
361
362       case Node.DOCUMENT_NODE:
363
364         print(((Document) node).getDocumentElement(), out);
365         out.flush();
366         break;
367
368       case Node.ELEMENT_NODE:
369         if (canOutput) {
370           out.write('<');
371
372           out.write(node.getNodeName());
373           NamedNodeMap attrs = node.getAttributes();
374
375           for (int i = 0; i < attrs.getLength(); i++) {
376             String attrName = attrs.item(i).getNodeName();
377             String attrValue = attrs.item(i).getNodeValue();
378             if (attrValue.startsWith("//")){
379               attrValue=regularExpressionLeadingSlashes.substitute(attrValue, "/");
380             }
381                             
382             if (checkAttr(attrName) && checkAttrValue(attrValue)) {
383               out.write(' ');
384               out.write(attrs.item(i).getNodeName());
385               out.write("=\"");
386
387               out.write(attrs.item(i).getNodeValue());
388               out.write('"');
389             }
390           }
391
392           if (node.getChildNodes() == null || node.getChildNodes().getLength() == 0) {
393             out.write("/");
394           }
395           out.write('>');
396         }
397         NodeList children = node.getChildNodes();
398         if (children != null) {
399           int len = children.getLength();
400           for (int i = 0; i < len; i++) {
401             print(children.item(i), out);
402           }
403         }
404         break;
405
406       case Node.TEXT_NODE:
407         String value = node.getNodeValue();
408         try {
409           value = regularExpressionLT.substituteAll(value, "&lt;");
410           value = regularExpressionGT.substituteAll(value, "&gt;");
411         }
412         catch (Throwable t) {
413           value = "";
414         }
415         out.write(value);
416
417         break;
418
419     }
420
421     if (type == Node.ELEMENT_NODE && canOutput && node.getChildNodes() != null && node.getChildNodes().getLength() > 0) {
422       out.write("</");
423       out.write(node.getNodeName());
424       out.write('>');
425     }
426
427     out.flush();
428   }
429
430   public static class Utility extends ReflectionGeneratorFunctionsAdapter {
431     public Utility() {
432       super(new MirBasicUtilityFunctions());
433     }
434
435     public Object getDatetime() {
436       return new GeneratorDateTimeFunctions.DateTimeFunctions(
437           MirPropertiesConfiguration.instance().getString("Mir.DefaultTimezone"));
438     }
439
440     public Object getCompressWhitespace() {
441       return new freemarker.template.utility.CompressWhitespace();
442     }
443   }
444 }