1 23 24 package org.objectweb.medor.filter.jorm.lib; 25 26 import org.objectweb.jorm.metainfo.api.Class; 27 import org.objectweb.jorm.metainfo.api.ClassRef; 28 import org.objectweb.jorm.metainfo.api.GenClassRef; 29 import org.objectweb.jorm.metainfo.api.MetaObject; 30 import org.objectweb.jorm.metainfo.api.Reference; 31 import org.objectweb.jorm.metainfo.api.TypedElement; 32 import org.objectweb.medor.api.Field; 33 import org.objectweb.medor.api.MedorException; 34 import org.objectweb.medor.expression.api.ExpressionException; 35 import org.objectweb.medor.expression.api.MalformedExpressionException; 36 import org.objectweb.medor.expression.api.Operator; 37 import org.objectweb.medor.expression.api.ParameterOperand; 38 import org.objectweb.medor.expression.api.Operand; 39 import org.objectweb.medor.expression.api.TypingException; 40 import org.objectweb.medor.expression.api.Expression; 41 import org.objectweb.medor.expression.lib.BasicBinaryOperator; 42 import org.objectweb.medor.expression.lib.BasicOperand; 43 import org.objectweb.medor.filter.api.FieldOperand; 44 import org.objectweb.medor.query.api.PropagatedField; 45 import org.objectweb.medor.query.jorm.lib.PNameField; 46 47 import java.util.Map ; 48 import java.util.ArrayList ; 49 50 56 public class NavigatorOperator extends BasicBinaryOperator { 57 58 protected boolean isPName; 59 private MetaObject jormMO; 60 private FieldOperand myFop; 61 62 63 73 public NavigatorOperator(FieldOperand fop, String att) 74 throws MedorException { 75 super(fop, new BasicOperand(att)); 76 77 myFop = fop; 78 Field f = fop.getField(); 80 if (f instanceof PropagatedField) { 81 Field[] fs = ((PropagatedField) f).getOriginFields(); 82 f = fs[0]; 83 } 84 if (!(f instanceof PNameField)) 85 throw new MedorException( 86 "Impossible to create a NavigatorOperator without PNameField " 87 + "as begin of expression: " + f); 88 PNameField pnf = (PNameField) f; 89 91 92 if (pnf.isClassPName()) { 93 if (pnf.isInGenClass()) 94 throw new MedorException("Impossible to navigate with a GenClass: pnf=" + pnf.getName() + " / attribut=" + att); 95 jormMO = getTypedElement(pnf.getMetaObjectClass(), att); 98 type = ((TypedElement) jormMO).getType(); 99 } 100 else { 101 jormMO = pnf.getReference(); 104 if (jormMO instanceof ClassRef) { 105 jormMO = getTypedElement(((ClassRef) jormMO).getMOClass(), att); 106 type = ((TypedElement) jormMO).getType(); 108 } 109 else if (jormMO instanceof GenClassRef) { 110 type = ((GenClassRef) jormMO).getType(); 112 } 113 } 114 if (jormMO == null) 115 throw new MedorException("No field " + att + " found"); 116 isPName = jormMO instanceof Reference; 117 } 118 119 public Object clone(Object clone, Map obj2clone) throws CloneNotSupportedException { 120 clone = super.clone(clone, obj2clone); 121 ((NavigatorOperator) clone).isPName = isPName; 122 ((NavigatorOperator) clone).jormMO = jormMO; 123 return clone; 124 } 125 126 134 public NavigatorOperator(NavigatorOperator nop, String att) 135 throws MedorException { 136 super(nop, new BasicOperand(att)); 137 if (!nop.isPName()) 138 throw new MedorException( 139 "The specified Navigator operand is not a PName"); 140 141 myFop = nop.getFieldOperand(); 142 jormMO = nop.getMetaObject(); 143 if (jormMO instanceof ClassRef) { 144 jormMO = ((ClassRef) jormMO).getMOClass().getTypedElement(att); 145 type = ((TypedElement) jormMO).getType(); 146 } 147 else if (jormMO instanceof GenClassRef) { 149 throw new MedorException("Impossible to navigate with a GenClass: pnf=" + nop + " / attribut=" + att); 150 } 151 if (jormMO == null) 152 throw new MedorException("No field " + att + " found"); 153 isPName = jormMO instanceof Reference; 154 } 155 156 161 public boolean isPName() { 162 return isPName; 163 } 164 165 169 public FieldOperand getFieldOperand() { 170 return myFop; 171 } 172 173 180 public ArrayList getPath() throws MedorException { 181 ArrayList theList = new ArrayList (); 182 return getPath(theList); 183 } 184 185 193 public ArrayList getPath(ArrayList theList) throws MedorException { 194 Expression e = getExpression(0); 195 if (! (e instanceof FieldOperand)) { 196 theList = ((NavigatorOperator)e).getPath(theList); 198 } 199 try { 200 theList.add(((Operand) getExpression(1)).getString()); 201 } catch (TypingException exc) { 202 throw new MedorException(exc); 203 } 204 return theList; 205 } 206 207 208 212 public MetaObject getMetaObject() { 213 return jormMO; 214 } 215 216 220 public String getOperatorString() { 221 return Operator.NAV; 222 } 223 224 228 public Operand evaluate(ParameterOperand[] pos, Object o) 229 throws ExpressionException { 230 throw new ExpressionException("This operator is not evaluable, and must be " 231 + "transformed into JoinProjects instance during the optimization step."); 232 } 233 234 243 public Operand compileExpression() throws MalformedExpressionException { 244 throw new MalformedExpressionException("This operator is not evaluable," 245 + " and must be transform edinto JoinProjects instance during the" 246 + " optimization step."); 247 } 248 249 private TypedElement getTypedElement(Class clazz, String fn) { 250 int idx = fn.length(); 251 TypedElement te = null; 252 te = clazz.getTypedElement(fn); 253 while ((idx = fn.lastIndexOf(".", idx)) != -1 && te == null) { 254 te = clazz.getTypedElement(fn.substring(idx + 1, fn.length())); 255 idx--; 256 } 257 return te; 258 } 259 } 260 | Popular Tags |