Improved rendering
authoraxelcl <axelcl>
Sat, 19 Feb 2005 00:23:32 +0000 (00:23 +0000)
committeraxelcl <axelcl>
Sat, 19 Feb 2005 00:23:32 +0000 (00:23 +0000)
archive/org.plog4u.wiki.test/src/org/plog4u/wiki/test/filter/HTTPUrlFilterTest.java [new file with mode: 0644]
archive/org.plog4u.wiki.test/src/org/plog4u/wiki/test/filter/WPLinkFilterTest.java [new file with mode: 0644]
archive/org.plog4u.wiki/src/org/plog4u/wiki/filter/Encoder.java [new file with mode: 0644]
archive/org.plog4u.wiki/src/org/plog4u/wiki/filter/WikipediaFilter.java
archive/org.plog4u.wiki/src/org/plog4u/wiki/filter/WikipediaParser.java

diff --git a/archive/org.plog4u.wiki.test/src/org/plog4u/wiki/test/filter/HTTPUrlFilterTest.java b/archive/org.plog4u.wiki.test/src/org/plog4u/wiki/test/filter/HTTPUrlFilterTest.java
new file mode 100644 (file)
index 0000000..e223aff
--- /dev/null
@@ -0,0 +1,29 @@
+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&amp;test_me\">http://www.plog4u.de/index.php&amp;test_me</a></span> ", filter.filter("see http://www.plog4u.de/index.php&test_me ", context));
+  }
+}
diff --git a/archive/org.plog4u.wiki.test/src/org/plog4u/wiki/test/filter/WPLinkFilterTest.java b/archive/org.plog4u.wiki.test/src/org/plog4u/wiki/test/filter/WPLinkFilterTest.java
new file mode 100644 (file)
index 0000000..f66699e
--- /dev/null
@@ -0,0 +1,26 @@
+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 &amp; Tricks", filter.filter("[[Category:Tips & Tricks]]", context));
+  }
+
+}
diff --git a/archive/org.plog4u.wiki/src/org/plog4u/wiki/filter/Encoder.java b/archive/org.plog4u.wiki/src/org/plog4u/wiki/filter/Encoder.java
new file mode 100644 (file)
index 0000000..5f472d4
--- /dev/null
@@ -0,0 +1,109 @@
+/*
+ * 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("&", "&amp;");
+    ESCAPED_CHARS.put("\"", "&quot;");
+    ESCAPED_CHARS.put("'", toEntity('\''));
+    ESCAPED_CHARS.put(">", "&gt;");
+    ESCAPED_CHARS.put("<", "&lt;");
+  }
+  /**
+   * 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();
+  }
+}
index 0645fb9..af37725 100644 (file)
@@ -701,95 +701,95 @@ public class WikipediaFilter extends FilterSupport implements CacheFilter, IWiki
     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("&#91;<span class=\"error\">");
-            result.append(name);
-            result.append("?</span>&#93;");
-          }
-        } 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("&#91;<span class=\"error\">");
+//            result.append(name);
+//            result.append("?</span>&#93;");
+//          }
+//        } 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) {
index 08a0e3b..0662a87 100644 (file)
@@ -1,14 +1,10 @@
 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;
@@ -20,11 +16,9 @@ import org.radeox.api.engine.IncludeRenderEngine;
 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;
 
 /**
@@ -199,7 +193,7 @@ public class WikipediaParser {
           } else {
             lastIndex++;
           }
-          fResultBuffer.append("&#38;");
+          fResultBuffer.append("&amp;");
           break;
         case '\'': // special html escape character
           if (lastIndex < (currentIndex - 1)) {
@@ -217,7 +211,7 @@ public class WikipediaParser {
           } else {
             lastIndex++;
           }
-          fResultBuffer.append("&#34;");
+          fResultBuffer.append("&quot;");
           break;
         }
       }
@@ -242,7 +236,7 @@ public class WikipediaParser {
           fResultBuffer.append(text.substring(lastIndex, currentIndex - 1));
           lastIndex = currentIndex;
         }
-        fResultBuffer.append("&#60;");
+        fResultBuffer.append("&lt;");
         break;
       case '>': // special html escape character
         if (lastIndex < (currentIndex - 1)) {
@@ -251,7 +245,7 @@ public class WikipediaParser {
         } else {
           lastIndex++;
         }
-        fResultBuffer.append("&#62;");
+        fResultBuffer.append("&gt;");
         break;
       case '&': // special html escape character
         if (lastIndex < (currentIndex - 1)) {
@@ -260,7 +254,7 @@ public class WikipediaParser {
         } else {
           lastIndex++;
         }
-        fResultBuffer.append("&#38;");
+        fResultBuffer.append("&amp;");
         break;
       case '\'': // special html escape character
         if (lastIndex < (currentIndex - 1)) {
@@ -278,7 +272,7 @@ public class WikipediaParser {
         } else {
           lastIndex++;
         }
-        fResultBuffer.append("&#34;");
+        fResultBuffer.append("&quot;");
         break;
       }
     }
@@ -700,7 +694,7 @@ public class WikipediaParser {
             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);
@@ -837,7 +831,7 @@ public class WikipediaParser {
           if (foundUrl) {
             String urlString = new String(fSource, urlStartPosition - 1, fCurrentPosition - urlStartPosition);
             fCurrentPosition--;
-            WikipediaFilter.createExternalLink(fResultBuffer, fWikiEngine, urlString);
+            createExternalLink(urlString);
             continue;
           }
           break;
@@ -912,17 +906,17 @@ public class WikipediaParser {
             // 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 '<':
@@ -1341,9 +1335,11 @@ public class WikipediaParser {
         }
         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 {
@@ -1796,6 +1792,21 @@ public class WikipediaParser {
   //    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) {
@@ -1803,7 +1814,7 @@ public class WikipediaParser {
       // 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>");
@@ -1833,31 +1844,32 @@ public class WikipediaParser {
         //                                     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("&#91;<span class=\"error\">");
-            fResultBuffer.append(name);
-            fResultBuffer.append("?</span>&#93;");
-          }
-        } 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("&#91;<span class=\"error\">");
+//            fResultBuffer.append(name);
+//            fResultBuffer.append("?</span>&#93;");
+//          }
+//        } else {
           // internal link
+          name = Encoder.escape(name);
           if (name.startsWith("Image:") && (fWikiEngine instanceof ImageRenderEngine)) {
             // server part of rendering images
             ImageRenderEngine imageEngine = (ImageRenderEngine) fWikiEngine;
@@ -1898,7 +1910,7 @@ public class WikipediaParser {
             // cannot display/create wiki, so just display the text
             fResultBuffer.append(name);
           }
-        }
+//        }
       }
     }
   }