1 11 package org.eclipse.team.internal.ccvs.core.syncinfo; 12 13 14 import java.io.*; 15 16 import org.eclipse.core.runtime.*; 17 import org.eclipse.osgi.util.NLS; 18 import org.eclipse.team.internal.ccvs.core.*; 19 import org.eclipse.team.internal.ccvs.core.connection.CVSRepositoryLocation; 20 import org.eclipse.team.internal.ccvs.core.resources.CVSEntryLineTag; 21 import org.eclipse.team.internal.ccvs.core.util.Util; 22 23 30 public class FolderSyncInfo { 31 32 public static final String VIRTUAL_DIRECTORY = "CVSROOT/Emptydir"; 35 protected String repository; 37 38 protected String root; 40 41 private CVSEntryLineTag tag; 43 44 protected boolean isStatic; 47 48 56 public FolderSyncInfo(String repo, String root, CVSTag tag, boolean isStatic) { 57 Assert.isNotNull(repo); 58 Assert.isNotNull(root); 59 this.repository = repo; 60 this.root = root.intern(); 62 ensureRepositoryRelativeToRoot(); 63 this.isStatic = isStatic; 64 setTag(tag); 65 } 66 67 70 private void ensureRepositoryRelativeToRoot() { 71 String rootDir; 72 try { 73 rootDir = getRootDirectory(); 74 } catch (CVSException e) { 75 return; 77 } 78 if (repository.startsWith(rootDir)) { 79 repository = repository.substring(rootDir.length()); 80 } 81 if (repository.startsWith(ResourceSyncInfo.SEPARATOR)) { 82 repository = repository.substring(ResourceSyncInfo.SEPARATOR.length()); 83 } 84 } 85 86 public boolean equals(Object other) { 87 if(other == this) return true; 88 if (!(other instanceof FolderSyncInfo)) return false; 89 90 FolderSyncInfo syncInfo = ((FolderSyncInfo)other); 91 if (!getRoot().equals(syncInfo.getRoot())) return false; 92 if (!getRepository().equals(syncInfo.getRepository())) return false; 93 if (getIsStatic() != syncInfo.getIsStatic()) return false; 94 if ((getTag() == null) || (syncInfo.getTag() == null)) { 95 if ((getTag() == null) && (syncInfo.getTag() != null) && (syncInfo.getTag().getType() != CVSTag.HEAD)) { 96 return false; 97 } else if ((syncInfo.getTag() == null) && (getTag() != null) && (getTag().getType() != CVSTag.HEAD)) { 98 return false; 99 } 100 } else if (!getTag().equals(syncInfo.getTag())) { 101 return false; 102 } 103 return true; 104 } 105 110 public String getRoot() { 111 return root; 112 } 113 114 125 private String getRootDirectory() throws CVSException { 126 try { 127 String root = getRoot(); 128 int index = root.indexOf(CVSRepositoryLocation.HOST_SEPARATOR); 129 if (index == -1) { 130 index = root.indexOf(CVSRepositoryLocation.COLON); 132 if (index == 0) { 133 index = root.indexOf(CVSRepositoryLocation.COLON, index + 1); 136 index = root.indexOf(CVSRepositoryLocation.COLON, index + 1); 137 } 138 if (index == -1) { 139 index = root.indexOf(ResourceSyncInfo.SEPARATOR); 142 if (index != -1) index--; 144 } 145 } else { 146 index = root.indexOf(CVSRepositoryLocation.COLON, index + 1); 148 } 149 index++; 150 char c = root.charAt(index); 152 while (Character.isDigit(c)) { 153 c = root.charAt(++index); 154 } 155 return root.substring(index); 156 } catch (IndexOutOfBoundsException e) { 157 IStatus status = new CVSStatus(IStatus.ERROR,CVSMessages.FolderSyncInfo_Maleformed_root_4, e); 158 throw new CVSException(status); 159 } 160 } 161 162 167 public CVSEntryLineTag getTag() { 168 return tag; 169 } 170 171 176 public String getRepository() { 177 return repository; 178 } 179 180 185 public boolean getIsStatic() { 186 return isStatic; 187 } 188 189 211 public String getRemoteLocation() throws CVSException { 212 return Util.appendPath(getRootDirectory(), getRepository()); 213 } 214 215 219 public int hashCode() { 220 return getRoot().hashCode() | getRepository().hashCode(); 221 } 222 223 228 protected void setTag(CVSTag tag) { 229 if (tag == null || tag.equals(CVSTag.DEFAULT)) { 230 this.tag = null; 231 } else { 232 this.tag = new CVSEntryLineTag(tag); 233 } 234 } 235 238 public String toString() { 239 return getRoot() + "/" + getRepository() + "/" + getTag(); } 241 242 public MutableFolderSyncInfo cloneMutable() { 243 MutableFolderSyncInfo newSync = new MutableFolderSyncInfo(this); 244 return newSync; 245 } 246 247 254 public boolean isSameMapping(FolderSyncInfo other) { 255 if (other == null) return false; 256 return (this.getRoot().equals(other.getRoot()) 257 && this.getRepository().equals(other.getRepository())) ; 258 } 259 260 264 public byte[] getBytes() throws CVSException { 265 ByteArrayOutputStream out = new ByteArrayOutputStream(); 266 DataOutputStream dos = new DataOutputStream(out); 267 try { 268 dos.writeUTF(getRoot()); 269 dos.writeUTF(getRepository()); 270 CVSEntryLineTag t = getTag(); 271 if (t == null) { 272 dos.writeUTF(""); } else { 274 dos.writeUTF(t.toString()); 275 } 276 dos.writeBoolean(getIsStatic()); 277 dos.close(); 278 } catch (IOException e) { 279 throw CVSException.wrapException(e); 280 } 281 return out.toByteArray(); 282 } 283 284 288 public static FolderSyncInfo getFolderSyncInfo(byte[] bytes) throws CVSException { 289 Assert.isNotNull(bytes, "getFolderSyncInfo cannot be called with null parameter"); ByteArrayInputStream in = new ByteArrayInputStream(bytes); 291 DataInputStream dis = new DataInputStream(in); 292 String root; 293 String repository; 294 CVSEntryLineTag tag; 295 boolean isStatic; 296 try { 297 root = dis.readUTF(); 298 repository = dis.readUTF(); 299 String tagName = dis.readUTF(); 300 if (tagName.length() == 0) { 301 tag = null; 302 } else { 303 tag = new CVSEntryLineTag(tagName); 304 } 305 isStatic = dis.readBoolean(); 306 } catch (IOException e) { 307 Status status = new Status(Status.ERROR, CVSProviderPlugin.ID, NLS.bind(CVSMessages.FolderSyncInfo_InvalidSyncInfoBytes, new String (bytes)), e); 308 CVSException ex = new CVSException(status); 309 throw ex; 310 } 311 return new FolderSyncInfo(repository, root, tag, isStatic); 312 } 313 314 321 public boolean isVirtualDirectory() { 322 return getRepository().equals(VIRTUAL_DIRECTORY); 323 } 324 325 public FolderSyncInfo asImmutable() { 326 return this; 327 } 328 } 329 | Popular Tags |