X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=source%2Fmircoders%2Flocalizer%2Fbasic%2FMirBasicProducerAssistantLocalizer.java;h=3c9166f61c198559f191226e8269961f8d007c94;hb=2d3a3231809a1af4e373c3fd13902eeee26728c9;hp=92027382a248071e217d9440399e68bd6a6df7ff;hpb=ec89f6dc6e5cae2f6da9a2bb014eaac01beab882;p=mir.git diff --git a/source/mircoders/localizer/basic/MirBasicProducerAssistantLocalizer.java b/source/mircoders/localizer/basic/MirBasicProducerAssistantLocalizer.java index 92027382..3c9166f6 100755 --- a/source/mircoders/localizer/basic/MirBasicProducerAssistantLocalizer.java +++ b/source/mircoders/localizer/basic/MirBasicProducerAssistantLocalizer.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2001, 2002 The Mir-coders group + * Copyright (C) 2001, 2002 The Mir-coders group * * This file is part of Mir. * @@ -18,154 +18,299 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * In addition, as a special exception, The Mir-coders gives permission to link - * the code of this program with the com.oreilly.servlet library, any library - * licensed under the Apache Software License, The Sun (tm) Java Advanced - * Imaging library (JAI), The Sun JIMI library (or with modified versions of - * the above that use the same license as the above), and distribute linked - * combinations including the two. You must obey the GNU General Public - * License in all respects for all of the code used other than the above - * mentioned libraries. If you modify this file, you may extend this exception - * to your version of the file, but you are not obligated to do so. If you do - * not wish to do so, delete this exception statement from your version. + * the code of this program with any library licensed under the Apache Software License, + * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library + * (or with modified versions of the above that use the same license as the above), + * and distribute linked combinations including the two. You must obey the + * GNU General Public License in all respects for all of the code used other than + * the above mentioned libraries. If you modify this file, you may extend this + * exception to your version of the file, but you are not obligated to do so. + * If you do not wish to do so, delete this exception statement from your version. */ - 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 mir.config.MirPropertiesConfiguration; -import mir.config.MirPropertiesConfiguration.PropertiesConfigExc; -import mir.entity.EntityList; 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.GeneratorHTMLFunctions; -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.MirLocalizerExc; +import mircoders.localizer.MirLocalizerFailure; import mircoders.localizer.MirProducerAssistantLocalizer; -import mircoders.module.ModuleLanguage; -import mircoders.module.ModuleTopics; -import mircoders.storage.DatabaseLanguage; -import mircoders.storage.DatabaseTopics; + +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; - public void initializeGenerationValueSet(Map aValueSet) { - Iterator i; + public void initializeGenerationValueSet(Map aValueSet) throws MirLocalizerExc, MirLocalizerFailure { + try { + Iterator i; - Map configMap = new HashMap(); - Map utilityMap = new HashMap(); + Map configMap = new HashMap(); - logger = new LoggerWrapper("Localizer.Basic.ProducerAssistant"); + logger = new LoggerWrapper("Localizer.ProducerAssistant"); // obsolete: - configMap.put("producerDocRoot", MirGlobal.getConfigProperty("Producer.DocRoot")); - configMap.put("storageRoot", MirGlobal.getConfigProperty("Producer.StorageRoot")); - configMap.put("productionHost", MirGlobal.getConfigProperty("Producer.ProductionHost")); - configMap.put("openAction", MirGlobal.getConfigProperty("Producer.OpenAction")); - configMap.put("docRoot", MirGlobal.getConfigProperty("RootUri")); - configMap.put("actionRoot", MirGlobal.getConfigProperty("RootUri")+"/servlet/Mir"); - configMap.put("now", new DateToMapAdapter((new GregorianCalendar()).getTime())); - configMap.put("videoHost", MirGlobal.getConfigProperty("Producer.Video.Host")); - configMap.put("audioHost", MirGlobal.getConfigProperty("Producer.Audio.Host")); - configMap.put("imageHost", MirGlobal.getConfigProperty("Producer.Image.Host")); - configMap.put("imagePath", MirGlobal.getConfigProperty("Producer.Image.Path")); - configMap.put("mirVersion", MirGlobal.getConfigProperty("Mir.Version")); - configMap.put("defEncoding", MirGlobal.getConfigProperty("Mir.DefaultEncoding")); + configMap.put("producerDocRoot", MirGlobal.config().getString("Producer.DocRoot")); + configMap.put("storageRoot", MirGlobal.config().getString("Producer.StorageRoot")); + configMap.put("productionHost", MirGlobal.config().getString("Producer.ProductionHost")); + 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 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")); + configMap.put("imagePath", MirGlobal.config().getString("Producer.Image.Path")); + configMap.put("mirVersion", MirGlobal.config().getString("Mir.Version")); + configMap.put("defEncoding", MirGlobal.config().getString("Mir.DefaultEncoding")); // "new": - try { - configMap.putAll( MirPropertiesConfiguration.instance().allSettings() ); - } catch (PropertiesConfigExc e) { - e.printStackTrace(System.err); - } + 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()); + aValueSet.put("config", configMap); + aValueSet.put("utility", new Utility()); - aValueSet.put("config", configMap); - aValueSet.put("utility", utilityMap); + aValueSet.put("languages", + new EntityIteratorAdapter("", "", 20, MirGlobal.localizer().dataModel().adapterModel(), "language")); + aValueSet.put("topics", + new EntityIteratorAdapter("", "", 20, MirGlobal.localizer().dataModel().adapterModel(), "topic")); - EntityList topicList=null; - EntityList entityList=null; - EntityList languageList=null; + Map articleTypeMap = new HashMap(); + articleTypeMap.put("openposting", "0"); + articleTypeMap.put("newswire", "1"); + articleTypeMap.put("feature", "2"); + articleTypeMap.put("topicspecial", "3"); + articleTypeMap.put("startspecial", "4"); - try { - ModuleTopics topicsModule = new ModuleTopics(DatabaseTopics.getInstance()); - ModuleLanguage languageModule = new ModuleLanguage(DatabaseLanguage.getInstance()); + i = new EntityIteratorAdapter("", "", 20, MirGlobal.localizer().dataModel().adapterModel(), "articleType"); + while (i.hasNext()) { + EntityAdapter articleType = (EntityAdapter) i.next(); + + articleTypeMap.put(articleType.get("name"), articleType.get("id")); + } + aValueSet.put("articletype", articleTypeMap); - topicList = topicsModule.getTopicsList(); - languageList = languageModule.getByWhereClause("", "id", -1); + Map commentStatusMap = new HashMap(); + i = new EntityIteratorAdapter("", "", 20, MirGlobal.localizer().dataModel().adapterModel(), "commentStatus"); + while (i.hasNext()) { + EntityAdapter commentStatus = (EntityAdapter) i.next(); + commentStatusMap.put(commentStatus.get("name"), commentStatus.get("id")); + } + aValueSet.put("commentstatus", commentStatusMap); + aValueSet.put("languageCodeToId", new getLanguageIdFunction()); } catch (Throwable t) { - logger.error("initializeGenerationValueSet: Exception "+t.getMessage()); + logger.error("initializeGenerationValueSet: Exception while collecting comment statuses" + t.getMessage()); + + throw new MirLocalizerFailure(t); } - aValueSet.put("topics", topicList); - aValueSet.put("imclist", entityList); + } + + public static class getLanguageIdFunction implements Generator.Function { + private Map languageCodeToId; + private String otherLanguageId; + private LoggerWrapper logger = new LoggerWrapper("Localizer.Earth.getLanguageIdFunction"); - Map articleTypeMap = new HashMap(); - articleTypeMap.put("openposting", "0"); - articleTypeMap.put("newswire", "1"); - articleTypeMap.put("feature", "2"); - articleTypeMap.put("topicspecial", "3"); - articleTypeMap.put("startspecial", "4"); + public getLanguageIdFunction() throws MirLocalizerFailure { + try { + otherLanguageId = ""; + languageCodeToId = new HashMap(); - try { - i = new EntityIteratorAdapter( "", "", 20, MirGlobal.localizer().dataModel().adapterModel(), "articleType" ); + 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"); - while (i.hasNext()) { - EntityAdapter articleType = (EntityAdapter) i.next(); + languageCodeToId.put(language.get("code"), language.get("id")); + } + } + catch (Throwable t) { + logger.error(t.toString()); - articleTypeMap.put(articleType.get("name"), articleType.get("id")); + throw new MirLocalizerFailure(t); } } - catch (Throwable t) { - logger.error("initializeGenerationValueSet: Exception while collecting article types "+t.getMessage()); - } - aValueSet.put("articletype", articleTypeMap); - Map commentStatusMap = new HashMap(); - try { - i = new EntityIteratorAdapter( "", "", 20, MirGlobal.localizer().dataModel().adapterModel(), "commentStatus" ); + public Object perform(List aParameters) throws GeneratorExc, GeneratorFailure { + try { + if (aParameters.size() != 1) + throw new GeneratorExc("getLanguageIdFunction: 1 parameter expected: language-code"); - while (i.hasNext()) { - EntityAdapter commentStatus = (EntityAdapter) i.next(); + String result = (String) languageCodeToId.get(aParameters.get(0)); + if (result == null) + result = otherLanguageId; - commentStatusMap.put(commentStatus.get("name"), commentStatus.get("id")); + 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("initializeGenerationValueSet: Exception while collecting comment statuses"+t.getMessage()); + logger.error("error while filtering non-HTML text: " + t.toString()); + + throw new RuntimeException(t.toString()); + } + } + public String filterHTMLText(String 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 boolean checkAttr(String attrName) { + 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")) + 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(); + if (checkAttr(attrName)) { + 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: + out.write(node.getNodeValue()); + break; + + } + + if (type == Node.ELEMENT_NODE && canOutput && node.getChildNodes()!=null && node.getChildNodes().getLength()>0) { + 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(); } - aValueSet.put("commentstatus", commentStatusMap); - - }; - - public String filterText(String aText) { - return StringUtil.createHTML( - StringUtil.deleteForbiddenTags(aText), - MirGlobal.getConfigProperty("Producer.ImageRoot"), - MirGlobal.getConfigProperty("Producer.MailLinkName"), - MirGlobal.getConfigProperty("Producer.ExtLinkName"), - MirGlobal.getConfigProperty("Producer.IntLinkName") - ); } }