package com.mckessonaps.jspwiki.handler.newpage; import java.io.UnsupportedEncodingException; import java.util.Iterator; import java.util.Map; import com.ecyrd.jspwiki.*; import com.ecyrd.jspwiki.forms.FormHandler; import com.ecyrd.jspwiki.plugin.PluginException; import com.ecyrd.jspwiki.plugin.PluginManager; import com.ecyrd.jspwiki.plugin.WikiPlugin; /** * Creates a new page using the information provided by a form. Requried * parameters are: _body and newPageName Optional parameters are: * newPageNamePrefix - if you want a prefix automatically applied; autoIncrement - * if you want suffix numbers generated to make a unique page name. Patched two * bugs: - Umlate where not correctly translated - You can use Plugins now, use * ({Plugin}) in your template syntax. Christoph Sauer, 14-Sep-2006 */ public class NewPageHandler2 implements FormHandler, WikiPlugin { private static final String PARAM_NEW_PAGE_NAME = "newPageName"; private static final String PARAM_NEW_PAGE_NAME_PREFIX = "newPageNamePrefix"; private static final String PARAM_AUTO_INCREMENT = "autoIncrement"; public String execute(WikiContext context, Map params) throws PluginException { String template = (String) params.get(PluginManager.PARAM_BODY); if ((null == template) || (template.trim().equals(""))) return "No template text for the new page was provided in the body of FormOutput."; // String newPageName = (String) params.get(PARAM_NEW_PAGE_NAME); String newPageName = safeParameter(params, PARAM_NEW_PAGE_NAME, context); if ((null == newPageName) || (newPageName.trim().equals(""))) return "Parameter 'newPageName' must be provided"; String prefix = (String) params.get(PARAM_NEW_PAGE_NAME_PREFIX); if (null != prefix) { newPageName = prefix.trim() + newPageName; } newPageName = TranslatorReader.cleanLink(newPageName); if (exists(context, newPageName)) { boolean autoIncrement = (null != params.get(PARAM_AUTO_INCREMENT)); if (!autoIncrement) return "" + newPageName + " already exists."; newPageName = autoIncrement(context, newPageName); } String newPageWikiText = expandTemplate(params, template, context); String message = saveNewPage(context, newPageWikiText, newPageName); return message; } private boolean exists(WikiContext context, String newPageName) { return context.getEngine().pageExists(newPageName); } private String autoIncrement(WikiContext context, String barePageName) { int suffix = 1; String pageName = barePageName + suffix; while (exists(context, pageName)) { suffix++; pageName = barePageName + suffix; } return pageName; } private String expandTemplate(Map params, String template, WikiContext context) { for (Iterator iter = params.keySet().iterator(); iter.hasNext();) { String paramName = (String) iter.next(); String paramLabel = "\\$\\(" + paramName + "\\)"; String value = safeParameter(params, paramName, context); template = template.replaceAll(paramLabel, value); template = escapePluginSyntax(template); } return template; } private String safeParameter(Map params, String paramName, WikiContext context) { WikiEngine engine = context.getEngine(); String encoding = "UTF-8"; if (engine != null) { encoding = engine.getContentEncoding(); } // FIX: // check if returned parameter is actually a String (and not probably // a String array -> _bounds parameter!) String value = ""; if (params.get(paramName) instanceof String) { value = (String) params.get(paramName); } try { // see Problem Description in coment to // WikiEngine.saveGetParameter() value = new String(value.getBytes("ISO-8859-1"), encoding); } catch (UnsupportedEncodingException e) { System.out.println("WARNING ScriptParameter: Unsupported encoding" + e); value = (String) params.get(paramName); } return value; } private String saveNewPage(WikiContext context, String template, String newPageName) { String message; // FIX: // constructor for the WikiPage requires the WikiEngine WikiPage newPage = new WikiPage(context.getEngine(), newPageName); WikiContext newContext = (WikiContext) context.clone(); newContext.setPage(newPage); try { context.getEngine().saveText(newContext, template); message = "" + newPageName + " created."; } catch (WikiException e) { message = "Failed to save page: " + newPageName; } return message; } // to enable Plugins within plugins this is quick workaround: use // ({ }) instead of [{ }] in our dynamic output. private String escapePluginSyntax(String s) { if (s == null) { return null; } s = s.replaceAll("\\(\\{", "\\[\\{"); s = s.replaceAll("\\}\\)", "\\}\\]"); return s; } }