001: package vqwiki.lex;
002:
003: import java.net.URLEncoder;
004: import java.util.Hashtable;
005: import java.util.Stack;
006: import org.apache.log4j.Logger;
007: import vqwiki.Environment;
008: import vqwiki.WikiBase;
009: import vqwiki.servlets.WikiServlet;
010: import vqwiki.utils.JSPUtils;
011: import vqwiki.utils.Utilities;
012:
013: /**
014: * Utility methods used with the Mediawiki lexers.
015: *
016: * @author Ryan Holliday
017: */
018: public class MediaWikiUtil {
019:
020: private static Logger logger = Logger.getLogger(MediaWikiUtil.class
021: .getName());
022:
023: /**
024: * Given a String that represents a Wiki HTML link (a URL with an optional
025: * link text that is enclosed in brackets), return a formatted HTML anchor tag.
026: *
027: * @param raw The raw Wiki syntax that is to be converted into an HTML link.
028: * @return A formatted HTML link for the Wiki syntax.
029: */
030: protected static String buildHtmlLink(String raw) {
031: if (raw == null || raw.length() <= 2) {
032: // no link, display the raw text
033: return raw;
034: }
035: // strip the first and last brackets
036: String link = raw.substring(1, raw.length() - 1).trim();
037: return buildHtmlLinkRaw(link);
038: }
039:
040: /**
041: * Given a String that represents a raw HTML link (a URL link that is
042: * not enclosed in brackets), return a formatted HTML anchor tag.
043: *
044: * @param raw The raw HTML link that is to be converted into an HTML link.
045: * @return A formatted HTML link.
046: */
047: protected static String buildHtmlLinkRaw(String raw) {
048: if (raw == null)
049: return raw;
050: String link = raw.trim();
051: if (link.length() <= 0) {
052: // no link to display
053: return raw;
054: }
055: // search for link text (space followed by text)
056: String punctuation = Utilities.extractTrailingPunctuation(link);
057: String text = "";
058: int pos = link.indexOf(' ');
059: if (pos == -1) {
060: pos = link.indexOf('\t');
061: }
062: if (pos > 0) {
063: text = link.substring(pos + 1).trim();
064: link = link.substring(0, pos).trim();
065: punctuation = "";
066: } else {
067: link = link.substring(0,
068: link.length() - punctuation.length()).trim();
069: text = link;
070: }
071: String html = linkHtml(link, text, punctuation);
072: return (html != null) ? html : raw;
073: }
074:
075: /**
076: *
077: */
078: protected static String buildWikiLink(String raw, String virtualWiki) {
079: if (raw == null || raw.length() <= 4) {
080: // no topic, display the raw text
081: return raw;
082: }
083: // strip the first and last brackets
084: String topic = raw.substring(2, raw.length() - 2).trim();
085: if (topic.length() <= 0) {
086: // empty brackets, no topic to display
087: return raw;
088: }
089: // search for topic text ("|" followed by text)
090: String text = topic.trim();
091: int pos = topic.indexOf('|');
092: if (pos > 0) {
093: text = topic.substring(pos + 1).trim();
094: topic = topic.substring(0, pos).trim();
095: }
096: String url = "Wiki?" + JSPUtils.encodeURL(topic);
097: if (!exists(topic, virtualWiki)) {
098: url = "Wiki?topic=" + JSPUtils.encodeURL(topic)
099: + "&action=" + WikiServlet.ACTION_EDIT;
100: return "<a class=\"newtopic\" title=\"" + topic
101: + "\" href=\"" + url + "\">" + text + "</a>";
102: }
103: return "<a title=\"" + topic + "\" href=\"" + url + "\">"
104: + text + "</a>";
105: }
106:
107: /**
108: *
109: */
110: public static String escapeHtml(String html) {
111: if (html == null)
112: return html;
113: StringBuffer escaped = new StringBuffer();
114: for (int i = 0; i < html.length(); i++) {
115: if (html.charAt(i) == '<') {
116: escaped.append("<");
117: } else if (html.charAt(i) == '>') {
118: escaped.append(">");
119: } else {
120: escaped.append(html.charAt(i));
121: }
122: }
123: return escaped.toString();
124: }
125:
126: /**
127: *
128: */
129: protected static boolean exists(String topic, String virtualWiki) {
130: // FIXME - this causes a database query for every topic in the page,
131: // which is very inefficient
132: try {
133: return WikiBase.getInstance().exists(virtualWiki, topic);
134: } catch (Exception e) {
135: logger.error(e);
136: }
137: return false;
138: }
139:
140: /**
141: *
142: */
143: protected static String linkHtml(String link, String text,
144: String punctuation) {
145: String html = null;
146: String linkLower = link.toLowerCase();
147: if (linkLower.startsWith("http://")) {
148: html = "<a class=\"externallink\" rel=\"nofollow\" title=\""
149: + link
150: + "\" href=\""
151: + link
152: + "\">"
153: + text
154: + "</a>" + punctuation;
155: } else if (linkLower.startsWith("https://")) {
156: text += "⇔";
157: html = "<a class=\"externallink\" rel=\"nofollow\" title=\""
158: + link
159: + "\" href=\""
160: + link
161: + "\">"
162: + text
163: + "</a>" + punctuation;
164: } else if (linkLower.startsWith("ftp://")) {
165: text += "⇔";
166: html = "<a class=\"externallink\" rel=\"nofollow\" title=\""
167: + link
168: + "\" href=\""
169: + link
170: + "\">"
171: + text
172: + "</a>" + punctuation;
173: } else if (linkLower.startsWith("mailto://")) {
174: text += "⇔";
175: html = "<a class=\"externallink\" rel=\"nofollow\" title=\""
176: + link
177: + "\" href=\""
178: + link
179: + "\">"
180: + text
181: + "</a>" + punctuation;
182: } else if (linkLower.startsWith("news://")) {
183: text += "⇔";
184: html = "<a class=\"externallink\" rel=\"nofollow\" title=\""
185: + link
186: + "\" href=\""
187: + link
188: + "\">"
189: + text
190: + "</a>" + punctuation;
191: } else if (linkLower.startsWith("telnet://")) {
192: text += "⇔";
193: html = "<a class=\"externallink\" rel=\"nofollow\" title=\""
194: + link
195: + "\" href=\""
196: + link
197: + "\">"
198: + text
199: + "</a>" + punctuation;
200: } else if (linkLower.startsWith("file://")) {
201: text += "⇔";
202: html = "<a class=\"externallink\" rel=\"nofollow\" title=\""
203: + link
204: + "\" href=\""
205: + link
206: + "\">"
207: + text
208: + "</a>" + punctuation;
209: }
210: return html;
211: }
212: }
|