- protected static Logfile logger = Logfile.getInstance( MirGlobal.getConfigProperty("Home") + "/" + MirGlobal.getConfigProperty("Mir.Localizer.Logfile"));
-
- public void initializeGenerationValueSet(Map aValueSet) {
- // ML: these config settings will be included more beautifully as soon as the new config system is in place
-
- Map configMap = new HashMap();
- Map utilityMap = new HashMap();
-
- 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("all", Configuration.getConfs());
-
- utilityMap.put("compressWhitespace", new freemarker.template.utility.CompressWhitespace() );
- utilityMap.put("encodeHTML", new GeneratorHTMLFunctions.encodeHTMLGeneratorFunction());
- utilityMap.put("encodeURI", new GeneratorHTMLFunctions.encodeURIGeneratorFunction());
-
- aValueSet.put("config", configMap);
- aValueSet.put("utility", utilityMap);
-
- EntityList topicList=null;
- EntityList entityList=null;
- EntityList parentList=null;
- EntityList languageList=null;
+ protected LoggerWrapper logger;
+
+ private HTMLStripper stripper;
+ private RE regularExpressionLT;
+ private RE regularExpressionGT;
+ private RE regularExpressionWhitespace;
+ private RE regularExpressionLeadingSlashes;
+
+
+ public MirBasicProducerAssistantLocalizer() throws MirLocalizerFailure {
+ try {
+ stripper = new HTMLStripper();
+
+ regularExpressionLT = new RE("<");
+ regularExpressionGT = new RE(">");
+ regularExpressionWhitespace = new RE("\\s+|
|
");
+ regularExpressionLeadingSlashes = new RE("^//+");
+ }
+ catch (Throwable t) {
+ throw new MirLocalizerFailure(t);
+ }
+ }
+
+ public void initializeGenerationValueSet(Map aValueSet) throws MirLocalizerExc, MirLocalizerFailure {
+ try {
+ Iterator i;
+
+ Map configMap = new HashMap();
+
+ logger = new LoggerWrapper("Localizer.ProducerAssistant");
+
+// obsolete:
+ 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":
+ configMap.putAll(MirPropertiesConfiguration.instance().allSettings());
+
+ aValueSet.put("config", configMap);
+
+ aValueSet.put("utility", new Utility());
+
+ aValueSet.put("languages",
+ new EntityIteratorAdapter("", "", 20, MirGlobal.localizer().dataModel().adapterModel(), "language"));
+
+ aValueSet.put("topics",
+ new EntityIteratorAdapter("", "", 20, MirGlobal.localizer().dataModel().adapterModel(), "topic"));
+
+ Map articleTypeMap = new HashMap();
+ articleTypeMap.put("openposting", "0");
+ articleTypeMap.put("newswire", "1");
+ articleTypeMap.put("feature", "2");
+ articleTypeMap.put("topicspecial", "3");
+ articleTypeMap.put("startspecial", "4");
+
+ 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);
+
+ 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 while collecting comment statuses" + 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 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 =
+ stripper.createHTML(
+ stripper.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 Generator.Interceptor createGenerationInterceptor() throws MirLocalizerExc, MirLocalizerFailure {
+
+ if (MirGlobal.config().getBoolean("Mir.Producer.UseInterceptor", true)) {
+ return new Generator.Interceptor() {
+
+ public Object intercept(Object anObject) {
+ if (anObject instanceof EntityAdapter) {
+ return new InterceptedEntityAdapter((EntityAdapter) anObject);
+ }
+
+ return anObject;
+ }
+ };
+ }
+ else {
+ return null;
+ }
+ }
+
+ public class InterceptedEntityAdapter {
+ private EntityAdapter adapter;
+
+ InterceptedEntityAdapter(EntityAdapter anEntityAdapter) {
+ adapter = anEntityAdapter;
+ }
+
+ public Object get(String aField) {
+ Object result = adapter.get(aField);
+ if (result instanceof String) {
+ return filterHTMLText((String) result);
+ }
+ else {
+ return result;
+ }
+ }
+
+ public Object getRaw() {
+ return new RawEntityAdapter(adapter);
+ }
+ }