001: /*
002: JSPWiki - a JSP-based WikiWiki clone.
003:
004: Copyright (C) 2001-2005 Janne Jalkanen (Janne.Jalkanen@iki.fi)
005:
006: This program is free software; you can redistribute it and/or modify
007: it under the terms of the GNU Lesser General Public License as published by
008: the Free Software Foundation; either version 2.1 of the License, or
009: (at your option) any later version.
010:
011: This program is distributed in the hope that it will be useful,
012: but WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: GNU Lesser General Public License for more details.
015:
016: You should have received a copy of the GNU Lesser General Public License
017: along with this program; if not, write to the Free Software
018: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020: package com.ecyrd.jspwiki.plugin;
021:
022: import org.apache.log4j.Logger;
023:
024: import com.ecyrd.jspwiki.*;
025: import java.util.*;
026: import java.io.StringReader;
027: import java.io.IOException;
028:
029: /**
030: * This is a base class for all plugins using referral things.
031: *
032: * <p>Parameters:<br>
033: * maxwidth: maximum width of generated links<br>
034: * separator: separator between generated links (wikitext)<br>
035: * after: output after the link
036: * before: output before the link
037: * @author Janne Jalkanen
038: */
039: public abstract class AbstractReferralPlugin implements WikiPlugin {
040: private static Logger log = Logger
041: .getLogger(AbstractReferralPlugin.class);
042:
043: public static final int ALL_ITEMS = -1;
044: public static final String PARAM_MAXWIDTH = "maxwidth";
045: public static final String PARAM_SEPARATOR = "separator";
046: public static final String PARAM_AFTER = "after";
047: public static final String PARAM_BEFORE = "before";
048:
049: protected int m_maxwidth = Integer.MAX_VALUE;
050: protected String m_before = ""; // null not blank
051: protected String m_separator = ""; // null not blank
052: protected String m_after = "\\\\";
053:
054: protected WikiEngine m_engine;
055:
056: public static final String ALL_WIKIS = "*";
057: public static final String PARAM_WIKI = "wiki";
058: protected String m_wiki = null;
059:
060: /**
061: * Used to initialize some things. All plugins must call this first.
062: *
063: * @since 1.6.4
064: */
065: public void initialize(WikiContext context, Map params)
066: throws PluginException {
067: m_engine = context.getEngine();
068: m_maxwidth = TextUtil.parseIntParameter((String) params
069: .get(PARAM_MAXWIDTH), Integer.MAX_VALUE);
070: if (m_maxwidth < 0)
071: m_maxwidth = 0;
072:
073: String s = (String) params.get(PARAM_SEPARATOR);
074:
075: if (s != null) {
076: m_separator = s;
077: // pre-2.1.145 there was a separator at the end of the list
078: // if they set the parameters, we use the new format of
079: // before Item1 after separator before Item2 after separator before Item3 after
080: m_after = "";
081: }
082:
083: s = (String) params.get(PARAM_BEFORE);
084:
085: if (s != null) {
086: m_before = s;
087: }
088:
089: s = (String) params.get(PARAM_AFTER);
090:
091: if (s != null) {
092: m_after = s;
093: }
094:
095: s = (String) params.get(PARAM_WIKI);
096:
097: if (s != null) {
098: m_wiki = s;
099: }
100:
101: // log.debug( "Requested maximum width is "+m_maxwidth );
102: }
103:
104: /**
105: * Makes WikiText from a Collection.
106: *
107: * @param links Collection to make into WikiText.
108: * @param separator Separator string to use.
109: * @param numItems How many items to show.
110: */
111: protected String wikitizeCollection(Collection links,
112: String separator, int numItems) {
113: if (links == null || links.isEmpty())
114: return ("");
115:
116: StringBuffer output = new StringBuffer();
117:
118: Iterator it = links.iterator();
119: int count = 0;
120:
121: //
122: // The output will be B Item[1] A S B Item[2] A S B Item[3] A
123: //
124: while (it.hasNext()
125: && ((count < numItems) || (numItems == ALL_ITEMS))) {
126: String value = (String) it.next();
127:
128: if (count > 0) {
129: output.append(m_after);
130: output.append(m_separator);
131: }
132:
133: output.append(m_before);
134:
135: // Make a Wiki markup link. See TranslatorReader.
136: output.append("[" + m_engine.beautifyTitle(value) + "]");
137: count++;
138: }
139:
140: //
141: // Output final item - if there have been none, no "after" is printed
142: //
143: if (count > 0)
144: output.append(m_after);
145:
146: return (output.toString());
147: }
148:
149: /**
150: * Makes HTML with common parameters.
151: *
152: * @since 1.6.4
153: */
154: protected String makeHTML(WikiContext context, String wikitext) {
155: String result = "";
156: TranslatorReader in = null;
157:
158: try {
159: in = new TranslatorReader(context, new StringReader(
160: wikitext));
161: in.addLinkTransmutator(new CutMutator(m_maxwidth));
162: in.enableImageInlining(false);
163:
164: result = FileUtil.readContents(in);
165: } catch (IOException e) {
166: log.error("Failed to convert page data to HTML", e);
167: } finally {
168: try {
169: if (in != null)
170: in.close();
171: } catch (Exception e) {
172: log.fatal("Closing failed", e);
173: }
174: }
175:
176: return result;
177: }
178:
179: /**
180: * A simple class that just cuts a String to a maximum
181: * length, adding three dots after the cutpoint.
182: */
183: private class CutMutator implements StringTransmutator {
184: private int m_length;
185:
186: public CutMutator(int length) {
187: m_length = length;
188: }
189:
190: public String mutate(WikiContext context, String text) {
191: if (text.length() > m_length) {
192: return text.substring(0, m_length) + "...";
193: }
194:
195: return text;
196: }
197: }
198: }
|