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.api.core.Context; 023import org.apache.wiki.api.exceptions.PluginException; 024import org.apache.wiki.api.plugin.Plugin; 025import org.apache.wiki.preferences.Preferences; 026import org.apache.wiki.util.XHTML; 027import org.apache.wiki.util.XhtmlUtil; 028import org.jdom2.Element; 029 030import java.util.HashMap; 031import java.util.Map; 032import java.util.ResourceBundle; 033 034/** 035 * Creates a Form text area element. You may specify the size of the textarea 036 * by using the {@link #PARAM_COLS} and {@link #PARAM_ROWS} to signify the width 037 * and height of the area. 038 */ 039public class FormTextarea extends FormElement { 040 /** Parameter name for setting the rows value. Value is <tt>{@value}</tt>. */ 041 public static final String PARAM_ROWS = "rows"; 042 043 /** Parameter name for setting the columns value. Value is <tt>{@value}</tt>. */ 044 public static final String PARAM_COLS = "cols"; 045 046 /** 047 * {@inheritDoc} 048 */ 049 @Override 050 public String execute( final Context ctx, final Map< String, String > params ) throws PluginException { 051 // Don't render if no error and error-only-rendering is on. 052 final FormInfo info = getFormInfo( ctx ); 053 Map< String, String > previousValues = null; 054 final ResourceBundle rb = Preferences.getBundle( ctx, Plugin.CORE_PLUGINS_RESOURCEBUNDLE ); 055 056 if ( info != null ) { 057 if ( info.hide() ) { 058 return "<p>" + rb.getString( "formclose.noneedtoshow" ) + "</p>"; 059 } 060 previousValues = info.getSubmission(); 061 } 062 063 if ( previousValues == null ) { 064 previousValues = new HashMap<>(); 065 } 066 067 final Element field = buildTextArea( params, previousValues, rb ); 068 069 // We should look for extra params, e.g. width, ..., here. 070 return XhtmlUtil.serialize(field, XhtmlUtil.EXPAND_EMPTY_NODES ); // ctx.getEngine().getContentEncoding() 071 } 072 073 private Element buildTextArea( 074 final Map< String, String > params, 075 final Map< String, String > previousValues, 076 final ResourceBundle rb ) 077 throws PluginException { 078 final String inputName = params.get(PARAM_INPUTNAME); 079 final String rows = params.get(PARAM_ROWS); 080 final String cols = params.get(PARAM_COLS); 081 082 if ( inputName == null ) { 083 throw new PluginException( rb.getString( "formtextarea.namemissing" ) ); 084 } 085 086 // In order to isolate posted form elements into their own map, prefix the variable name here. It will be stripped 087 // when the handler plugin is executed. 088 final Element field = XhtmlUtil.element(XHTML.textarea); 089 field.setAttribute(XHTML.ATTR_name,HANDLERPARAM_PREFIX + inputName); 090 if ( rows != null ) { 091 field.setAttribute(XHTML.ATTR_rows,rows); 092 } 093 if ( cols != null ) { 094 field.setAttribute(XHTML.ATTR_cols,cols); 095 } 096 097 if ( previousValues != null ) { 098 String oldValue = previousValues.get( inputName ); 099 if( oldValue != null ) { 100 field.addContent( oldValue ); 101 } else { 102 oldValue = params.get( PARAM_VALUE ); 103 if( oldValue != null ) { 104 field.addContent( oldValue ); 105 } 106 } 107 } 108 return field; 109 } 110 111}