KickJava   Java API By Example, From Geeks To Geeks.

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


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.api.PException;
27 import org.objectweb.jorm.naming.api.PNameCoder;
28 import org.objectweb.jorm.naming.api.PName;
29 import org.objectweb.jorm.type.api.PType;
30 import org.objectweb.medor.expression.api.MalformedExpressionException;
31 import org.objectweb.medor.api.Field;
32 import org.objectweb.medor.api.MedorException;
33 import org.objectweb.medor.expression.api.TypingException;
34 import org.objectweb.medor.eval.lib.TuplePNameGetter;
35 import org.objectweb.medor.filter.api.FieldOperand;
36 import org.objectweb.medor.expression.api.Operator;
37 import org.objectweb.medor.expression.api.ParameterOperand;
38 import org.objectweb.medor.expression.api.ExpressionException;
39 import org.objectweb.medor.expression.api.Expression;
40 import org.objectweb.medor.expression.api.Operand;
41 import org.objectweb.medor.expression.lib.BasicVariableOperand;
42 import org.objectweb.medor.expression.lib.BasicOperator;
43 import org.objectweb.medor.query.api.QueryTreeField;
44 import org.objectweb.util.monolog.api.BasicLevel;
45
46 import java.util.HashMap JavaDoc;
47 import java.util.Map JavaDoc;
48
49 /**
50  * A CompositePName is an Operator representing the construction of a PName
51  * from a NamingContext and several Fields.
52  * <p>
53  * The NamingContext is provided as a ParameterOperand. The name of the
54  * ParameterOperand is defined to be:
55  * <ol><li>The fully qualified class name for the PName of the class itself</li>
56  * <li>The fully qualified class name, to which the attribute name is
57  * concatenated for a reference PName</li>
58  * </ol>
59  * The Fields are provided as an array of FieldOperand.
60  * It relies on a TuplePNameGetter
61  * (@see org.objectweb.medor.query.jorm.lib.TuplePNameGetter) to construct the
62  * PName.
63
64  * @author <A HREF="mailto:alia.mourad@rd.francetelecom.com><b>
65  * Mourad Alia
66  * </b></A>
67  * <A HREF="mailto:alexandre.lefebvre@rd.francetelecom.com><b>
68  * Alexandre Lefebvre
69  </b></A>
70  */

