1 33 34 package com.icesoft.metadata.generators; 35 36 import com.icesoft.jsfmeta.util.AbstractGenerator; 37 import com.icesoft.jsfmeta.util.InternalConfig; 38 import com.icesoft.jsfmeta.util.JavaSourceWriter; 39 import com.sun.rave.jsfmeta.beans.*; 40 import java.io.*; 41 import java.util.*; 42 43 public class ComponentTestBeanInfoGenerator extends AbstractGenerator { 44 45 46 private boolean base; 47 48 private String baseBI; 49 50 private String categoryDescriptors; 51 52 private String defaultMarkupSection; 53 54 protected HashMap bundleMaps; 55 56 private String defaultTaglibPrefix; 57 58 private String defaultTaglibURI; 59 60 private String implBD; 61 62 private String implPD; 63 64 protected boolean useComponentResourceBundles; 65 66 public ComponentTestBeanInfoGenerator(InternalConfig internalConfig) { 67 super(internalConfig); 68 base = false; 69 categoryDescriptors = "com.icesoft.faces.ide.creator2.util.CategoryDescriptors"; 70 defaultMarkupSection = "FORM"; 71 bundleMaps = new HashMap(); 72 useComponentResourceBundles = false; 73 baseBI = internalConfig.getProperty("project.base.beaninfo"); 74 implBD = internalConfig.getProperty("project.impl.beanDescriptor"); 75 implPD = internalConfig.getProperty("project.impl.propertyDescriptor"); 76 } 77 78 public boolean getBase() { 79 return base; 80 } 81 82 public void setBase(boolean base) { 83 this.base = base; 84 } 85 86 public String getBaseBI() { 87 return baseBI; 88 } 89 90 public void setBaseBI(String baseBI) { 91 this.baseBI = baseBI; 92 } 93 94 public String getCategoryDescriptors() { 95 return categoryDescriptors; 96 } 97 98 public void setCategoryDescriptors(String categoryDescriptors) { 99 this.categoryDescriptors = categoryDescriptors; 100 } 101 102 public String getDefaultMarkupSection() { 103 return defaultMarkupSection; 104 } 105 106 public void setDefaultMarkupSection(String defaultMarkupSection) { 107 this.defaultMarkupSection = defaultMarkupSection; 108 } 109 110 protected HashMap getBundleMap(String packageName, String languageCode) { 111 112 HashMap packageMap = (HashMap) bundleMaps.get(packageName); 113 if (packageMap == null) { 114 packageMap = new HashMap(); 115 bundleMaps.put(packageName, packageMap); 116 } 117 118 HashMap map = (HashMap) packageMap.get(languageCode); 119 if (map == null) { 120 map = new HashMap(); 121 packageMap.put(languageCode, map); 122 } 123 return map; 124 } 125 126 protected HashMap getBundleMaps() { 127 return bundleMaps; 128 } 129 130 protected void putBundleMap(String packageName, String languageCode, 131 String key, String value) { 132 HashMap map = getBundleMap(packageName, languageCode); 133 map.put(key, value); 134 } 135 136 public String getDefaultTaglibPrefix() { 137 return defaultTaglibPrefix; 138 } 139 140 public void setDefaultTaglibPrefix(String defaultTaglibPrefix) { 141 this.defaultTaglibPrefix = defaultTaglibPrefix; 142 } 143 144 public String getDefaultTaglibURI() { 145 return defaultTaglibURI; 146 } 147 148 public void setDefaultTaglibURI(String defaultTaglibURI) { 149 this.defaultTaglibURI = defaultTaglibURI; 150 } 151 152 public String getImplBD() { 153 return implBD; 154 } 155 156 public void setImplBD(String implBD) { 157 this.implBD = implBD; 158 } 159 160 public String getImplPD() { 161 return implPD; 162 } 163 164 public void setImplPD(String implPD) { 165 this.implPD = implPD; 166 } 167 168 public boolean getUseComponentResourceBundles() { 169 return useComponentResourceBundles; 170 } 171 172 public void setUseComponentResourceBundles(boolean flag) { 173 useComponentResourceBundles = flag; 174 } 175 176 public void generate() throws IOException { 177 178 ComponentBean cbs[] = getConfig().getComponents(); 179 for (int i = 0; i < cbs.length; i++){ 180 if (generated(cbs[i].getComponentClass())) { 181 generate(cbs[i]); 182 } 183 } 184 } 185 186 private void beanDescriptor(ComponentBean cb, RendererBean rb) 187 throws IOException { 188 String implBD = this.implBD; 189 int period = implBD.lastIndexOf("."); 190 if (period >= 0) 191 implBD = implBD.substring(period + 1); 192 193 JavaSourceWriter writer = getWriter(); 194 195 writer.startJavaDoc(); 196 writer 197 .emitJavaDoc("<p>Return the <code>BeanDescriptor</code> for this bean.</p>"); 198 writer.endJavaDoc(); 199 writer.startMethod("getBeanDescriptor", "BeanDescriptor", null, null); 200 writer.emitNewline(); 201 writer.emitExpression("if (beanDescriptor != null) {", true); 202 writer.indent(); 203 writer.emitExpression("return beanDescriptor;", true); 204 writer.outdent(); 205 writer.emitExpression("}", true); 206 writer.emitNewline(); 207 writer.emitExpression( 208 "beanDescriptor = new " + implBD + "(beanClass);", true); 209 writer.emitNewline(); 210 writer.emitExpression("return beanDescriptor;", true); 211 writer.emitNewline(); 212 writer.endMethod(); 213 writer.emitNewline(); 214 } 215 216 private void body(ComponentBean cb, RendererBean rb) throws IOException { 217 String baseBI = this.baseBI; 218 int period = baseBI.lastIndexOf("."); 219 if (period >= 0) 220 baseBI = baseBI.substring(period + 1); 221 JavaSourceWriter writer = getWriter(); 222 String simple = simpleClassName(cb.getComponentClass()); 223 simple = simple + "BeanInfo"; 224 writer.startClass(simple, baseBI, null, !getBase(), getBase()); 225 writer.emitNewline(); 226 227 constructor(cb, rb); 228 instance(cb, rb); 229 beanDescriptor(cb, rb); 230 defaultPropertyIndex(cb, rb); 231 propertyDescriptors(cb, rb); 232 writer.endClass(); 233 } 234 235 private void constructor(ComponentBean cb, RendererBean rb) 236 throws IOException { 237 JavaSourceWriter writer = getWriter(); 238 String simple = simpleClassName(cb.getComponentClass()); 239 writer.startJavaDoc(); 240 writer.emitJavaDoc("<p>Construct a new <code>" + simpleClassName(cb) 241 + "</code>.</p>"); 242 writer.endJavaDoc(); 243 writer.startMethod(simpleClassName(cb), null, null, null); 244 writer.emitNewline(); 245 writer.emitExpression("beanClass = " + simple + ".class;", true); 246 writer.emitNewline(); 247 writer.endMethod(); 248 writer.emitNewline(); 249 } 250 251 private void defaultPropertyIndex(ComponentBean cb, RendererBean rb) 252 throws IOException { 253 JavaSourceWriter writer = getWriter(); 254 writer.startJavaDoc(); 255 writer.emitJavaDoc("<p>Return the index of the default property, or"); 256 writer.emitJavaDoc("-1 if there is no default property.</p>"); 257 writer.endJavaDoc(); 258 writer.startMethod("getDefaultPropertyIndex", "int", null, null); 259 writer.emitNewline(); 260 writer.indent(); 261 writer.emitExpression("defaultPropertyIndex = -1;", true); 262 writer.emitExpression("return defaultPropertyIndex;", true); 263 writer.endMethod(); 264 writer.outdent(); 265 writer.emitNewline(); 266 return; 267 268 } 269 270 private void generate(ComponentBean cb) throws IOException { 271 272 if(cb.getComponentClass().startsWith("javax.faces.component.")){ 273 return; 274 } 275 if (cb.isSuppressed()) 276 return; 277 RendererBean rb = renderer(cb); 278 if (rb == null) { 279 rb = new RendererBean(); 280 } 281 File outputFile = outputFile(cb.getComponentClass() + "BeanInfo"); 282 283 outputFile.mkdirs(); 284 outputFile.delete(); 285 try { 286 getWriter().setOutputWriter( 287 new BufferedWriter(new FileWriter(outputFile))); 288 } catch (Exception e) { 289 e.printStackTrace(); 290 } 291 292 header(cb, rb); 293 body(cb, rb); 294 295 getWriter().flush(); 296 getWriter().close(); 297 } 298 299 private void header(ComponentBean cb, RendererBean rb) throws IOException { 300 JavaSourceWriter writer = getWriter(); 301 writer.outdent(); 302 String packageName = packageName(cb); 303 if (packageName.length() > 0) { 304 writer.emitPackage(packageName); 305 writer.emitNewline(); 306 } 307 writer.emitImport("java.awt.Image"); 308 writer.emitImport("java.beans.BeanDescriptor"); 309 writer.emitImport("java.beans.BeanInfo"); 310 writer.emitImport("java.beans.IntrospectionException"); 311 writer.emitImport("java.beans.PropertyDescriptor"); 312 writer.emitImport("java.util.Locale"); 313 writer.emitImport("java.util.ResourceBundle"); 314 writer.emitNewline(); 315 writer.emitNewline(); 316 writer.emitImport(baseBI); 317 if (!"java.beans.BeanDescriptor".equals(implBD)) { 318 writer.emitImport(implBD); 319 } 320 if (!"java.beans.PropertyDescriptor".equals(implPD)) { 321 writer.emitImport(implPD); 322 } 323 writer.emitNewline(); 324 writer.startJavaDoc(); 325 writer.emitJavaDoc("<p>Auto-generated design time metadata class."); 326 writer.emitJavaDoc("Do <strong>NOT</strong> modify; all changes"); 327 writer.emitJavaDoc("<strong>will</strong> be lost!</p>"); 328 writer.endJavaDoc(); 329 writer.emitNewline(); 330 } 331 332 private void instance(ComponentBean cb, RendererBean rb) throws IOException { 333 334 JavaSourceWriter writer = getWriter(); 335 writer.startJavaDoc(); 336 writer.emitJavaDoc("<p>The bean class that this BeanInfo represents."); 337 writer.endJavaDoc(); 338 writer.emitExpression("protected Class beanClass;", true); 339 writer.emitNewline(); 340 writer.startJavaDoc(); 341 writer.emitJavaDoc("<p>The cached BeanDescriptor.</p>"); 342 writer.endJavaDoc(); 343 writer.emitExpression("protected BeanDescriptor beanDescriptor;", true); 344 writer.emitNewline(); 345 writer.startJavaDoc(); 346 writer.emitJavaDoc("<p>The index of the default property.</p>"); 347 writer.endJavaDoc(); 348 writer.emitExpression("protected int defaultPropertyIndex = -1;", true); 349 writer.emitNewline(); 350 writer.startJavaDoc(); 351 writer.emitJavaDoc("<p>The name of the default property.</p>"); 352 writer.endJavaDoc(); 353 writer.emitExpression("protected String defaultPropertyName;", true); 354 writer.emitNewline(); 355 writer.startJavaDoc(); 356 writer.emitJavaDoc("<p>The cached property descriptors.</p>"); 357 writer.endJavaDoc(); 358 writer.emitExpression( 359 "protected PropertyDescriptor[] propDescriptors;", true); 360 writer.emitNewline(); 361 return; 362 363 } 364 365 private String packageName(ComponentBean cb) { 366 String componentClass = cb.getComponentClass(); 367 int last = componentClass.lastIndexOf('.'); 368 if (last >= 0){ 369 return componentClass.substring(0, last); 370 }else{ 371 return ""; 372 } 373 } 374 375 private void propertyDescriptor(ComponentBean cb, RendererBean rb, 376 PropertyBean pb, ComponentBean componentBeanBeingGenerated) 377 throws IOException { 378 String implPD = this.implPD; 379 int period = implPD.lastIndexOf("."); 380 if (period >= 0){ 381 implPD = implPD.substring(period + 1); 382 } 383 JavaSourceWriter writer = getWriter(); 384 DescriptionBean description = pb.getShortDescription(""); 385 if (description == null){ 386 description = pb.getDescription(""); 387 } 388 DescriptionBean descriptions[] = pb.getShortDescriptions(); 389 if (descriptions == null){ 390 descriptions = pb.getDescriptions(); 391 } 392 String name = pb.getPropertyName(); 393 String readMethod = pb.getReadMethod(); 394 if (pb.isWriteOnly()){ 395 readMethod = null; 396 }else if (readMethod == null){ 397 if ("boolean".equals(pb.getPropertyClass())){ 398 readMethod = "is" + Character.toUpperCase(name.charAt(0)) 399 + name.substring(1); 400 }else{ 401 readMethod = "get" + Character.toUpperCase(name.charAt(0))+ name.substring(1); 402 } 403 } 404 String writeMethod = pb.getWriteMethod(); 405 if (pb.isReadOnly()){ 406 writeMethod = null; 407 }else if (writeMethod == null){ 408 writeMethod = "set" + Character.toUpperCase(name.charAt(0)) 409 + name.substring(1); 410 } 411 StringBuffer psb = new StringBuffer ("PropertyDescriptor prop_"); 412 psb.append(name); 413 psb.append(" = new "); 414 psb.append(implPD); 415 psb.append("("); 416 psb.append(JavaSourceWriter.toJavaString(name)); 417 psb.append(",beanClass,"); 418 psb.append(readMethod != null ? JavaSourceWriter 419 .toJavaString(readMethod) : "null"); 420 psb.append(","); 421 psb.append(writeMethod != null ? JavaSourceWriter 422 .toJavaString(writeMethod) : "null"); 423 psb.append(");"); 424 writer.emitExpression(psb.toString(), true); 425 writer.emitNewline(); 426 } 427 428 private void propertyDescriptors(ComponentBean cb, RendererBean rb) 429 throws IOException { 430 JavaSourceWriter writer = getWriter(); 431 Map map = new TreeMap(); 432 writer.startJavaDoc(); 433 writer 434 .emitJavaDoc("<p>Return the <code>PropertyDescriptor</code>s for this bean.</p>"); 435 writer.endJavaDoc(); 436 writer.startMethod("getPropertyDescriptors", "PropertyDescriptor[]", 437 null, null); 438 writer.emitNewline(); 439 writer.emitExpression("if (propDescriptors != null) {", true); 440 writer.indent(); 441 writer.emitExpression("return propDescriptors;", true); 442 writer.outdent(); 443 writer.emitExpression("}", true); 444 writer.emitNewline(); 445 writer.emitExpression("try {", true); 446 writer.emitNewline(); 447 writer.indent(); 448 propertyDescriptors(cb, rb, ((Map) (map)), cb); 449 writer.emitExpression("propDescriptors = new PropertyDescriptor[] {", 450 true); 451 writer.indent(); 452 String prop; 453 for (Iterator props = map.keySet().iterator(); props.hasNext(); writer 454 .emitExpression("prop_" + prop + ",", true)){ 455 prop = (String ) props.next(); 456 } 457 458 writer.outdent(); 459 writer.emitExpression("};", true); 460 461 writer.emitExpression("return propDescriptors;", true); 462 writer.emitNewline(); 463 writer.outdent(); 464 writer.emitExpression("} catch (IntrospectionException e) {", true); 465 writer.indent(); 466 writer.emitExpression("e.printStackTrace();", true); 467 writer.emitExpression("return null;", true); 468 writer.outdent(); 469 writer.emitExpression("}", true); 470 writer.emitNewline(); 471 writer.endMethod(); 472 writer.emitNewline(); 473 } 474 475 private void propertyDescriptors(ComponentBean cb, RendererBean rb, 476 Map map, ComponentBean componentBeanBeingGenerated) 477 throws IOException { 478 479 PropertyBean pbs[] = cb.getProperties(); 480 for (int i = 0; i < pbs.length; i++) { 481 if (map.containsKey(pbs[i].getPropertyName()) 482 || "valid".equals(pbs[i].getPropertyName())){ 483 continue; 484 } 485 486 PropertyBean pb = merge(pbs[i], rb.getAttribute(pbs[i] 487 .getPropertyName())); 488 if (!pb.isSuppressed()) { 489 propertyDescriptor(cb, rb, pb, componentBeanBeingGenerated); 490 map.put(pb.getPropertyName(), null); 491 } 492 } 493 494 String baseComponentType = cb.getBaseComponentType(); 495 if (baseComponentType != null) { 496 ComponentBean bcb = getConfig().getComponent(baseComponentType); 497 propertyDescriptors(bcb, rb, map, componentBeanBeingGenerated); 498 } 499 } 500 501 private String simpleClassName(ComponentBean cb) { 502 return simpleClassName(cb.getComponentClass()) + "BeanInfo"; 503 } 504 505 } 506 | Popular Tags |