1 23 24 package org.objectweb.jorm.metainfo.lib; 25 26 import org.objectweb.jorm.metainfo.api.MetaObject; 27 import org.objectweb.jorm.metainfo.api.ScalarField; 28 import org.objectweb.jorm.type.api.PType; 29 import org.objectweb.jorm.api.PException; 30 import org.objectweb.util.monolog.api.BasicLevel; 31 32 import java.text.SimpleDateFormat ; 33 import java.text.ParseException ; 34 import java.math.BigDecimal ; 35 import java.math.BigInteger ; 36 37 44 public class BasicScalarField extends BasicPrimitiveElement implements ScalarField { 45 46 49 private String nullvalueStr = null; 50 private Object nullValue = null; 51 private boolean hasNullValue = false;; 52 53 60 public BasicScalarField(String name, PType type, int size, int scale, MetaObject parent) { 61 super(name, type, size, scale, parent); 62 } 63 64 69 public void setNullValue(String value) { 70 nullvalueStr = value; 71 hasNullValue = true; 72 try { 73 nullValue = parseValue(getType(), value); 74 } catch (Exception e) { 75 hasNullValue = false; 76 if (logger != null) { 77 logger.log(BasicLevel.WARN, 78 "The null value specified for the field '" + getName() 79 + "' is malformed:", e); 80 } 81 } 82 } 83 84 88 public String getNullValue() { 89 return nullvalueStr; 90 } 91 92 public Object getNullValueObject() { 93 return nullValue; 94 } 95 96 100 public boolean hasNullValue() { 101 return hasNullValue; 102 } 103 104 public static Object parseValue(PType type, String value) 105 throws NumberFormatException , ParseException , PException { 106 if (type != null) { 107 if (value == null) { 108 return null; 109 } else { 110 switch (type.getTypeCode()) { 111 case PType.TYPECODE_BYTE: 112 case PType.TYPECODE_OBJBYTE: 113 return Byte.valueOf(value); 114 case PType.TYPECODE_CHAR: 115 case PType.TYPECODE_OBJCHAR: 116 return new Character (value.charAt(0)); 117 case PType.TYPECODE_SHORT: 118 case PType.TYPECODE_OBJSHORT: 119 return Short.valueOf(value); 120 case PType.TYPECODE_INT: 121 case PType.TYPECODE_OBJINT: 122 return Integer.valueOf(value); 123 case PType.TYPECODE_LONG: 124 case PType.TYPECODE_OBJLONG: 125 return Long.valueOf(value); 126 case PType.TYPECODE_DATE: 127 return new SimpleDateFormat ().parse(value); 128 case PType.TYPECODE_CHARARRAY: 129 return value.toCharArray(); 130 case PType.TYPECODE_BYTEARRAY: 131 return value.getBytes(); 132 case PType.TYPECODE_STRING: 133 return value; 134 case PType.TYPECODE_BIGDECIMAL: 135 return new BigDecimal (value); 136 case PType.TYPECODE_BIGINTEGER: 137 return new BigInteger (value); 138 default: 139 throw new PException("Impossible to parse with unkknwon type: " 140 + type.getJormName()); 141 } 142 } 143 } else { 144 throw new PException("Impossible to parse without type: " 145 + type.getJormName()); 146 } 147 } 148 149 } 150 | Popular Tags |