KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > core > LaunchConfigurationInfo


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.debug.internal.core;
12
13  
14 import java.io.IOException JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.Comparator JavaDoc;
17 import java.util.HashMap JavaDoc;
18 import java.util.HashSet JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Set JavaDoc;
23 import java.util.TreeMap JavaDoc;
24
25 import javax.xml.parsers.ParserConfigurationException JavaDoc;
26 import javax.xml.transform.TransformerException JavaDoc;
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 JavaDoc;
35 import org.w3c.dom.Element JavaDoc;
36 import org.w3c.dom.Node JavaDoc;
37 import org.w3c.dom.NodeList JavaDoc;
38
39 import com.ibm.icu.text.MessageFormat;
40  
41 /**
42  * The information associated with a launch configuration handle.
43  */

44 public class LaunchConfigurationInfo {
45     
46     /**
47      * Constants fo XML element names and attrbiutes
48      */

49     private static final String JavaDoc KEY = "key"; //$NON-NLS-1$
50
private static final String JavaDoc VALUE = "value"; //$NON-NLS-1$
51
private static final String JavaDoc SET_ENTRY = "setEntry"; //$NON-NLS-1$
52
private static final String JavaDoc LAUNCH_CONFIGURATION = "launchConfiguration"; //$NON-NLS-1$
53
private static final String JavaDoc MAP_ENTRY = "mapEntry"; //$NON-NLS-1$
54
private static final String JavaDoc LIST_ENTRY = "listEntry"; //$NON-NLS-1$
55
private static final String JavaDoc SET_ATTRIBUTE = "setAttribute"; //$NON-NLS-1$
56
private static final String JavaDoc MAP_ATTRIBUTE = "mapAttribute"; //$NON-NLS-1$
57
private static final String JavaDoc LIST_ATTRIBUTE = "listAttribute"; //$NON-NLS-1$
58
private static final String JavaDoc BOOLEAN_ATTRIBUTE = "booleanAttribute"; //$NON-NLS-1$
59
private static final String JavaDoc INT_ATTRIBUTE = "intAttribute"; //$NON-NLS-1$
60
private static final String JavaDoc STRING_ATTRIBUTE = "stringAttribute"; //$NON-NLS-1$
61
private static final String JavaDoc TYPE = "type"; //$NON-NLS-1$
62

63     /**
64      * This configurations attribute table. Keys are <code>String</code>s and
65      * values are one of <code>String</code>, <code>Integer</code>, or
66      * <code>Boolean</code>.
67      */

68     private TreeMap JavaDoc fAttributes;
69     
70     /**
71      * This launch configuration's type
72      */

73     private ILaunchConfigurationType fType;
74     
75     /**
76      * Whether running on Sun 1.4 VM - see bug 110215
77      */

78     private static boolean fgIsSun14x = false;
79     
80     static {
81         String JavaDoc vendor = System.getProperty("java.vm.vendor"); //$NON-NLS-1$
82
if (vendor.startsWith("Sun Microsystems")) { //$NON-NLS-1$
83
String JavaDoc version = System.getProperty("java.vm.version"); //$NON-NLS-1$
84
if (version.startsWith("1.4")) { //$NON-NLS-1$
85
fgIsSun14x = true;
86             }
87         }
88     }
89     
90     /**
91      * Constructs a new empty info
92      */

93     protected LaunchConfigurationInfo() {
94         setAttributeTable(new TreeMap JavaDoc());
95     }
96     
97     /**
98      * Returns this configuration's attribute table.
99      *
100      * @return attribute table
101      */

102     private TreeMap JavaDoc getAttributeTable() {
103         return fAttributes;
104     }
105
106     /**
107      * Sets this configuration's attribute table.
108      *
109      * @param table
110      * attribute table
111      */

112     private void setAttributeTable(TreeMap JavaDoc table) {
113         fAttributes = table;
114     }
115     
116     /**
117      * Sets the attributes in this info to those in the given map.
118      *
119      * @param map
120      */

121     protected void setAttributes(Map JavaDoc map) {
122         if (map == null) {
123             setAttributeTable(new TreeMap JavaDoc());
124             return;
125         }
126         setAttributeTable(new TreeMap JavaDoc(map));
127     }
128     
129     /**
130      * Returns the <code>String</code> attribute with the given key or the
131      * given default value if undefined.
132      *
133      * @return attribute specified by given key or the defaultValue if undefined
134      * @throws CoreException
135      * if the attribute with the given key exists but is not a
136      * <code>String</code>
137      */

138     protected String JavaDoc getStringAttribute(String JavaDoc key, String JavaDoc defaultValue) throws CoreException {
139         Object JavaDoc attr = getAttributeTable().get(key);
140         if (attr != null) {
141             if (attr instanceof String JavaDoc) {
142                 return (String JavaDoc)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 JavaDoc[] {key}), null
148                 )
149             );
150         }
151         return defaultValue;
152     }
153     
154     /**
155      * Returns the <code>int</code> attribute with the given key or the given
156      * default value if undefined.
157      *
158      * @return attribute specified by given key or the defaultValue if undefined
159      * @throws CoreException
160      * if the attribute with the given key exists but is not an
161      * <code>int</code>
162      */

