1 19 package org.netbeans.modules.subversion; 20 21 import java.net.MalformedURLException ; 22 import org.openide.ErrorManager; 23 import org.tigris.subversion.svnclientadapter.SVNRevision; 24 import org.tigris.subversion.svnclientadapter.SVNUrl; 25 26 30 public class RepositoryFile { 31 32 public static final String [] ROOT_FOLDER_PATH = new String [0]; 33 34 private final SVNUrl repositoryUrl; 35 private final SVNRevision revision; 36 private SVNUrl fileUrl; 37 private String [] pathSegments; 38 private String path; 39 private String toString = null; 40 private boolean repositoryRoot; 41 42 private String name; 43 44 public RepositoryFile(SVNUrl repositoryUrl, SVNRevision revision) { 45 assert repositoryUrl != null; 46 assert revision != null; 47 48 this.repositoryUrl = repositoryUrl; 49 this.revision = revision; 50 repositoryRoot = true; 51 } 52 53 public RepositoryFile(SVNUrl repositoryUrl, SVNUrl fileUrl, SVNRevision revision) { 54 this(repositoryUrl, revision); 55 this.fileUrl = fileUrl; 56 repositoryRoot = fileUrl == null; 57 58 if(!repositoryRoot) { 59 String [] fileUrlSegments = fileUrl.getPathSegments(); 60 int fileSegmentsLength = fileUrlSegments.length; 61 int repositorySegmentsLength = repositoryUrl.getPathSegments().length; 62 pathSegments = new String [fileSegmentsLength - repositorySegmentsLength]; 63 StringBuffer sb = new StringBuffer (); 64 for (int i = repositorySegmentsLength; i < fileSegmentsLength; i++) { 65 pathSegments[i-repositorySegmentsLength] = fileUrlSegments[i]; 66 sb.append(fileUrlSegments[i]); 67 if(i-repositorySegmentsLength < pathSegments.length-1) { 68 sb.append("/"); } 70 } 71 path = sb.toString(); 72 } 73 } 74 75 public RepositoryFile(SVNUrl repositoryUrl, String [] pathSegments, SVNRevision revision) throws MalformedURLException { 76 this(repositoryUrl, revision); 77 this.pathSegments = pathSegments; 78 repositoryRoot = pathSegments == null; 79 80 if(!repositoryRoot) { 81 StringBuffer sb = new StringBuffer (); 82 for (int i = 0; i < pathSegments.length; i++) { 83 sb.append(pathSegments[i]); 84 if(i<pathSegments.length-1) { 85 sb.append("/"); } 87 } 88 path = sb.toString(); 89 fileUrl = repositoryUrl.appendPath(path); 90 } 91 } 92 93 public RepositoryFile(SVNUrl repositoryUrl, String path, SVNRevision revision) throws MalformedURLException { 94 this(repositoryUrl, revision); 95 this.path = path; 96 repositoryRoot = path == null; 97 98 if(!repositoryRoot) { 99 fileUrl = repositoryUrl.appendPath(path); 100 pathSegments = path.split("/"); } 102 } 103 104 public SVNUrl getRepositoryUrl() { 105 return repositoryUrl; 106 } 107 108 public SVNRevision getRevision() { 109 return revision; 110 } 111 112 public SVNUrl getFileUrl() { 113 if(isRepositoryRoot()) { 114 return getRepositoryUrl(); 115 } 116 return fileUrl; 117 } 118 119 public String [] getPathSegments() { 120 if(isRepositoryRoot()) { 121 return ROOT_FOLDER_PATH; 122 } 123 return pathSegments; 124 } 125 126 public String getPath() { 127 if(isRepositoryRoot()) { 128 return ""; } 130 return path; 131 } 132 133 public boolean isRepositoryRoot() { 134 return repositoryRoot; 135 } 136 137 public String getName() { 138 if(name == null) { 139 if(isRepositoryRoot()) { 140 String url = getRepositoryUrl().toString(); 141 int idx = url.indexOf("://"); if(idx >= 0) { 143 url = url.substring(idx+3); 144 } 145 return url; 146 } else { 147 return getFileUrl().getLastPathSegment(); 148 } 149 } 150 return name; 151 } 152 153 public String toString() { 154 if(toString == null) { 155 StringBuffer sb = new StringBuffer (); 156 sb.append(fileUrl); 157 sb.append("@"); sb.append(revision); 159 toString = sb.toString(); 160 } 161 162 return toString; 163 } 164 165 public RepositoryFile appendPath(String path) { 166 return new RepositoryFile(repositoryUrl, getFileUrl().appendPath(path), revision); 167 } 168 169 public RepositoryFile replaceLastSegment(String segment, int level) { 170 assert segment != null && !segment.equals(""); assert level > -1 && level < fileUrl.getPathSegments().length; 172 assert !isRepositoryRoot(); 174 String fileUrlString = fileUrl.toString(); 175 int fromIdx = fileUrlString.lastIndexOf('/'); 176 int toIdx = fileUrlString.length() - 1; 177 for (int i = 0; i < level; i++) { 178 toIdx = fromIdx - 1; 179 fromIdx = fileUrlString.lastIndexOf('/', fromIdx - 1); 180 } 181 182 assert !(fromIdx < repositoryUrl.toString().length()); 183 assert toIdx >= fromIdx && toIdx < fileUrlString.length(); 184 185 SVNUrl newUrl = null; 186 try { 187 newUrl = new SVNUrl(fileUrlString.substring(0, fromIdx + 1) + segment + fileUrlString.substring(toIdx + 1)); 188 } catch (MalformedURLException ex) { 189 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); } 191 return new RepositoryFile(repositoryUrl, newUrl, revision); 192 } 193 194 public RepositoryFile removeLastSegment() { 195 assert !isRepositoryRoot(); 197 String fileUrlStrint = fileUrl.toString(); 198 int idx = fileUrlStrint.lastIndexOf('/'); 199 200 assert !(idx < repositoryUrl.toString().length()); 201 202 SVNUrl newUrl = null; 203 try { 204 newUrl = new SVNUrl(fileUrlStrint.substring(0, idx)); 205 } catch (MalformedURLException ex) { 206 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); } 208 return new RepositoryFile(repositoryUrl, newUrl, revision); 209 } 210 211 } 212
| Popular Tags
|