*/
package mircoders.localizer.basic;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.StringWriter;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Iterator;
+import java.util.List;
import java.util.Map;
+import gnu.regexp.RE;
+
import mir.config.MirPropertiesConfiguration;
import mir.entity.adapter.EntityAdapter;
import mir.entity.adapter.EntityIteratorAdapter;
+import mir.generator.Generator;
+import mir.generator.GeneratorExc;
+import mir.generator.GeneratorFailure;
import mir.log.LoggerWrapper;
import mir.misc.StringUtil;
-import mir.util.DateToMapAdapter;
-import mir.util.GeneratorExpressionFunctions;
-import mir.util.*;
-import mir.util.GeneratorIntegerFunctions;
-import mir.util.GeneratorListFunctions;
-import mir.util.GeneratorStringFunctions;
+import mir.util.GeneratorDateTimeFunctions;
+import mir.util.GeneratorFormatAdapters;
+import mir.util.generator.ReflectionGeneratorFunctionsAdapter;
+import mir.util.StringRoutines;
import mircoders.global.MirGlobal;
-import mircoders.localizer.*;
+import mircoders.localizer.MirLocalizerExc;
+import mircoders.localizer.MirLocalizerFailure;
+import mircoders.localizer.MirProducerAssistantLocalizer;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.w3c.tidy.Configuration;
+import org.w3c.tidy.Tidy;
public class MirBasicProducerAssistantLocalizer implements MirProducerAssistantLocalizer {
protected LoggerWrapper logger;
+ private RE regularExpressionLT;
+ private RE regularExpressionGT;
+ private RE regularExpressionWhitespace;
+
+ public MirBasicProducerAssistantLocalizer() throws MirLocalizerFailure {
+ try{
+ regularExpressionLT = new RE("<");
+ regularExpressionGT = new RE(">");
+ regularExpressionWhitespace = new RE("\\s+");
+ }
+ catch (Throwable t) {
+ throw new MirLocalizerFailure(t);
+ }
+ }
+
public void initializeGenerationValueSet(Map aValueSet) throws MirLocalizerExc, MirLocalizerFailure {
try {
Iterator i;
Map configMap = new HashMap();
- Map utilityMap = new HashMap();
logger = new LoggerWrapper("Localizer.ProducerAssistant");
configMap.put("openAction", MirGlobal.config().getString("Producer.OpenAction"));
configMap.put("docRoot", MirGlobal.config().getString("RootUri"));
configMap.put("actionRoot", MirGlobal.config().getString("RootUri") + "/servlet/Mir");
- configMap.put("now", new DateToMapAdapter( (new GregorianCalendar()).getTime()));
+ configMap.put("now", new GeneratorFormatAdapters.DateFormatAdapter(new GregorianCalendar().getTime(), MirGlobal.config().getString("Mir.DefaultTimezone")));
configMap.put("videoHost", MirGlobal.config().getString("Producer.Video.Host"));
configMap.put("audioHost", MirGlobal.config().getString("Producer.Audio.Host"));
configMap.put("imageHost", MirGlobal.config().getString("Producer.Image.Host"));
// "new":
configMap.putAll(MirPropertiesConfiguration.instance().allSettings());
- utilityMap.put("compressWhitespace", new freemarker.template.utility.CompressWhitespace());
- utilityMap.put("encodeHTML", new GeneratorHTMLFunctions.encodeHTMLGeneratorFunction());
- utilityMap.put("encodeXML", new GeneratorHTMLFunctions.encodeXMLGeneratorFunction());
- utilityMap.put("encodeURI", new GeneratorHTMLFunctions.encodeURIGeneratorFunction());
- utilityMap.put("subString", new GeneratorStringFunctions.subStringFunction());
- utilityMap.put("subList", new GeneratorListFunctions.subListFunction());
- utilityMap.put("isOdd", new GeneratorIntegerFunctions.isOddFunction());
- utilityMap.put("increment", new GeneratorIntegerFunctions.incrementFunction());
- utilityMap.put("evaluate", new GeneratorExpressionFunctions.evaluateExpressionFunction());
- utilityMap.put("constructString", new GeneratorStringFunctions.constructStructuredStringFunction());
- utilityMap.put("escapeJDBCString", new GeneratorStringFunctions.jdbcStringEscapeFunction());
- utilityMap.put("regexpreplace", new GeneratorRegularExpressionFunctions.regularExpressionReplaceFunction());
-
aValueSet.put("config", configMap);
- aValueSet.put("utility", utilityMap);
+
+ aValueSet.put("utility", new Utility());
aValueSet.put("languages",
new EntityIteratorAdapter("", "", 20, MirGlobal.localizer().dataModel().adapterModel(), "language"));
commentStatusMap.put(commentStatus.get("name"), commentStatus.get("id"));
}
aValueSet.put("commentstatus", commentStatusMap);
+ aValueSet.put("languageCodeToId", new getLanguageIdFunction());
}
catch (Throwable t) {
logger.error("initializeGenerationValueSet: Exception while collecting comment statuses" + t.getMessage());
- throw new RuntimeException(t.getMessage());
+
+ throw new MirLocalizerFailure(t);
}
- };
+ }
+
+ public static class getLanguageIdFunction implements Generator.Function {
+ private Map languageCodeToId;
+ private String otherLanguageId;
+ private LoggerWrapper logger = new LoggerWrapper("Localizer.Earth.getLanguageIdFunction");
- public String filterNonHTMLText(String aText) {
- return StringUtil.createHTML(
- StringUtil.removeHTMLTags(aText),
- MirGlobal.config().getString("Producer.ImageRoot"),
- MirGlobal.config().getString("Producer.MailLinkName"),
- MirGlobal.config().getString("Producer.ExtLinkName"),
- MirGlobal.config().getString("Producer.IntLinkName")
- );
+ public getLanguageIdFunction() throws MirLocalizerFailure {
+ try {
+ otherLanguageId = "";
+ languageCodeToId = new HashMap();
+
+ Iterator i = new EntityIteratorAdapter("", "", 20, MirGlobal.localizer().dataModel().adapterModel(), "language");
+ while (i.hasNext()) {
+ EntityAdapter language = (EntityAdapter) i.next();
+ if (language.get("code").equals("ot"))
+ otherLanguageId = (String) language.get("id");
+
+ languageCodeToId.put(language.get("code"), language.get("id"));
+ }
+ }
+ catch (Throwable t) {
+ logger.error(t.toString());
+
+ throw new MirLocalizerFailure(t);
+ }
+ }
+
+ public Object perform(List aParameters) throws GeneratorExc, GeneratorFailure {
+ try {
+ if (aParameters.size() != 1)
+ throw new GeneratorExc("getLanguageIdFunction: 1 parameter expected: language-code");
+
+ String result = (String) languageCodeToId.get(aParameters.get(0));
+ if (result == null)
+ result = otherLanguageId;
+
+ return result;
+ }
+ catch (GeneratorExc e) {
+ throw e;
+ }
+ catch (Throwable t) {
+ throw new GeneratorFailure("getLanguageIdFunction: " + t.getMessage(), t);
+ }
+ }
}
+
+ public String filterNonHTMLText(String aText) {
+
+ logger.debug("about to filter non HTML Text of length " + aText.length());
+ try {
+ String result =
+ StringUtil.createHTML(
+ StringUtil.removeHTMLTags(aText),
+ MirGlobal.config().getString("Producer.ImageRoot"),
+ MirGlobal.config().getString("Producer.MailLinkName"),
+ MirGlobal.config().getString("Producer.ExtLinkName"),
+ MirGlobal.config().getString("Producer.IntLinkName")
+ );
+ logger.debug("done filtering non-HTML text ");
+ return result;
+ }
+ catch (Throwable t) {
+ logger.error("error while filtering non-HTML text: " + t.toString());
+
+ throw new RuntimeException(t.toString());
+ }
+ }
public String filterHTMLText(String aText) {
- return StringUtil.deleteForbiddenTags(aText);
+ try {
+ StringWriter out = new StringWriter();
+ Tidy tidy = new Tidy();
+ ByteArrayInputStream in = new ByteArrayInputStream(aText.getBytes("UTF8"));
+ tidy.setMakeClean(true);
+ tidy.setCharEncoding(Configuration.UTF8);
+ tidy.setErrout(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
+ print(tidy.parseDOM(in, null), out);
+
+ return out.toString();
+ }
+ catch (IOException e) {
+ return e.getMessage();
+ }
+ }
+
+
+ private String[] badAttributeValuePrefixes= {"javascript","vbscript","about","wysiwyg","data","view-source","ms-its","mhtml","shell","lynxexec","lynxcgi","hcp","ms-help","help","disk","vnd.ms.radio","opera","res","resource","chrome","mocha","livescript"};
+
+
+ private String[] badAttributes = {"onabort", "onblur", "onchange", "onclick", "ondblclick", "onerror", "onfocus", "onkeydown", "onKeypress", "onkeyup", "onload", "onmousedown", "onmousemove", "onmouseout", "onmouseover", "onmouseup", "onreset", "onselect", "onsubmit", "onunload","onload","onclick","onfocus","onblur","style","height","width"};
+
+ private boolean isBadAttr(String attrName){
+ for (int i=0;i<badAttributes.length;i++){
+ if (badAttributes[i].toLowerCase().equals(attrName.toLowerCase()))
+ return true;
+ }
+ return false;
+ }
+
+ private String stripWhitespace(String aString){
+ try{
+ return regularExpressionWhitespace.substituteAll(aString, "");
+ }
+ catch (Throwable t){
+ return "";
+ }
+ }
+
+ private boolean checkAttr(String attrName) {
+ if (isBadAttr(attrName)){
+ return false;
+ }
+ return true;
+
+ }
+
+ private boolean checkAttrValue(String attrValue) {
+ for (int i=0;i<badAttributeValuePrefixes.length;i++){
+ if ((stripWhitespace(attrValue.toLowerCase())).startsWith(badAttributeValuePrefixes[i].toLowerCase()+":")){
+ return false;
+ }
+ }
+ return true;
+ }
+
+
+ private boolean checkNode(String nodeName) {
+ List languages = StringRoutines.splitString(MirGlobal.config().getString("Localizer.HTML.Whitelist"), ";");
+
+ Iterator i = languages.iterator();
+ while (i.hasNext()) {
+ if (nodeName.equals(i.next()))
+ return true;
+ }
+ return false;
+ }
+
+ private void print(Node node, StringWriter out) throws IOException {
+ if (node == null) {
+ return;
+ }
+ int type = node.getNodeType();
+ boolean canOutput = checkNode(node.getNodeName());
+
+ switch (type) {
+
+ case Node.DOCUMENT_NODE:
+
+ print(((Document) node).getDocumentElement(), out);
+ out.flush();
+ break;
+
+ case Node.ELEMENT_NODE:
+ if (canOutput) {
+ out.write('<');
+
+ out.write(node.getNodeName());
+ NamedNodeMap attrs = node.getAttributes();
+
+ for (int i = 0; i < attrs.getLength(); i++) {
+ String attrName = attrs.item(i).getNodeName();
+ String attrValue = attrs.item(i).getNodeValue();
+ if (checkAttr(attrName) && checkAttrValue(attrValue)) {
+ out.write(' ');
+ out.write(attrs.item(i).getNodeName());
+ out.write("=\"");
+
+ out.write(attrs.item(i).getNodeValue());
+ out.write('"');
+ }
+ }
+
+ if (node.getChildNodes()==null || node.getChildNodes().getLength()==0) {
+ out.write("/");
+ }
+ out.write('>');
+ }
+ NodeList children = node.getChildNodes();
+ if (children != null) {
+ int len = children.getLength();
+ for (int i = 0; i < len; i++) {
+ print(children.item(i), out);
+ }
+ }
+ break;
+
+ case Node.TEXT_NODE:
+ String value=node.getNodeValue();
+ try{
+ value=regularExpressionLT.substituteAll(value, "<");
+ value=regularExpressionGT.substituteAll(value, ">");
+ }
+ catch (Throwable t){
+ value="";
+ }
+ out.write(value);
+
+ break;
+
+ }
+
+ if (type == Node.ELEMENT_NODE && canOutput && node.getChildNodes()!=null && node.getChildNodes().getLength()>0) {
+ out.write("</");
+ out.write(node.getNodeName());
+ out.write('>');
+ }
+
+ out.flush();
+ }
+
+ public static class Utility extends ReflectionGeneratorFunctionsAdapter {
+ public Utility () {
+ super(new MirBasicUtilityFunctions());
+ }
+
+ public Object getDatetime() {
+ return new GeneratorDateTimeFunctions.DateTimeFunctions(
+ MirPropertiesConfiguration.instance().getString("Mir.DefaultTimezone"));
+ }
+
+ public Object getCompressWhitespace() {
+ return new freemarker.template.utility.CompressWhitespace();
+ }
}
}