1 18 package org.objectweb.jorm.generator.lib; 19 20 import org.objectweb.jorm.metainfo.api.Class; 21 import org.objectweb.jorm.metainfo.api.CompositeName; 22 import org.objectweb.jorm.metainfo.api.NameDef; 23 import org.objectweb.jorm.metainfo.api.Package; 24 import org.objectweb.jorm.metainfo.api.TypedElement; 25 import org.objectweb.jorm.metainfo.api.ScalarField; 26 import org.objectweb.jorm.util.io.api.TargetHolder; 27 import org.objectweb.jorm.compiler.api.JormCompilerParameter; 28 import org.objectweb.jorm.compiler.api.PExceptionCompiler; 29 import org.objectweb.jorm.api.PException; 30 import org.objectweb.jorm.type.api.PType; 31 import org.objectweb.jorm.naming.lib.MapPNameGetter; 32 import org.objectweb.jorm.naming.api.PNameCoder; 33 import org.objectweb.util.monolog.api.BasicLevel; 34 import org.objectweb.util.monolog.api.Logger; 35 import org.objectweb.medor.expression.api.Expression; 36 import org.objectweb.medor.expression.api.ExpressionException; 37 import org.objectweb.medor.expression.api.ParameterOperand; 38 import org.apache.velocity.context.Context; 39 import org.apache.velocity.VelocityContext; 40 41 import java.util.ArrayList ; 42 import java.util.Collection ; 43 import java.util.Iterator ; 44 import java.util.List ; 45 import java.io.File ; 46 import java.io.FileWriter ; 47 48 52 public class KFPNCGenerator extends CommonGenerator { 53 public void generate(Class clazz, 54 TargetHolder holder, 55 JormCompilerParameter cp) throws PException { 56 57 if (!clazz.getSuperClasses().isEmpty()) { 59 if (logger.isLoggable(BasicLevel.DEBUG)) { 60 logger.log(BasicLevel.DEBUG, "KFPNC: No generation done because " 61 + clazz.getFQName() + " is not the ancestor"); 62 } 63 return; 64 } 65 66 Collection nds = clazz.getNameDefs(); 67 if (nds.size() < 1) { 68 logger.log(BasicLevel.WARN, "FPNC: No NameDef defined for the class " + clazz.getFQName()); 69 return; 70 } 71 if (nds.size() == 1) { 72 NameDef nd = (NameDef) nds.iterator().next(); 73 generate(clazz, nd, null, holder); 74 } else { 75 Iterator it = nds.iterator(); 76 while(it.hasNext()) { 77 NameDef nd = (NameDef) it.next(); 78 String ndName = (nd.getName() != null 79 && nd.getName().length() == 0 80 ? null 81 : nd.getName()); 82 generate(clazz, nd, ndName, holder); 83 } 84 } 85 } 86 87 96 private void generate(Class clazz, 97 NameDef nd, 98 String ndName, 99 TargetHolder holder) throws PException { 100 if (!mustGeneratedKFPNC(clazz, nd)) { 101 if (logger.isLoggable(BasicLevel.DEBUG)) { 102 logger.log(BasicLevel.DEBUG, "KFPNC: No generation done because " 103 + clazz.getFQName() + " has no inheritance filter based on keys"); 104 } 105 return; 106 } 107 Expression exp; 108 try { 109 exp = clazz.getInheritanceFilter(nd); 110 } catch (ExpressionException e) { 111 throw new PException(e); 112 } 113 if (exp == null) { 114 logger.log(BasicLevel.WARN, "KFPNC: No generation done because " 115 + clazz.getFQName() + " has no inheritance filter"); 116 return; 117 } 118 generate(clazz, nd, ndName, exp, holder); 119 } 120 121 122 131 private void generate(Class clazz, 132 NameDef nd, 133 String ndName, 134 Expression exp, 135 TargetHolder holder) throws PException { 136 String packName = ((Package ) clazz.getParent()).getName(); 138 if (ndName != null) { 139 if (packName != null && packName.length() > 0) { 140 packName += "." + ndName; 141 } else { 142 packName = ndName; 143 } 144 } 145 String className = clazz.getName() + "KFPNC"; 146 String fileName = className; 147 if (packName != null && packName.length() > 0) { 148 fileName = packName + "." + fileName; 149 fileName = fileName.replace('.', File.separatorChar); 150 } 151 logger.log(BasicLevel.DEBUG, "Generate the " + fileName); 152 153 154 Context ctx = new VelocityContext(); 155 ctx.put("class", clazz); 156 ctx.put("helper", this); 157 ctx.put("header", GEN_TEMPLATE_DIR + "Header.vm"); 158 ctx.put("className", className); 159 ctx.put("packName", packName); 160 161 PType expressionType = exp.getType(); 162 ctx.put("expressionType", expressionType.getProgName()); 163 164 String codingType; 166 String nullValueStr; 167 CompositeName cn = null; 168 ctx.put("isComposite", nd.isNameRef() ? Boolean.TRUE : Boolean.FALSE); 169 if (nd.isNameRef()) { 170 cn = nd.getNameRef().getCompositeName(); 171 ctx.put("compositeName", cn); 172 Collection fields = nd.getNameRef().getCompositeName().getAllFields(); 173 StringBuffer sb = new StringBuffer (); 174 sb.append("new "); 175 sb.append(cn.getName()); 176 sb.append("PName("); 177 Iterator it = fields.iterator(); 178 it = fields.iterator(); 179 while(it.hasNext()) { 180 ScalarField sf = (ScalarField) it.next(); 181 String nullValue = sf.getNullValue(); 182 if (nullValue == null) { 183 nullValue = getStrNullValue(sf.getType()); 184 } 185 sb.append(nullValue); 186 sb.append(", "); 187 } 188 sb.append("this)"); 189 nullValueStr = sb.toString(); 190 codingType = "CTCOMPOSITE"; 191 } else if (nd.isFieldName()) { 192 TypedElement te = clazz.getTypedElement(nd.getFieldName()); 193 nullValueStr = getStrNullValueAsObject(te.getType()); 194 codingType = getCTDeclaration(te.getType()); 195 } else { 196 return; 197 } 198 ctx.put("nullValue", nullValueStr); 199 ctx.put("codingType", codingType); 200 201 NamingFilterExpressionHelper mi; 203 try { 204 mi = new NamingKeyFilterExpressionHelper(exp, cn, logger); 205 } catch (ExpressionException e) { 206 throw new PException(e); 207 } 208 ctx.put("mi", mi); 209 210 try { 212 FileWriter fw = holder.getFileWriter(fileName + ".java"); 213 if (template == null) { 214 template = velocityEngine.getTemplate(GEN_TEMPLATE_DIR + "KFPNC.vm"); 215 } 216 template.merge(ctx, fw); 217 fw.flush(); 218 fw.close(); 219 } catch (Exception e) { 220 throw new PExceptionCompiler(e, "Problem while generating " + fileName + ".java"); 221 } 222 } 223 224 private String getStrNullValueAsObject(PType type) { 225 StringBuffer sb = new StringBuffer (); 226 sb.append("new org.objectweb.jorm.facility.naming.basidir.BasidName("); 227 sb.append(getStrAsObject(getStrNullValue(type), type)); 228 sb.append(", new org.objectweb.jorm.facility.naming.basidir.BasidBinder("); 229 sb.append(getCTDeclaration(type)); 230 sb.append("))"); 231 return sb.toString(); 232 } 233 234 private String getStrAsObject(String str, PType type) { 235 switch (type.getTypeCode()) { 236 case PType.TYPECODE_BYTE: 237 return "new Byte(" + str +")"; 238 case PType.TYPECODE_CHAR: 239 return "new Character(" + str +")"; 240 case PType.TYPECODE_SHORT: 241 return "new Short(" + str +")"; 242 case PType.TYPECODE_INT: 243 return "new Integer(" + str +")"; 244 case PType.TYPECODE_LONG: 245 return "new Long(" + str +")"; 246 case PType.TYPECODE_OBJBYTE: 247 case PType.TYPECODE_OBJCHAR: 248 case PType.TYPECODE_OBJSHORT: 249 case PType.TYPECODE_OBJINT: 250 case PType.TYPECODE_OBJLONG: 251 case PType.TYPECODE_DATE: 252 case PType.TYPECODE_CHARARRAY: 253 case PType.TYPECODE_BYTEARRAY: 254 case PType.TYPECODE_STRING: 255 case PType.TYPECODE_BIGDECIMAL: 256 case PType.TYPECODE_BIGINTEGER: 257 default: 258 return str; 259 } 260 } 261 262 private String getStrNullValue(PType type) { 263 switch (type.getTypeCode()) { 264 case PType.TYPECODE_BYTE: 265 return "(byte) -1"; 266 case PType.TYPECODE_CHAR: 267 return "(char) 0"; 268 case PType.TYPECODE_SHORT: 269 return "(short) -1"; 270 case PType.TYPECODE_INT: 271 return "(int) -1"; 272 case PType.TYPECODE_LONG: 273 return "(long) -1"; 274 case PType.TYPECODE_OBJBYTE: 275 case PType.TYPECODE_OBJCHAR: 276 case PType.TYPECODE_OBJSHORT: 277 case PType.TYPECODE_OBJINT: 278 case PType.TYPECODE_OBJLONG: 279 case PType.TYPECODE_DATE: 280 case PType.TYPECODE_CHARARRAY: 281 case PType.TYPECODE_BYTEARRAY: 282 case PType.TYPECODE_STRING: 283 case PType.TYPECODE_BIGDECIMAL: 284 case PType.TYPECODE_BIGINTEGER: 285 return "null"; 286 default: 287 return null; 288 } 289 290 } 291 292 private boolean mustGeneratedKFPNC(Class clazz, NameDef nd) { 293 if (!clazz.getSuperClasses().isEmpty()) { 294 logger.log(BasicLevel.DEBUG, "class " + clazz.getName() + " has parent => no KFPNC"); 295 return false; 296 } 297 try { 298 if (clazz.getInheritanceFilter(nd) == null) { 299 logger.log(BasicLevel.DEBUG, "class " + clazz.getName() + " has no filter => no KFPNC"); 300 return false; 301 } 302 } catch (ExpressionException e) { 303 logger.log(BasicLevel.ERROR, 304 "Error during the fetching of the key naming inheritance filter: ", e); 305 return false; 306 } 307 return hasNamingKey(clazz, nd); 308 } 309 310 private boolean hasNamingKey(Class clazz, NameDef nd) { 311 if (clazz.isAbstract()) { 312 logger.log(BasicLevel.DEBUG, "class " + clazz.getName() + " is abstract"); 313 Iterator it = clazz.getSubClasses().iterator(); 315 while(it.hasNext()) { 316 Class subclazz = (Class ) it.next(); 317 if (hasNamingKey(subclazz, nd)) { 318 return true; 319 } 320 } 321 return false; 322 } else { 323 Object key = clazz.getInheritanceNamingKey(nd); 324 logger.log(BasicLevel.DEBUG, "class " + clazz.getName() + " key: " + key); 325 return key != null; 326 } 327 } 328 329 public class NamingKeyFilterExpressionHelper extends NamingFilterExpressionHelper { 330 331 private PType filterType; 332 private CompositeName co; 333 public NamingKeyFilterExpressionHelper(Expression e, 334 CompositeName co, 335 Logger logger) 336 throws PException, ExpressionException { 337 super(logger); 338 this.filterType = e.getType(); 339 this.co = co; 340 fillMatchInfo(e); 341 } 342 343 public String getFilter() { 344 return getStrAsObject(super.getFilter(), filterType); 345 } 346 347 protected void fillMatchInfo(ParameterOperand po) throws PException, ExpressionException { 348 logger.log(BasicLevel.DEBUG, "ParameterOperand " + po.getName()); 349 if (field2declarations.get(po.getName()) == null) { 350 StringBuffer declaration = new StringBuffer (); 351 declaration.append(po.getType().getJavaName()); 352 declaration.append(' '); 353 declaration.append(po.getName()); 354 declaration.append(" = "); 355 if (co == null) { 356 declaration.append("((PNameGetter) png)."); 357 declaration.append(getPNameGetterGetFunction(po.getType())); 358 declaration.append("(\""); 359 declaration.append(po.getName()); 360 declaration.append("\", ctx)"); 361 } else { 362 declaration.append("(isSpecific ? (("); 363 declaration.append(co.getName()); 364 declaration.append("PNG) png).pnGet"); 365 declaration.append(upperFL(po.getName())); 366 declaration.append("(ctx) : ((PNameGetter) png)."); 367 declaration.append(getPNameGetterGetFunction(po.getType())); 368 declaration.append("(\""); 369 declaration.append(po.getName()); 370 declaration.append("\", ctx))"); 371 } 372 declaration.append(";"); 373 field2declarations.put(po.getName(), declaration); 374 if (logger.isLoggable(BasicLevel.DEBUG)) { 375 logger.log(BasicLevel.DEBUG, "Add declaration: " + declaration); 376 } 377 } 378 filter.append(po.getName()); 379 } 380 } 381 382 383 } 384 | Popular Tags |