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    
020    package org.apache.wiki.forms;
021    
022    import org.apache.wiki.WikiContext;
023    import org.apache.wiki.api.exceptions.PluginException;
024    import org.apache.wiki.api.plugin.WikiPlugin;
025    import org.apache.wiki.preferences.Preferences;
026    import org.apache.wiki.util.TextUtil;
027    import org.apache.wiki.util.XHTML;
028    import org.apache.wiki.util.XhtmlUtil;
029    import org.jdom2.Element;
030    
031    import java.util.HashMap;
032    import java.util.Map;
033    import java.util.ResourceBundle;
034    
035    /**
036     *  Creates a simple input text field.
037     */
038    public 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        /**
047         * Generates a dynamic form element on the WikiPage.
048         * 
049         * {@inheritDoc}
050         */
051        public String execute( WikiContext ctx, Map< String, String > params )
052            throws PluginException
053        {
054            String inputName  = params.get( PARAM_INPUTNAME );
055            String inputValue = params.get( PARAM_VALUE );
056            String inputType  = params.get( PARAM_TYPE );
057            String size       = params.get( PARAM_SIZE );
058            ResourceBundle rb = Preferences.getBundle( ctx, WikiPlugin.CORE_PLUGINS_RESOURCEBUNDLE );
059    
060            if ( inputName == null ) {
061                throw new PluginException( rb.getString( "forminput.namemissing" ) );
062            }
063            if ( inputValue == null ) {
064                inputValue = "";
065            }
066            
067            // Don't render if no error and error-only-rendering is on.
068            FormInfo info = getFormInfo( ctx );
069            Map< String, String > previousValues = null;
070            if ( info != null )
071            {
072                if ( info.hide() )
073                {
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            {
082                previousValues = new HashMap< String, String >();
083            }
084    
085            // In order to isolate posted form elements into their own
086            // map, prefix the variable name here. It will be stripped
087            // when the handler plugin is executed.
088            
089            Element field = XhtmlUtil.input(inputType,HANDLERPARAM_PREFIX + inputName,inputValue);
090            
091            String checked = params.get("checked");
092            field.setAttribute(XHTML.ATTR_class,
093                    String.valueOf(TextUtil.isPositive(checked) || checked.equalsIgnoreCase("checked")));
094            
095            String oldValue = previousValues.get( inputName );
096            if ( oldValue != null )
097            {
098                field.setAttribute(XHTML.ATTR_value,oldValue);
099            }
100    
101            if ( size != null ) {
102                field.setAttribute(XHTML.ATTR_size,size);
103            }
104    
105            return XhtmlUtil.serialize(field); // ctx.getEngine().getContentEncoding()
106        }
107    }