163     protected int getIntAttribute(String JavaDoc key, int defaultValue) throws CoreException {
164         Object JavaDoc attr = getAttributeTable().get(key);
165         if (attr != null) {
166             if (attr instanceof Integer JavaDoc) {
167                 return ((Integer JavaDoc)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 JavaDoc[] {key}), null
173                 )
174             );
175         }
176         return defaultValue;
177     }
178     
179     /**
180      * Returns the <code>boolean</code> attribute with the given key or the
181      * given default value if undefined.
182      *
183      * @return attribute specified by given key or the defaultValue if undefined
184      * @throws CoreException
185      * if the attribute with the given key exists but is not a
186      * <code>boolean</code>
187      */

188     protected boolean getBooleanAttribute(String JavaDoc key, boolean defaultValue) throws CoreException {
189         Object JavaDoc attr = getAttributeTable().get(key);
190         if (attr != null) {
191             if (attr instanceof Boolean JavaDoc) {
192                 return ((Boolean JavaDoc)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 JavaDoc[] {key}), null
198                 )
199             );
200         }
201         return defaultValue;
202     }
203     
204     /**
205      * Returns the <code>java.util.List</code> attribute with the given key or
206      * the given default value if undefined.
207      *
208      * @return attribute specified by given key or the defaultValue if undefined
209      * @throws CoreException
210      * if the attribute with the given key exists but is not a
211      * <code>java.util.List</code>
212      */

213     protected List JavaDoc getListAttribute(String JavaDoc key, List JavaDoc defaultValue) throws CoreException {
214         Object JavaDoc attr = getAttributeTable().get(key);
215         if (attr != null) {
216             if (attr instanceof List JavaDoc) {
217                 return (List JavaDoc)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 JavaDoc[] {key}), null
223                 )
224             );
225         }
226         return defaultValue;
227     }
228     
229     /**
230      * Returns the <code>java.util.Set</code> attribute with the given key or
231      * the given default value if undefined.
232      *
233      * @return attribute specified by given key or the defaultValue if undefined
234      * @throws CoreException
235      * if the attribute with the given key exists but is not a
236      * <code>java.util.Set</code>
237      *
238      * @since 3.3
239      */

240     protected Set JavaDoc getSetAttribute(String JavaDoc key, Set JavaDoc defaultValue) throws CoreException {
241         Object JavaDoc attr = getAttributeTable().get(key);
242         if (attr != null) {
243             if (attr instanceof Set JavaDoc) {
244                 return (Set JavaDoc)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 JavaDoc[] {key}), null
250                 )
251             );
252         }
253         return defaultValue;
254     }
255     
256     /**
257      * Returns the <code>java.util.Map</code> attribute with the given key or
258      * the given default value if undefined.
259      *
260      * @return attribute specified by given key or the defaultValue if undefined
261      * @throws CoreException
262      * if the attribute with the given key exists but is not a
263      * <code>java.util.Map</code>
264      */

265     protected Map JavaDoc getMapAttribute(String JavaDoc key, Map JavaDoc defaultValue) throws CoreException {
266         Object JavaDoc attr = getAttributeTable().get(key);
267         if (attr != null) {
268             if (attr instanceof Map JavaDoc) {
269                 return (Map JavaDoc)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 JavaDoc[] {key}), null
275                 )
276             );
277         }
278         return defaultValue;
279     }
280     
281     /**
282      * Sets this configuration's type.
283      *
284      * @param type
285      * launch configuration type
286      */

287     protected void setType(ILaunchConfigurationType type) {
288         fType = type;
289     }
290     
291     /**
292      * Returns this configuration's type.
293      *
294      * @return launch configuration type
295      */

296     protected ILaunchConfigurationType getType() {
297         return fType;
298     }
299     
300     
301     /**
302      * Returns a copy of this info object
303      *
304      * @return copy of this info
305      */

306     protected LaunchConfigurationInfo getCopy() {
307         LaunchConfigurationInfo copy = new LaunchConfigurationInfo();
308         copy.setType(getType());
309         copy.setAttributeTable(getAttributes());
310         return copy;
311     }
312     
313     /**
314      * Returns a copy of this info's attribute map.
315      *
316      * @return a copy of this info's attribute map
317      */

