KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > ProjectSetContentHandler


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.team.internal.ui;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.Map JavaDoc;
17
18 import org.xml.sax.Attributes JavaDoc;
19 import org.xml.sax.SAXException JavaDoc;
20 import org.xml.sax.helpers.DefaultHandler JavaDoc;
21
22 public class ProjectSetContentHandler extends DefaultHandler JavaDoc {
23     boolean inPsf = false;
24     boolean inProvider = false;
25     boolean inProject = false;
26     Map JavaDoc map;
27     String JavaDoc id;
28     List JavaDoc references;
29     boolean isVersionOne = false;
30     
31     public void startElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc atts) throws SAXException JavaDoc {
32         String JavaDoc elementName = getElementName(namespaceURI, localName, qName);
33         if (elementName.equals("psf")) { //$NON-NLS-1$
34
map = new HashMap JavaDoc();
35             inPsf = true;
36             String JavaDoc version = atts.getValue("version"); //$NON-NLS-1$
37
isVersionOne = version.equals("1.0"); //$NON-NLS-1$
38
return;
39         }
40         if (isVersionOne) return;
41         if (elementName.equals("provider")) { //$NON-NLS-1$
42
if (!inPsf) throw new SAXException JavaDoc(TeamUIMessages.ProjectSetContentHandler_Element_provider_must_be_contained_in_element_psf_4);
43             inProvider = true;
44             id = atts.getValue("id"); //$NON-NLS-1$
45
references = new ArrayList JavaDoc();
46             return;
47         }
48         if (elementName.equals("project")) { //$NON-NLS-1$
49
if (!inProvider) throw new SAXException JavaDoc(TeamUIMessages.ProjectSetContentHandler_Element_project_must_be_contained_in_element_provider_7);
50             inProject = true;
51             String JavaDoc reference = atts.getValue("reference"); //$NON-NLS-1$
52
references.add(reference);
53             return;
54         }
55     }
56
57     public void endElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName) throws SAXException JavaDoc {
58         String JavaDoc elementName = getElementName(namespaceURI, localName, qName);
59         if (elementName.equals("psf")) { //$NON-NLS-1$
60
inPsf = false;
61             return;
62         }
63         if (isVersionOne) return;
64         if (elementName.equals("provider")) { //$NON-NLS-1$
65
map.put(id, references);
66             references = null;
67             inProvider = false;
68             return;
69         }
70         if (elementName.equals("project")) { //$NON-NLS-1$
71
inProject = false;
72             return;
73         }
74     }
75     
76     public Map JavaDoc getReferences() {
77         return map;
78     }
79     
80     public boolean isVersionOne() {
81         return isVersionOne;
82     }
83     
84     /*
85      * Couldn't figure out from the SAX API exactly when localName vs. qName is used.
86      * However, the XML for project sets doesn't use namespaces so either of the two names
87      * is fine. Therefore, use whichever one is provided.
88      */

89     private String JavaDoc getElementName(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName) {
90         if (localName != null && localName.length() > 0) {
91             return localName;
92         } else {
93             return qName;
94         }
95     }
96 }
97
Popular Tags