001: /**
002: * Controls generation of external links based on the linking.properties file at the bottom
003: * of the classpath.
004: *
005: * The file controls how link syntax is hyperlinked
006: * Syntax: prefix=mylink
007: * mylink can include the keyword expansion ${url} to add the suffix of the link
008: * if you want to have the external link open in a new frame, or in a different frame,
009: * a key of the form "prefix.target" can be specified, e.g.:
010: *
011: * c2=http://c2.com/cgi/wiki?${url}
012: * c2.target=#blank
013: *
014: * @author garethc
015: * 8/11/2002 08:53:38
016: */package vqwiki.lex;
017:
018: import org.apache.log4j.Logger;
019: import vqwiki.Environment;
020:
021: import java.io.IOException;
022: import java.util.Properties;
023:
024: public class LinkExtender {
025:
026: private static final Logger logger = Logger
027: .getLogger(LinkExtender.class);
028: private static final String LINKING_PROPERTIES_FILE = "/linking.properties";
029: public static final String URL_KEYWORD = "${url}";
030: private static Properties linkingProperties;
031:
032: /**
033: * Use the prefix to produce a hyperlink based on entries in the linking.properties file
034: *
035: * @param prefix the prefix
036: * @param url the parameter as given by the user
037: * @param text
038: * @return
039: * @throws Exception
040: */
041: public static String generateLink(String prefix, String url,
042: String text) throws Exception {
043: return generateLink(prefix, url, text, false);
044: }
045:
046: /**
047: * Use the prefix to produce a hyperlink based on entries in the linking.properties file
048: *
049: * @param prefix the prefix
050: * @param url the parameter as given by the user
051: * @param text
052: * @param onlyExternalLinks true if there are only external links converted, false otherwise.
053: * @return HTML representation of this link.
054: * @throws Exception
055: */
056: public static String generateLink(String prefix, String url,
057: String text, boolean onlyExternalLinks) throws Exception {
058: logger.debug("generating link for prefix=" + prefix + ",url="
059: + url);
060: String displayName = null;
061: int displayNameDelimLocation = url.indexOf('|');
062: if (displayNameDelimLocation >= 0) {
063: displayName = url.substring(displayNameDelimLocation + 1);
064: url = url.substring(0, displayNameDelimLocation);
065: }
066: Properties linkingProperties = getLinkingProperties();
067: String expansion = linkingProperties.getProperty(prefix);
068: if (expansion == null) {
069: logger.info("no expansion found for link extension: "
070: + prefix);
071: return "<span class=\"extendlinkproblem\">" + text
072: + "</span>";
073: }
074: while (true) {
075: int urlLocation = expansion.indexOf(URL_KEYWORD);
076: if (urlLocation >= 0) {
077: StringBuffer buffer = new StringBuffer();
078: buffer.append(expansion.substring(0, urlLocation));
079: buffer.append(url);
080: buffer.append(expansion.substring(urlLocation
081: + URL_KEYWORD.length(), expansion.length()));
082: expansion = buffer.toString();
083: } else {
084: break;
085: }
086: }
087: String target = linkingProperties.getProperty(prefix
088: + ".target");
089: StringBuffer buffer = new StringBuffer();
090: if (expansion.startsWith("http") || expansion.startsWith("ftp")
091: || expansion.startsWith("mailto")
092: || expansion.startsWith("news")
093: || expansion.startsWith("telnet")
094: || expansion.startsWith("file")) {
095: buffer.append("<a class=\"externallink\" href=\"");
096: } else {
097: if (onlyExternalLinks) {
098: if (displayName == null) {
099: String showPrefix = linkingProperties
100: .getProperty(prefix + ".showprefix");
101: if (showPrefix != null
102: && showPrefix.equals("false")) {
103: return url;
104: } else {
105: return text;
106: }
107: } else {
108: return displayName;
109: }
110: }
111: buffer.append("<a href=\"");
112: }
113: buffer.append(expansion);
114: buffer.append("\" ");
115: if (target != null) {
116: buffer.append("target=\"");
117: buffer.append(target);
118: buffer.append("\"");
119: }
120: buffer.append(">");
121: if (displayName == null) {
122: String showPrefix = linkingProperties.getProperty(prefix
123: + ".showprefix");
124: if (showPrefix != null && showPrefix.equals("false")) {
125: buffer.append(url);
126: } else {
127: buffer.append(text);
128: }
129: } else {
130: buffer.append(displayName);
131: }
132: buffer.append("</a>");
133: return buffer.toString();
134: }
135:
136: private static Properties getLinkingProperties() throws IOException {
137: if (linkingProperties == null) {
138: linkingProperties = new Properties();
139: linkingProperties.load(Environment.getInstance()
140: .getResourceAsStream(LINKING_PROPERTIES_FILE));
141: }
142: return linkingProperties;
143: }
144:
145: /**
146: * Produce a link that doesn't use the prefix: scheme but can have a linking.properties entry
147: * hyperlinks.target=...
148: *
149: * @param destination
150: * @return
151: */
152: public static String generateNonExpandedLink(String destination)
153: throws IOException {
154: StringBuffer buffer = new StringBuffer();
155: if (destination.startsWith("http")
156: || destination.startsWith("ftp")
157: || destination.startsWith("mailto")
158: || destination.startsWith("news")
159: || destination.startsWith("telnet")
160: || destination.startsWith("file")) {
161: buffer.append("<a class=\"externallink\" href=\"");
162: } else {
163: buffer.append("<a href=\"");
164: }
165: buffer.append(destination);
166: buffer.append("\" ");
167: String target = getLinkingProperties().getProperty(
168: "hyperlinks.target");
169: if (target != null) {
170: buffer.append("target=\"");
171: buffer.append(target);
172: buffer.append("\"");
173: }
174: buffer.append(">");
175: buffer.append(destination);
176: buffer.append("</a>");
177: return buffer.toString();
178: }
179: }
|