KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > repo > RepositoriesViewContentHandler


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.ccvs.ui.repo;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.Stack JavaDoc;
17
18 import org.eclipse.core.runtime.Path;
19 import org.eclipse.osgi.util.NLS;
20 import org.eclipse.team.internal.ccvs.core.*;
21 import org.eclipse.team.internal.ccvs.core.util.KnownRepositories;
22 import org.eclipse.team.internal.ccvs.ui.CVSUIMessages;
23 import org.xml.sax.*;
24 import org.xml.sax.helpers.DefaultHandler JavaDoc;
25
26 public class RepositoriesViewContentHandler extends DefaultHandler JavaDoc {
27
28     public static final String JavaDoc REPOSITORIES_VIEW_TAG = "repositories-view"; //$NON-NLS-1$
29

30     public static final String JavaDoc REPOSITORY_TAG = "repository"; //$NON-NLS-1$
31
public static final String JavaDoc WORKING_SET_TAG = "working-set"; //$NON-NLS-1$
32
public static final String JavaDoc CURRENT_WORKING_SET_TAG = "current-working-set"; //$NON-NLS-1$
33
public static final String JavaDoc MODULE_TAG = "module"; //$NON-NLS-1$
34
public static final String JavaDoc TAG_TAG = "tag"; //$NON-NLS-1$
35
public static final String JavaDoc AUTO_REFRESH_FILE_TAG = "auto-refresh-file"; //$NON-NLS-1$
36
public static final String JavaDoc DATE_TAGS_TAG = "date-tags"; //$NON-NLS-1$
37
public static final String JavaDoc DATE_TAG_TAG = "date-tag"; //$NON-NLS-1$
38

39     public static final String JavaDoc ID_ATTRIBUTE = "id"; //$NON-NLS-1$
40
public static final String JavaDoc NAME_ATTRIBUTE = "name"; //$NON-NLS-1$
41
public static final String JavaDoc PATH_ATTRIBUTE = "path"; //$NON-NLS-1$
42
public static final String JavaDoc FULL_PATH_ATTRIBUTE = "full-path"; //$NON-NLS-1$
43
public static final String JavaDoc TYPE_ATTRIBUTE = "type"; //$NON-NLS-1$
44
public static final String JavaDoc READ_ID_ATTRIBUTE = "read-id"; //$NON-NLS-1$
45
public static final String JavaDoc WRITE_ID_ATTRIBUTE = "write-id"; //$NON-NLS-1$
46
public static final String JavaDoc LAST_ACCESS_TIME_ATTRIBUTE = "lastAcessTime"; //$NON-NLS-1$
47

48     public static final String JavaDoc[] TAG_TYPES = {"head", "branch", "version", "date"}; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
49
public static final String JavaDoc DEFAULT_TAG_TYPE = "version"; //$NON-NLS-1$
50
public static final String JavaDoc DEFINED_MODULE_TYPE = "defined"; //$NON-NLS-1$
51

52     private RepositoryManager manager;
53     private StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
54     private Stack JavaDoc tagStack = new Stack JavaDoc();
55     private RepositoryRoot currentRepositoryRoot;
56     private String JavaDoc currentRemotePath;
57     private List JavaDoc tags;
58     private List JavaDoc dateTags;
59     private List JavaDoc autoRefreshFiles;
60     private boolean ignoreElements;
61
62     private long lastAccessTime;
63
64     public RepositoriesViewContentHandler(RepositoryManager manager) {
65         this.manager = manager;
66     }
67     
68     /**
69      * @see ContentHandler#characters(char[], int, int)
70      */

71     public void characters(char[] chars, int startIndex, int length) throws SAXException {
72         buffer.append(chars, startIndex, length);
73     }
74
75     /**
76      * @see ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
77      */

78     public void endElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName) throws SAXException {
79         
80         String JavaDoc elementName = getElementName(namespaceURI, localName, qName);
81         if (!elementName.equals(tagStack.peek())) {
82             throw new SAXException(NLS.bind(CVSUIMessages.RepositoriesViewContentHandler_unmatchedTag, new String JavaDoc[] { elementName }));
83         }
84         
85         if (elementName.equals(REPOSITORIES_VIEW_TAG)) {
86             // all done
87
} else if (elementName.equals(REPOSITORY_TAG)) {
88             if (!ignoreElements) {
89                 manager.add(currentRepositoryRoot);
90             }
91             currentRepositoryRoot = null;
92         } else if (elementName.equals(WORKING_SET_TAG)) {
93             // This tag is no longer used
94
ignoreElements = false;
95         } else if (elementName.equals(CURRENT_WORKING_SET_TAG)) {
96             // This tag is no longer used
97
ignoreElements = false;
98         } else if (elementName.equals(MODULE_TAG)) {
99             if (! ignoreElements && currentRepositoryRoot != null) {
100                 currentRepositoryRoot.addTags(currentRemotePath,
101                     (CVSTag[]) tags.toArray(new CVSTag[tags.size()]));
102                 if (lastAccessTime > 0)
103                     currentRepositoryRoot.setLastAccessedTime(currentRemotePath, lastAccessTime);
104                 currentRepositoryRoot.setAutoRefreshFiles(currentRemotePath,
105                     (String JavaDoc[]) autoRefreshFiles.toArray(new String JavaDoc[autoRefreshFiles.size()]));
106             }
107         }else if(elementName.equals(DATE_TAG_TAG)){
108             if (! ignoreElements && currentRepositoryRoot != null) {
109                 Iterator JavaDoc iter = dateTags.iterator();
110                 while(iter.hasNext()){
111                     CVSTag tag = (CVSTag)iter.next();
112                     currentRepositoryRoot.addDateTag(tag);
113                 }
114             }
115         }
116         tagStack.pop();
117     }
118         
119     /**
120      * @see ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
121      */

