KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > medor > filter > jorm > lib > SinglePName


1 /**
2  * MEDOR: Middleware Enabling Distributed Object Requests
3  *
4  * Copyright (C) 2001-2003 France Telecom R&D
5  * Contact: alexandre.lefebvre@rd.francetelecom.com
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  *
21  * Initial developers: M. Alia, A. Lefebvre
22  */

23
24 package org.objectweb.medor.filter.jorm.lib;
25
26 import org.objectweb.jorm.naming.api.PNamingContext;
27 import org.objectweb.jorm.type.api.PType;
28 import org.objectweb.medor.api.MedorException;
29 import org.objectweb.medor.expression.api.ExpressionException;
30 import org.objectweb.medor.expression.api.MalformedExpressionException;
31 import org.objectweb.medor.expression.api.ParameterOperand;
32 import org.objectweb.medor.expression.api.TypingException;
33 import org.objectweb.medor.expression.api.Operand;
34 import org.objectweb.medor.filter.api.FieldOperand;
35 import org.objectweb.medor.expression.lib.BasicBinaryOperator;
36 import org.objectweb.medor.expression.lib.BasicVariableOperand;
37 import org.objectweb.medor.tuple.api.Tuple;
38 import org.objectweb.medor.type.lib.QTypeTuple;
39
40 import java.math.BigDecimal JavaDoc;
41 import java.math.BigInteger JavaDoc;
42
43 /**
44  * A SinglePName is an Operator representing the construction of a PName
45  * from a NamingContext and a single Field.
46  * <p>
47  * The NamingContext is provided as a ParameterOperand. The name of the
48  * ParameterOperand is defined to be:
49  * <ol><li>The fully qualified class name for the PName of the class itself</li>
50  * <li>The fully qualified class name, to which the attribute name is
51  * concatenated for a reference PName</li>
52  * </ol>
53  * The Field is provided as a FieldOperand.
54  *
55  * @author <A HREF="mailto:alia.mourad@rd.francetelecom.com><b>
56  * Mourad Alia
57  * </b></A>
58  * <A HREF="mailto:alexandre.lefebvre@rd.francetelecom.com><b>
59  * Alexandre Lefebvre
60  </b></A>
61  */

