1 23 24 package org.objectweb.jorm.generator.lib; 25 26 import org.apache.velocity.VelocityContext; 27 import org.apache.velocity.context.Context; 28 import org.objectweb.jorm.api.PException; 29 import org.objectweb.jorm.compiler.api.JormCompilerParameter; 30 import org.objectweb.jorm.compiler.api.PExceptionCompiler; 31 import org.objectweb.jorm.metainfo.api.CompositeName; 32 import org.objectweb.jorm.metainfo.api.ScalarField; 33 import org.objectweb.jorm.metainfo.api.Package; 34 import org.objectweb.jorm.metainfo.api.TypedElement; 35 import org.objectweb.jorm.type.api.PType; 36 import org.objectweb.jorm.type.api.PExceptionTyping; 37 import org.objectweb.jorm.util.io.api.TargetHolder; 38 import org.objectweb.util.monolog.api.BasicLevel; 39 40 import java.io.File ; 41 import java.io.FileWriter ; 42 import java.util.Iterator ; 43 44 63 public class PNameGenerator extends CommonGenerator { 64 public CommonHelper helper = null; 65 66 73 public void generate(CompositeName co, 74 TargetHolder holder, 75 JormCompilerParameter cp) throws PException { 76 String packName = ((Package ) co.getParent()).getName(); 78 String fileName = co.getName() + "PName"; 79 if (packName != null && packName.length() > 0) { 80 fileName = packName + "." + fileName; 81 fileName = fileName.replace('.', File.separatorChar); 82 } 83 logger.log(BasicLevel.DEBUG, "Generate the " + fileName + " PName"); 84 Context ctx = new VelocityContext(); 85 ctx.put("compositename", co); 86 helper = new CommonHelper(); 87 helper.setLogger(logger); 88 ctx.put("tools", helper); 89 ctx.put("nametools", this); 90 Boolean hasca = Boolean.FALSE; 91 Boolean hasba = Boolean.FALSE; 92 StringBuffer constructParam = new StringBuffer (); 93 Iterator it = co.getAllFields().iterator(); 94 while (it.hasNext()) { 95 TypedElement te = (TypedElement) it.next(); 96 int tc = te.getType().getTypeCode(); 97 if (tc == PType.TYPECODE_BYTEARRAY) { 98 hasba = Boolean.TRUE; 99 } else if (tc == PType.TYPECODE_CHARARRAY) { 100 hasca = Boolean.TRUE; 101 } 102 constructParam.append(te.getType().getJavaName()); 103 constructParam.append(" "); 104 constructParam.append(te.getName()); 105 constructParam.append(", "); 106 } 107 constructParam.append("PNameManager pnc"); 108 ctx.put("consparam", constructParam.toString()); 109 110 ctx.put("containsCharArray", hasca); 111 ctx.put("containsByteArray", hasba); 112 ctx.put("header", GEN_TEMPLATE_DIR + "Header.vm"); 113 try { 114 FileWriter fw = holder.getFileWriter(fileName + ".java"); 116 if (template == null) { 117 template = velocityEngine.getTemplate(GEN_TEMPLATE_DIR + "PName.vm"); 118 } 119 template.merge(ctx, fw); 120 fw.flush(); 121 fw.close(); 122 } catch (Exception e) { 123 throw new PExceptionCompiler(e, "Problem while generating PName."); 124 } 125 } 126 127 130 public String getCnHashCode(CompositeName cn) throws PException { 131 String result = "0", plus = " + "; 132 Iterator fs = cn.getAllFields().iterator(); 133 while (fs.hasNext()) { 136 ScalarField f = (ScalarField) fs.next(); 137 result += plus; 138 switch (f.getType().getTypeCode()) { 139 case PType.TYPECODE_BYTE: 140 case PType.TYPECODE_CHAR: 141 case PType.TYPECODE_SHORT: 142 case PType.TYPECODE_INT: 143 result += f.getName(); 144 break; 145 case PType.TYPECODE_LONG: 146 result += "((int) " + f.getName() + ")"; 147 break; 148 case PType.TYPECODE_OBJCHAR: 149 case PType.TYPECODE_OBJBYTE: 150 case PType.TYPECODE_OBJSHORT: 151 case PType.TYPECODE_OBJINT: 152 case PType.TYPECODE_OBJLONG: 153 case PType.TYPECODE_DATE: 154 case PType.TYPECODE_BIGDECIMAL: 155 case PType.TYPECODE_BIGINTEGER: 156 case PType.TYPECODE_STRING: 157 result += "(" + f.getName() + " == null ? 0 :" + f.getName() + ".hashCode())"; 158 break; 159 case PType.TYPECODE_BYTEARRAY: 160 case PType.TYPECODE_CHARARRAY: 161 default: 162 result += "0"; 163 } 164 } 165 return result; 166 } 167 168 171 public String getCnEquals(CompositeName cn) throws PException { 172 String result = "", and = " && "; 173 boolean addAnd = false; 174 Iterator fs = cn.getAllFields().iterator(); 175 while (fs.hasNext()) { 176 ScalarField f = (ScalarField) fs.next(); 177 if (addAnd) { 178 result += and; 179 } else { 180 addAnd = true; 181 } 182 switch (f.getType().getTypeCode()) { 183 case PType.TYPECODE_BYTE: 184 case PType.TYPECODE_CHAR: 185 case PType.TYPECODE_SHORT: 186 case PType.TYPECODE_INT: 187 case PType.TYPECODE_LONG: 188 result += "(" + f.getName() + " == pn." + f.getName() + ")"; 189 break; 190 case PType.TYPECODE_OBJBYTE: 191 case PType.TYPECODE_OBJCHAR: 192 case PType.TYPECODE_OBJSHORT: 193 case PType.TYPECODE_OBJINT: 194 case PType.TYPECODE_OBJLONG: 195 case PType.TYPECODE_DATE: 196 case PType.TYPECODE_BIGDECIMAL: 197 case PType.TYPECODE_BIGINTEGER: 198 case PType.TYPECODE_STRING: 199 result += "(" + f.getName() + " == null" + 200 " ? (pn." + f.getName() + "== null)" + 201 " : " + f.getName() + ".equals(pn." + f.getName() + "))"; 202 break; 203 case PType.TYPECODE_BYTEARRAY: 204 case PType.TYPECODE_CHARARRAY: 205 result += "equal(" + f.getName() + ", pn." + f.getName() + ")"; 206 break; 207 default: 208 throw new PExceptionTyping( 209 "Unsupported PType: " + f.getType().getJormName()); 210 } 211 } 212 return result; 213 } 214 215 218 public String getPNameNullTest(CompositeName cn) throws PException { 219 String result = ""; 220 Iterator fs = cn.getAllFields().iterator(); 221 while (fs.hasNext()) { 222 ScalarField f = (ScalarField) fs.next(); 223 String nullvalue = getNullValue(cn, f); 224 if (helper.canBeNullValue(f.getType())) { 225 result += "(" + f.getName() + " == null" 228 + "? null == " + nullvalue 229 + ": " + f.getName() + ".equals(" + nullvalue + "))"; 230 } else { 231 result += f.getName() + " == " + nullvalue; 232 } 233 if (fs.hasNext()) result += " && "; 234 } 235 return result; 236 } 237 238 241 public String getPNameNullValue(CompositeName cn, String pncVal) throws PException { 242 String result = ""; 243 Iterator fs = cn.getAllFields().iterator(); 244 while (fs.hasNext()) { 245 ScalarField f = (ScalarField) fs.next(); 246 result = result + getNullValue(cn, f) + ", "; 247 } 248 return result + pncVal; 249 } 250 251 public String getNullValue(CompositeName cn, ScalarField f) 252 throws PException { 253 String nullValue = f.getNullValue(); 254 if (nullValue == null) { 255 nullValue = (helper.canBeNullValue(f.getType()) 256 ? "NULLOVALUE" 257 : "NULLVALUE"); 258 } else { 259 if (f.getType().getTypeCode() == PType.TYPECODE_STRING) 260 nullValue = "\"" + nullValue + "\""; 261 else if (f.getType().getTypeCode() == PType.TYPECODE_CHAR) 262 nullValue = "\'" + nullValue + "\'"; 263 } 264 return "(" + f.getType().getJavaName() + ")" + nullValue; 265 } 266 } 267 | Popular Tags |