1 11 package org.eclipse.team.internal.ccvs.ui.repo; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 import java.util.Stack ; 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 ; 25 26 public class RepositoriesViewContentHandler extends DefaultHandler { 27 28 public static final String REPOSITORIES_VIEW_TAG = "repositories-view"; 30 public static final String REPOSITORY_TAG = "repository"; public static final String WORKING_SET_TAG = "working-set"; public static final String CURRENT_WORKING_SET_TAG = "current-working-set"; public static final String MODULE_TAG = "module"; public static final String TAG_TAG = "tag"; public static final String AUTO_REFRESH_FILE_TAG = "auto-refresh-file"; public static final String DATE_TAGS_TAG = "date-tags"; public static final String DATE_TAG_TAG = "date-tag"; 39 public static final String ID_ATTRIBUTE = "id"; public static final String NAME_ATTRIBUTE = "name"; public static final String PATH_ATTRIBUTE = "path"; public static final String FULL_PATH_ATTRIBUTE = "full-path"; public static final String TYPE_ATTRIBUTE = "type"; public static final String READ_ID_ATTRIBUTE = "read-id"; public static final String WRITE_ID_ATTRIBUTE = "write-id"; public static final String LAST_ACCESS_TIME_ATTRIBUTE = "lastAcessTime"; 48 public static final String [] TAG_TYPES = {"head", "branch", "version", "date"}; public static final String DEFAULT_TAG_TYPE = "version"; public static final String DEFINED_MODULE_TYPE = "defined"; 52 private RepositoryManager manager; 53 private StringBuffer buffer = new StringBuffer (); 54 private Stack tagStack = new Stack (); 55 private RepositoryRoot currentRepositoryRoot; 56 private String currentRemotePath; 57 private List tags; 58 private List dateTags; 59 private List autoRefreshFiles; 60 private boolean ignoreElements; 61 62 private long lastAccessTime; 63 64 public RepositoriesViewContentHandler(RepositoryManager manager) { 65 this.manager = manager; 66 } 67 68 71 public void characters(char[] chars, int startIndex, int length) throws SAXException { 72 buffer.append(chars, startIndex, length); 73 } 74 75 78 public void endElement(String namespaceURI, String localName, String qName) throws SAXException { 79 80 String elementName = getElementName(namespaceURI, localName, qName); 81 if (!elementName.equals(tagStack.peek())) { 82 throw new SAXException(NLS.bind(CVSUIMessages.RepositoriesViewContentHandler_unmatchedTag, new String [] { elementName })); 83 } 84 85 if (elementName.equals(REPOSITORIES_VIEW_TAG)) { 86 } 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 ignoreElements = false; 95 } else if (elementName.equals(CURRENT_WORKING_SET_TAG)) { 96 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 []) autoRefreshFiles.toArray(new String [autoRefreshFiles.size()])); 106 } 107 }else if(elementName.equals(DATE_TAG_TAG)){ 108 if (! ignoreElements && currentRepositoryRoot != null) { 109 Iterator 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 122 public void startElement( 123 String namespaceURI, 124 String localName, 125 String qName, 126 Attributes atts) 127 throws SAXException { 128 129 String elementName = getElementName(namespaceURI, localName, qName); 130 if (elementName.equals(REPOSITORIES_VIEW_TAG)) { 131 } else if (elementName.equals(REPOSITORY_TAG)) { 133 String id = atts.getValue(ID_ATTRIBUTE); 134 if (id == null) { 135 throw new SAXException(NLS.bind(CVSUIMessages.RepositoriesViewContentHandler_missingAttribute, new String [] { 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 [] { id }), e); 145 } 146 currentRepositoryRoot = new RepositoryRoot(root); 147 String name = atts.getValue(NAME_ATTRIBUTE); 148 if (name != null) { 149 currentRepositoryRoot.setName(name); 150 } 151 } else if(elementName.equals(DATE_TAGS_TAG)){ 152 dateTags = new ArrayList (); 154 } else if (elementName.equals(DATE_TAG_TAG)){ 155 String name = atts.getValue(NAME_ATTRIBUTE); 156 if (name == null) { 157 throw new SAXException(NLS.bind(CVSUIMessages.RepositoriesViewContentHandler_missingAttribute, new String [] { DATE_TAGS_TAG, NAME_ATTRIBUTE })); 158 } 159 dateTags.add(new CVSTag(name, CVSTag.DATE)); 160 }else if (elementName.equals(WORKING_SET_TAG)) { 161 String name = atts.getValue(NAME_ATTRIBUTE); 162 if (name == null) { 163 throw new SAXException(NLS.bind(CVSUIMessages.RepositoriesViewContentHandler_missingAttribute, new String [] { WORKING_SET_TAG, NAME_ATTRIBUTE })); 164 } 165 ignoreElements = true; 167 } else if (elementName.equals(MODULE_TAG)) { 168 String path = atts.getValue(PATH_ATTRIBUTE); 169 if (path == null) { 170 throw new SAXException(NLS.bind(CVSUIMessages.RepositoriesViewContentHandler_missingAttribute, new String [] { MODULE_TAG, PATH_ATTRIBUTE })); 171 } 172 String 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 cachedTimeString = atts.getValue(LAST_ACCESS_TIME_ATTRIBUTE); 178 if (cachedTimeString != null) { 179 try { 180 Long time = Long.valueOf(cachedTimeString); 181 cachedTime = time.longValue(); 182 } catch (NumberFormatException e) { 183 } 185 } 186 startModule(path, cachedTime); 187 } else if (elementName.equals(TAG_TAG)) { 188 String type = atts.getValue(TYPE_ATTRIBUTE); 189 if (type == null) { 190 type = DEFAULT_TAG_TYPE; 191 } 192 String name = atts.getValue(NAME_ATTRIBUTE); 193 if (name == null) { 194 throw new SAXException(NLS.bind(CVSUIMessages.RepositoriesViewContentHandler_missingAttribute, new String [] { TAG_TAG, NAME_ATTRIBUTE })); 195 } 196 tags.add(new CVSTag(name, getCVSTagType(type))); 197 } else if (elementName.equals(AUTO_REFRESH_FILE_TAG)) { 198 String path = atts.getValue(FULL_PATH_ATTRIBUTE); 199 if (path == null) { 200 path = atts.getValue(PATH_ATTRIBUTE); 202 if (path == null) { 203 throw new SAXException(NLS.bind(CVSUIMessages.RepositoriesViewContentHandler_missingAttribute, new String [] { 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 ignoreElements = true; 215 } 216 buffer = new StringBuffer (); 218 tagStack.push(elementName); 219 } 220 221 private void startModule(String path, long cachedTime) { 222 currentRemotePath = path; 223 tags = new ArrayList (); 224 this.lastAccessTime = cachedTime; 225 autoRefreshFiles = new ArrayList (); 226 } 227 228 232 public int getCVSTagType(String 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 245 private String getElementName(String namespaceURI, String localName, String qName) { 246 if (localName != null && localName.length() > 0) { 247 return localName; 248 } else { 249 return qName; 250 } 251 } 252 } 253 | Popular Tags |