001// Copyright 2004, 2005 The Apache Software Foundation 002// 003// Licensed under the Apache License, Version 2.0 (the "License"); 004// you may not use this file except in compliance with the License. 005// You may obtain a copy of the License at 006// 007// http://www.apache.org/licenses/LICENSE-2.0 008// 009// Unless required by applicable law or agreed to in writing, software 010// distributed under the License is distributed on an "AS IS" BASIS, 011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 012// See the License for the specific language governing permissions and 013// limitations under the License. 014 015package org.apache.hivemind.schema.rules; 016 017import java.util.Map; 018 019import org.apache.hivemind.ApplicationRuntimeException; 020import org.apache.hivemind.HiveMind; 021import org.apache.hivemind.Location; 022import org.apache.hivemind.internal.Module; 023import org.apache.hivemind.schema.Translator; 024 025/** 026 * Translates strings to integer values. 027 * 028 * @author Howard Lewis Ship 029 */ 030public class IntTranslator implements Translator 031{ 032 private int _minValue; 033 private boolean _isMinValue; 034 private int _maxValue; 035 private boolean _isMaxValue; 036 private int _defaultValue = 0; 037 038 public IntTranslator() 039 { 040 } 041 042 /** 043 * Initializers: 044 * <ul> 045 * <li>default: default value for empty or invalid input 046 * <li>min: minimum acceptible value 047 * <li>max: maximum acceptible value 048 * </ul> 049 */ 050 public IntTranslator(String initializer) 051 { 052 Map m = RuleUtils.convertInitializer(initializer); 053 054 String defaultInit = (String) m.get("default"); 055 056 if (defaultInit != null) 057 _defaultValue = Integer.parseInt(defaultInit); 058 059 String minInit = (String) m.get("min"); 060 if (minInit != null) 061 { 062 _isMinValue = true; 063 _minValue = Integer.parseInt(minInit); 064 } 065 066 String maxInit = (String) m.get("max"); 067 if (maxInit != null) 068 { 069 _isMaxValue = true; 070 _maxValue = Integer.parseInt(maxInit); 071 } 072 } 073 074 /** 075 * Converts the string to an Integer. The empty string is returned as zero. 076 * On failure, an error is logged and the method returns zero. 077 */ 078 public Object translate( 079 Module contributingModule, 080 Class propertyType, 081 String inputValue, 082 Location location) 083 { 084 if (HiveMind.isBlank(inputValue)) 085 return new Integer(_defaultValue); 086 087 int value; 088 089 try 090 { 091 value = Integer.parseInt(inputValue); 092 } 093 catch (Exception ex) 094 { 095 throw new ApplicationRuntimeException( 096 RulesMessages.invalidIntValue(inputValue), 097 location, 098 null); 099 } 100 101 if (_isMinValue && value < _minValue) 102 throw new ApplicationRuntimeException(RulesMessages.minIntValue(inputValue, _minValue)); 103 104 if (_isMaxValue && value > _maxValue) 105 throw new ApplicationRuntimeException(RulesMessages.maxIntValue(inputValue, _maxValue)); 106 107 return new Integer(value); 108 } 109 110}