1 package org.apache.fulcrum.intake.model; 2 3 /* 4 * Licensed to the Apache Software Foundation (ASF) under one 5 * or more contributor license agreements. See the NOTICE file 6 * distributed with this work for additional information 7 * regarding copyright ownership. The ASF licenses this file 8 * to you under the Apache License, Version 2.0 (the 9 * "License"); you may not use this file except in compliance 10 * with the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, 15 * software distributed under the License is distributed on an 16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 17 * KIND, either express or implied. See the License for the 18 * specific language governing permissions and limitations 19 * under the License. 20 */ 21 22 import org.apache.fulcrum.intake.IntakeException; 23 import org.apache.fulcrum.intake.validator.IntegerValidator; 24 25 /** 26 * Processor for int fields. 27 * 28 * @version $Id$ 29 */ 30 public class IntegerField 31 extends Field<Integer> 32 { 33 /** Serial version */ 34 private static final long serialVersionUID = -4186153269451228653L; 35 36 /** 37 * Constructor. 38 * 39 * @param field xml field definition object 40 * @param group xml group definition object 41 * @throws IntakeException thrown by superclass 42 */ 43 public IntegerField(XmlField field, Group group) 44 throws IntakeException 45 { 46 super(field, group); 47 } 48 49 /** 50 * Sets the default value for an Integer Field 51 * 52 * @param prop Parameter for the default values 53 */ 54 @Override 55 public void setDefaultValue(String prop) 56 { 57 defaultValue = null; 58 59 if (prop == null) 60 { 61 return; 62 } 63 64 defaultValue = Integer.valueOf(prop); 65 } 66 67 /** 68 * Set the empty Value. This value is used if Intake 69 * maps a field to a parameter returned by the user and 70 * the corresponding field is either empty (empty string) 71 * or non-existent. 72 * 73 * @param prop The value to use if the field is empty. 74 */ 75 @Override 76 public void setEmptyValue(String prop) 77 { 78 emptyValue = null; 79 80 if (prop == null) 81 { 82 return; 83 } 84 85 emptyValue = Integer.valueOf(prop); 86 } 87 88 /** 89 * Provides access to emptyValue such that the value returned will be 90 * acceptable as an argument parameter to Method.invoke. Subclasses 91 * that deal with primitive types should ensure that they return an 92 * appropriate value wrapped in the object wrapper class for the 93 * primitive type. 94 * 95 * @return the value to use when the field is empty or an Object that 96 * wraps the empty value for primitive types. 97 */ 98 @Override 99 protected Object getSafeEmptyValue() 100 { 101 if (isMultiValued()) 102 { 103 return new int[0]; 104 } 105 else 106 { 107 return (null == getEmptyValue()) 108 ? Integer.valueOf(0) : getEmptyValue(); 109 } 110 } 111 112 /** 113 * A suitable validator. 114 * 115 * @return A suitable validator 116 */ 117 @Override 118 protected String getDefaultValidator() 119 { 120 return IntegerValidator.class.getName(); 121 } 122 123 /** 124 * Sets the value of the field from data in the parser. 125 */ 126 @Override 127 protected void doSetValue() 128 { 129 if (isMultiValued()) 130 { 131 Integer[] inputs = parser.getIntObjects(getKey()); 132 int[] values = new int[inputs.length]; 133 134 for (int i = 0; i < inputs.length; i++) 135 { 136 values[i] = inputs[i] == null 137 ? getEmptyValue().intValue() 138 : inputs[i].intValue(); 139 } 140 141 setTestValue(values); 142 } 143 else 144 { 145 setTestValue(parser.getIntObject(getKey(), getEmptyValue())); 146 } 147 } 148 149 }