1 19 20 package org.netbeans.lib.jmi.mapping; 21 22 import org.netbeans.api.mdr.JMIStreamFactory; 23 import org.netbeans.lib.jmi.util.ContainsIterator; 24 import org.netbeans.lib.jmi.util.Logger; 25 import javax.jmi.model.*; 26 import java.io.BufferedReader ; 27 import java.io.IOException ; 28 import java.io.PrintStream ; 29 import java.io.StringReader ; 30 import java.text.MessageFormat ; 31 import java.util.*; 32 33 38 public class JavaMapper extends GenericMapper { 39 40 42 private static final String DOC_ATTR_GETTER = "Returns the value of attribute {0}."; private static final String DOC_ATTR_GETTER_RETURN = "Value of attribute {0}."; private static final String DOC_ATTR_SETTER = "Sets the value of {0} attribute. See '{'@link #{1}'}' for description on the attribute."; private static final String DOC_ATTR_SETTER_PARAM = "New value to be set."; private static final String DOC_ATTR_GETTER_RETURN_ORDERED = "Value of {0} attribute. Element type: '{'@link {1}'}'"; private static final String DOC_ATTR_GETTER_RETURN_MULTI = "Value of {0} attribute. Element type: '{'@link {1}'}'"; 53 private static final String DOC_ASSOC_PROXY = "{0} association proxy interface."; private static final String DOC_ASSOC_EXISTS = "Queries whether a link currently exists between a given pair of instance objects in the association's link set."; private static final String DOC_ASSOC_EXISTS_PARAM1 = "Value of the first association end."; private static final String DOC_ASSOC_EXISTS_PARAM2 = "Value of the second association end."; private static final String DOC_ASSOC_EXISTS_RETURN = "Returns true if the queried link exists."; private static final String DOC_ASSOC_ADD = "Creates a link between the pair of instance objects in the association's link set."; private static final String DOC_ASSOC_REMOVE = "Removes a link between a pair of instance objects in the current association's link set."; private static final String DOC_ASSOC_GET_END1_SINGLE = "Queries the instance object that is related to a particular instance object by a link in the current association's link set."; private static final String DOC_ASSOC_GET_END1_MULTI = "Queries the instance objects that are related to a particular instance object by a link in the current association's link set."; private static final String DOC_ASSOC_GET_END1_ORDERED = "Queries the instance objects that are related to a particular instance object by a link in the current association's link set."; private static final String DOC_ASSOC_GET_END1_PARAM = "Required value of the first association end."; private static final String DOC_ASSOC_GET_END1_RETURN_SINGLE = "Related object or <code>null</code> if none exists."; private static final String DOC_ASSOC_GET_END1_RETURN_MULTI = "Collection of related objects."; private static final String DOC_ASSOC_GET_END1_RETURN_ORDERED = "List of related objects."; private static final String DOC_ASSOC_GET_END2_PARAM = "Required value of the second association end."; 75 private static final String DOC_INSTANCE = "{0} object instance interface."; 78 private static final String DOC_REFERENCE_GETTER = "Returns the value of reference {0}."; private static final String DOC_REFERENCE_GETTER_RETURN = "Value of reference {0}."; private static final String DOC_REFERENCE_SETTER = "Sets the value of reference {0}. See '{'@link #{1}'}' for description on the reference."; private static final String DOC_REFERENCE_SETTER_PARAM = "New value to be set."; private static final String DOC_REFERENCE_GETTER_RETURN_ORDERED = "Value of reference {0}. Element type: '{'@link {1}'}'"; private static final String DOC_REFERENCE_GETTER_RETURN_MULTI = "Value of reference {0}. Element type: '{'@link {1}'}'"; 89 private static final String DOC_CLASS_PROXY = "{0} class proxy interface."; private static final String DOC_CLASS_PROXY_CREATE = "The default factory operation used to create an instance object."; private static final String DOC_CLASS_PROXY_CREATE_RETURN = "The created instance object."; private static final String DOC_CLASS_PROXY_CREATE2 = "Creates an instance object having attributes initialized by the passed values."; 97 private static final String DOC_PACKAGE = "{0} package interface."; private static final String DOC_PACKAGE_GET_IMPORT = "Returns clustered package {1}."; private static final String DOC_PACKAGE_GET_IMPORT_RETURN = "Proxy object related to clustered package {1}."; private static final String DOC_PACKAGE_GET_NESTED = "Returns nested package {1}."; private static final String DOC_PACKAGE_GET_NESTED_RETURN = "Proxy object related to nested package {1}."; private static final String DOC_PACKAGE_GET_CLASS = "Returns {1} class proxy object."; private static final String DOC_PACKAGE_GET_CLASS_RETURN = "{1} class proxy object."; private static final String DOC_PACKAGE_GET_ASSOC = "Returns {1} association proxy object."; private static final String DOC_PACKAGE_GET_ASSOC_RETURN = "{1} association proxy object."; 112 private static final String DOC_EXCEPTION = "{0} exception implementation class."; private static final String DOC_EXCEPTION_CONSTRUCTOR = "The public constructor."; private static final String DOC_EXCEPTION_GETTER = "Returns value of parameter {1}."; private static final String DOC_EXCEPTION_GETTER_RETURN = "Value of parameter {1}."; 120 private static final String DOC_ENUM_INTERFACE = "{0} enumeration interface."; 123 private static final String DOC_ENUM_CLASS = "{0} enumeration class implementation."; private static final String DOC_ENUM_LITERAL = "Enumeration constant corresponding to literal {1}."; private static final String DOC_ENUM_REFTYPENAME = "Returns fully qualified name of the enumeration type."; private static final String DOC_ENUM_REFTYPENAME_RETURN = "List containing all parts of the fully qualified name."; private static final String DOC_ENUM_TOSTRING = "Returns a string representation of the enumeration value."; private static final String DOC_ENUM_TOSTRING_RETURN = "A string representation of the enumeration value."; private static final String DOC_ENUM_HASHCODE = "Returns a hash code for this the enumeration value."; private static final String DOC_ENUM_HASHCODE_RETURN = "A hash code for this enumeration value."; private static final String DOC_ENUM_EQUALS = "Indicates whether some other object is equal to this enumeration value."; private static final String DOC_ENUM_EQUALS_RETURN = "true if the other object is the enumeration of the same type and of the same value."; private static final String DOC_ENUM_EQUALS_PARAM = "The reference object with which to compare."; private static final String DOC_ENUM_FORNAME = "Translates literal name to correspondent enumeration value."; private static final String DOC_ENUM_FORNAME_RETURN = "Enumeration value corresponding to the passed literal."; private static final String DOC_ENUM_FORNAME_PARAM = "Enumeration literal."; private static final String DOC_ENUM_READRESOLVE = "Resolves serialized instance of enumeration value."; private static final String DOC_ENUM_READRESOLVE_RETURN = "Resolved enumeration value."; 148 private static final String DOC_STRUCT = "{0} structure interface."; private static final String DOC_STRUCT_CREATE = "Creates an instance of {0} structure type."; private static final String DOC_STRUCT_CREATE_RETURN = "Value of {0}."; private static final String DOC_STRUCT_GETTER = "Returns value of {1} field."; private static final String DOC_STRUCT_GETTER_RETURN = "Value of {1} field."; 157 private static final String DOC_WARNING = "<p><em><strong>Note:</strong> This type should not be subclassed or implemented by clients. It is generated from a MOF metamodel and automatically implemented by MDR (see <a HREF=\"http://mdr.netbeans.org/\">mdr.netbeans.org</a>).</em></p>"; 160 162 private int indentation = 0; 164 165 private final JMIStreamFactory generator; 167 168 private static final String TAB = " "; private PrintStream stream = null; 172 private String header = null; 174 175 private void generate(String text) throws IOException { 177 generateTabs(); 179 stream.println(text); 181 checkStreamForErrors(stream); 182 } 183 184 private void generateTabs() throws IOException { 185 for (int i = 0; i < indentation; i++) { 187 stream.print(TAB); 188 } 189 checkStreamForErrors(stream); 190 } 191 192 private void newLine() throws IOException { 194 stream.println(); 195 checkStreamForErrors(stream); 196 } 197 198 private void indent() { 200 indentation++; 201 } 202 203 private void indent(int count) { 205 indentation = count; 206 } 207 208 private void unindent() { 210 indentation--; 211 } 212 213 protected boolean createStream(List pkg, String name) throws IOException { 214 if (stream != null) { 215 closeStream(); 216 throw new IllegalStateException ("Attempting to create stream before previous stream was closed."); 217 } 218 java.io.OutputStream os = generator.createStream(pkg, name); 219 if (os != null) { 220 stream = new PrintStream (os); 221 } 222 return stream != null; 223 } 224 225 protected void closeStream() throws IOException { 226 if (stream == null) 227 throw new IllegalStateException ("Attempting to close the stream without opening it first."); 228 PrintStream ps = stream; 229 stream = null; 230 ps.close(); 231 checkStreamForErrors(ps); 232 } 233 234 private static void checkStreamForErrors(PrintStream ps) throws IOException { 235 if (ps.checkError()) 236 throw new IOException ("Error writing to print stream during JMI Source generator."); 237 } 238 239 245 private void interfaceHeader(String pkg, String name, Object [] ancestors, ModelElement elem, String comment) throws IOException { 246 classHeader(pkg, name, ancestors, null, true, elem, comment); 247 } 248 249 257 private void classHeader(String pkg, String name, Object [] ancestors, Object [] interfaces, boolean isInterface, ModelElement elem, String comment) throws IOException { 258 classHeader(pkg, name, ancestors, interfaces, isInterface, false, elem, comment); 259 } 260 261 269 private void classHeader(String pkg, String name, Object [] ancestors, Object [] interfaces, boolean isInterface, boolean isFinal, ModelElement elem, String comment) throws IOException { 270 indent(0); 272 273 StringBuffer sbAnc = new StringBuffer (); 274 StringBuffer sbIfc = new StringBuffer (); 275 String type; 276 277 if (header != null) { 280 generate(header); 281 newLine(); 282 } 283 generate("package " + pkg + ";"); newLine(); 286 287 classJavaDoc (elem, comment); 289 if (ancestors != null) { 291 for (int i = 0; i < ancestors.length; sbAnc.append((String ) ancestors[i++])) { 292 if (sbAnc.length() > 0) sbAnc.append(", "); else sbAnc.append(" extends "); } 295 } 296 if (isInterface) { 297 type = "interface"; } else { 299 if (isFinal) type = "final class"; else type = "class"; if (interfaces != null) { 302 for (int i = 0; i < interfaces.length; sbIfc.append((String ) interfaces[i++])) { 303 if (sbIfc.length() > 0) sbIfc.append(", "); else sbIfc.append(" implements "); } 306 } 307 } 308 generate("public " + type + " " + name + sbAnc.toString() + sbIfc.toString() + " {"); 310 indent(); 312 } 313 314 private void interfaceFooter(String name) throws IOException { 316 classFooter(name, true); 317 } 318 319 private void classFooter(String name, boolean isInterface) throws IOException { 320 unindent(); 322 323 generate("}"); } 326 327 private void interfaceMethod(String returnType, String name, Object [] parameters, Object [] exceptions) throws IOException { 329 StringBuffer sb = new StringBuffer (); 330 StringBuffer sb2 = new StringBuffer (); 331 332 if (parameters != null) 333 for (int i = 0; i < parameters.length; sb.append((String ) parameters[i++] + " " + (String ) parameters[i++])) { if (sb.length() > 0) sb.append(", "); } 336 337 if (exceptions != null) 338 for (int i = 0; i < exceptions.length; sb2.append((String ) exceptions[i++])) { 339 if (sb2.length() > 0) sb2.append(", "); else sb2.append(" throws "); } 342 343 generate("public " + returnType + " " + name + "(" + sb + ")" + sb2 + ";"); } 345 346 private void interfaceField(String fieldType, String name, String value) throws IOException { 348 generate("public final " + fieldType + " " + name + " = " + value + ";"); } 350 351 private void attributeTemplate(Object objAttribute) throws IOException { 354 Attribute attr = (Attribute) objAttribute; 355 String attrName = firstUpper(tagProvider.getSubstName(attr)); 356 Classifier attrType = getAttrType(attr); 357 String attrTypeName = getTypeName(attr, GENERIC_STYLE_FAKE); 358 359 if (attr.getMultiplicity().getUpper() == 1) { 360 String name; 362 String setterName; 363 if (attrType instanceof PrimitiveType && attrType.getName().equals("Boolean")) { if (attrName.substring(0, 2).equals("Is")) name = firstLower(attrName); else name = "is" + attrName; setterName = "set" + name.substring(2); } else { 368 name = "get" + attrName; setterName = "set" + attrName; } 371 372 String getterType = attrTypeName; 374 if (attr.getMultiplicity().getLower() == 1) { 375 getterType = getPrimitiveName(getterType); 376 } 377 378 methodJavaDoc(attr, true, DOC_ATTR_GETTER, DOC_ATTR_GETTER_RETURN, null, null, null); 379 interfaceMethod(getterType, name, null, null); 380 381 if (attr.isChangeable()) { 383 methodJavaDoc( 384 attr, false, DOC_ATTR_SETTER, null, new String [] {"newValue"}, new Object [] {DOC_ATTR_SETTER_PARAM}, new String [] {name} 386 ); 387 interfaceMethod("void", setterName, new String [] {getterType, "newValue"}, null); } 389 } else if (attr.getMultiplicity().getUpper() != 0) { 390 boolean ordered = attr.getMultiplicity().isOrdered(); 392 methodJavaDoc(attr, true, DOC_ATTR_GETTER, 393 (ordered ? DOC_ATTR_GETTER_RETURN_ORDERED : DOC_ATTR_GETTER_RETURN_MULTI), null, null, 394 new String [] {getTypeName(attrType)} 395 ); 396 interfaceMethod(attrTypeName, "get" + attrName, null, null); } 398 } 399 400 private void operationTemplate(Object objOperation) throws IOException { 402 Operation oper = (Operation) objOperation; 403 Collection exceptions = new ArrayList(); 404 Collection parameters = new ArrayList(); 405 ArrayList parametersList = new ArrayList(); 406 ArrayList parametersNames = new ArrayList(); 407 408 String operType = "void"; Parameter param, returnParam = null; 410 ModelElement element; 411 412 for (Iterator it = oper.getContents().iterator(); it.hasNext();) { 413 element = (ModelElement) it.next(); 414 if (element instanceof Parameter) { 415 param = (Parameter) element; 416 if (param.getDirection().equals(DirectionKindEnum.RETURN_DIR)) { 417 operType = getTypeName(param, GENERIC_STYLE_FAKE); 418 returnParam = param; 419 } else { 420 parameters.add(getTypeName(param, GENERIC_STYLE_FAKE)); 421 String name = tagProvider.getSubstName(param) + (param.getDirection().equals(DirectionKindEnum.IN_DIR) ? "" : "[]"); parameters.add(name); 423 424 parametersList.add (param); 425 parametersNames.add (name); 426 } 427 } 428 } 429 430 for (Iterator it = oper.getExceptions().iterator(); it.hasNext();) { 431 element = (ModelElement) it.next(); 432 exceptions.add(tagProvider.getTypeFullName(element)); 433 } 434 435 int size = parametersList.size (); 436 Object [] paramsArray = new Object [size]; 437 paramsArray = parametersList.toArray (paramsArray); 438 String [] namesArray = new String [size]; 439 namesArray = (String []) parametersNames.toArray (namesArray); 440 441 methodJavaDoc(oper, true, null, returnParam, namesArray, paramsArray, null); 442 interfaceMethod(operType, tagProvider.getSubstName(oper), parameters.toArray(), exceptions.toArray()); 443 } 444 445 private void constantTemplate(Object objConstant) throws IOException { 447 Constant constant = (Constant) objConstant; 448 DataType type = (DataType) getAttrType(constant); 449 String value = constant.getValue(); 450 451 if (type.getName ().equals ("String")) { value = "\"" + value.replace('\n', ' ') + "\""; } 454 455 fieldJavaDoc (constant); 456 interfaceField(getTypeName2(constant), tagProvider.getSubstName(constant), value); 457 } 458 459 private void referenceTemplate(Object objReference) throws IOException { 461 Reference ref = (Reference) objReference; 462 String refName = firstUpper(tagProvider.getSubstName(ref)); 463 String refType = getTypeName(getAttrType(ref)); 464 465 if (ref.getMultiplicity().getUpper() == 1) { 466 methodJavaDoc ( 468 ref, true, DOC_REFERENCE_GETTER, DOC_REFERENCE_GETTER_RETURN, 469 null, null, null 470 ); 471 interfaceMethod(refType, "get" + refName, null, null); 473 if (ref.isChangeable()) { 475 methodJavaDoc ( 476 ref, false, DOC_REFERENCE_SETTER, null, new String [] {"newValue"}, new Object [] {DOC_REFERENCE_SETTER_PARAM}, new String [] {"get" + refName} ); 479 interfaceMethod("void", "set" + refName, new String [] {refType, "newValue"}, null); } 481 } else if (ref.getMultiplicity().getUpper() != 0) { 482 boolean isOrdered = ref.getMultiplicity().isOrdered(); 484 methodJavaDoc ( 485 ref, true, DOC_REFERENCE_GETTER, 486 isOrdered ? DOC_REFERENCE_GETTER_RETURN_ORDERED : DOC_REFERENCE_GETTER_RETURN_MULTI, 487 null, null, new String [] {refType} 488 ); 489 interfaceMethod(getTypeName(ref, GENERIC_STYLE_FAKE), "get" + refName, null, null); } 491 } 492 493 protected void classProxyTemplate(javax.jmi.model.MofClass objClass) throws IOException { 495 final ClassContents contents = getClassContents(objClass); 496 interfaceHeader(tagProvider.getTypePrefix(objClass), tagProvider.getSubstName(objClass) + CLASS_POSTFIX, new String [] {"javax.jmi.reflect.RefClass"}, objClass, DOC_CLASS_PROXY); 498 if (!objClass.isAbstract()) { 499 methodJavaDoc ( 501 objClass, false, DOC_CLASS_PROXY_CREATE, 502 DOC_CLASS_PROXY_CREATE_RETURN, null, null, null 503 ); 504 interfaceMethod(tagProvider.getSubstName(objClass), "create" + tagProvider.getSubstName(objClass), null, null); 506 if (contents.allAttributes.size() > 0) { 508 Object [] params = contents.allAttributes.toArray(); 509 Object [] paramsObjects = contents.allAttributesObjects.toArray(); 510 String [] paramsNames = new String [paramsObjects.length]; 511 for (int x = 0; x < paramsNames.length; x++) { 512 paramsNames [x] = ((ModelElement) paramsObjects [x]).getName (); 513 } 514 String name = tagProvider.getSubstName(objClass); 515 methodJavaDoc ( 516 objClass, false, DOC_CLASS_PROXY_CREATE2, DOC_CLASS_PROXY_CREATE_RETURN, 517 paramsNames, paramsObjects, null 518 ); 519 interfaceMethod(tagProvider.getSubstName(objClass), "create" + name, params, null); } 522 } 523 524 for (Iterator it = contents.clsOperations.iterator(); it.hasNext();) 526 operationTemplate(it.next()); 527 528 for (Iterator it = contents.clsAttributes.iterator(); it.hasNext();) 530 attributeTemplate(it.next()); 531 532 for (Iterator it = contents.structures.iterator(); it.hasNext();) { 534 structTemplate(it.next()); 535 } 536 537 interfaceFooter(tagProvider.getSubstName(objClass) + CLASS_POSTFIX); 538 } 539 540 protected void classInstanceTemplate(MofClass objClass) throws IOException { 541 final ClassContents contents = getClassContents(objClass); 542 543 final Collection ancestors = new ArrayList(); 545 final Collection supertypes = objClass.getSupertypes(); 546 547 for (Iterator it = supertypes.iterator(); it.hasNext();) { 548 ancestors.add(tagProvider.getTypeFullName((ModelElement) it.next())); 549 } 550 if (ancestors.size() == 0) ancestors.add("javax.jmi.reflect.RefObject"); 552 interfaceHeader(tagProvider.getTypePrefix(objClass), tagProvider.getSubstName(objClass), ancestors.toArray(), objClass, DOC_INSTANCE); 553 554 for (Iterator it = contents.constants.iterator(); it.hasNext();) 556 constantTemplate(it.next()); 557 558 for (Iterator it = contents.instOperations.iterator(); it.hasNext();) 560 operationTemplate(it.next()); 561 562 for (Iterator it = contents.instAttributes.iterator(); it.hasNext();) 564 attributeTemplate(it.next()); 565 566 for (Iterator it = contents.instReferences.iterator(); it.hasNext();) 568 referenceTemplate(it.next()); 569 570 interfaceFooter(tagProvider.getSubstName(objClass)); 571 } 572 573 private ClassContents getClassContents(MofClass cls) { 579 if (cachedClassContents == null || cachedClassContents.objClass != cls) 580 cachedClassContents = new ClassContents(cls); 581 return cachedClassContents; 582 } 583 private ClassContents cachedClassContents; 584 585 private final class ClassContents { 586 final Collection instAttributes = new ArrayList(); 587 final Collection instOperations = new ArrayList(); 588 final Collection instReferences = new ArrayList(); 589 final Collection clsAttributes = new ArrayList(); 590 final Collection clsOperations = new ArrayList(); 591 final Collection allAttributes = new ArrayList(); 592 final Collection allAttributesObjects = new ArrayList (); 593 final Collection constants = new ArrayList(); 594 final Collection enumerations = new ArrayList(); 595 final Collection structures = new ArrayList(); 596 final MofClass objClass; 597 598 private ClassContents(MofClass objClass) { 599 this.objClass = objClass; 600 final Collection elements = objClass.getContents(); 601 602 ModelElement element; 603 Feature feature; 604 605 for (Iterator it = elements.iterator(); it.hasNext();) { 606 element = (ModelElement) it.next(); 607 608 if (element instanceof Constant) { 609 constants.add(element); 610 } else if (element instanceof Feature) { 611 feature = (Feature) element; 612 if (feature.getVisibility().equals(VisibilityKindEnum.PUBLIC_VIS)) { 613 if (feature instanceof Attribute) { 614 if (feature.getScope().equals(ScopeKindEnum.INSTANCE_LEVEL)) 615 instAttributes.add(feature); 616 } else if (feature instanceof Operation) { 617 if (feature.getScope().equals(ScopeKindEnum.INSTANCE_LEVEL)) 618 instOperations.add(feature); 619 } else if (feature instanceof Reference) { 620 instReferences.add(feature); 621 } 622 } 623 } else if (element instanceof EnumerationType) { 624 enumerations.add(element); 625 } else if (element instanceof StructureType) { 626 structures.add(element); 627 } 628 } 629 630 for (Iterator it = new ContainsIterator(objClass); it.hasNext();) { 631 if ((element = (ModelElement) it.next()) instanceof Attribute) { 632 feature = (Attribute) element; 633 if (feature.getScope().equals(ScopeKindEnum.CLASSIFIER_LEVEL)) { 634 clsAttributes.add(feature); 635 } else { 636 if (!((Attribute) feature).isDerived()) { 637 allAttributes.add(getTypeName((Attribute) feature, GENERIC_STYLE_FAKE)); 638 allAttributes.add(tagProvider.getSubstName(feature)); 639 allAttributesObjects.add(feature); 640 } 641 } 642 } else if (element instanceof Operation) { 643 feature = (Operation) element; 644 if (feature.getScope().equals(ScopeKindEnum.CLASSIFIER_LEVEL)) { 645 clsOperations.add(feature); 646 } 647 } 648 } 649 } 650 } 651 652 protected void associationTemplate(Association objAssociation) throws IOException { 654 Collection contents = objAssociation.getContents(); 655 AssociationEnd[] ends = new AssociationEnd[2]; 656 Object element; 657 int i = 0; 658 boolean single, ordered; 659 660 for (Iterator it = contents.iterator(); it.hasNext() && i < 2;) { 661 element = it.next(); 662 if (element instanceof AssociationEnd) 663 ends[i++] = (AssociationEnd) element; 664 } 665 666 String end1Name = tagProvider.getSubstName(ends[0]); 667 String end1Class = getTypeName(getAttrType(ends[0])); 668 String end2Name = tagProvider.getSubstName(ends[1]); 669 String end2Class = getTypeName(getAttrType(ends[1])); 670 671 interfaceHeader(tagProvider.getTypePrefix(objAssociation), tagProvider.getSubstName(objAssociation), new String [] {"javax.jmi.reflect.RefAssociation"}, objAssociation, DOC_ASSOC_PROXY); 673 methodJavaDoc ( 675 objAssociation, false, DOC_ASSOC_EXISTS, DOC_ASSOC_EXISTS_RETURN, 676 new String [] {end1Name, end2Name}, new Object [] {DOC_ASSOC_EXISTS_PARAM1, DOC_ASSOC_EXISTS_PARAM2}, null 677 ); 678 interfaceMethod("boolean", "exists", new String [] {end1Class, end1Name, end2Class, end2Name}, null); 680 if (ends[0].isNavigable()) { 682 single = ends[0].getMultiplicity().getUpper() == 1; 683 ordered = ends[0].getMultiplicity().isOrdered(); 684 methodJavaDoc ( 685 objAssociation, false, 686 single ? DOC_ASSOC_GET_END1_SINGLE : (ordered ? DOC_ASSOC_GET_END1_ORDERED : DOC_ASSOC_GET_END1_MULTI), 687 single ? DOC_ASSOC_GET_END1_RETURN_SINGLE : (ordered ? DOC_ASSOC_GET_END1_RETURN_ORDERED : DOC_ASSOC_GET_END1_RETURN_MULTI), 688 new String [] {end2Name}, new Object [] {DOC_ASSOC_GET_END2_PARAM}, null 689 ); 690 interfaceMethod(single ? end1Class : (ordered ? DT_ORDERED : DT_MULTIVALUED), 692 "get" + firstUpper(end1Name), new String [] {end2Class, end2Name}, null); } 694 695 if (ends[1].isNavigable()) { 697 single = ends[1].getMultiplicity().getUpper() == 1; 698 ordered = ends[1].getMultiplicity().isOrdered(); 699 methodJavaDoc ( 700 objAssociation, false, 701 single ? DOC_ASSOC_GET_END1_SINGLE : (ordered ? DOC_ASSOC_GET_END1_ORDERED : DOC_ASSOC_GET_END1_MULTI), 702 single ? DOC_ASSOC_GET_END1_RETURN_SINGLE : (ordered ? DOC_ASSOC_GET_END1_RETURN_ORDERED : DOC_ASSOC_GET_END1_RETURN_MULTI), 703 new String [] {end1Name}, new Object [] {DOC_ASSOC_GET_END1_PARAM}, null 704 ); 705 interfaceMethod(single ? end2Class : (ordered ? DT_ORDERED : DT_MULTIVALUED), 707 "get" + firstUpper(end2Name), new String [] {end1Class, end1Name}, null); } 709 710 if (ends[0].isChangeable() && ends[1].isChangeable()) { 711 String [] names = new String [] {end1Name, end2Name}; 712 Object [] params = new Object [] {DOC_ASSOC_EXISTS_PARAM1, DOC_ASSOC_EXISTS_PARAM2}; 713 714 methodJavaDoc ( 716 objAssociation, false, DOC_ASSOC_ADD, null, 717 names, params, null 718 ); 719 interfaceMethod("boolean", "add", new String [] {end1Class, end1Name, end2Class, end2Name}, null); 721 methodJavaDoc ( 723 objAssociation, false, DOC_ASSOC_REMOVE, null, 724 names, params, null 725 ); 726 interfaceMethod("boolean", "remove", new String [] {end1Class, end1Name, end2Class, end2Name}, null); } 728 729 interfaceFooter(tagProvider.getSubstName(objAssociation)); 730 } 731 732 protected void packageTemplate(MofPackage objPackage) throws IOException { 734 Collection elements; 735 ModelElement element; 736 Collection ancestors = new ArrayList(); 737 Collection supertypes = objPackage.getSupertypes(); 738 for (Iterator it = supertypes.iterator(); it.hasNext();) { 739 ancestors.add(tagProvider.getTypeFullName((ModelElement) it.next()) + PACKAGE_POSTFIX); 740 } 741 if (ancestors.size() == 0) ancestors.add("javax.jmi.reflect.RefPackage"); interfaceHeader(tagProvider.getTypePrefix(objPackage), tagProvider.getSubstName(objPackage) + PACKAGE_POSTFIX, ancestors.toArray(), objPackage, DOC_PACKAGE); 743 744 ArrayList nestedPackages = new ArrayList(); 745 ArrayList classes = new ArrayList(); 746 ArrayList associations = new ArrayList(); 747 ArrayList structureTypes = new ArrayList(); 748 749 elements = objPackage.getContents(); 750 for (Iterator it = elements.iterator(); it.hasNext();) { 751 element = (ModelElement) it.next(); 752 if (element instanceof Association) { 753 associations.add(element); 754 } else if (element instanceof javax.jmi.model.MofClass) { 755 classes.add(element); 756 } else if (element instanceof MofPackage) { 757 nestedPackages.add(element); 758 } else if (element instanceof StructureType) { 759 structureTypes.add(element); 760 } else if (element instanceof Import) { 761 Import imp = (Import) element; 765 if (imp.isClustered() && VisibilityKindEnum.PUBLIC_VIS.equals(imp.getVisibility())) { 766 Namespace ns = imp.getImportedNamespace(); 767 if (ns instanceof MofPackage && 768 VisibilityKindEnum.PUBLIC_VIS.equals(((MofPackage) ns).getVisibility())) { 769 String typeName = tagProvider.getTypeFullName(ns) + PACKAGE_POSTFIX; 770 String elementName = tagProvider.getSubstName(element); 771 interfaceMethod(typeName, "get" + elementName, null, null); } 773 } else { 774 Logger.getDefault().log("import: " + imp.getName() + " mofid: " + imp.refMofId() + " clustered: " + imp.isClustered() + " visibility: " + imp.getVisibility()); } 776 } 777 } 778 779 addProxyGetter(objPackage, nestedPackages, PACKAGE_POSTFIX); 780 addProxyGetter(objPackage, classes, CLASS_POSTFIX); 781 addProxyGetter(objPackage, associations, ""); 783 for (Iterator i = structureTypes.iterator(); i.hasNext(); ) { 784 structTemplate(i.next()); 785 } 786 787 interfaceFooter(tagProvider.getSubstName(objPackage) + PACKAGE_POSTFIX); 788 } 789 790 void addProxyGetter(MofPackage objPackage, ArrayList list, String suffix) throws IOException { 791 for (Iterator i = list.iterator(); i.hasNext(); ) { 792 GeneralizableElement element = (GeneralizableElement) i.next(); 793 if (VisibilityKindEnum.PUBLIC_VIS.equals(element.getVisibility())) { 794 String methodComment = null, returnComment = null; 795 if (element instanceof Import) { 796 methodComment = DOC_PACKAGE_GET_IMPORT; 797 returnComment = DOC_PACKAGE_GET_IMPORT_RETURN; 798 } else if (element instanceof MofPackage) { 799 methodComment = DOC_PACKAGE_GET_NESTED; 800 returnComment = DOC_PACKAGE_GET_NESTED_RETURN; 801 } else if (element instanceof MofClass) { 802 methodComment = DOC_PACKAGE_GET_CLASS; 803 returnComment = DOC_PACKAGE_GET_CLASS_RETURN; 804 } else if (element instanceof Association) { 805 methodComment = DOC_PACKAGE_GET_ASSOC; 806 returnComment = DOC_PACKAGE_GET_ASSOC_RETURN; 807 } 808 String typeName = tagProvider.getTypeFullName(element); 809 String elementName = tagProvider.getSubstName(element); 810 if (methodComment != null) 811 methodJavaDoc(objPackage, false, methodComment, returnComment, null, null, new String [] {elementName}); 812 interfaceMethod(typeName + suffix, "get" + elementName, null, null); } 814 } 815 } 816 817 818 protected void exceptionTemplate(MofException objException) throws IOException { 820 Collection contents = objException.getContents(); 821 ArrayList params = new ArrayList(); 822 ArrayList paramsObjects = new ArrayList(); 823 ArrayList paramsNames = new ArrayList(); 824 ArrayList codeConstr = new ArrayList(); 825 StringBuffer paramsConstr = new StringBuffer (100); 826 StringBuffer codeMsg = new StringBuffer (100); 827 828 Object element; 829 Parameter param; 830 String typeName; 831 String paramName; 832 String getterName; 833 834 for (Iterator it = contents.iterator(); it.hasNext();) { 835 element = it.next(); 836 if (element instanceof Parameter) { 837 param = (Parameter) element; 838 paramsObjects.add (param); 839 params.add(getTypeName(param, GENERIC_STYLE_FAKE)); 840 paramName = firstLower(tagProvider.getSubstName(param)); 841 paramsNames.add (paramName); 842 params.add(removeUnderscores(paramName)); 843 getterName = paramName; 844 if (param.getType() instanceof PrimitiveType && param.getType().getName().equals("Boolean")) { if (getterName.indexOf("is") != 0) { getterName = "is_" + getterName; } 848 } else { 849 getterName = "get_" + getterName; } 851 params.add(removeUnderscores(getterName)); 852 } 853 } 854 855 String exceptionName = tagProvider.getSubstName(objException); 856 857 classHeader(tagProvider.getTypePrefix(objException), exceptionName, new Object [] {DT_EXCEPTION}, null, false, objException, DOC_EXCEPTION); 858 859 for (Iterator it = params.iterator(); it.hasNext(); it.next()) { 860 typeName = (String ) it.next(); 861 paramName = (String ) it.next(); 862 generate("private final " + typeName + " " + paramName + ";"); codeConstr.add("this." + paramName + " = " + paramName + ";"); paramsConstr.append(", " + typeName + " " + paramName); getterName = paramName + ": \" + " + paramName; if (codeMsg.length() == 0) { 867 codeMsg.append("\"" + getterName); } else { 869 codeMsg.append(" + \", " + getterName); } 871 } 872 873 methodJavaDoc ( 875 objException, false, DOC_EXCEPTION_CONSTRUCTOR, null, 876 (String []) paramsNames.toArray (new String [] {}), paramsObjects.toArray (), null 877 ); 878 generate("public " + exceptionName + "(" + (paramsConstr.length() == 0 ? "" : paramsConstr.toString().substring(2)) + ") {"); indent(); 880 generate("super(" + codeMsg.toString() + ");"); for (Iterator it = codeConstr.iterator(); it.hasNext();) { 882 generate((String ) it.next()); 883 } 884 unindent(); 885 generate("}"); 887 for (Iterator it = params.iterator(); it.hasNext();) { 889 typeName = (String ) it.next(); 890 paramName = (String ) it.next(); 891 getterName = (String ) it.next(); 892 methodJavaDoc ( 893 objException, false, DOC_EXCEPTION_GETTER, DOC_EXCEPTION_GETTER_RETURN, 894 null, null, new String [] {paramName} 895 ); 896 generate("public " + typeName + " " + getterName + "() {"); indent(); 898 generate("return " + paramName + ";"); unindent(); 900 generate("}"); } 902 903 classFooter(exceptionName, false); 904 } 905 906 protected void enumerationInterfaceTemplate(EnumerationType objEnumeration) throws IOException { 907 String packageName = tagProvider.getTypePrefix(objEnumeration); 909 String ifcName = tagProvider.getSubstName(objEnumeration); 910 911 interfaceHeader(packageName, ifcName, new Object [] {"javax.jmi.reflect.RefEnum"}, objEnumeration, DOC_ENUM_INTERFACE); interfaceFooter(tagProvider.getSubstName(objEnumeration)); 913 } 914 915 protected void enumerationClassTemplate(EnumerationType objEnumeration) throws IOException { 916 String packageName = tagProvider.getTypePrefix(objEnumeration); 917 String ifcName = tagProvider.getSubstName(objEnumeration); 918 String clsName = ifcName + ENUM_POSTFIX; 919 920 List literals = objEnumeration.getLabels(); 921 String literal; 922 923 classHeader(packageName, clsName, new Object [0], new Object [] {ifcName}, false, true, objEnumeration, DOC_ENUM_CLASS); 924 925 for (Iterator it = literals.iterator(); it.hasNext();) { 927 literal = (String ) it.next(); 928 methodJavaDoc (objEnumeration, false, DOC_ENUM_LITERAL, null, null, null, new String [] {literal}); 929 generate("public static final " + clsName + " " + tagProvider.mapEnumLiteral(literal) + " = new " + clsName + "(\"" + literal + "\");"); } 931 newLine(); 932 933 generate("private static final java.util.List typeName;"); generate("private final java.lang.String literalName;"); newLine(); 937 938 generate("static {"); indent(); 941 generate("java.util.ArrayList temp = new java.util.ArrayList();"); 943 String enumFQN = ""; for (Iterator it = objEnumeration.getQualifiedName().iterator(); it.hasNext();) { 945 String fqnElement = (String ) it.next(); 946 generate("temp.add(\"" + fqnElement + "\");"); enumFQN += fqnElement + (it.hasNext() ? "." : ""); } 949 950 generate("typeName = java.util.Collections.unmodifiableList(temp);"); unindent(); 952 generate("}"); newLine(); 954 955 generate("private " + clsName + "(java.lang.String literalName) {"); indent(); 958 generate("this.literalName = literalName;"); unindent(); 960 generate("}"); newLine(); 962 963 methodJavaDoc ( 965 objEnumeration, false, DOC_ENUM_REFTYPENAME, DOC_ENUM_REFTYPENAME_RETURN, 966 null, null, null 967 ); 968 generate("public java.util.List refTypeName() {"); indent(); 971 generate("return typeName;"); unindent(); 973 generate("}"); newLine(); 975 976 methodJavaDoc ( 978 objEnumeration, false, DOC_ENUM_TOSTRING, DOC_ENUM_TOSTRING_RETURN, 979 null, null, null 980 ); 981 generate("public java.lang.String toString() {"); indent(); 983 generate("return literalName;"); unindent(); 985 generate("}"); newLine(); 987 988 methodJavaDoc ( 990 objEnumeration, false, DOC_ENUM_HASHCODE, DOC_ENUM_HASHCODE_RETURN, 991 null, null, null 992 ); 993 generate("public int hashCode() {"); indent(); 995 generate("return literalName.hashCode();"); unindent(); 997 generate("}"); newLine(); 999 1000 methodJavaDoc ( 1002 objEnumeration, false, DOC_ENUM_EQUALS, DOC_ENUM_EQUALS_RETURN, 1003 new String [] {"o"}, new Object [] {DOC_ENUM_EQUALS_PARAM}, null ); 1005 generate("public boolean equals(java.lang.Object o) {"); indent(); 1007 generate("if (o instanceof " + clsName + ") return (o == this);"); generate("else if (o instanceof " + ifcName + ") return (o.toString().equals(literalName));"); generate("else return ((o instanceof javax.jmi.reflect.RefEnum) && ((javax.jmi.reflect.RefEnum) o).refTypeName().equals(typeName) && o.toString().equals(literalName));"); unindent(); 1011 generate("}"); newLine(); 1013 1014 methodJavaDoc ( 1016 objEnumeration, false, DOC_ENUM_FORNAME, DOC_ENUM_FORNAME_RETURN, 1017 new String [] {"name"}, new Object [] {DOC_ENUM_FORNAME_PARAM}, null ); 1019 generate("public static " + ifcName + " forName(java.lang.String name) {"); indent(); 1021 for (Iterator it = literals.iterator(); it.hasNext();) { 1022 literal = (String ) it.next(); 1023 generate("if (name.equals(\"" + literal + "\")) return " + tagProvider.mapEnumLiteral(literal) + ";"); } 1025 generate("throw new java.lang.IllegalArgumentException(\"Unknown literal name '\" + name + \"' for enumeration '" + enumFQN + "'\");"); unindent(); 1027 generate("}"); 1029 methodJavaDoc ( 1031 objEnumeration, false, DOC_ENUM_READRESOLVE, DOC_ENUM_READRESOLVE_RETURN, 1032 null, null, null 1033 ); 1034 generate("protected java.lang.Object readResolve() throws java.io.ObjectStreamException {"); indent(); 1036 generate("try {"); indent(); 1038 generate("return forName(literalName);"); unindent(); 1040 generate("} catch (java.lang.IllegalArgumentException e) {"); indent(); 1042 generate("throw new java.io.InvalidObjectException(e.getMessage());"); unindent(); 1044 generate("}"); unindent(); 1046 generate("}"); 1048 classFooter(ifcName, false); 1049 } 1050 1051 protected void structureTemplate(StructureType objStructure) throws IOException { 1052 String memberName, fieldName; 1053 1054 interfaceHeader(tagProvider.getTypePrefix(objStructure), tagProvider.getSubstName(objStructure), new Object [] {"javax.jmi.reflect.RefStruct"}, objStructure, DOC_STRUCT); 1057 List fields = objStructure.getContents(); 1058 ModelElement field; 1059 Classifier fieldType; 1060 1061 for (Iterator it = fields.iterator(); it.hasNext();) { 1063 field = (ModelElement) it.next(); 1064 if (field instanceof StructureField) { 1065 fieldType = ((StructureField) field).getType(); 1066 memberName = firstUpper (tagProvider.getSubstName(field)); 1067 fieldName = memberName; 1068 if (fieldType instanceof PrimitiveType && fieldType.getName().equals("Boolean")) { if (memberName.indexOf("Is") != 0) { memberName = "is" + memberName; } else { 1072 memberName = firstLower(memberName); 1073 } 1074 } else { 1075 memberName = "get" + memberName; } 1077 methodJavaDoc ( 1078 objStructure, false, DOC_STRUCT_GETTER, DOC_STRUCT_GETTER_RETURN, 1079 null, null, new String [] {fieldName} 1080 ); 1081 interfaceMethod(getPrimitiveName(getTypeName(fieldType)), memberName, null, null); 1082 } 1083 } 1084 interfaceFooter(tagProvider.getSubstName(objStructure)); 1085 } 1086 1087 void structTemplate(Object objStructure) throws IOException { 1089 DataType dataType = (DataType) objStructure; 1090 ArrayList parameters = new ArrayList(); 1091 ArrayList structureFields = new ArrayList (); 1092 ArrayList paramNames = new ArrayList (); 1093 List fields = dataType.getContents(); 1094 ModelElement field; 1095 1096 for (Iterator it = fields.iterator(); it.hasNext();) { 1098 field = (ModelElement) it.next(); 1099 if (field instanceof StructureField) { 1100 parameters.add(getTypeName2((StructureField) field)); 1101 String name = tagProvider.getSubstName(field); 1102 parameters.add(name); 1103 paramNames.add(name); 1104 structureFields.add(field); 1105 } 1106 } 1107 1108 String params[] = new String [parameters.size ()]; 1109 params = (String []) parameters.toArray(params); 1110 1111 int size = structureFields.size(); 1112 Object [] structFields = new Object [size]; 1113 String [] parNames = new String [size]; 1114 structFields = structureFields.toArray(structFields); 1115 parNames = (String []) paramNames.toArray(parNames); 1116 1117 methodJavaDoc( 1119 dataType, false, DOC_STRUCT_CREATE, DOC_STRUCT_CREATE_RETURN, 1120 parNames, structFields, null 1121 ); 1122 interfaceMethod(tagProvider.getTypeFullName(dataType), "create" + firstUpper(tagProvider.getSubstName(dataType)), params, null); } 1124 1125 1127 private void classJavaDoc (ModelElement elem, String genericComment) throws IOException { 1128 generate ("/**"); if (genericComment != null) { 1130 javaDoc (MessageFormat.format (genericComment, new String [] {elem.getName ()})); 1131 } 1132 javaDoc (elem.getAnnotation ()); 1133 javaDoc (" "); javaDoc (DOC_WARNING); 1135 generate (" */"); } 1137 1138 private void fieldJavaDoc (ModelElement elem) throws IOException { 1139 generate ("/**"); javaDoc (elem.getAnnotation ()); 1141 generate (" */"); } 1143 1144 private void methodJavaDoc (ModelElement elem, boolean annotate, String genericComment, 1145 Object retValDescriptor, String [] args, Object [] argDescriptors, String [] variables) 1146 throws IOException { 1147 1148 String text; 1149 String [] vars = (variables == null) ? new String [1] : new String [variables.length + 1]; 1150 vars [0] = elem.getName (); 1151 if (variables != null) { 1152 for (int x = 0; x < variables.length; x++) 1153 vars [x + 1] = variables [x]; 1154 } 1155 1156 generate ("/**"); 1158 if (genericComment != null) { 1160 javaDoc (MessageFormat.format (genericComment, vars)); 1161 } 1162 1163 if (annotate) { 1165 javaDoc (elem.getAnnotation ()); 1166 } 1167 1168 if (args != null) { 1170 for (int x = 0; x < args.length; x++) { 1171 if (argDescriptors [x] instanceof ModelElement) { 1172 text = ((ModelElement) argDescriptors [x]).getAnnotation (); 1173 } else { 1174 text = (String ) argDescriptors [x]; 1175 if (text != null) { 1176 text = MessageFormat.format (text, vars); 1177 } 1178 } 1179 if (text != null) { 1180 javaDoc ("@param " + args [x] + " " + text); } 1182 } } 1185 if (retValDescriptor instanceof ModelElement) { 1187 text = elem.getAnnotation (); 1188 } else { 1189 text = (String ) retValDescriptor; 1190 if (text != null) 1191 text = MessageFormat.format (text, vars); 1192 } 1193 if (text != null) { 1194 javaDoc ("@return " + text); } 1196 1197 1199 generate (" */"); } 1201 1202 private void javaDoc (String text) throws IOException { 1203 if (text == null) 1204 return; 1205 final int MAX_LINE_LENGTH = 70; 1206 1207 BufferedReader reader = new BufferedReader (new StringReader (text)); 1208 String line = reader.readLine (); 1209 while (line != null) { 1210 int length = line.length (); 1211 int pos = 0, pos2; 1212 String s; 1213 while (pos < length) { 1214 if (length - pos <= MAX_LINE_LENGTH) { 1215 s = line.substring (pos); 1216 pos = length; 1217 } else { 1218 pos2 = pos + MAX_LINE_LENGTH; 1219 while ((pos2 < length) && (!Character.isWhitespace (text.charAt (pos2)))) { 1221 pos2++; 1222 } while ((pos2 < length) && (Character.isWhitespace (text.charAt (pos2)))) { 1225 pos2++; 1226 } s = line.substring (pos, pos2); 1228 pos = pos2; 1229 } 1230 generate (" * " + s); } line = reader.readLine (); 1233 } } 1235 1236 1238 1244 public JavaMapper(JMIStreamFactory sf, String header) { 1245 super(); 1246 generator = sf; 1247 if ((header != null) && (header.trim().length() > 0)) { 1248 this.header = "/*\n"; StringTokenizer tokenizer = new StringTokenizer (header, "\n"); while (tokenizer.hasMoreTokens ()) 1251 this.header += " * " + tokenizer.nextToken () + "\n"; this.header += " */"; } 1254 } 1255} 1256 | Popular Tags |