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.TextUtil;
027import org.apache.wiki.util.XHTML;
028import org.apache.wiki.util.XhtmlUtil;
029import org.jdom2.Element;
030
031import java.util.HashMap;
032import java.util.Map;
033import java.util.ResourceBundle;
034
035/**
036 *  Creates a simple input text field.
037 */
038public class FormInput extends FormElement {
039
040    /** Parameter name for setting the type.  Value is <tt>{@value}</tt>. */
041    public static final String PARAM_TYPE  = "type";
042
043    /** Parameter name for setting the size of the input field.  Value is <tt>{@value}</tt>. */
044    public static final String PARAM_SIZE  = "size";
045
046    public static final String PARAM_CHECKED  = "checked";
047
048    /**
049     * Generates a dynamic form element on the WikiPage.
050     * 
051     * {@inheritDoc}
052     */
053    @Override
054    public String execute( final Context ctx, final Map< String, String > params ) throws PluginException {
055        final String inputName  = params.get( PARAM_INPUTNAME );
056        String inputValue = params.get( PARAM_VALUE );
057        final String inputType  = params.get( PARAM_TYPE );
058        final String size       = params.get( PARAM_SIZE );
059        final String checked    = params.get( PARAM_CHECKED );
060        final ResourceBundle rb = Preferences.getBundle( ctx, Plugin.CORE_PLUGINS_RESOURCEBUNDLE );
061
062        if ( inputName == null ) {
063            throw new PluginException( rb.getString( "forminput.namemissing" ) );
064        }
065        if ( inputValue == null ) {
066            inputValue = "";
067        }
068        
069        // Don't render if no error and error-only-rendering is on.
070        final FormInfo info = getFormInfo( ctx );
071        Map< String, String > previousValues = null;
072        if ( info != null ) {
073            if ( info.hide() ) {
074//              return XhtmlUtil.serialize(XhtmlUtil.element(XHTML.p,rb.getString("forminput.noneedtoshow"))); // nope
075                return "<p>" + rb.getString( "forminput.noneedtoshow" ) + "</p>";
076            }
077            previousValues = info.getSubmission();
078        }
079
080        if ( previousValues == null ) {
081            previousValues = new HashMap<>();
082        }
083
084        // In order to isolate posted form elements into their own
085        // map, prefix the variable name here. It will be stripped
086        // when the handler plugin is executed.
087        
088        final Element field = XhtmlUtil.input(inputType,HANDLERPARAM_PREFIX + inputName,inputValue);
089        field.setAttribute( XHTML.ATTR_class, String.valueOf( TextUtil.isPositive( checked ) || "checked".equalsIgnoreCase( checked ) ) );
090
091        final String oldValue = previousValues.get( inputName );
092        if ( oldValue != null ) {
093            field.setAttribute(XHTML.ATTR_value,oldValue);
094        }
095
096        if ( size != null ) {
097            field.setAttribute(XHTML.ATTR_size,size);
098        }
099
100        return XhtmlUtil.serialize(field); // ctx.getEngine().getContentEncoding()
101    }
102
103}