1 23 package com.sun.enterprise.deployment; 24 25 import java.util.*; 26 import java.io.*; 27 import java.io.Serializable ; 28 import com.sun.enterprise.util.NotificationListener; 29 import com.sun.enterprise.util.NotificationEvent; 30 import com.sun.enterprise.deployment.util.DescriptorVisitor; 31 32 40 41 public class Descriptor extends DynamicAttributesDescriptor implements Serializable { 42 43 44 public static String DESCRIPTOR_CHANGED = "Descriptor change"; 45 public static String NAME_CHANGED = "NameChanged"; 46 public static String DESCRIPTION_CHANGED = "DescriptionChanged"; 47 public static String LARGE_ICON_CHANGED = "LargeIconChanged"; 48 public static String SMALL_ICON_CHANGED = "SmallIconChanged"; 49 50 51 private static boolean boundsChecking = true; 52 53 54 protected Vector listeners = new Vector(); 55 56 57 private Map displayNames = null; 58 59 60 private Map descriptions = null; 61 62 63 private Map largeIcons = null; 64 private Map smallIcons = null; 65 66 67 72 public Descriptor() { 73 this.listeners = new Vector(); 74 } 75 76 79 protected Descriptor(Descriptor other) { 80 if (other.displayNames!=null) 81 this.displayNames = new HashMap(other.displayNames); 82 if (other.descriptions!=null) 83 this.descriptions = new HashMap(other.descriptions); 84 if (other.largeIcons!=null) 85 this.largeIcons = new HashMap(other.largeIcons); 86 if (other.smallIcons!=null) 87 this.smallIcons = new HashMap(other.smallIcons); 88 } 89 90 98 public Descriptor(String name, String description) { 99 this(); 100 setLocalizedDisplayName(null, name); 101 setLocalizedDescription(null, description); 102 } 103 104 111 public static void setBoundsChecking(boolean b) { 112 boundsChecking = b; 113 } 114 115 121 public static boolean isBoundsChecking() { 122 return boundsChecking; 123 } 124 125 131 public void setName(String name) { 132 setLocalizedDisplayName(null, name); 133 } 134 135 141 public String getName() { 142 return getLocalizedDisplayName(null); 143 } 144 145 150 public void setLocalizedDisplayName(String lang, String displayName) { 151 152 if (lang==null) { 153 lang = Locale.getDefault().getLanguage(); 154 } 155 if (displayNames==null) { 156 displayNames = new HashMap(); 157 } 158 displayNames.put(lang, displayName); 159 changed(NAME_CHANGED); 160 } 161 162 166 public String getLocalizedDisplayName(String language) { 167 168 if (displayNames==null) { 169 return ""; 170 } 171 172 String originalLanguage = language; 173 if (language==null) { 174 language=Locale.getDefault().getLanguage(); 175 } 176 177 String localizedName = (String ) displayNames.get(language); 178 if (localizedName!=null) { 179 return localizedName; 180 } 181 182 if (originalLanguage==null && displayNames.size()>0) { 188 return (String ) displayNames.values().iterator().next(); 189 } 190 return ""; 192 } 193 194 197 public Map getLocalizedDisplayNames() { 198 return displayNames; 199 } 200 201 204 public void setDisplayName(String name) { 205 setName(name); 206 } 207 208 211 public String getDisplayName() { 212 return getName(); 213 } 214 215 221 public void setDescription(String description) { 222 setLocalizedDescription(null, description); 223 } 224 225 231 public String getDescription() { 232 return getLocalizedDescription(null); 233 } 234 235 240 public void setLocalizedDescription(String lang, String description) { 241 if (lang==null) { 242 lang = Locale.getDefault().getLanguage(); 243 } 244 if (descriptions==null) { 245 descriptions = new HashMap(); 246 } 247 descriptions.put(lang, description); 248 changed(DESCRIPTION_CHANGED); 249 } 250 251 255 public String getLocalizedDescription(String lang) { 256 257 if (descriptions==null) 258 return ""; 259 if (lang==null) { 260 lang = Locale.getDefault().getLanguage(); 261 } 262 String description = (String ) descriptions.get(lang); 263 if (description==null) { 264 return ""; 265 } 266 return description; 267 } 268 269 272 public Map getLocalizedDescriptions() { 273 return descriptions; 274 } 275 276 281 public void setLocalizedLargeIconUri(String lang, String uri) { 282 if (lang==null) { 283 lang = Locale.getDefault().getLanguage(); 284 } 285 if (largeIcons==null) { 286 largeIcons = new HashMap(); 287 } 288 largeIcons.put(lang, uri); 289 changed(LARGE_ICON_CHANGED); 290 } 291 292 296 public String getLocalizedLargeIconUri(String lang) { 297 if (largeIcons==null) { 298 return null; 299 } 300 if (lang==null) { 301 lang = Locale.getDefault().getLanguage(); 302 } 303 return (String ) largeIcons.get(lang); 304 } 305 306 310 public Map getLocalizedLargeIconUris() { 311 return largeIcons; 312 } 313 314 319 public void setLocalizedSmallIconUri(String lang, String uri) { 320 if (lang==null) { 321 lang = Locale.getDefault().getLanguage(); 322 } 323 if (smallIcons==null) { 324 smallIcons = new HashMap(); 325 } 326 smallIcons.put(lang, uri); 327 changed(LARGE_ICON_CHANGED); 328 } 329 330 333 public String getLocalizedSmallIconUri(String lang) { 334 if (smallIcons==null) { 335 return null; 336 } 337 if (lang==null) { 338 lang = Locale.getDefault().getLanguage(); 339 } 340 return (String ) smallIcons.get(lang); 341 } 342 343 346 public Map getLocalizedSmallIconUris() { 347 return smallIcons; 348 } 349 350 356 public String getLargeIconUri() { 357 return getLocalizedLargeIconUri(null); 358 } 359 360 366 public void setLargeIconUri(String largeIconUri) { 367 setLocalizedLargeIconUri(null, largeIconUri); 368 } 369 370 376 public String getSmallIconUri() { 377 return getLocalizedSmallIconUri(null); 378 } 379 380 386 public void setSmallIconUri(String smallIconUri) { 387 setLocalizedSmallIconUri(null, smallIconUri); 388 } 389 390 396 public void addNotificationListener(NotificationListener nl) { 397 if ((nl != null) && !listeners.contains(nl)) { listeners.addElement(nl); 399 } 400 } 401 402 408 public void removeNotificationListener(NotificationListener nl) { 409 listeners.removeElement(nl); 410 } 411 412 416 public List getNotificationListeners() { 417 Vector listenersClone = null; 418 synchronized (listeners) { 419 listenersClone = (Vector)this.listeners.clone(); 420 } 421 return listenersClone; 422 } 423 424 428 public void changed(String attribute) { 429 this.changed(); 432 } 433 434 438 public void changed() { 439 String attribute = ""; 440 NotificationEvent ne = new NotificationEvent(this, DESCRIPTOR_CHANGED, this, attribute); 441 442 List listenersClone = this.getNotificationListeners(); 443 for (Iterator e = listenersClone.iterator(); e.hasNext();) { 444 NotificationListener nl = (NotificationListener)e.next(); 445 nl.notification(ne); 446 } 447 } 448 449 454 private static String stripIntegerEndingFrom(String s) { 455 return recursiveStripIntegerEndingFrom(s); 456 } 457 458 463 private static String recursiveStripIntegerEndingFrom(String s) { 464 if (s.length() > 1) { 465 String shorterByOne = s.substring(0, s.length() - 1); 466 467 String lastBit = s.substring(s.length() - 1, s.length()); 468 try { 469 Integer.parseInt(lastBit); 470 return recursiveStripIntegerEndingFrom(shorterByOne); 471 } catch (NumberFormatException nfe) { 472 return s; 473 } 474 475 } 476 return s; 477 } 478 479 484 private static String uniquifyString(String trialName, Vector v, int index) { 485 for(Enumeration e = v.elements(); e.hasMoreElements();) { 486 String next = (String ) e.nextElement(); 487 if (next.equals(trialName)) { 488 index++; 489 return uniquifyString(stripIntegerEndingFrom(trialName) + index, v, index); 490 } 491 } 492 return trialName; 493 } 494 495 503 public static String createUniqueFilenameAmongst(String trialName, Vector otherNames) { 504 505 506 int p = trialName.lastIndexOf("."); 507 if (p < 0) { 508 return uniquifyString(trialName, otherNames, 0); 509 } 510 String ext = trialName.substring(p); 511 String file = trialName.substring(0, p); 512 513 514 Vector nameList = new Vector(); 515 for (Enumeration e = otherNames.elements(); e.hasMoreElements();) { 516 String name = e.nextElement().toString(); 517 if (name.endsWith(ext)) { 518 nameList.add(name.substring(0, name.length() - ext.length())); 519 } 520 } 521 String unique = uniquifyString(file, nameList, 0); 522 return unique + ext; 523 524 } 525 526 534 public static String createUniqueNameAmongst(String trialName, Vector otherNames) { 535 return uniquifyString(trialName, otherNames, 0); 536 } 537 538 546 public static String createUniqueNameAmongstNamedDescriptors(String trialName, Set descriptors) { 547 Vector v = new Vector(); 548 for (Iterator itr = descriptors.iterator(); itr.hasNext();) { 549 Descriptor next = (Descriptor) itr.next(); 550 v.addElement(next.getName()); 551 } 552 return createUniqueNameAmongst(trialName, v); 553 } 554 555 559 public void addDeploymentExtension(DeploymentExtensionDescriptor de) { 560 Vector extensions = (Vector) getExtraAttribute("deployment-extension"); 561 if (extensions==null) { 562 extensions = new Vector(); 563 addExtraAttribute("deployment-extension", extensions); 564 } 565 extensions.add(de); 566 } 567 568 571 public Iterator getDeploymentExtensions() { 572 Vector extensions = (Vector) getExtraAttribute("deployment-extension"); 573 if (extensions!=null) { 574 return extensions.iterator(); 575 } 576 return null; 577 } 578 579 582 public void addPrefixMapping(String mapping, String uri) { 583 Map prefixMapping = getPrefixMapping(); 584 if (prefixMapping==null) { 585 prefixMapping = new java.util.HashMap (); 586 addExtraAttribute("prefix-mapping", prefixMapping); 587 } 588 prefixMapping.put(mapping, uri); 589 } 590 591 594 public Map getPrefixMapping() { 595 return (Map) getExtraAttribute("prefix-mapping"); 596 } 597 598 601 public void print(StringBuffer toStringBuffer) { 602 StringBuffer sb = toStringBuffer; 603 604 if (displayNames!=null) { 605 sb.append("Display Names:"); 606 displayLocalizedMap(sb, displayNames); 607 } 608 if (descriptions!=null) { 609 sb.append("\n Descriptions"); 610 displayLocalizedMap(sb, descriptions); 611 } 612 if (smallIcons!=null) { 613 sb.append("\n SmallIcons"); 614 displayLocalizedMap(sb, smallIcons); 615 } 616 if (largeIcons!=null) { 617 sb.append("\n LargeIcons"); 618 displayLocalizedMap(sb, largeIcons); 619 } 620 Map prefix = getPrefixMapping(); 621 if (prefix!=null) 622 sb.append("\n Prefix Mapping = ").append(prefix); 623 Iterator itr = getDeploymentExtensions(); 624 if (itr!=null && itr.hasNext()) { 625 do { 626 sb.append("\n Deployment Extension : "); 627 ((Descriptor)(itr.next())).print(sb); 628 } while (itr.hasNext()); 629 } 630 sb.append("\n"); 631 super.print(sb); 632 } 633 634 637 private void displayLocalizedMap(StringBuffer sb, Map localizedMap) { 638 for (Iterator itr = localizedMap.keySet().iterator();itr.hasNext();) { 639 String lang = (String ) itr.next(); 640 sb.append("\n lang[" + lang + "] = " + localizedMap.get(lang)); 641 } 642 } 643 644 645 650 public void visit(DescriptorVisitor aVisitor) { 651 aVisitor.accept(this); 652 } 653 654 657 private String getDefaultLanguage() { 658 return Locale.getDefault().getLanguage(); 659 } 660 661 public String docType = null; 663 664 public String getDocType(){ 665 return docType; 666 } 667 668 public void fillDocType(InputStream in){ 669 try{ 670 BufferedReader inr = new BufferedReader(new InputStreamReader(in)); 671 String s = inr.readLine(); 672 while (s != null) { 673 if (s.indexOf("DOCTYPE") > -1) { 674 docType = s; 675 in.close(); 676 return; 677 } 678 s = inr.readLine(); 679 } 680 }catch(Exception e){ 681 } 682 } 683 } 685 | Popular Tags |