318     protected TreeMap JavaDoc getAttributes() {
319         return (TreeMap JavaDoc)getAttributeTable().clone();
320     }
321     
322     /**
323      * Sets the given attribute to the given value. Only working copy's should
324      * use this API.
325      *
326      * @param key
327      * attribute key
328      * @param value
329      * attribute value
330      */

331     protected void setAttribute(String JavaDoc key, Object JavaDoc value) {
332         if (value == null) {
333             getAttributeTable().remove(key);
334         } else {
335             getAttributeTable().put(key, value);
336         }
337     }
338     
339     /**
340      * Returns the content of this info as XML
341      *
342      * @return the content of this info as XML
343      * @throws CoreException
344      * if a attribute has been set with a null key
345      * @throws IOException
346      * if an exception occurs creating the XML
347      * @throws ParserConfigurationException
348      * if an exception occurs creating the XML
349      * @throws TransformerException
350      * if an exception occurs creating the XML
351      */

352     protected String JavaDoc getAsXML() throws CoreException, IOException JavaDoc, ParserConfigurationException JavaDoc, TransformerException JavaDoc {
353
354         Document JavaDoc doc = LaunchManager.getDocument();
355         Element JavaDoc configRootElement = doc.createElement(LAUNCH_CONFIGURATION);
356         doc.appendChild(configRootElement);
357         
358         configRootElement.setAttribute(TYPE, getType().getIdentifier());
359         
360         Iterator JavaDoc keys = getAttributeTable().keySet().iterator();
361         while (keys.hasNext()) {
362             String JavaDoc key = (String JavaDoc)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 JavaDoc value = getAttributeTable().get(key);
372             if (value == null) {
373                 continue;
374             }
375             Element JavaDoc element = null;
376             String JavaDoc valueString = null;
377             if (value instanceof String JavaDoc) {
378                 valueString = (String JavaDoc)value;
379                 element = createKeyValueElement(doc, STRING_ATTRIBUTE, key, valueString);
380             } else if (value instanceof Integer JavaDoc) {
381                 valueString = ((Integer JavaDoc)value).toString();
382                 element = createKeyValueElement(doc, INT_ATTRIBUTE, key, valueString);
383             } else if (value instanceof Boolean JavaDoc) {
384                 valueString = ((Boolean JavaDoc)value).toString();
385                 element = createKeyValueElement(doc, BOOLEAN_ATTRIBUTE, key, valueString);
386             } else if (value instanceof List JavaDoc) {
387                 element = createListElement(doc, LIST_ATTRIBUTE, key, (List JavaDoc)value);
388             } else if (value instanceof Map JavaDoc) {
389                 element = createMapElement(doc, MAP_ATTRIBUTE, key, (Map JavaDoc)value);
390             } else if(value instanceof Set JavaDoc) {
391                 element = createSetElement(doc, SET_ATTRIBUTE, key, (Set JavaDoc)value);
392             }
393             configRootElement.appendChild(element);
394         }
395
396         return LaunchManager.serializeDocument(doc);
397     }
398     
399     /**
400      * Helper method that creates a 'key value' element of the specified type
401      * with the specified attribute values.
402      */

403     protected Element JavaDoc createKeyValueElement(Document JavaDoc doc, String JavaDoc elementType, String JavaDoc key, String JavaDoc value) {
404         Element JavaDoc element = doc.createElement(elementType);
405         element.setAttribute(KEY, key);
406         element.setAttribute(VALUE, value);
407         return element;
408     }
409     
410     /**
411      * Creates a new <code>Element</code> for the specified
412      * <code>java.util.List</code>
413      *
414      * @param doc the doc to add the element to
415      * @param elementType the type of the element
416      * @param setKey the key for the element
417      * @param list the list to fill the new element with
418      * @return the new element
419      */

420     protected Element JavaDoc createListElement(Document JavaDoc doc, String JavaDoc elementType, String JavaDoc listKey, List JavaDoc list) {
421         Element JavaDoc listElement = doc.createElement(elementType);
422         listElement.setAttribute(KEY, listKey);
423         Iterator JavaDoc iterator = list.iterator();
424         while (iterator.hasNext()) {
425             String JavaDoc value = (String JavaDoc) iterator.next();
426             Element JavaDoc element = doc.createElement(LIST_ENTRY);
427             element.setAttribute(VALUE, value);
428             listElement.appendChild(element);
429         }
430         return listElement;
431     }
432     
433     /**
434      * Creates a new <code>Element</code> for the specified
435      * <code>java.util.Set</code>
436      *
437      * @param doc the doc to add the element to
438      * @param elementType the type of the element
439      * @param setKey the key for the element
440      * @param set the set to fill the new element with
441      * @return the new element
442      *
443      * @since 3.3
444      */

