1 19 20 package org.netbeans.modules.java.ui.nodes.elements; 21 22 import java.text.*; 23 import java.util.*; 24 import java.io.*; 25 import java.lang.reflect.Modifier ; 26 27 import org.openide.util.NbBundle; 28 import org.openide.src.ElementProperties; 29 import org.netbeans.jmi.javamodel.*; 30 import org.netbeans.modules.java.ui.nodes.editors.IdentifierArrayEditor; 31 import org.netbeans.modules.java.ui.nodes.editors.MethodParameterArrayEditor; 32 import org.netbeans.modules.java.ui.nodes.editors.TypeParameterArrayEditor; 33 34 import javax.jmi.reflect.JmiException; 35 import org.netbeans.modules.javacore.api.JavaModel; 36 37 138 public final class ElementFormat extends Format { 139 140 142 143 static final long serialVersionUID = 3775521938640169753L; 144 145 148 private static final String PROPERTIES_NAMES_INDEX = "mnfCtrscpaieMT"; 150 153 private static final String [] PROPERTIES_NAMES = { 154 ElementProperties.PROP_MODIFIERS, ElementProperties.PROP_NAME, ElementProperties.PROP_NAME, ElementProperties.PROP_NAME, ElementProperties.PROP_TYPE, ElementProperties.PROP_RETURN, ElementProperties.PROP_SUPERCLASS, ElementProperties.PROP_STATIC, ElementProperties.PROP_PARAMETERS, ElementProperties.PROP_PARAMETERS, ElementProperties.PROP_INTERFACES, ElementProperties.PROP_EXCEPTIONS, ElementProperties.PROP_MODIFIERS, ElementProperties2.PROP_TYPE_PARAMETERS }; 169 170 171 private static final byte STATUS_OUTSIDE = 0; 172 private static final byte STATUS_INSIDE = 1; 173 private static final byte STATUS_RBRACE = 2; 174 175 177 178 private String pattern; 179 180 private boolean source; 182 183 186 private transient LinkedList list; 187 188 190 194 public ElementFormat(String pattern) { 195 applyPattern(pattern); 196 source = true; 197 } 198 199 206 public void setSourceFormat(boolean source) { 207 this.source = source; 208 } 209 210 215 public boolean isSourceFormat() { 216 return source; 217 } 218 219 222 public String getPattern() { 223 return pattern; 224 } 225 226 233 public StringBuffer format(Object o, StringBuffer toAppendTo, FieldPosition pos) { 234 try { 235 JavaModel.getJavaRepository().beginTrans(false); 236 try { 237 Element element = (Element) o; 238 JavaModel.setClassPath(element.getResource()); 239 Iterator it = list.iterator(); 240 while (it.hasNext()) { 241 Object obj = it.next(); 242 if (obj instanceof String ) { 243 toAppendTo.append((String )obj); 244 } 245 else { 246 ((Tag)obj).format(element, toAppendTo); 247 } 248 } 249 return toAppendTo; 250 } finally { 251 JavaModel.getJavaRepository().endTrans(); 252 } 253 } catch (ClassCastException e) { 254 IllegalArgumentException iae = new IllegalArgumentException (NbBundle.getMessage(ElementFormat.class, "MSG_badArgument")); iae.initCause(e); 256 throw iae; 257 } catch (JmiException e) { 258 IllegalArgumentException iae = new IllegalArgumentException ("format failed"); iae.initCause(e); 260 throw iae; 261 } 262 } 263 264 268 public String format(Element element) { 269 return format(element, new StringBuffer (), null).toString(); 270 } 271 272 277 public boolean dependsOnProperty(String prop) { 278 Iterator it = list.iterator(); 279 while (it.hasNext()) { 280 Object obj = it.next(); 281 if (obj instanceof Tag) { 282 int index = PROPERTIES_NAMES_INDEX.indexOf(((Tag)obj).kind); 283 if (PROPERTIES_NAMES[index].equals(prop)) 284 return true; 285 } 286 } 287 return false; 288 } 289 290 295 public Object parseObject (String source, ParsePosition status) { 296 return null; 297 } 298 299 300 private void readObject(ObjectInputStream s) throws ClassNotFoundException , IOException { 301 s.defaultReadObject(); 302 applyPattern(pattern); 303 } 304 305 307 308 private void applyPattern(String pattern) { 309 this.pattern = pattern; 310 list = new LinkedList(); 311 312 byte status = STATUS_OUTSIDE; 313 StringTokenizer tokenizer = new StringTokenizer(pattern, "{}", true); while (tokenizer.hasMoreTokens()) { 315 String token = tokenizer.nextToken(); 316 switch (status) { 317 318 case STATUS_OUTSIDE: 319 if (token.equals("}")) throw new IllegalArgumentException (NbBundle.getMessage(ElementFormat.class, "MSG_badPattern")); if (token.equals("{")) status = STATUS_INSIDE; 323 else 324 list.add(token); 325 break; 326 327 case STATUS_INSIDE: 328 if ((token.equals("{")) || (token.equals("}"))) throw new IllegalArgumentException (NbBundle.getMessage(ElementFormat.class, "MSG_badPattern")); list.add(createTag(token)); 331 status = STATUS_RBRACE; 332 break; 333 334 case STATUS_RBRACE: 335 if (!token.equals("}")) throw new IllegalArgumentException (NbBundle.getMessage(ElementFormat.class, "MSG_badPattern")); status = STATUS_OUTSIDE; 338 break; 339 340 } 341 } 342 } 343 344 348 private Tag createTag(String s) { 349 if (s.length() > 0) { 350 char c = s.charAt(0); 351 String [] params = new String [0]; 352 353 if (s.length() > 1) { 354 if ((s.length() < 2) || (s.charAt(1) != ',')) 355 throw new IllegalArgumentException (NbBundle.getMessage(ElementFormat.class, "MSG_badPattern")); params = parseParams(s.substring(2)); 357 } 358 359 if ("mnfCtrscM".indexOf(c) != -1) { switch (params.length) { 361 case 0: return new Tag(c, "", ""); case 2: return new Tag(c, params[0], params[1]); 363 } 364 } 365 else if ("paieT".indexOf(c) != -1) { switch (params.length) { 367 case 0: return new ArrayTag(c, "", "", ", "); case 2: return new ArrayTag(c, params[0], params[1], ", "); case 3: return new ArrayTag(c, params[0], params[1], params[2]); 370 } 371 } 372 } 373 throw new IllegalArgumentException (NbBundle.getMessage(ElementFormat.class, "MSG_badPattern")); } 375 376 380 private String [] parseParams(String s) { 381 StringTokenizer tokenizer = new StringTokenizer(s, ",", true); ArrayList list = new ArrayList(); 383 StringBuffer token = new StringBuffer (); 384 boolean comma = false; 385 boolean inString = false; 386 387 while (tokenizer.hasMoreTokens()) { 388 String t = tokenizer.nextToken(); 389 390 if (inString) { 391 token.append(t); 392 if (t.endsWith("\"")) { if (token.length() > 1) 394 token.setLength(token.length() - 1); 395 list.add(token.toString()); 396 token.setLength(0); 397 inString = false; 398 comma = true; 399 } 400 continue; 401 } 402 403 if (t.equals(",")) { if (comma) 405 comma = false; 406 else 407 list.add(""); continue; 409 } 410 411 if (comma) 412 throw new IllegalArgumentException (NbBundle.getMessage(ElementFormat.class, "MSG_badPattern")); 414 String stringToAdd = t; 415 416 if (t.startsWith("\"")) { if ((t.endsWith("\"")) && (t.length() > 1)) { stringToAdd = (t.length() <= 2) ? "" : t.substring(1, t.length() - 1); } 420 else { 421 token.append(t.substring(1)); 422 inString = true; 423 continue; 424 } 425 } 426 427 list.add(stringToAdd); 428 comma = true; 429 token.setLength(0); 430 } 431 if (!comma) 432 list.add(""); 434 String [] ret = new String [list.size()]; 435 list.toArray(ret); 436 return ret; 437 } 438 439 445 public static String elementName(NamedElement el) throws JmiException { 446 if (el instanceof JavaClass) { 447 return ((JavaClass) el).getSimpleName(); 448 } 449 450 if (el instanceof Constructor) { 451 JavaClass jc = (JavaClass) ((Constructor) el).getDeclaringClass(); 452 return jc.getSimpleName(); 453 } 454 455 return el.getName(); 456 } 457 458 private static String elementFullName(NamedElement el) throws JmiException { 459 if (el instanceof JavaClass) { 460 return el.getName(); 461 } 462 463 if (!(el instanceof ClassMember)) { 464 return elementName(el); 465 } 466 467 JavaClass jc = (JavaClass) ((ClassMember) el).getDeclaringClass(); 468 StringBuffer fullName = new StringBuffer (30).append(jc.getName()).append('.'); 469 if (el instanceof Constructor) { 470 fullName.append(jc.getSimpleName()); 471 } else { 472 fullName.append(el.getName()); 473 } 474 return fullName.toString(); 475 } 476 477 private static void resolveClassName(JavaClass jc, StringBuffer sb) { 478 ClassDefinition cd = jc.getDeclaringClass(); 479 if (!(cd instanceof JavaClass)) { 480 sb.append(jc.getSimpleName()); 481 return; 482 } 483 resolveClassName((JavaClass) cd, sb); 484 sb.append('.').append(jc.getSimpleName()); 485 } 486 487 public String toString() { 488 return this.pattern; 489 } 490 491 492 private static class Tag extends Object implements Serializable { 493 494 char kind; 495 496 497 String prefix; 498 499 500 String suffix; 501 502 static final long serialVersionUID =4946774706959011193L; 503 504 Tag(char kind, String prefix, String suffix) { 505 this.kind = kind; 506 this.prefix = prefix; 507 this.suffix = suffix; 508 } 509 510 514 void format(Element element, StringBuffer buf) { 515 try { 516 int mark = buf.length(); 517 buf.append(prefix); 518 519 switch (kind) { 520 case 'm': 521 buf.append(Modifier.toString(((ClassMember) element).getModifiers() & ~Modifier.INTERFACE)); 522 break; 523 524 case 'n': 525 buf.append(elementName((NamedElement) element)); 526 break; 527 528 case 'f': 529 buf.append(elementFullName((NamedElement) element)); 530 break; 531 532 case 'C': 533 resolveClassName((JavaClass) element, buf); 534 break; 535 536 case 't': 537 buf.append(((TypedElement) element).getType().getName()); 538 break; 539 540 case 'r': 541 buf.append(((TypedElement) element).getType().getName()); 542 break; 543 544 case 's': 545 JavaClass superClass = ((JavaClass) element).getSuperClass(); 546 if (superClass != null) { 547 buf.append(elementFullName(superClass)); 548 } 549 break; 550 551 case 'c': 552 int modifiers = ((Initializer) element).getModifiers(); 553 if (Modifier.isStatic(modifiers)) 554 buf.append(Modifier.toString(Modifier.STATIC)); 555 break; 556 557 case 'M': 558 buf.append(Modifier.isInterface((((ClassMember) element).getModifiers()))? "interface": "class"); break; 560 } 561 562 if (buf.length() > mark + prefix.length()) { 563 buf.append(suffix); 564 } 565 else { 566 buf.setLength(mark); 567 } 568 } 569 catch (ClassCastException e) { 570 throw new IllegalArgumentException (NbBundle.getMessage(ElementFormat.class, "MSG_badPattern")); } 572 } 573 } 574 575 577 private static final class ArrayTag extends Tag { 578 579 String delim; 580 581 static final long serialVersionUID =2060398944304753010L; 582 583 ArrayTag(char kind, String prefix, String suffix, String delim) { 584 super(kind, prefix, suffix); 585 this.delim = delim; 586 } 587 588 private void identifiers2String(List l, StringBuffer buf) throws JmiException { 589 Iterator it = l.iterator(); 590 boolean toDelimit = false; 591 while (it.hasNext()) { 592 if (toDelimit) { 593 buf.append(delim); 594 } else { 595 toDelimit = true; 596 } 597 MultipartId id = (MultipartId) it.next(); 598 buf.append(IdentifierArrayEditor.multipartIdToName(id)); 599 } 600 } 601 602 606 void format(Element element, StringBuffer buf) { 607 try { 608 int mark = buf.length(); 609 buf.append(prefix); 610 611 switch (kind) { 612 case 'e': 613 identifiers2String(((CallableFeature) element).getExceptionNames(), buf); 614 break; 615 616 case 'p': 617 case 'a': 618 if (element instanceof EnumConstant) { 619 constantParams2String((EnumConstant) element, buf); 620 break; 621 } 622 List l = ((CallableFeature) element).getParameters(); 623 buf.append( 624 MethodParameterArrayEditor.params2String( 625 (Parameter[]) l.toArray(new Parameter[0]), delim, kind == 'p') 626 ); 627 break; 628 629 case 'i': 630 identifiers2String(((JavaClass) element).getInterfaceNames(), buf); 631 break; 632 case 'T': 633 typeParams2String(((GenericElement) element), buf); 634 break; 635 } 636 637 if (buf.length() > mark + prefix.length()) { 638 buf.append(suffix); 639 } 640 else { 641 buf.setLength(mark); 642 } 643 } catch (ClassCastException e) { 644 throw new IllegalArgumentException (NbBundle.getMessage(ElementFormat.class, "MSG_badPattern")); } 646 } 647 648 private static void constantParams2String(EnumConstant ec, StringBuffer sb) throws JmiException { 649 String value = ec.getInitialValueText(); 650 sb.append(value != null? value: ""); } 652 653 private void typeParams2String(GenericElement ge, StringBuffer sb) throws JmiException { 654 TypeParameter[] tps = (TypeParameter[]) ge.getTypeParameters().toArray(new TypeParameter[0]); 655 String s = TypeParameterArrayEditor.params2String(tps, delim); 656 sb.append(s); 657 } 658 } 659 } 660 | Popular Tags |