62 public class SinglePName extends BasicBinaryOperator {
63
64     /**
65      * Constructs a SinglePName from a ParameterOperand representing the
66      * naming context, a FieldOperand, and the PType of the corresponding
67      * PName.
68      * @param f is the FieldOperand from which the value is taken.
69      * @param pnc is the Operand representing the naming context.
70      * @param nameType is the PType of the PName created by this operator.
71      */

72     public SinglePName(FieldOperand f, Operand pnc, PType nameType) {
73         super(nameType, f, pnc);
74     }
75
76     /**
77      *
78      */

79     public Operand evaluate(ParameterOperand[] pos, Object JavaDoc t)
80         throws ExpressionException {
81         Tuple o = (Tuple) t;
82         //assign the value of the NamingContext
83
expressions[1].evaluate(pos, o);
84         //gets the naming context
85
PNamingContext nc =
86             (PNamingContext) ((Operand) expressions[1]).getObject();
87         if (result == null)
88             result = new BasicVariableOperand(type);
89         PType fieldType = ((FieldOperand) expressions[0]).getField().getType();
90         int i = ((FieldOperand) expressions[0]).getIndex();
91         try {
92             switch (fieldType.getTypeCode()) {
93             case QTypeTuple.TYPECODE_BYTE:
94                 if (!o.isDefined(i)) {
95                     result.setValue(nc.getNull());
96                 } else {
97                     result.setValue(nc.decodeByte(o.getByte(i)));
98                 }
99                 break;
100             case QTypeTuple.TYPECODE_CHAR:
101                 if (!o.isDefined(i)) {
102                     result.setValue(nc.getNull());
103                 } else {
104                     result.setValue(nc.decodeChar(o.getChar(i)));
105                 }
106                 break;
107             case QTypeTuple.TYPECODE_DATE:
108                 result.setValue(nc.decodeDate(o.getDate(i)));
109                 break;
110             case QTypeTuple.TYPECODE_INT:
111                 if (!o.isDefined(i)) {
112                     result.setValue(nc.getNull());
113                 } else {
114                     result.setValue(nc.decodeInt(o.getInt(i)));
115                 }
116                 break;
117             case QTypeTuple.TYPECODE_LONG:
118                 if (!o.isDefined(i)) {
119                     result.setValue(nc.getNull());
120                 } else {
121                     result.setValue(nc.decodeLong(o.getLong(i)));
122                 }
123                 break;
124             case QTypeTuple.TYPECODE_SHORT:
125                 if (!o.isDefined(i)) {
126                     result.setValue(nc.getNull());
127                 } else {
128                     result.setValue(nc.decodeShort(o.getShort(i)));
129                 }
130                 break;
131             case QTypeTuple.TYPECODE_STRING:
132                 result.setValue(nc.decodeString(o.getString(i)));
133                 break;
134             case QTypeTuple.TYPECODE_OBJBYTE:
135                 result.setValue(nc.decodeObyte((Byte JavaDoc) o.getObject(i)));
136                 break;
137             case QTypeTuple.TYPECODE_OBJCHAR:
138                 result.setValue(nc.decodeOchar((Character JavaDoc) o.getObject(i)));
139                 break;
140             case QTypeTuple.TYPECODE_OBJINT:
141                 result.setValue(nc.decodeOint((Integer JavaDoc) o.getObject(i)));
142                 break;
143             case QTypeTuple.TYPECODE_OBJLONG:
144                 result.setValue(nc.decodeOlong((Long JavaDoc) o.getObject(i)));
145                 break;
146             case QTypeTuple.TYPECODE_OBJSHORT:
147                 result.setValue(nc.decodeOshort((Short JavaDoc) o.getObject(i)));
148                 break;
149             case QTypeTuple.TYPECODE_BIGINTEGER:
150                 result.setValue(nc.decodeBigInteger((BigInteger JavaDoc) o.getObject(i)));
151                 break;
152             case QTypeTuple.TYPECODE_BIGDECIMAL:
153                 result.setValue(nc.decodeBigDecimal((BigDecimal JavaDoc) o.getObject(i)));
154                 break;
155             default:
156                 throw new MedorException("Impossible to construct PName from field"); // This case don't accure if we compile at first
157
}
158         }
159         catch (Exception JavaDoc e) {
160             throw new ExpressionException("Error while building SinglePName", e);
161         }
162         return result;
163     }
164
165     /**
166      * Checks the semantic integrity of an expression.
167      * It checks that all types are compatible and prepare the expression to be
168      * evaluable. It also creates buffers where stores the result. Notes that
169      * when evaluating there is no creation of new objects. This method changes
170      * the state of this expression object, it will be evaluable and not
171      * modifiable.
172      * @throws org.objectweb.medor.expression.api.TypingException when incompatible types error
173      * occures.
174      * @throws org.objectweb.medor.expression.api.MalformedExpressionException if syntax error
175      */

176     public Operand compileExpression()
177         throws TypingException, MalformedExpressionException {
178         if ((expressions[0] != null) && (expressions[1] != null)) {
179             result = new BasicVariableOperand(type);
180             verified = true;
181         }
182         else
183         // Operands not yet assigned
184
throw new MalformedExpressionException("null children value");
185         return result;
186     }
187
188     public FieldOperand getField() {
189         return (FieldOperand) expressions[0];
190     }
191
192     public ParameterOperand getPNameManagerParameter() {
193         return (expressions[1] instanceof ParameterOperand
194             ? (ParameterOperand) expressions[1]
195             : null);
196     }
197
198     //Implementation of Operator interface
199
/**
200      * There is no Java operator associated to a PName
201      */

202     public String JavaDoc getOperatorString() {
203         return "singlePName";
204     }
205 }
206
Popular Tags