From 855a59e03d05068abbd2d1e4f3256908269e518e Mon Sep 17 00:00:00 2001 From: zapata Date: Thu, 12 Sep 2002 04:42:45 +0000 Subject: [PATCH] solved the bug that caused problems when an article's title contained "'s --- source/mir/generator/FreemarkerGenerator.java | 2 +- source/mir/misc/HTMLTemplateProcessor.java | 14 +++++ source/mir/util/GeneratorHTMLFunctions.java | 24 +++----- source/mir/util/ParameterExpander.java | 2 +- source/mir/util/StringRoutines.java | 89 +++++++++++++++++++++++++++ 5 files changed, 114 insertions(+), 17 deletions(-) create mode 100755 source/mir/util/StringRoutines.java diff --git a/source/mir/generator/FreemarkerGenerator.java b/source/mir/generator/FreemarkerGenerator.java index 298adbc3..8dc0a671 100755 --- a/source/mir/generator/FreemarkerGenerator.java +++ b/source/mir/generator/FreemarkerGenerator.java @@ -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) diff --git a/source/mir/misc/HTMLTemplateProcessor.java b/source/mir/misc/HTMLTemplateProcessor.java index 56d0b300..3f014cc3 100755 --- a/source/mir/misc/HTMLTemplateProcessor.java +++ b/source/mir/misc/HTMLTemplateProcessor.java @@ -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)); diff --git a/source/mir/util/GeneratorHTMLFunctions.java b/source/mir/util/GeneratorHTMLFunctions.java index 99ee1b1b..4ec3afbb 100755 --- a/source/mir/util/GeneratorHTMLFunctions.java +++ b/source/mir/util/GeneratorHTMLFunctions.java @@ -37,24 +37,17 @@ import java.net.*; import mir.misc.*; import mir.generator.*; -/** - *

Title:

- *

Description:

- *

Copyright: Copyright (c) 2002

- *

Company:

- * @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)); }; } } diff --git a/source/mir/util/ParameterExpander.java b/source/mir/util/ParameterExpander.java index 0943ab1b..96d1d66a 100755 --- a/source/mir/util/ParameterExpander.java +++ b/source/mir/util/ParameterExpander.java @@ -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 index 00000000..06ee501d --- /dev/null +++ b/source/mir/util/StringRoutines.java @@ -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', '"', '\'' }; + 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