001/* 002 Licensed to the Apache Software Foundation (ASF) under one 003 or more contributor license agreements. See the NOTICE file 004 distributed with this work for additional information 005 regarding copyright ownership. The ASF licenses this file 006 to you under the Apache License, Version 2.0 (the 007 "License"); you may not use this file except in compliance 008 with the License. You may obtain a copy of the License at 009 010 http://www.apache.org/licenses/LICENSE-2.0 011 012 Unless required by applicable law or agreed to in writing, 013 software distributed under the License is distributed on an 014 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 KIND, either express or implied. See the License for the 016 specific language governing permissions and limitations 017 under the License. 018 */ 019 020package org.apache.wiki.forms; 021 022import org.apache.wiki.WikiContext; 023import org.apache.wiki.api.exceptions.PluginException; 024import org.apache.wiki.api.plugin.WikiPlugin; 025import org.apache.wiki.preferences.Preferences; 026import org.apache.wiki.util.XHTML; 027import org.apache.wiki.util.XhtmlUtil; 028import org.jdom2.Element; 029import org.jdom2.output.Format; 030 031import java.util.HashMap; 032import java.util.Map; 033import java.util.ResourceBundle; 034 035/** 036 * Creates a Form text area element. You may specify the size of the textarea 037 * by using the {@link #PARAM_COLS} and {@link #PARAM_ROWS} to signify the width 038 * and height of the area. 039 */ 040public class FormTextarea extends FormElement 041{ 042 /** Parameter name for setting the rows value. Value is <tt>{@value}</tt>. */ 043 public static final String PARAM_ROWS = "rows"; 044 045 /** Parameter name for setting the columns value. Value is <tt>{@value}</tt>. */ 046 public static final String PARAM_COLS = "cols"; 047 048 /** 049 * {@inheritDoc} 050 */ 051 public String execute( WikiContext ctx, Map< String, String > params ) throws PluginException { 052 // Don't render if no error and error-only-rendering is on. 053 FormInfo info = getFormInfo( ctx ); 054 Map< String, String > previousValues = null; 055 ResourceBundle rb = Preferences.getBundle( ctx, WikiPlugin.CORE_PLUGINS_RESOURCEBUNDLE ); 056 057 if ( info != null ) { 058 if ( info.hide() ) { 059 return "<p>" + rb.getString( "formclose.noneedtoshow" ) + "</p>"; 060 } 061 previousValues = info.getSubmission(); 062 } 063 064 if ( previousValues == null ) { 065 previousValues = new HashMap< String, String >(); 066 } 067 068 Element field = buildTextArea( params, previousValues, rb ); 069 070 // We should look for extra params, e.g. width, ..., here. 071 return XhtmlUtil.serialize(field, XhtmlUtil.EXPAND_EMPTY_NODES ); // ctx.getEngine().getContentEncoding() 072 } 073 074 private Element buildTextArea( 075 Map<String,String> params, 076 Map<String,String> previousValues, 077 ResourceBundle rb ) 078 throws PluginException 079 { 080 String inputName = params.get(PARAM_INPUTNAME); 081 String rows = params.get(PARAM_ROWS); 082 String cols = params.get(PARAM_COLS); 083 084 if ( inputName == null ) { 085 throw new PluginException( rb.getString( "formtextarea.namemissing" ) ); 086 } 087 088 // In order to isolate posted form elements into their own 089 // map, prefix the variable name here. It will be stripped 090 // when the handler plugin is executed. 091 Element field = XhtmlUtil.element(XHTML.textarea); 092 field.setAttribute(XHTML.ATTR_name,HANDLERPARAM_PREFIX + inputName); 093 if ( rows != null ) { 094 field.setAttribute(XHTML.ATTR_rows,rows); 095 } 096 if ( cols != null ) { 097 field.setAttribute(XHTML.ATTR_cols,cols); 098 } 099 100 if ( previousValues != null ) 101 { 102 String oldValue = previousValues.get( inputName ); 103 if ( oldValue != null ) 104 { 105 field.addContent(oldValue); 106 } 107 else 108 { 109 oldValue = params.get(PARAM_VALUE); 110 if ( oldValue != null ) { 111 field.addContent(oldValue); 112 } 113 } 114 } 115 return field; 116 } 117 118}