package mir.misc;
import freemarker.template.*;
+import mir.util.*;
+import mir.generator.*;
import mir.entity.Entity;
import mir.entity.EntityList;
import mir.storage.StorageObjectException;
// 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) {
}
outPutHash.put("data", tmr);
outPutHash.put("config", configHash);
+ outPutHash.put("utility", utilityHash);
MessageResources messages = MessageResources.getMessageResources(bundles);
outPutHash.put("lang", new MessageMethodModel(locale, messages));
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));
};
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));
};
}
}
--- /dev/null
+/*
+ * 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 = { "&", "<", ">", """, "'" };
+
+ 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