122     public void startElement(
123             String JavaDoc namespaceURI,
124             String JavaDoc localName,
125             String JavaDoc qName,
126             Attributes atts)
127             throws SAXException {
128         
129         String JavaDoc elementName = getElementName(namespaceURI, localName, qName);
130         if (elementName.equals(REPOSITORIES_VIEW_TAG)) {
131             // just started
132
} else if (elementName.equals(REPOSITORY_TAG)) {
133             String JavaDoc id = atts.getValue(ID_ATTRIBUTE);
134             if (id == null) {
135                 throw new SAXException(NLS.bind(CVSUIMessages.RepositoriesViewContentHandler_missingAttribute, new String JavaDoc[] { REPOSITORY_TAG, ID_ATTRIBUTE }));
136             }
137             ICVSRepositoryLocation root;
138             try {
139                 root = KnownRepositories.getInstance().getRepository(id);
140                 if (!KnownRepositories.getInstance().isKnownRepository(id)) {
141                     KnownRepositories.getInstance().addRepository(root, false);
142                 }
143             } catch (CVSException e) {
144                 throw new SAXException(NLS.bind(CVSUIMessages.RepositoriesViewContentHandler_errorCreatingRoot, new String JavaDoc[] { id }), e);
145             }
146             currentRepositoryRoot = new RepositoryRoot(root);
147             String JavaDoc name = atts.getValue(NAME_ATTRIBUTE);
148             if (name != null) {
149                 currentRepositoryRoot.setName(name);
150             }
151         } else if(elementName.equals(DATE_TAGS_TAG)){
152             //prepare to collect date tag
153
dateTags = new ArrayList JavaDoc();
154         } else if (elementName.equals(DATE_TAG_TAG)){
155             String JavaDoc name = atts.getValue(NAME_ATTRIBUTE);
156             if (name == null) {
157                 throw new SAXException(NLS.bind(CVSUIMessages.RepositoriesViewContentHandler_missingAttribute, new String JavaDoc[] { DATE_TAGS_TAG, NAME_ATTRIBUTE }));
158             }
159             dateTags.add(new CVSTag(name, CVSTag.DATE));
160         }else if (elementName.equals(WORKING_SET_TAG)) {
161             String JavaDoc name = atts.getValue(NAME_ATTRIBUTE);
162             if (name == null) {
163                 throw new SAXException(NLS.bind(CVSUIMessages.RepositoriesViewContentHandler_missingAttribute, new String JavaDoc[] { WORKING_SET_TAG, NAME_ATTRIBUTE }));
164             }
165             // Ignore any elements until the corresponding end tag is reached
166
ignoreElements = true;
167         } else if (elementName.equals(MODULE_TAG)) {
168             String JavaDoc path = atts.getValue(PATH_ATTRIBUTE);
169             if (path == null) {
170                 throw new SAXException(NLS.bind(CVSUIMessages.RepositoriesViewContentHandler_missingAttribute, new String JavaDoc[] { MODULE_TAG, PATH_ATTRIBUTE }));
171             }
172             String JavaDoc type = atts.getValue(TYPE_ATTRIBUTE);
173             if (type != null && type.equals(DEFINED_MODULE_TYPE)) {
174                 path = RepositoryRoot.asDefinedModulePath(path);
175             }
176             long cachedTime = 0;
177             String JavaDoc cachedTimeString = atts.getValue(LAST_ACCESS_TIME_ATTRIBUTE);
178             if (cachedTimeString != null) {
179                 try {
180                     Long JavaDoc time = Long.valueOf(cachedTimeString);
181                     cachedTime = time.longValue();
182                 } catch (NumberFormatException JavaDoc e) {
183                     // Ignore
184
}
185             }
186             startModule(path, cachedTime);
187         } else if (elementName.equals(TAG_TAG)) {
188             String JavaDoc type = atts.getValue(TYPE_ATTRIBUTE);
189             if (type == null) {
190                 type = DEFAULT_TAG_TYPE;
191             }
192             String JavaDoc name = atts.getValue(NAME_ATTRIBUTE);
193             if (name == null) {
194                 throw new SAXException(NLS.bind(CVSUIMessages.RepositoriesViewContentHandler_missingAttribute, new String JavaDoc[] { TAG_TAG, NAME_ATTRIBUTE }));
195             }
196             tags.add(new CVSTag(name, getCVSTagType(type)));
197         } else if (elementName.equals(AUTO_REFRESH_FILE_TAG)) {
198             String JavaDoc path = atts.getValue(FULL_PATH_ATTRIBUTE);
199             if (path == null) {
200                 // get the old path attribute format which was relative to the module
201
path = atts.getValue(PATH_ATTRIBUTE);
202                 if (path == null) {
203                     throw new SAXException(NLS.bind(CVSUIMessages.RepositoriesViewContentHandler_missingAttribute, new String JavaDoc[] { AUTO_REFRESH_FILE_TAG, FULL_PATH_ATTRIBUTE }));
204                 }
205                 if (RepositoryRoot.isDefinedModuleName(currentRemotePath)) {
206                     path = null;
207                 } else {
208                     path = new Path(null, currentRemotePath).append(path).toString();
209                 }
210             }
211             if (path != null) autoRefreshFiles.add(path);
212         } else if (elementName.equals(CURRENT_WORKING_SET_TAG)) {
213             // Ignore any elements until the corresponding end tag is reached
214
ignoreElements = true;
215         }
216         // empty buffer
217
buffer = new StringBuffer JavaDoc();
218         tagStack.push(elementName);
219     }
220
221     private void startModule(String JavaDoc path, long cachedTime) {
222         currentRemotePath = path;
223         tags = new ArrayList JavaDoc();
224         this.lastAccessTime = cachedTime;
225         autoRefreshFiles = new ArrayList JavaDoc();
226     }
227     
228     /**
229      * Method getCVSTagType.
230      * @param type
231      */

232     public int getCVSTagType(String JavaDoc type) {
233         for (int i = 0; i < TAG_TYPES.length; i++) {
234             if (TAG_TYPES[i].equals(type))
235                 return i;
236         }
237         return CVSTag.VERSION;
238     }
239     
240     /*
241      * Couldn't figure out from the SAX API exactly when localName vs. qName is used.
242      * However, the XML for project sets doesn't use namespaces so either of the two names
243      * is fine. Therefore, use whichever one is provided.
244      */

245     private String JavaDoc getElementName(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName) {
246         if (localName != null && localName.length() > 0) {
247             return localName;
248         } else {
249             return qName;
250         }
251     }
252 }
253
Popular Tags