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.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 public String execute( WikiContext ctx, Map< String, String > params ) 054 throws PluginException 055 { 056 String inputName = params.get( PARAM_INPUTNAME ); 057 String inputValue = params.get( PARAM_VALUE ); 058 String inputType = params.get( PARAM_TYPE ); 059 String size = params.get( PARAM_SIZE ); 060 String checked = params.get( PARAM_CHECKED ); 061 ResourceBundle rb = Preferences.getBundle( ctx, WikiPlugin.CORE_PLUGINS_RESOURCEBUNDLE ); 062 063 if ( inputName == null ) { 064 throw new PluginException( rb.getString( "forminput.namemissing" ) ); 065 } 066 if ( inputValue == null ) { 067 inputValue = ""; 068 } 069 070 // Don't render if no error and error-only-rendering is on. 071 FormInfo info = getFormInfo( ctx ); 072 Map< String, String > previousValues = null; 073 if ( info != null ) 074 { 075 if ( info.hide() ) 076 { 077// return XhtmlUtil.serialize(XhtmlUtil.element(XHTML.p,rb.getString("forminput.noneedtoshow"))); // nope 078 return "<p>" + rb.getString( "forminput.noneedtoshow" ) + "</p>"; 079 } 080 previousValues = info.getSubmission(); 081 } 082 083 if ( previousValues == null ) 084 { 085 previousValues = new HashMap< String, String >(); 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 092 Element field = XhtmlUtil.input(inputType,HANDLERPARAM_PREFIX + inputName,inputValue); 093 094 field.setAttribute(XHTML.ATTR_class, 095 String.valueOf(TextUtil.isPositive(checked) || "checked".equalsIgnoreCase(checked))); 096 097 String oldValue = previousValues.get( inputName ); 098 if ( oldValue != null ) 099 { 100 field.setAttribute(XHTML.ATTR_value,oldValue); 101 } 102 103 if ( size != null ) { 104 field.setAttribute(XHTML.ATTR_size,size); 105 } 106 107 return XhtmlUtil.serialize(field); // ctx.getEngine().getContentEncoding() 108 } 109}