001: /*
002: JSPWiki - a JSP-based WikiWiki clone.
003:
004: Copyright (C) 2001-2006 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.parser;
021:
022: import java.text.MessageFormat;
023: import java.util.HashMap;
024: import java.util.Iterator;
025: import java.util.Map;
026: import java.util.ResourceBundle;
027:
028: import org.jdom.Text;
029:
030: import com.ecyrd.jspwiki.WikiContext;
031: import com.ecyrd.jspwiki.WikiEngine;
032: import com.ecyrd.jspwiki.plugin.PluginException;
033: import com.ecyrd.jspwiki.plugin.WikiPlugin;
034: import com.ecyrd.jspwiki.render.RenderingManager;
035:
036: /**
037: * Stores the contents of a plugin in a WikiDocument DOM tree.
038: *
039: * @author Janne Jalkanen
040: * @since 2.4
041: */
042: public class PluginContent extends Text {
043: private static final String BLANK = "";
044: private static final String CMDLINE = "_cmdline";
045: private static final String ELEMENT_BR = "<br/>";
046: private static final String EMITTABLE_PLUGINS = "Image|FormOpen|FormClose|FormInput|FormTextarea|FormSelect";
047: private static final String LINEBREAK = "\n";
048: private static final String PLUGIN_START = "[{";
049: private static final String PLUGIN_END = "}]";
050: private static final String SPACE = " ";
051:
052: private static final long serialVersionUID = 1L;
053:
054: private String m_pluginName;
055: private Map m_params;
056:
057: public PluginContent(String pluginName, Map parameters) {
058: m_pluginName = pluginName;
059: m_params = parameters;
060: }
061:
062: /**
063: * @since 2.5.7
064: */
065: public String getPluginName() {
066: return m_pluginName;
067: }
068:
069: public Object getParameter(String name) {
070: return m_params.get(name);
071: }
072:
073: public Map getParameters() {
074: return m_params;
075: }
076:
077: public String getValue() {
078: return getText();
079: }
080:
081: public String getText() {
082: String result;
083:
084: WikiDocument doc = (WikiDocument) getDocument();
085:
086: if (doc == null) {
087: //
088: // This element has not yet been attached anywhere, so we simply assume there is
089: // no rendering and return the plugin name. This is required e.g. when the
090: // paragraphify() checks whether the element is empty or not. We can't of course
091: // know whether the rendering would result in an empty string or not, but let us
092: // assume it does not.
093: //
094:
095: return getPluginName();
096: }
097:
098: WikiContext context = doc.getContext();
099:
100: Boolean wysiwygVariable = (Boolean) context
101: .getVariable(RenderingManager.WYSIWYG_EDITOR_MODE);
102: boolean wysiwygEditorMode = false;
103: if (wysiwygVariable != null) {
104: wysiwygEditorMode = wysiwygVariable.booleanValue();
105: }
106:
107: try {
108: //
109: // Determine whether we should emit the actual code for this plugin or
110: // whether we should execute it. For some plugins we always execute it,
111: // since they can be edited visually.
112: //
113: // FIXME: The plugin name matching should not be done here, but in a per-editor resource
114: if (wysiwygEditorMode
115: && !m_pluginName.matches(EMITTABLE_PLUGINS)) {
116: result = PLUGIN_START + m_pluginName + SPACE;
117:
118: // convert newlines to <br> in case the plugin has a body.
119: String cmdLine = ((String) m_params.get(CMDLINE))
120: .replaceAll(LINEBREAK, ELEMENT_BR);
121:
122: result = result + cmdLine + PLUGIN_END;
123: } else {
124: Boolean b = (Boolean) context
125: .getVariable(RenderingManager.VAR_EXECUTE_PLUGINS);
126: if (b != null && !b.booleanValue())
127: return BLANK;
128:
129: WikiEngine engine = context.getEngine();
130:
131: HashMap parsedParams = new HashMap();
132:
133: //
134: // Parse any variable instances from the string
135: //
136: for (Iterator i = m_params.entrySet().iterator(); i
137: .hasNext();) {
138: Map.Entry e = (Map.Entry) i.next();
139:
140: Object val = e.getValue();
141:
142: if (val instanceof String) {
143: val = engine.getVariableManager()
144: .expandVariables(context, (String) val);
145: }
146:
147: parsedParams.put(e.getKey(), val);
148: }
149:
150: result = engine.getPluginManager().execute(context,
151: m_pluginName, parsedParams);
152: }
153: } catch (Exception e) {
154: if (wysiwygEditorMode) {
155: result = "";
156: } else {
157: // log.info("Failed to execute plugin",e);
158: ResourceBundle rb = context
159: .getBundle(WikiPlugin.CORE_PLUGINS_RESOURCEBUNDLE);
160: Object[] args = { e.getMessage() };
161: result = JSPWikiMarkupParser
162: .makeError(
163: MessageFormat
164: .format(
165: rb
166: .getString("plugin.error.insertionfailed"),
167: args)).getText();
168: }
169: }
170:
171: return result;
172: }
173:
174: /**
175: * Executes the executeParse() method.
176: *
177: * @param m_context
178: */
179: public void executeParse(WikiContext m_context)
180: throws PluginException {
181: m_context.getEngine().getPluginManager().executeParse(this,
182: m_context);
183: }
184:
185: }
|