|
From: <jbo...@li...> - 2005-08-11 19:45:53
|
Author: ral...@jb...
Date: 2005-08-11 15:44:28 -0400 (Thu, 11 Aug 2005)
New Revision: 826
Added:
trunk/forge/portal-extensions/forge-wiki/src/java/org/jboss/wiki/plugins/WikiParser.java
Log:
simple version of wiki parser - transforms wiki links to outside or wikipage links
Added: trunk/forge/portal-extensions/forge-wiki/src/java/org/jboss/wiki/plugins/WikiParser.java
===================================================================
--- trunk/forge/portal-extensions/forge-wiki/src/java/org/jboss/wiki/plugins/WikiParser.java 2005-08-11 19:39:43 UTC (rev 825)
+++ trunk/forge/portal-extensions/forge-wiki/src/java/org/jboss/wiki/plugins/WikiParser.java 2005-08-11 19:44:28 UTC (rev 826)
@@ -0,0 +1,196 @@
+package org.jboss.wiki.plugins;
+
+/**
+ * @author rali
+ *
+ *
+ */
+import java.util.regex.*;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.StringTokenizer;
+
+public class WikiParser {
+
+ private static Pattern myPattern;
+
+ private Matcher myMatcher;
+
+ /* URL components, later to be configurable */
+ private static String portalHome = "http://forge.sicore.org:8080/portal/";
+
+ private static String wikiHome = "index.html?ctrl:id=window.default.WikiPortletWindow";
+
+ private static String actionType = "&ctrl:type=";
+
+ private static String page = "&page=";
+
+ public ArrayList mediaPatterns;
+
+ public ArrayList linkPatterns;
+
+ public static String[] textlinks;
+
+ public static String[] notextlinks;
+
+ private String[] mediaFormats = { "*.png", "*.jpeg", "*.gif" };
+
+ public WikiParser() {
+ }
+
+ /*
+ * compile all links to wiki pages or external pages of the form [...] or
+ * [...|...]
+ */
+ public static String parseLinks(String text, String actionURL) {
+ String translatedContent = text;
+ String link;// = "";
+ String href1regex = "\\[.+\\|.+\\]";
+ String href2regex = "\\[.+\\]";
+ Pattern tLinks = Pattern.compile(href1regex);
+ Pattern links = Pattern.compile(href2regex);
+ Matcher textlinks = tLinks.matcher(text);
+ Matcher nontextlinks = links.matcher(text);
+
+ Matcher match;
+
+ while ((match = tLinks.matcher(translatedContent)).find()) {
+ link = (translatedContent.substring(translatedContent.indexOf("|",
+ match.start() + 1) + 1, match.end() - 1)).trim();
+ if (isExternalLink(link)) //outside link
+ {
+ translatedContent = match.replaceFirst("<a href=\""
+ + link
+ + "\">"
+ + translatedContent.substring(match.start() + 1,
+ translatedContent.indexOf("|",
+ match.start() + 1)) + "</a>");
+ } else
+ //need to implement pageExists
+ translatedContent = match.replaceFirst("<a href=\""
+ + portalHome
+ + wikiHome
+ + actionType
+ + "action"
+ + page
+ + cleanLink(link)
+ + "\">"
+ + translatedContent.substring(match.start() + 1,
+ translatedContent.indexOf("|",
+ match.start() + 1)) + "</a>");
+ }
+
+ while ((match = links.matcher(translatedContent)).find()) {
+ link = (translatedContent.substring(match.start() + 1,
+ match.end() - 1));
+ if (isExternalLink(link)) //outside link
+ {
+ translatedContent = match.replaceFirst("<a href=\""
+ + link
+ + "\">"
+ + link + "</a>");
+ }else
+ translatedContent = match.replaceFirst("<a href=\"" + portalHome
+ + wikiHome
+ + actionType
+ + "action"
+ + page
+ + cleanLink(link)
+ + "\">"+ link + "</a>");
+ }
+
+ translatedContent = translatedContent.replace("\n", "<BR>\n");
+
+ return translatedContent;
+ }
+
+ /* a collection of all media formats allowed on wiki */
+ private Collection getImagePatterns() {
+ ArrayList patterns = new ArrayList();
+ for (int i = 0; i < mediaFormats.length; i++) {
+ patterns.add(mediaFormats[i]);
+ }
+
+ return patterns;
+ }
+
+ /**
+ * Figures out if a link is an off-site link. This recognizes the most
+ * common protocols by checking how it starts.
+ */
+ private static boolean isExternalLink(String link) {
+ return link.startsWith("http:") || link.startsWith("ftp:")
+ || link.startsWith("https:") || link.startsWith("mailto:")
+ || link.startsWith("news:") || link.startsWith("file:");
+ }
+
+ /**
+ * Cleans a Wiki name.
+ * <P>[ This is a link ] -> ThisIsALink
+ *
+ * @param link
+ * Link to be cleared. Null is safe, and causes this to return
+ * null.
+ * @return A cleaned link.
+ *
+ * @since 2.0
+ */
+ public static String cleanLink(String link) {
+ StringBuffer clean = new StringBuffer();
+
+ if (link == null)
+ return null;
+
+ //
+ // Compress away all whitespace and capitalize
+ // all words in between.
+ //
+
+ StringTokenizer st = new StringTokenizer(link, " -");
+
+ while (st.hasMoreTokens()) {
+ StringBuffer component = new StringBuffer(st.nextToken());
+
+ component.setCharAt(0, Character.toUpperCase(component.charAt(0)));
+
+ //
+ // We must do this, because otherwise compiling on JDK 1.4 causes
+ // a downwards incompatibility to JDK 1.3.
+ //
+ clean.append(component.toString());
+ }
+
+ //
+ // Remove non-alphanumeric characters that should not
+ // be put inside WikiNames. Note that all valid
+ // Unicode letters are considered okay for WikiNames.
+ // It is the problem of the WikiPageProvider to take
+ // care of actually storing that information.
+ //
+
+ for (int i = 0; i < clean.length(); i++) {
+ if (!(Character.isLetterOrDigit(clean.charAt(i))
+ || clean.charAt(i) == '_' || clean.charAt(i) == '.')) {
+ clean.deleteCharAt(i);
+ --i; // We just shortened this buffer.
+ }
+ }
+
+ return clean.toString();
+ }
+
+// public static void main(String[] args) {
+// String wikitext = "__Forge Portal__FORGE USER HOW TO:\\\\\n"
+// + "[Add a new project and modify its data|http://www.NewProject.com]\\\\\n"
+// + "[Add and manage project downloads|Project Downloads]\\\\\n"
+// + "[Add and manage project freezone|Project *Freezone]\\\\\n"
+// + "[Project Freezone]\\\\\n"
+// + "[http://www.google.com]\\\\\n";
+// WikiContext wc = new WikiContext();
+//
+// String elements = parseLinks(wikitext, wc);
+//
+// System.out.println(elements);
+// }
+
+}
\ No newline at end of file
|