KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jorm > metainfo > lib > BasicScalarField


1 /**
2  * JORM: an implementation of a generic mapping system for persistent Java
3  * objects. Two mapping are supported: to RDBMS and to binary files.
4  * Copyright (C) 2001-2003 France Telecom R&D - INRIA
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * Contact: jorm-team@objectweb.org
21  *
22  */

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 JavaDoc;
33 import java.text.ParseException JavaDoc;
34 import java.math.BigDecimal JavaDoc;
35 import java.math.BigInteger JavaDoc;
36
37 /**
38  * A ScalarField describes corresponds to a hidden field of a ClassObject or
39  * a Generic Class. For Classes, it is typically used to represent PNames. For
40  * Generic Class, it can also be used for the index attributes of the
41  * Generic Class.
42  * @author X. Spengler
43  */

44 public class BasicScalarField extends BasicPrimitiveElement implements ScalarField {
45
46     /**
47      * This string represents the null value for the field.
48      */

49     private String JavaDoc nullvalueStr = null;
50     private Object JavaDoc nullValue = null;
51     private boolean hasNullValue = false;;
52
53     /**
54      * Builds a new BasicScalarField object.
55      *
56      * @param name the name of the scalar field
57      * @param type the PType of the field
58      * @param parent the parent meta-object of the current field
59      */

60     public BasicScalarField(String JavaDoc name, PType type, int size, int scale, MetaObject parent) {
61         super(name, type, size, scale, parent);
62     }
63
64     /**
65      * Set the null value for this field.
66      * @param value a string representation of the nullvalue
67      *
68      */

69     public void setNullValue(String JavaDoc value) {
70         nullvalueStr = value;
71         hasNullValue = true;
72         try {
73             nullValue = parseValue(getType(), value);
74         } catch (Exception JavaDoc 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     /**
85      * get the null value for this field.
86      * @return the string representation of the null value or null if there is no null value for this field.
87      */

88     public String JavaDoc getNullValue() {
89         return nullvalueStr;
90     }
91
92     public Object JavaDoc getNullValueObject() {
93         return nullValue;
94     }
95
96     /**
97      * Test if the null value exists for this field.
98      * @return true if the null value exist for this field.
99      */

100     public boolean hasNullValue() {
101         return hasNullValue;
102     }
103
104     public static Object JavaDoc parseValue(PType type, String JavaDoc value)
105         throws NumberFormatException JavaDoc, ParseException JavaDoc, 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 JavaDoc(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 JavaDoc().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 JavaDoc(value);
136                 case PType.TYPECODE_BIGINTEGER:
137                     return new BigInteger JavaDoc(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