001 package org.apache.fulcrum.intake.model; 002 003 /* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022 import org.apache.commons.lang.StringUtils; 023 024 import org.apache.fulcrum.intake.IntakeException; 025 import org.apache.fulcrum.intake.validator.StringValidator; 026 import org.apache.fulcrum.intake.xmlmodel.XmlField; 027 028 /** 029 * Text field. 030 * 031 * @author <a href="mailto:jmcnally@collab.net">John McNally</a> 032 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a> 033 * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a> 034 * @version $Id: StringField.java 535465 2007-05-05 06:58:06Z tv $ 035 */ 036 public class StringField 037 extends Field 038 { 039 040 /** 041 * Constructor. 042 * 043 * @param field xml field definition object 044 * @param group xml group definition object 045 * @throws IntakeException thrown by superclass 046 */ 047 public StringField(XmlField field, Group group) 048 throws IntakeException 049 { 050 super(field, group); 051 } 052 053 /** 054 * Produces the fully qualified class name of the default validator. 055 * 056 * @return class name of the default validator 057 */ 058 protected String getDefaultValidator() 059 { 060 return StringValidator.class.getName(); 061 } 062 063 /** 064 * Sets the default value for a String field 065 * 066 * @param prop Parameter for the default values 067 */ 068 public void setDefaultValue(String prop) 069 { 070 defaultValue = prop; 071 } 072 073 /** 074 * Set the empty Value. This value is used if Intake 075 * maps a field to a parameter returned by the user and 076 * the corresponding field is either empty (empty string) 077 * or non-existant. 078 * 079 * @param prop The value to use if the field is empty. 080 */ 081 public void setEmptyValue(String prop) 082 { 083 emptyValue = prop; 084 } 085 086 /** 087 * Sets the value of the field from data in the parser. 088 */ 089 protected void doSetValue() 090 { 091 if (isMultiValued) 092 { 093 String[] ss = parser.getStrings(getKey()); 094 String[] sval = new String[ss.length]; 095 for (int i = 0; i < ss.length; i++) 096 { 097 sval[i] = (StringUtils.isNotEmpty(ss[i])) ? ss[i] : (String) getEmptyValue(); 098 } 099 setTestValue(sval); 100 } 101 else 102 { 103 String val = parser.getString(getKey()); 104 setTestValue(StringUtils.isNotEmpty(val) ? val : (String) getEmptyValue()); 105 } 106 } 107 108 /** 109 * Set the value of required. 110 * 111 * @param v Value to assign to required. 112 * @param message an error message 113 */ 114 public void setRequired(boolean v, String message) 115 { 116 this.required = v; 117 if (v) 118 { 119 if (isMultiValued) 120 { 121 String[] ss = (String[]) getTestValue(); 122 if (ss == null || ss.length == 0) 123 { 124 validFlag = false; 125 this.message = message; 126 } 127 else 128 { 129 boolean set = false; 130 for (int i = 0; i < ss.length; i++) 131 { 132 set |= StringUtils.isNotEmpty(ss[i]); 133 if (set) 134 { 135 break; 136 } 137 } 138 if (!set) 139 { 140 validFlag = false; 141 this.message = message; 142 } 143 } 144 } 145 else 146 { 147 if (!setFlag || StringUtils.isEmpty((String) getTestValue())) 148 { 149 validFlag = false; 150 this.message = message; 151 } 152 } 153 } 154 } 155 }