KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > subversion > RepositoryFile


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.subversion;
20
21 import java.net.MalformedURLException JavaDoc;
22 import org.openide.ErrorManager;
23 import org.tigris.subversion.svnclientadapter.SVNRevision;
24 import org.tigris.subversion.svnclientadapter.SVNUrl;
25
26 /**
27  *
28  * @author Tomas Stupka
29  */

30 public class RepositoryFile {
31
32     public static final String JavaDoc[] ROOT_FOLDER_PATH = new String JavaDoc[0];
33
34     private final SVNUrl repositoryUrl;
35     private final SVNRevision revision;
36     private SVNUrl fileUrl;
37     private String JavaDoc[] pathSegments;
38     private String JavaDoc path;
39     private String JavaDoc toString = null;
40     private boolean repositoryRoot;
41
42     private String JavaDoc 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 JavaDoc[] fileUrlSegments = fileUrl.getPathSegments();
60             int fileSegmentsLength = fileUrlSegments.length;
61             int repositorySegmentsLength = repositoryUrl.getPathSegments().length;
62             pathSegments = new String JavaDoc[fileSegmentsLength - repositorySegmentsLength];
63             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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("/"); // NOI18N
69
}
70             }
71             path = sb.toString();
72         }
73     }
74
75     public RepositoryFile(SVNUrl repositoryUrl, String JavaDoc[] pathSegments, SVNRevision revision) throws MalformedURLException JavaDoc {
76         this(repositoryUrl, revision);
77         this.pathSegments = pathSegments;
78         repositoryRoot = pathSegments == null;
79         
80         if(!repositoryRoot) {
81             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
82             for (int i = 0; i < pathSegments.length; i++) {
83                 sb.append(pathSegments[i]);
84                 if(i<pathSegments.length-1) {
85                 sb.append("/"); // NOI18N
86
}
87             }
88             path = sb.toString();
89             fileUrl = repositoryUrl.appendPath(path);
90         }
91     }
92     
93     public RepositoryFile(SVNUrl repositoryUrl, String JavaDoc path, SVNRevision revision) throws MalformedURLException JavaDoc {
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("/"); // NOI18N
101
}
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 JavaDoc[] getPathSegments() {
120         if(isRepositoryRoot()) {
121             return ROOT_FOLDER_PATH;
122         }
123         return pathSegments;
124     }
125
126     public String JavaDoc getPath() {
127         if(isRepositoryRoot()) {
128             return ""; // NOI18N
129
}
130         return path;
131     }
132     
133     public boolean isRepositoryRoot() {
134         return repositoryRoot;
135     }
136
137     public String JavaDoc getName() {
138         if(name == null) {
139             if(isRepositoryRoot()) {
140                 String JavaDoc url = getRepositoryUrl().toString();
141                 int idx = url.indexOf("://"); // NOI18N
142
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 JavaDoc toString() {
154         if(toString == null) {
155             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
156             sb.append(fileUrl);
157             sb.append("@"); // NOI18N
158
sb.append(revision);
159             toString = sb.toString();
160         }
161         
162         return toString;
163     }
164
165     public RepositoryFile appendPath(String JavaDoc path) {
166         return new RepositoryFile(repositoryUrl, getFileUrl().appendPath(path), revision);
167     }
168
169     public RepositoryFile replaceLastSegment(String JavaDoc segment, int level) {
170         assert segment != null && !segment.equals(""); // NOI18N
171
assert level > -1 && level < fileUrl.getPathSegments().length;
172         assert !isRepositoryRoot(); // can't do this
173

174         String JavaDoc 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 JavaDoc ex) {
189             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); // should not happen
190
}
191         return new RepositoryFile(repositoryUrl, newUrl, revision);
192     }
193
194     public RepositoryFile removeLastSegment() {
195         assert !isRepositoryRoot(); // can't do this
196

197         String JavaDoc 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 JavaDoc ex) {
206             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); // should not happen
207
}
208         return new RepositoryFile(repositoryUrl, newUrl, revision);
209     }
210     
211 }
212
Popular Tags