--- /dev/null
+package org.plog4u.wiki.test.filter;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.plog4u.wiki.filter.WikipediaFilter;
+
+public class HTTPUrlFilterTest extends FilterTestSupport {
+ public HTTPUrlFilterTest(String name) {
+ super(name);
+ }
+
+ protected void setUp() throws Exception {
+ filter = new WikipediaFilter();
+ super.setUp();
+ }
+
+ public static Test suite() {
+ return new TestSuite(HTTPUrlFilterTest.class);
+ }
+
+ public void testUrl() {
+ assertEquals("see <span class=\"nobr\"><a href=\"http://www.plog4u.de\">http://www.plog4u.de</a></span> ", filter.filter("see http://www.plog4u.de ", context));
+ }
+
+ public void testUrl2() {
+ assertEquals("see <span class=\"nobr\"><a href=\"http://www.plog4u.de/index.php&test_me\">http://www.plog4u.de/index.php&test_me</a></span> ", filter.filter("see http://www.plog4u.de/index.php&test_me ", context));
+ }
+}
--- /dev/null
+package org.plog4u.wiki.test.filter;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.plog4u.wiki.filter.WikipediaFilter;
+
+public class WPLinkFilterTest extends FilterTestSupport {
+ public WPLinkFilterTest(String name) {
+ super(name);
+ }
+
+ protected void setUp() throws Exception {
+ filter = new WikipediaFilter();
+ super.setUp();
+ }
+
+ public static Test suite() {
+ return new TestSuite(WPLinkFilterTest.class);
+ }
+
+ public void testLink() {
+ assertEquals("Category:Tips & Tricks", filter.filter("[[Category:Tips & Tricks]]", context));
+ }
+
+}
--- /dev/null
+/*
+ * This file is part of "SnipSnap Radeox Rendering Engine".
+ *
+ * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
+ * All Rights Reserved.
+ *
+ * Please visit http://radeox.org/ for updates and contact.
+ *
+ * --LICENSE NOTICE--
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ * --LICENSE NOTICE--
+ */
+
+package org.plog4u.wiki.filter;
+
+import org.radeox.regex.Pattern;
+import org.radeox.regex.Matcher;
+import org.radeox.regex.Substitution;
+import org.radeox.regex.MatchResult;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.StringTokenizer;
+
+/*
+ * Escapes and encodes Strings for web usage
+ *
+ * @author stephan
+ * @version $Id: Encoder.java,v 1.1 2005-02-19 00:19:41 axelcl Exp $
+ */
+
+public class Encoder {
+ private final static String DELIMITER = "&\"'<>";
+ private final static Map ESCAPED_CHARS = new HashMap();
+ // private final static Pattern entityPattern = Pattern.compile("&(#?[0-9a-fA-F]+);");
+
+// static {
+// ESCAPED_CHARS.put("&", toEntity('&'));
+// ESCAPED_CHARS.put("\"", toEntity('"'));
+// ESCAPED_CHARS.put("'", toEntity('\''));
+// ESCAPED_CHARS.put(">", toEntity('>'));
+// ESCAPED_CHARS.put("<", toEntity('<'));
+// }
+
+ static {
+ ESCAPED_CHARS.put("&", "&");
+ ESCAPED_CHARS.put("\"", """);
+ ESCAPED_CHARS.put("'", toEntity('\''));
+ ESCAPED_CHARS.put(">", ">");
+ ESCAPED_CHARS.put("<", "<");
+ }
+ /**
+ * Encoder special characters that may occur in a HTML so it can be displayed
+ * safely.
+ * @param str the original string
+ * @return the escaped string
+ */
+ public static String escape(String str) {
+ if (str==null || str.equals("")) {
+ return "";
+ }
+ StringBuffer result = new StringBuffer();
+ StringTokenizer tokenizer = new StringTokenizer(str, DELIMITER, true);
+ while(tokenizer.hasMoreTokens()) {
+ String currentToken = tokenizer.nextToken();
+ if(ESCAPED_CHARS.containsKey(currentToken)) {
+ result.append(ESCAPED_CHARS.get(currentToken));
+ } else {
+ result.append(currentToken);
+ }
+ }
+ return result.toString();
+ }
+
+ public static String unescape(String str) {
+ StringBuffer result = new StringBuffer();
+
+ org.radeox.regex.Compiler compiler = org.radeox.regex.Compiler.create();
+ Pattern entityPattern = compiler.compile("&(#?[0-9a-fA-F]+);");
+
+ Matcher matcher = Matcher.create(str, entityPattern);
+ result.append(matcher.substitute(new Substitution() {
+ public void handleMatch(StringBuffer buffer, MatchResult result) {
+ buffer.append(toChar(result.group(1)));
+ }
+ }));
+ return result.toString();
+ }
+
+ public static String toEntity(int c) {
+ return "&#" + c + ";";
+ }
+
+ public static char toChar(String number) {
+ return (char) Integer.decode(number.substring(1)).intValue();
+ }
+}
result.append("</a></span>");
}
- public static void handleSnipLink(StringBuffer result, RenderEngine wikiEngine, String name) {
- if (name != null) {
- int index = name.indexOf("http://");
- // Configuration probably wrote [http://radeox.org] instead of http://radeox.org
- if (index != -1) {
- createExternalLink(result, wikiEngine, name.substring(index));
- // show error
- // fResult.append("<div class=\"error\">Do not surround URLs with [...].</div>");
- } else {
- // trim the name and unescape it
- name = Encoder.unescape(name.trim());
- // Is there an alias like [alias|link] ?
- int pipeIndex = name.indexOf('|');
- String alias = "";
- if (-1 != pipeIndex) {
- alias = name.substring(0, pipeIndex);
- name = name.substring(pipeIndex + 1);
- }
-
- int hashIndex = name.lastIndexOf('#');
-
- String hash = "";
- if (-1 != hashIndex && hashIndex != name.length() - 1) {
- hash = name.substring(hashIndex + 1);
- name = name.substring(0, hashIndex);
- }
-
- int colonIndex = name.indexOf(':');
- // typed link ?
- if (-1 != colonIndex) {
- // for now throw away the fType information
- name = name.substring(colonIndex + 1);
- }
-
- int atIndex = name.lastIndexOf('@');
- // InterWiki link ?
- if (-1 != atIndex) {
- String extSpace = name.substring(atIndex + 1);
- // known extarnal space ?
- InterWiki interWiki = InterWiki.getInstance();
- if (interWiki.contains(extSpace)) {
- name = name.substring(0, atIndex);
- Writer writer = new StringBufferWriter(result);
- try {
- if (-1 != hashIndex) {
- interWiki.expand(writer, extSpace, name, hash);
- } else {
- interWiki.expand(writer, extSpace, name, "");
- }
- } catch (IOException e) {
-// log.debug("InterWiki " + extSpace + " not found.");
- }
- } else {
- result.append("[<span class=\"error\">");
- result.append(name);
- result.append("?</span>]");
- }
- } else {
- // internal link
- if (wikiEngine != null && wikiEngine instanceof WikiRenderEngine) {
- if (((WikiRenderEngine) wikiEngine).exists(name)) {
- String view = name;
- if (-1 != pipeIndex) {
- view = alias;
- }
- // Do not add hash if an alias was given
- if (-1 != hashIndex) {
- ((WikiRenderEngine) wikiEngine).appendLink(result, name, view, hash);
- } else {
- ((WikiRenderEngine) wikiEngine).appendLink(result, name, view);
- }
- } else if (((WikiRenderEngine) wikiEngine).showCreate()) {
- ((WikiRenderEngine) wikiEngine).appendCreateLink(result, name, name);
- // links with "create" are not cacheable because
- // a missing wiki could be created
- // TODO is this ok?
- // fContext.getRenderContext().setCacheable(false);
- } else {
- // cannot display/create wiki, so just display the text
- result.append(name);
- }
- } else {
- // cannot display/create wiki, so just display the text
- result.append(name);
- }
- }
- }
- }
- }
+// public static void handleSnipLink(StringBuffer result, RenderEngine wikiEngine, String name) {
+// if (name != null) {
+// int index = name.indexOf("http://");
+// // Configuration probably wrote [http://radeox.org] instead of http://radeox.org
+// if (index != -1) {
+// createExternalLink(result, wikiEngine, name.substring(index));
+// // show error
+// // fResult.append("<div class=\"error\">Do not surround URLs with [...].</div>");
+// } else {
+// // trim the name and unescape it
+// name = Encoder.unescape(name.trim());
+// // Is there an alias like [alias|link] ?
+// int pipeIndex = name.indexOf('|');
+// String alias = "";
+// if (-1 != pipeIndex) {
+// alias = name.substring(0, pipeIndex);
+// name = name.substring(pipeIndex + 1);
+// }
+//
+// int hashIndex = name.lastIndexOf('#');
+//
+// String hash = "";
+// if (-1 != hashIndex && hashIndex != name.length() - 1) {
+// hash = name.substring(hashIndex + 1);
+// name = name.substring(0, hashIndex);
+// }
+//
+// int colonIndex = name.indexOf(':');
+// // typed link ?
+// if (-1 != colonIndex) {
+// // for now throw away the fType information
+// name = name.substring(colonIndex + 1);
+// }
+//
+// int atIndex = name.lastIndexOf('@');
+// // InterWiki link ?
+// if (-1 != atIndex) {
+// String extSpace = name.substring(atIndex + 1);
+// // known extarnal space ?
+// InterWiki interWiki = InterWiki.getInstance();
+// if (interWiki.contains(extSpace)) {
+// name = name.substring(0, atIndex);
+// Writer writer = new StringBufferWriter(result);
+// try {
+// if (-1 != hashIndex) {
+// interWiki.expand(writer, extSpace, name, hash);
+// } else {
+// interWiki.expand(writer, extSpace, name, "");
+// }
+// } catch (IOException e) {
+//// log.debug("InterWiki " + extSpace + " not found.");
+// }
+// } else {
+// result.append("[<span class=\"error\">");
+// result.append(name);
+// result.append("?</span>]");
+// }
+// } else {
+// // internal link
+// if (wikiEngine != null && wikiEngine instanceof WikiRenderEngine) {
+// if (((WikiRenderEngine) wikiEngine).exists(name)) {
+// String view = name;
+// if (-1 != pipeIndex) {
+// view = alias;
+// }
+// // Do not add hash if an alias was given
+// if (-1 != hashIndex) {
+// ((WikiRenderEngine) wikiEngine).appendLink(result, name, view, hash);
+// } else {
+// ((WikiRenderEngine) wikiEngine).appendLink(result, name, view);
+// }
+// } else if (((WikiRenderEngine) wikiEngine).showCreate()) {
+// ((WikiRenderEngine) wikiEngine).appendCreateLink(result, name, name);
+// // links with "create" are not cacheable because
+// // a missing wiki could be created
+// // TODO is this ok?
+// // fContext.getRenderContext().setCacheable(false);
+// } else {
+// // cannot display/create wiki, so just display the text
+// result.append(name);
+// }
+// } else {
+// // cannot display/create wiki, so just display the text
+// result.append(name);
+// }
+// }
+// }
+// }
+// }
public static void handleWikipediaLink(StringBuffer result, RenderEngine wikiEngine, String name, String suffix) {
if (name != null) {
package org.plog4u.wiki.filter;
-import java.io.IOException;
-import java.io.Writer;
import java.util.ArrayList;
import java.util.NoSuchElementException;
import java.util.Stack;
import java.util.StringTokenizer;
-//import org.apache.commons.logging.Log;
-//import org.apache.commons.logging.LogFactory;
import org.plog4u.wiki.filter.WikipediaFilter.InvalidInputException;
import org.plog4u.wiki.filter.tags.AbstractTag;
import org.plog4u.wiki.filter.tags.CloseTagToken;
import org.radeox.api.engine.RenderEngine;
import org.radeox.api.engine.WikiRenderEngine;
import org.radeox.filter.context.FilterContext;
-import org.radeox.filter.interwiki.InterWiki;
import org.radeox.macro.Macro;
import org.radeox.macro.MacroRepository;
import org.radeox.macro.parameter.MacroParameter;
-import org.radeox.util.Encoder;
import org.radeox.util.StringBufferWriter;
/**
} else {
lastIndex++;
}
- fResultBuffer.append("&");
+ fResultBuffer.append("&");
break;
case '\'': // special html escape character
if (lastIndex < (currentIndex - 1)) {
} else {
lastIndex++;
}
- fResultBuffer.append(""");
+ fResultBuffer.append(""");
break;
}
}
fResultBuffer.append(text.substring(lastIndex, currentIndex - 1));
lastIndex = currentIndex;
}
- fResultBuffer.append("<");
+ fResultBuffer.append("<");
break;
case '>': // special html escape character
if (lastIndex < (currentIndex - 1)) {
} else {
lastIndex++;
}
- fResultBuffer.append(">");
+ fResultBuffer.append(">");
break;
case '&': // special html escape character
if (lastIndex < (currentIndex - 1)) {
} else {
lastIndex++;
}
- fResultBuffer.append("&");
+ fResultBuffer.append("&");
break;
case '\'': // special html escape character
if (lastIndex < (currentIndex - 1)) {
} else {
lastIndex++;
}
- fResultBuffer.append(""");
+ fResultBuffer.append(""");
break;
}
}
copyWhite(fWhiteStart, fWhiteStartPosition, 1);
fWhiteStart = false;
- int startHeadPosition = fCurrentPosition-1;
+ int startHeadPosition = fCurrentPosition - 1;
if (readUntilEOL()) {
// TODO not correct - improve this
String head = new String(fSource, startHeadPosition, fCurrentPosition - startHeadPosition);
if (foundUrl) {
String urlString = new String(fSource, urlStartPosition - 1, fCurrentPosition - urlStartPosition);
fCurrentPosition--;
- WikipediaFilter.createExternalLink(fResultBuffer, fWikiEngine, urlString);
+ createExternalLink(urlString);
continue;
}
break;
// table
// syntax
continue;
-// } else {
-// SnipSnap / Radeox Macro Syntax
-// if (readUntilChar('}')) {
-// String macroStartTag;
-//
-// macroStartTag = new String(fSource, startMacroPosition, fCurrentPosition - startMacroPosition - 1);
-// if (macroStartTag != null) {
-// createMacro(startMacroPosition, macroStartTag);
-// continue;
-// }
-// }
+ // } else {
+ // SnipSnap / Radeox Macro Syntax
+ // if (readUntilChar('}')) {
+ // String macroStartTag;
+ //
+ // macroStartTag = new String(fSource, startMacroPosition, fCurrentPosition - startMacroPosition - 1);
+ // if (macroStartTag != null) {
+ // createMacro(startMacroPosition, macroStartTag);
+ // continue;
+ // }
+ // }
}
break;
case '<':
}
fResultBuffer.append("<span class=\"nobr\">");
fResultBuffer.append("<a href=\"");
- fResultBuffer.append(Encoder.escape(urlString));
+// fResultBuffer.append(Encoder.escape(urlString));
+ copyWhite(urlString);
fResultBuffer.append("\">");
- fResultBuffer.append(Encoder.toEntity(alias.charAt(0)) + alias.substring(1));
+// fResultBuffer.append(Encoder.toEntity(alias.charAt(0)) + alias.substring(1));
+ copyWhite(alias);
fResultBuffer.append("</a></span>");
}
// else {
// fCurrentPosition = temp - 1;
// return false;
// }
+ public void createExternalLink(String urlString) {
+ // Does our engine know images?
+ if (fWikiEngine instanceof ImageRenderEngine) {
+ fResultBuffer.append(((ImageRenderEngine) fWikiEngine).getExternalImageLink());
+ }
+ fResultBuffer.append("<span class=\"nobr\">");
+ fResultBuffer.append("<a href=\"");
+// fResultBuffer.append(Encoder.escape(urlString));
+ copyWhite(urlString);
+ fResultBuffer.append("\">");
+// fResultBuffer.append(Encoder.escape(urlString));
+ copyWhite(urlString);
+ fResultBuffer.append("</a></span>");
+ }
+
private void handleWikipediaLink(String linkText, String suffix) {
String name = linkText;
if (name != null) {
// Configuration probably wrote [http://radeox.org] instead of
// http://radeox.org
if (index != -1) {
- WikipediaFilter.createExternalLink(fResultBuffer, fWikiEngine, name.substring(index));
+ createExternalLink(name.substring(index));
// show error
// fResult.append("<div class=\"error\">Do not surround URLs
// with [...].</div>");
// name = name.substring(colonIndex + 1);
// }
- int atIndex = name.lastIndexOf('@');
+// int atIndex = name.lastIndexOf('@');
// InterWiki link ?
- if (-1 != atIndex) {
- String extSpace = name.substring(atIndex + 1);
- // known extarnal space ?
- InterWiki interWiki = InterWiki.getInstance();
- if (interWiki.contains(extSpace)) {
- name = name.substring(0, atIndex);
- Writer writer = new StringBufferWriter(fResultBuffer);
- try {
- if (-1 != hashIndex) {
- interWiki.expand(writer, extSpace, name, hash);
- } else {
- interWiki.expand(writer, extSpace, name, "");
- }
- } catch (IOException e) {
- // log.debug("InterWiki " + extSpace + " not found.");
- }
- } else {
- fResultBuffer.append("[<span class=\"error\">");
- fResultBuffer.append(name);
- fResultBuffer.append("?</span>]");
- }
- } else {
+// if (-1 != atIndex) {
+// String extSpace = name.substring(atIndex + 1);
+// // known extarnal space ?
+// InterWiki interWiki = InterWiki.getInstance();
+// if (interWiki.contains(extSpace)) {
+// name = name.substring(0, atIndex);
+// Writer writer = new StringBufferWriter(fResultBuffer);
+// try {
+// if (-1 != hashIndex) {
+// interWiki.expand(writer, extSpace, name, hash);
+// } else {
+// interWiki.expand(writer, extSpace, name, "");
+// }
+// } catch (IOException e) {
+// // log.debug("InterWiki " + extSpace + " not found.");
+// }
+// } else {
+// fResultBuffer.append("[<span class=\"error\">");
+// fResultBuffer.append(name);
+// fResultBuffer.append("?</span>]");
+// }
+// } else {
// internal link
+ name = Encoder.escape(name);
if (name.startsWith("Image:") && (fWikiEngine instanceof ImageRenderEngine)) {
// server part of rendering images
ImageRenderEngine imageEngine = (ImageRenderEngine) fWikiEngine;
// cannot display/create wiki, so just display the text
fResultBuffer.append(name);
}
- }
+// }
}
}
}