445     protected Element JavaDoc createSetElement(Document JavaDoc doc, String JavaDoc elementType, String JavaDoc setKey, Set JavaDoc set) {
446         Element JavaDoc setElement = doc.createElement(elementType);
447         setElement.setAttribute(KEY, setKey);
448         Element JavaDoc element = null;
449         for(Iterator JavaDoc iter = set.iterator(); iter.hasNext();) {
450             element = doc.createElement(SET_ENTRY);
451             element.setAttribute(VALUE, (String JavaDoc) iter.next());
452             setElement.appendChild(element);
453         }
454         return setElement;
455     }
456     
457     /**
458      * Creates a new <code>Element</code> for the specified
459      * <code>java.util.Map</code>
460      *
461      * @param doc the doc to add the element to
462      * @param elementType the type of the element
463      * @param setKey the key for the element
464      * @param map the map to fill the new element with
465      * @return the new element
466      *
467      */

468     protected Element JavaDoc createMapElement(Document JavaDoc doc, String JavaDoc elementType, String JavaDoc mapKey, Map JavaDoc map) {
469         Element JavaDoc mapElement = doc.createElement(elementType);
470         mapElement.setAttribute(KEY, mapKey);
471         Iterator JavaDoc iterator = map.keySet().iterator();
472         while (iterator.hasNext()) {
473             String JavaDoc key = (String JavaDoc) iterator.next();
474             String JavaDoc value = (String JavaDoc) map.get(key);
475             Element JavaDoc 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     /**
484      * Initializes the mapping of attributes from the XML file
485      * @param root the root node from the XML document
486      * @throws CoreException
487      */

488     protected void initializeFromXML(Element JavaDoc root) throws CoreException {
489         if (!root.getNodeName().equalsIgnoreCase(LAUNCH_CONFIGURATION)) {
490             throw getInvalidFormatDebugException();
491         }
492         
493         // read type
494
String JavaDoc 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 JavaDoc message= MessageFormat.format(DebugCoreMessages.LaunchConfigurationInfo_missing_type, new Object JavaDoc[]{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 JavaDoc list = root.getChildNodes();
511         Node JavaDoc node = null;
512         Element JavaDoc element = null;
513         String JavaDoc 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 JavaDoc) 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     /**
538      * Loads a <code>String</code> from the specified element into the local attribute mapping
539      * @param element the element to load from
540      * @throws CoreException
541      */

542     protected void setStringAttribute(Element JavaDoc element) throws CoreException {
543         setAttribute(getKeyAttribute(element), getValueAttribute(element));
544     }
545     
546     /**
547      * Loads an <code>Integer</code> from the specified element into the local attribute mapping
548      * @param element the element to load from
549      * @throws CoreException
550      */

551     protected void setIntegerAttribute(Element JavaDoc element) throws CoreException {
552         setAttribute(getKeyAttribute(element), new Integer JavaDoc(getValueAttribute(element)));
553     }
554     
555     /**
556      * Loads a <code>Boolean</code> from the specified element into the local attribute mapping
557      * @param element the element to load from
558      * @throws CoreException
559      */

560     protected void setBooleanAttribute(Element JavaDoc element) throws CoreException {
561         setAttribute(getKeyAttribute(element), Boolean.valueOf(getValueAttribute(element)));
562     }
563     
564     /**
565      * Reads a <code>List</code> attribute from the specified XML node and
566      * loads it into the mapping of attributes
567      *
568      * @param element the element to read the list attribute from
569      * @throws CoreException if the element has an invalid format
570      */

571     protected void setListAttribute(Element JavaDoc element) throws CoreException {
572         String JavaDoc listKey = element.getAttribute(KEY);
573         NodeList JavaDoc nodeList = element.getChildNodes();
574         int entryCount = nodeList.getLength();
575         List JavaDoc list = new ArrayList JavaDoc(entryCount);
576         Node JavaDoc node = null;
577         Element JavaDoc 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 JavaDoc) 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     /**
592      * Reads a <code>Set</code> attribute from the specified XML node and
593      * loads it into the mapping of attributes
594      *
595      * @param element the element to read the set attribute from
596      * @throws CoreException if the element has an invalid format
597      *
598      * @since 3.3
599      */

600     protected void setSetAttribute(Element JavaDoc element) throws CoreException {
601         String JavaDoc setKey = element.getAttribute(KEY);
602         NodeList JavaDoc nodeList = element.getChildNodes();
603         int entryCount = nodeList.getLength();
604         Set JavaDoc set = new HashSet JavaDoc(entryCount);
605         Node JavaDoc node = null;
606         Element JavaDoc 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 JavaDoc)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     /**
621      * Reads a <code>Map</code> attribute from the specified XML node and
622      * loads it into the mapping of attributes
623      *
624      * @param element the element to read the map attribute from
625      * @throws CoreException if the element has an invalid format
626      */

627     protected void setMapAttribute(Element JavaDoc element) throws CoreException {
628         String JavaDoc mapKey = element.getAttribute(KEY);
629         NodeList JavaDoc nodeList = element.getChildNodes();
630         int entryCount = nodeList.getLength();
631         Map JavaDoc map = new HashMap JavaDoc(entryCount);
632         Node JavaDoc node = null;
633         Element JavaDoc 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 JavaDoc) 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     /**
648      * Returns the <code>String</code> representation of the 'key' attribute from the specified element
649      * @param element the element to read from
650      * @return the value
651      * @throws CoreException
652      */

653     protected String JavaDoc getKeyAttribute(Element JavaDoc element) throws CoreException {
654         String JavaDoc key = element.getAttribute(KEY);
655         if (key == null) {
656             throw getInvalidFormatDebugException();
657         }
658         return key;
659     }
660     
661     /**
662      * Returns the <code>String</code> representation of the 'value' attribute from the specified element
663      * @param element the element to read from
664      * @return the value
665      * @throws CoreException
666      */

667     protected String JavaDoc getValueAttribute(Element JavaDoc element) throws CoreException {
668         String JavaDoc value = element.getAttribute(VALUE);
669         if (value == null) {
670             throw getInvalidFormatDebugException();
671         }
672         return value;
673     }
674     
675     /**
676      * Returns an invalid format exception for reuse
677      * @return an invalid format exception
678      */

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     /**
690      * Two <code>LaunchConfigurationInfo</code> objects are equal if and only
691      * if they have the same type and they have the same set of attributes with
692      * the same values.
693      *
694      * @see Object#equals(Object)
695      */

696     public boolean equals(Object JavaDoc obj) {
697         
698         // Make sure it's a LaunchConfigurationInfo object
699
if (!(obj instanceof LaunchConfigurationInfo)) {
700             return false;
701         }
702         
703         // Make sure the types are the same
704
LaunchConfigurationInfo other = (LaunchConfigurationInfo) obj;
705         if (!fType.getIdentifier().equals(other.getType().getIdentifier())) {
706             return false;
707         }
708         
709         // Make sure the attributes are the same
710
return compareAttributes(fAttributes, other.getAttributeTable());
711     }
712     
713     /**
714      * Returns whether the two attribute maps are equal, consulting registered
715      * comparator extensions.
716      *
717      * @param map1 attribute map
718      * @param map2 attribute map
719      * @return whether the two attribute maps are equal
720      */

721     protected boolean compareAttributes(TreeMap JavaDoc map1, TreeMap JavaDoc map2) {
722         LaunchManager manager = (LaunchManager)DebugPlugin.getDefault().getLaunchManager();
723         if (map1.size() == map2.size()) {
724             Iterator JavaDoc attributes = map1.keySet().iterator();
725             while (attributes.hasNext()) {
726                 String JavaDoc key = (String JavaDoc)attributes.next();
727                 Object JavaDoc attr1 = map1.get(key);
728                 Object JavaDoc attr2 = map2.get(key);
729                 if (attr2 == null) {
730                     return false;
731                 }
732                 Comparator JavaDoc comp = manager.getComparator(key);
733                 if (comp == null) {
734                     if (fgIsSun14x) {
735                         if(attr2 instanceof String JavaDoc & attr1 instanceof String JavaDoc) {
736                             // this is a hack for bug 110215, on SUN 1.4.x, \r
737
// is stripped off when the stream is written to the
738
// DOM
739
// this is not the case for 1.5.x, so to be safe we
740
// are stripping \r off all strings before we
741
// compare for equality
742
attr1 = ((String JavaDoc)attr1).replaceAll("\\r", ""); //$NON-NLS-1$ //$NON-NLS-2$
743
attr2 = ((String JavaDoc)attr2).replaceAll("\\r", ""); //$NON-NLS-1$ //$NON-NLS-2$
744
}
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     /**
761      * @see java.lang.Object#hashCode()
762      */

763     public int hashCode() {
764         return fType.hashCode() + fAttributes.size();
765     }
766
767 }
768
769
Popular Tags