1 11 package org.eclipse.debug.internal.core; 12 13 14 import java.io.IOException ; 15 import java.util.ArrayList ; 16 import java.util.Comparator ; 17 import java.util.HashMap ; 18 import java.util.HashSet ; 19 import java.util.Iterator ; 20 import java.util.List ; 21 import java.util.Map ; 22 import java.util.Set ; 23 import java.util.TreeMap ; 24 25 import javax.xml.parsers.ParserConfigurationException ; 26 import javax.xml.transform.TransformerException ; 27 28 import org.eclipse.core.runtime.CoreException; 29 import org.eclipse.core.runtime.IStatus; 30 import org.eclipse.core.runtime.Status; 31 import org.eclipse.debug.core.DebugException; 32 import org.eclipse.debug.core.DebugPlugin; 33 import org.eclipse.debug.core.ILaunchConfigurationType; 34 import org.w3c.dom.Document ; 35 import org.w3c.dom.Element ; 36 import org.w3c.dom.Node ; 37 import org.w3c.dom.NodeList ; 38 39 import com.ibm.icu.text.MessageFormat; 40 41 44 public class LaunchConfigurationInfo { 45 46 49 private static final String KEY = "key"; private static final String VALUE = "value"; private static final String SET_ENTRY = "setEntry"; private static final String LAUNCH_CONFIGURATION = "launchConfiguration"; private static final String MAP_ENTRY = "mapEntry"; private static final String LIST_ENTRY = "listEntry"; private static final String SET_ATTRIBUTE = "setAttribute"; private static final String MAP_ATTRIBUTE = "mapAttribute"; private static final String LIST_ATTRIBUTE = "listAttribute"; private static final String BOOLEAN_ATTRIBUTE = "booleanAttribute"; private static final String INT_ATTRIBUTE = "intAttribute"; private static final String STRING_ATTRIBUTE = "stringAttribute"; private static final String TYPE = "type"; 63 68 private TreeMap fAttributes; 69 70 73 private ILaunchConfigurationType fType; 74 75 78 private static boolean fgIsSun14x = false; 79 80 static { 81 String vendor = System.getProperty("java.vm.vendor"); if (vendor.startsWith("Sun Microsystems")) { String version = System.getProperty("java.vm.version"); if (version.startsWith("1.4")) { fgIsSun14x = true; 86 } 87 } 88 } 89 90 93 protected LaunchConfigurationInfo() { 94 setAttributeTable(new TreeMap ()); 95 } 96 97 102 private TreeMap getAttributeTable() { 103 return fAttributes; 104 } 105 106 112 private void setAttributeTable(TreeMap table) { 113 fAttributes = table; 114 } 115 116 121 protected void setAttributes(Map map) { 122 if (map == null) { 123 setAttributeTable(new TreeMap ()); 124 return; 125 } 126 setAttributeTable(new TreeMap (map)); 127 } 128 129 138 protected String getStringAttribute(String key, String defaultValue) throws CoreException { 139 Object attr = getAttributeTable().get(key); 140 if (attr != null) { 141 if (attr instanceof String ) { 142 return (String )attr; 143 } 144 throw new DebugException( 145 new Status( 146 IStatus.ERROR, DebugPlugin.getUniqueIdentifier(), 147 DebugException.REQUEST_FAILED, MessageFormat.format(DebugCoreMessages.LaunchConfigurationInfo_Attribute__0__is_not_of_type_java_lang_String__1, new String [] {key}), null 148 ) 149 ); 150 } 151 return defaultValue; 152 } 153 154 163 protected int getIntAttribute(String key, int defaultValue) throws CoreException { 164 Object attr = getAttributeTable().get(key); 165 if (attr != null) { 166 if (attr instanceof Integer ) { 167 return ((Integer )attr).intValue(); 168 } 169 throw new DebugException( 170 new Status( 171 IStatus.ERROR, DebugPlugin.getUniqueIdentifier(), 172 DebugException.REQUEST_FAILED, MessageFormat.format(DebugCoreMessages.LaunchConfigurationInfo_Attribute__0__is_not_of_type_int__2, new String [] {key}), null 173 ) 174 ); 175 } 176 return defaultValue; 177 } 178 179 188 protected boolean getBooleanAttribute(String key, boolean defaultValue) throws CoreException { 189 Object attr = getAttributeTable().get(key); 190 if (attr != null) { 191 if (attr instanceof Boolean ) { 192 return ((Boolean )attr).booleanValue(); 193 } 194 throw new DebugException( 195 new Status( 196 IStatus.ERROR, DebugPlugin.getUniqueIdentifier(), 197 DebugException.REQUEST_FAILED, MessageFormat.format(DebugCoreMessages.LaunchConfigurationInfo_Attribute__0__is_not_of_type_boolean__3, new String [] {key}), null 198 ) 199 ); 200 } 201 return defaultValue; 202 } 203 204 213 protected List getListAttribute(String key, List defaultValue) throws CoreException { 214 Object attr = getAttributeTable().get(key); 215 if (attr != null) { 216 if (attr instanceof List ) { 217 return (List )attr; 218 } 219 throw new DebugException( 220 new Status( 221 IStatus.ERROR, DebugPlugin.getUniqueIdentifier(), 222 DebugException.REQUEST_FAILED, MessageFormat.format(DebugCoreMessages.LaunchConfigurationInfo_Attribute__0__is_not_of_type_java_util_List__1, new String [] {key}), null 223 ) 224 ); 225 } 226 return defaultValue; 227 } 228 229 240 protected Set getSetAttribute(String key, Set defaultValue) throws CoreException { 241 Object attr = getAttributeTable().get(key); 242 if (attr != null) { 243 if (attr instanceof Set ) { 244 return (Set )attr; 245 } 246 throw new DebugException( 247 new Status( 248 IStatus.ERROR, DebugPlugin.getUniqueIdentifier(), 249 DebugException.REQUEST_FAILED, MessageFormat.format(DebugCoreMessages.LaunchConfigurationInfo_35, new String [] {key}), null 250 ) 251 ); 252 } 253 return defaultValue; 254 } 255 256 265 protected Map getMapAttribute(String key, Map defaultValue) throws CoreException { 266 Object attr = getAttributeTable().get(key); 267 if (attr != null) { 268 if (attr instanceof Map ) { 269 return (Map )attr; 270 } 271 throw new DebugException( 272 new Status( 273 IStatus.ERROR, DebugPlugin.getUniqueIdentifier(), 274 DebugException.REQUEST_FAILED, MessageFormat.format(DebugCoreMessages.LaunchConfigurationInfo_Attribute__0__is_not_of_type_java_util_Map__1, new String [] {key}), null 275 ) 276 ); 277 } 278 return defaultValue; 279 } 280 281 287 protected void setType(ILaunchConfigurationType type) { 288 fType = type; 289 } 290 291 296 protected ILaunchConfigurationType getType() { 297 return fType; 298 } 299 300 301 306 protected LaunchConfigurationInfo getCopy() { 307 LaunchConfigurationInfo copy = new LaunchConfigurationInfo(); 308 copy.setType(getType()); 309 copy.setAttributeTable(getAttributes()); 310 return copy; 311 } 312 313 318 protected TreeMap getAttributes() { 319 return (TreeMap )getAttributeTable().clone(); 320 } 321 322 331 protected void setAttribute(String key, Object value) { 332 if (value == null) { 333 getAttributeTable().remove(key); 334 } else { 335 getAttributeTable().put(key, value); 336 } 337 } 338 339 352 protected String getAsXML() throws CoreException, IOException , ParserConfigurationException , TransformerException { 353 354 Document doc = LaunchManager.getDocument(); 355 Element configRootElement = doc.createElement(LAUNCH_CONFIGURATION); 356 doc.appendChild(configRootElement); 357 358 configRootElement.setAttribute(TYPE, getType().getIdentifier()); 359 360 Iterator keys = getAttributeTable().keySet().iterator(); 361 while (keys.hasNext()) { 362 String key = (String )keys.next(); 363 if (key == null) { 364 throw new DebugException( 365 new Status( 366 IStatus.ERROR, DebugPlugin.getUniqueIdentifier(), 367 DebugException.REQUEST_FAILED, DebugCoreMessages.LaunchConfigurationInfo_36, null 368 ) 369 ); 370 } 371 Object value = getAttributeTable().get(key); 372 if (value == null) { 373 continue; 374 } 375 Element element = null; 376 String valueString = null; 377 if (value instanceof String ) { 378 valueString = (String )value; 379 element = createKeyValueElement(doc, STRING_ATTRIBUTE, key, valueString); 380 } else if (value instanceof Integer ) { 381 valueString = ((Integer )value).toString(); 382 element = createKeyValueElement(doc, INT_ATTRIBUTE, key, valueString); 383 } else if (value instanceof Boolean ) { 384 valueString = ((Boolean )value).toString(); 385 element = createKeyValueElement(doc, BOOLEAN_ATTRIBUTE, key, valueString); 386 } else if (value instanceof List ) { 387 element = createListElement(doc, LIST_ATTRIBUTE, key, (List )value); 388 } else if (value instanceof Map ) { 389 element = createMapElement(doc, MAP_ATTRIBUTE, key, (Map )value); 390 } else if(value instanceof Set ) { 391 element = createSetElement(doc, SET_ATTRIBUTE, key, (Set )value); 392 } 393 configRootElement.appendChild(element); 394 } 395 396 return LaunchManager.serializeDocument(doc); 397 } 398 399 403 protected Element createKeyValueElement(Document doc, String elementType, String key, String value) { 404 Element element = doc.createElement(elementType); 405 element.setAttribute(KEY, key); 406 element.setAttribute(VALUE, value); 407 return element; 408 } 409 410 420 protected Element createListElement(Document doc, String elementType, String listKey, List list) { 421 Element listElement = doc.createElement(elementType); 422 listElement.setAttribute(KEY, listKey); 423 Iterator iterator = list.iterator(); 424 while (iterator.hasNext()) { 425 String value = (String ) iterator.next(); 426 Element element = doc.createElement(LIST_ENTRY); 427 element.setAttribute(VALUE, value); 428 listElement.appendChild(element); 429 } 430 return listElement; 431 } 432 433 445 protected Element createSetElement(Document doc, String elementType, String setKey, Set set) { 446 Element setElement = doc.createElement(elementType); 447 setElement.setAttribute(KEY, setKey); 448 Element element = null; 449 for(Iterator iter = set.iterator(); iter.hasNext();) { 450 element = doc.createElement(SET_ENTRY); 451 element.setAttribute(VALUE, (String ) iter.next()); 452 setElement.appendChild(element); 453 } 454 return setElement; 455 } 456 457 468 protected Element createMapElement(Document doc, String elementType, String mapKey, Map map) { 469 Element mapElement = doc.createElement(elementType); 470 mapElement.setAttribute(KEY, mapKey); 471 Iterator iterator = map.keySet().iterator(); 472 while (iterator.hasNext()) { 473 String key = (String ) iterator.next(); 474 String value = (String ) map.get(key); 475 Element element = doc.createElement(MAP_ENTRY); 476 element.setAttribute(KEY, key); 477 element.setAttribute(VALUE, value); 478 mapElement.appendChild(element); 479 } 480 return mapElement; 481 } 482 483 488 protected void initializeFromXML(Element root) throws CoreException { 489 if (!root.getNodeName().equalsIgnoreCase(LAUNCH_CONFIGURATION)) { 490 throw getInvalidFormatDebugException(); 491 } 492 493 String id = root.getAttribute(TYPE); 495 if (id == null) { 496 throw getInvalidFormatDebugException(); 497 } 498 499 ILaunchConfigurationType type = DebugPlugin.getDefault().getLaunchManager().getLaunchConfigurationType(id); 500 if (type == null) { 501 String message= MessageFormat.format(DebugCoreMessages.LaunchConfigurationInfo_missing_type, new Object []{id}); 502 throw new DebugException( 503 new Status( 504 IStatus.ERROR, DebugPlugin.getUniqueIdentifier(), 505 DebugException.MISSING_LAUNCH_CONFIGURATION_TYPE, message, null) 506 ); 507 } 508 setType(type); 509 510 NodeList list = root.getChildNodes(); 511 Node node = null; 512 Element element = null; 513 String nodeName = null; 514 for (int i = 0; i < list.getLength(); ++i) { 515 node = list.item(i); 516 short nodeType = node.getNodeType(); 517 if (nodeType == Node.ELEMENT_NODE) { 518 element = (Element ) node; 519 nodeName = element.getNodeName(); 520 if (nodeName.equalsIgnoreCase(STRING_ATTRIBUTE)) { 521 setStringAttribute(element); 522 } else if (nodeName.equalsIgnoreCase(INT_ATTRIBUTE)) { 523 setIntegerAttribute(element); 524 } else if (nodeName.equalsIgnoreCase(BOOLEAN_ATTRIBUTE)) { 525 setBooleanAttribute(element); 526 } else if (nodeName.equalsIgnoreCase(LIST_ATTRIBUTE)) { 527 setListAttribute(element); 528 } else if (nodeName.equalsIgnoreCase(MAP_ATTRIBUTE)) { 529 setMapAttribute(element); 530 } else if(nodeName.equalsIgnoreCase(SET_ATTRIBUTE)) { 531 setSetAttribute(element); 532 } 533 } 534 } 535 } 536 537 542 protected void setStringAttribute(Element element) throws CoreException { 543 setAttribute(getKeyAttribute(element), getValueAttribute(element)); 544 } 545 546 551 protected void setIntegerAttribute(Element element) throws CoreException { 552 setAttribute(getKeyAttribute(element), new Integer (getValueAttribute(element))); 553 } 554 555 560 protected void setBooleanAttribute(Element element) throws CoreException { 561 setAttribute(getKeyAttribute(element), Boolean.valueOf(getValueAttribute(element))); 562 } 563 564 571 protected void setListAttribute(Element element) throws CoreException { 572 String listKey = element.getAttribute(KEY); 573 NodeList nodeList = element.getChildNodes(); 574 int entryCount = nodeList.getLength(); 575 List list = new ArrayList (entryCount); 576 Node node = null; 577 Element selement = null; 578 for (int i = 0; i < entryCount; i++) { 579 node = nodeList.item(i); 580 if (node.getNodeType() == Node.ELEMENT_NODE) { 581 selement = (Element ) node; 582 if (!selement.getNodeName().equalsIgnoreCase(LIST_ENTRY)) { 583 throw getInvalidFormatDebugException(); 584 } 585 list.add(getValueAttribute(selement)); 586 } 587 } 588 setAttribute(listKey, list); 589 } 590 591 600 protected void setSetAttribute(Element element) throws CoreException { 601 String setKey = element.getAttribute(KEY); 602 NodeList nodeList = element.getChildNodes(); 603 int entryCount = nodeList.getLength(); 604 Set set = new HashSet (entryCount); 605 Node node = null; 606 Element selement = null; 607 for(int i = 0; i < entryCount; i++) { 608 node = nodeList.item(i); 609 if(node.getNodeType() == Node.ELEMENT_NODE) { 610 selement = (Element )node; 611 if(!selement.getNodeName().equalsIgnoreCase(SET_ENTRY)) { 612 throw getInvalidFormatDebugException(); 613 } 614 set.add(getValueAttribute(selement)); 615 } 616 } 617 setAttribute(setKey, set); 618 } 619 620 627 protected void setMapAttribute(Element element) throws CoreException { 628 String mapKey = element.getAttribute(KEY); 629 NodeList nodeList = element.getChildNodes(); 630 int entryCount = nodeList.getLength(); 631 Map map = new HashMap (entryCount); 632 Node node = null; 633 Element selement = null; 634 for (int i = 0; i < entryCount; i++) { 635 node = nodeList.item(i); 636 if (node.getNodeType() == Node.ELEMENT_NODE) { 637 selement = (Element ) node; 638 if (!selement.getNodeName().equalsIgnoreCase(MAP_ENTRY)) { 639 throw getInvalidFormatDebugException(); 640 } 641 map.put(getKeyAttribute(selement), getValueAttribute(selement)); 642 } 643 } 644 setAttribute(mapKey, map); 645 } 646 647 653 protected String getKeyAttribute(Element element) throws CoreException { 654 String key = element.getAttribute(KEY); 655 if (key == null) { 656 throw getInvalidFormatDebugException(); 657 } 658 return key; 659 } 660 661 667 protected String getValueAttribute(Element element) throws CoreException { 668 String value = element.getAttribute(VALUE); 669 if (value == null) { 670 throw getInvalidFormatDebugException(); 671 } 672 return value; 673 } 674 675 679 protected DebugException getInvalidFormatDebugException() { 680 return 681 new DebugException( 682 new Status( 683 IStatus.ERROR, DebugPlugin.getUniqueIdentifier(), 684 DebugException.REQUEST_FAILED, DebugCoreMessages.LaunchConfigurationInfo_Invalid_launch_configuration_XML__10, null 685 ) 686 ); 687 } 688 689 696 public boolean equals(Object obj) { 697 698 if (!(obj instanceof LaunchConfigurationInfo)) { 700 return false; 701 } 702 703 LaunchConfigurationInfo other = (LaunchConfigurationInfo) obj; 705 if (!fType.getIdentifier().equals(other.getType().getIdentifier())) { 706 return false; 707 } 708 709 return compareAttributes(fAttributes, other.getAttributeTable()); 711 } 712 713 721 protected boolean compareAttributes(TreeMap map1, TreeMap map2) { 722 LaunchManager manager = (LaunchManager)DebugPlugin.getDefault().getLaunchManager(); 723 if (map1.size() == map2.size()) { 724 Iterator attributes = map1.keySet().iterator(); 725 while (attributes.hasNext()) { 726 String key = (String )attributes.next(); 727 Object attr1 = map1.get(key); 728 Object attr2 = map2.get(key); 729 if (attr2 == null) { 730 return false; 731 } 732 Comparator comp = manager.getComparator(key); 733 if (comp == null) { 734 if (fgIsSun14x) { 735 if(attr2 instanceof String & attr1 instanceof String ) { 736 attr1 = ((String )attr1).replaceAll("\\r", ""); attr2 = ((String )attr2).replaceAll("\\r", ""); } 745 } 746 if (!attr1.equals(attr2)) { 747 return false; 748 } 749 } else { 750 if (comp.compare(attr1, attr2) != 0) { 751 return false; 752 } 753 } 754 } 755 return true; 756 } 757 return false; 758 } 759 760 763 public int hashCode() { 764 return fType.hashCode() + fAttributes.size(); 765 } 766 767 } 768 769 | Popular Tags |