71 public class CompositePName extends BasicOperator implements Operator {
72
73     protected String JavaDoc[] cofns = null;
74     protected TuplePNameGetter myGetter = null;
75
76
77     /**
78      * Constructs a SinglePName from a ParameterOperand representing the
79      * naming context, and an array of FieldOperand. The FieldOperands should
80      * be built on JormFields.
81      * @param fs is the array of FieldOperands from which the value is taken.
82      * @param po is the ParameterOperand representing the naming context.
83      * @param t is the PType of the PName created by this operator.
84      * @param compositefieldnames is the compositename fields. This array
85      * is linked to parameter fs (FieldOperand[]).
86      * @throws MedorException when the number of FieldOperand is not equal to
87      * the size of the proj parameter.
88      */

89     public CompositePName(FieldOperand[] fs,
90                           String JavaDoc[] compositefieldnames,
91                           Operand po,
92                           PType t) throws MedorException {
93         super(t);
94         expressions = new Expression[fs.length + 1];
95         expressions[0] = po;
96         System.arraycopy(fs, 0, expressions, 1, fs.length);
97         cofns = compositefieldnames;
98         Map JavaDoc name2Index = new HashMap JavaDoc(fs.length);
99         boolean debug = logger != null && logger.isLoggable(BasicLevel.DEBUG);
100         if (debug) {
101             logger.log(BasicLevel.DEBUG, "Composite PName for " + fs.length + " fields");
102         }
103         for (int i=0; i<fs.length; i++) {
104             QueryTreeField qtf = (QueryTreeField) fs[i].getField();
105             if (debug) {
106                 logger.log(BasicLevel.DEBUG, "Field " + qtf.getName());
107                 Field[] tsf = qtf.getQueryTree().getTupleStructure().getFields();
108                 logger.log(BasicLevel.DEBUG, "Fields of qt:");
109                 for (int jj = 0; jj < tsf.length; jj++) {
110                     logger.log(BasicLevel.DEBUG, "Field name " + tsf[jj].getName());
111                 }
112             }
113             int rank = qtf.getQueryTree().getTupleStructure().getFieldRank(qtf);
114             name2Index.put(compositefieldnames[i], new Integer JavaDoc(rank));
115         }
116         myGetter = new TuplePNameGetter(name2Index, po, logger);
117     }
118
119
120     /**
121      * It retrieves an array of the composite field name. The order of this
122      * array matches to the order of the FieldOperand array returned by the
123      * method getField()
124      */

125     public String JavaDoc[] getCompositeFieldName() {
126         return cofns;
127     }
128
129     public ParameterOperand getPNameManagerParameter() {
130         return (expressions[0] instanceof ParameterOperand
131             ? (ParameterOperand) expressions[0]
132             : null);
133     }
134
135     // IMPLEMENTATION OF THE Expression INTERFACE //
136
//--------------------------------------------//
137

138     public Object JavaDoc clone(Object JavaDoc clone, Map JavaDoc obj2clone) throws CloneNotSupportedException JavaDoc {
139         clone = super.clone(clone, obj2clone);
140         ((CompositePName) clone).cofns = cofns;
141         //clone the tuplePNameGetter because the field indexes can change
142
// independantly in the futur
143
if (myGetter != null) {
144             ((CompositePName) clone).myGetter = new TuplePNameGetter(myGetter);
145         }
146         return clone;
147     }
148
149     /**
150      * Gets the operand in wich the result will be put in
151      * @return an Operand Object
152      * @throws IllegalStateException if this expression is not
153      * compiled.
154      */

155     public Operand getResult() throws IllegalStateException JavaDoc {
156         if (verified) {
157             return result;
158         } else
159             throw new IllegalStateException JavaDoc("Can't get result of an uncompiled expression");
160     }
161
162     /**
163      *
164      */

165     public Operand evaluate(ParameterOperand[] pos, Object JavaDoc o)
166         throws ExpressionException {
167         //assign the value of the NamingContext
168
expressions[0].evaluate(pos, o);
169         //gets the naming context
170
if (result == null)
171             result = new BasicVariableOperand(type);
172         PNameCoder pnc = (PNameCoder) ((Operand) expressions[0]).getObject();
173         try {
174             PName pn = pnc.decodeAbstract(myGetter, o);
175             if (logger != null && logger.isLoggable(BasicLevel.DEBUG)) {
176                 logger.log(BasicLevel.DEBUG, "PName built: " + pn);
177             }
178             result.setValue(pn);
179         } catch (PException pe) {
180             throw new ExpressionException("Problem for builing composite PName from tuple", pe);
181         }
182         return result;
183     }
184
185     /**
186      * Checks the semantic integrity of an expression.
187      * It checks that all types are compatible and prepare the expression to be
188      * evaluable. It also creates buffers where stores the result. Notes that
189      * when evaluating there is no creation of new objects. This method changes
190      * the state of this expression object, it will be evaluable and not
191      * modifiable.
192      * @throws org.objectweb.medor.expression.api.TypingException when incompatible types error
193      * occurs.
194      * @throws org.objectweb.medor.expression.api.MalformedExpressionException if syntax error
195      */

196     public Operand compileExpression()
197         throws TypingException, MalformedExpressionException {
198         if (expressions != null && expressions.length > 1) {
199             result = new BasicVariableOperand(type);
200             verified = true;
201         } else
202         // Operands not yet assigned
203
throw new MalformedExpressionException("null children value");
204         return result;
205     }
206
207     public FieldOperand[] getFields() {
208         FieldOperand[] res = new FieldOperand[expressions.length - 1];
209         for(int i=0; i<res.length; i++) {
210             res[i] = (FieldOperand) expressions[i + 1];
211         }
212         return res;
213     }
214
215     //Implementation of Operator interface
216
/**
217      * There is no Java operator associated to a PName
218      */

219     public String JavaDoc getOperatorString() {
220         return "compositePName";
221     }
222 }
223
Popular Tags