1 4 package com.tc.config.schema.dynamic; 5 6 import org.apache.xmlbeans.XmlException; 7 import org.apache.xmlbeans.XmlObject; 8 9 import com.tc.config.schema.context.ConfigContext; 10 import com.tc.util.Assert; 11 12 import java.math.BigInteger ; 13 14 17 public class IntXPathBasedConfigItem extends XPathBasedConfigItem implements IntConfigItem { 18 19 private static final BigInteger MAX_INT_AS_BIG_INTEGER = new BigInteger (new Integer (Integer.MAX_VALUE).toString()); 20 private static final BigInteger MIN_INT_AS_BIG_INTEGER = new BigInteger (new Integer (Integer.MIN_VALUE).toString()); 21 22 public IntXPathBasedConfigItem(ConfigContext context, String xpath) { 23 super(context, xpath); 24 25 try { 26 if (!context.hasDefaultFor(xpath) && context.isOptional(xpath)) { 27 throw Assert 29 .failure("XPath '" + xpath + "' is optional and has no default. As such, you can't use it in " 30 + "a ConfigItem returning only an int; what will we return if it's not there? Add a default " 31 + "in the schema, or make it mandatory."); 32 } 33 } catch (XmlException xmle) { 34 throw Assert.failure("Unable to fetch default for '" + xpath + "'."); 35 } 36 } 37 38 protected Object fetchDataFromXmlObject(XmlObject xmlObject) { 39 BigInteger out = (BigInteger ) super.fetchDataFromXmlObjectByReflection(xmlObject, "getBigIntegerValue"); 40 41 if (out == null) return null; 42 43 boolean fits = (out.compareTo(MAX_INT_AS_BIG_INTEGER) <= 0) && (out.compareTo(MIN_INT_AS_BIG_INTEGER) >= 0); 44 if (!fits) throw Assert.failure("Value " + out 45 + " is too big to represent as an 'int'; you should either be using a " 46 + "ConfigItem that uses a BigInteger to represent its data, or the schema should " 47 + "restrict this value to one that fits in a Java 'int'."); 48 return new Integer (out.intValue()); 49 } 50 51 public int getInt() { 52 return ((Integer ) getObject()).intValue(); 53 } 54 55 } 56 | Popular Tags |