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 java.util.HashMap;
023import java.util.Map;
024import java.util.ResourceBundle;
025
026import org.apache.wiki.WikiContext;
027import org.apache.wiki.api.exceptions.PluginException;
028import org.apache.wiki.api.plugin.WikiPlugin;
029import org.apache.wiki.preferences.Preferences;
030import org.apache.wiki.util.XHTML;
031import org.apache.wiki.util.XhtmlUtil;
032import org.jdom2.Element;
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{
041    /** Parameter name for setting the rows value.  Value is <tt>{@value}</tt>. */
042    public static final String PARAM_ROWS = "rows";
043
044    /** Parameter name for setting the columns value.  Value is <tt>{@value}</tt>. */
045    public static final String PARAM_COLS = "cols";
046
047    /**
048     *  {@inheritDoc}
049     */
050    public String execute( WikiContext ctx, Map< String, String > params ) throws PluginException {
051        // Don't render if no error and error-only-rendering is on.
052        FormInfo info = getFormInfo( ctx );
053        Map< String, String > previousValues = null;
054        ResourceBundle rb = Preferences.getBundle( ctx, WikiPlugin.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< String, String >();
065        }
066
067        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            Map<String,String> params,
075            Map<String,String> previousValues,
076            ResourceBundle rb )
077            throws PluginException
078    {
079        String inputName = params.get(PARAM_INPUTNAME);
080        String rows = params.get(PARAM_ROWS);
081        String cols = params.get(PARAM_COLS);
082
083        if ( inputName == null ) {
084            throw new PluginException( rb.getString( "formtextarea.namemissing" ) );
085        }
086
087        // In order to isolate posted form elements into their own
088        // map, prefix the variable name here. It will be stripped
089        // when the handler plugin is executed.
090        Element field = XhtmlUtil.element(XHTML.textarea);
091        field.setAttribute(XHTML.ATTR_name,HANDLERPARAM_PREFIX + inputName);
092        if ( rows != null ) {
093            field.setAttribute(XHTML.ATTR_rows,rows);
094        }
095        if ( cols != null ) {
096            field.setAttribute(XHTML.ATTR_cols,cols);
097        }
098
099        if ( previousValues != null )
100        {
101            String oldValue = previousValues.get( inputName );
102            if ( oldValue != null )
103            {
104                field.addContent(oldValue);
105            }
106            else
107            {
108                oldValue = params.get(PARAM_VALUE);
109                if ( oldValue != null ) {
110                    field.addContent(oldValue);
111                }
112            }
113        }
114        return field;
115    }
116
117}