solved the bug that caused problems when an article's title contained "'s
authorzapata <zapata>
Thu, 12 Sep 2002 04:42:45 +0000 (04:42 +0000)
committerzapata <zapata>
Thu, 12 Sep 2002 04:42:45 +0000 (04:42 +0000)
source/mir/generator/FreemarkerGenerator.java
source/mir/misc/HTMLTemplateProcessor.java
source/mir/util/GeneratorHTMLFunctions.java
source/mir/util/ParameterExpander.java
source/mir/util/StringRoutines.java [new file with mode: 0755]

index 298adbc..8dc0a67 100755 (executable)
@@ -76,7 +76,7 @@ public class FreemarkerGenerator implements Generator {
     return new FunctionAdapter(aFunction);
   }
 
-  private static TemplateModel makeAdapter(Object anObject) throws TemplateModelException {
+  public static TemplateModel makeAdapter(Object anObject) throws TemplateModelException {
     if (anObject == null)
       return null;
     if (anObject instanceof TemplateModel)
index 56d0b30..3f014cc 100755 (executable)
@@ -32,6 +32,8 @@
 package mir.misc;
 
 import freemarker.template.*;
+import mir.util.*;
+import mir.generator.*;
 import mir.entity.Entity;
 import mir.entity.EntityList;
 import mir.storage.StorageObjectException;
@@ -247,6 +249,17 @@ public final class HTMLTemplateProcessor {
         // this conform to updated freemarker syntax
         configHash.put("compressWhitespace", new freemarker.template.utility.CompressWhitespace());
 
+        SimpleHash utilityHash = new SimpleHash();
+        try {
+          utilityHash.put("compressWhitespace", new freemarker.template.utility.CompressWhitespace());
+          utilityHash.put("encodeURI", FreemarkerGenerator.makeAdapter(new GeneratorHTMLFunctions.encodeURIGeneratorFunction()));
+          utilityHash.put("encodeHTML", FreemarkerGenerator.makeAdapter(new GeneratorHTMLFunctions.encodeHTMLGeneratorFunction()));
+        }
+        catch (Throwable t) {
+          throw new HTMLParseException(t.getMessage());
+        }
+
+
         SimpleHash outPutHash = new SimpleHash();
 
         if (extra != null) {
@@ -260,6 +273,7 @@ public final class HTMLTemplateProcessor {
         }
         outPutHash.put("data", tmr);
         outPutHash.put("config", configHash);
+        outPutHash.put("utility", utilityHash);
 
         MessageResources messages = MessageResources.getMessageResources(bundles);
         outPutHash.put("lang", new MessageMethodModel(locale, messages));
index 99ee1b1..4ec3afb 100755 (executable)
@@ -37,24 +37,17 @@ import java.net.*;
 import mir.misc.*;
 import mir.generator.*;
 
-/**
- * <p>Title: </p>
- * <p>Description: </p>
- * <p>Copyright: Copyright (c) 2002</p>
- * <p>Company: </p>
- * @author unascribed
- * @version 1.0
- */
-
 public class GeneratorHTMLFunctions {
   private GeneratorHTMLFunctions() {}
 
   public static class encodeURIGeneratorFunction implements Generator.GeneratorFunction {
     public Object perform(List aParameters) throws GeneratorExc {
       if (aParameters.size()!=1)
-        throw new GeneratorExc("encodeHTMLGeneratorFunction: only 1 parameter expected");
+        throw new GeneratorExc("encodeURIGeneratorFunction: only 1 parameter expected");
+      if (aParameters.get(0)==null)
+        return "";
       if (!(aParameters.get(0) instanceof String))
-        throw new GeneratorExc("encodeHTMLGeneratorFunction: parameter must be a string");
+        throw new GeneratorExc("encodeURIGeneratorFunction: parameter must be a string (not a "+aParameters.get(0).getClass().getName()+")");
 
       return URLEncoder.encode((String) aParameters.get(0));
     };
@@ -64,12 +57,13 @@ public class GeneratorHTMLFunctions {
     public Object perform(List aParameters) throws GeneratorExc {
       if (aParameters.size()!=1)
         throw new GeneratorExc("encodeHTMLGeneratorFunction: only 1 parameter expected");
+      if (aParameters.get(0)==null)
+        return "";
       if (!(aParameters.get(0) instanceof String))
-        throw new GeneratorExc("encodeHTMLGeneratorFunction: parameter must be a string");
+        throw new GeneratorExc("encodeHTMLGeneratorFunction: parameter must be a string (not a "+aParameters.get(0).getClass().getName()+")");
+
 
-      // YUCK! -mh
-      //return StringUtil.encodeHtml((String) aParameters.get(0));
-      return (String) aParameters.get(0);
+      return StringRoutines.encodeHTML((String) aParameters.get(0));
     };
   }
 }
index 0943ab1..96d1d66 100755 (executable)
@@ -29,7 +29,7 @@
  * not wish to do so, delete this exception statement from your version.
  */
 
-package  mir.util;
+package mir.util;
 
 import multex.Failure;
 import multex.Exc;
diff --git a/source/mir/util/StringRoutines.java b/source/mir/util/StringRoutines.java
new file mode 100755 (executable)
index 0000000..06ee501
--- /dev/null
@@ -0,0 +1,89 @@
+/*
+ * Copyright (C) 2001, 2002  The Mir-coders group
+ *
+ * This file is part of Mir.
+ *
+ * Mir is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Mir is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Mir; if not, write to the Free Software
+ * 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.
+ */
+
+package mir.util;
+
+import java.util.*;
+
+public class StringRoutines {
+
+  private StringRoutines() {
+  }
+
+  static int indexOfCharacters(String aString, char[] aCharacters, int aFrom) {
+    int i;
+    int result=-1;
+    int position;
+
+    for (i=0; i<aCharacters.length ; i++) {
+      position = aString.indexOf(aCharacters[i], aFrom);
+
+      if (position != -1 && ( result == -1 || position < result )) {
+        result = position;
+      }
+    }
+
+    return result;
+  }
+
+  public static String encodeHTML(String aText) {
+    final char[] CHARACTERS_TO_ESCAPE = { '&', '<', '>', '"', '\'' };
+    final String[] ESCAPE_CODES = { "&amp;", "&lt;", "&gt;", "&quot;", "&apos;" };
+
+    int position, nextPosition;
+    int i;
+    StringBuffer result = new StringBuffer();
+
+    position=0;
+
+    do {
+      nextPosition = indexOfCharacters(aText, CHARACTERS_TO_ESCAPE, position);
+      System.out.println("encodeHTML: "+ position + ":" + aText + " : " + nextPosition);
+
+      if (nextPosition<0)
+        nextPosition = aText.length();
+
+      result.append(aText.substring(position, nextPosition));
+
+      if (nextPosition<aText.length())
+        for (i=0; i<CHARACTERS_TO_ESCAPE.length; i++) {
+          if (CHARACTERS_TO_ESCAPE[i] == aText.charAt(nextPosition)) {
+            result.append(ESCAPE_CODES[i]);
+            break;
+          }
+        }
+      position=nextPosition+1;
+    }
+    while (nextPosition<aText.length()) ;
+
+    return result.toString();
+  }
+}
\ No newline at end of file