001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.subversion;
042:
043: import java.net.MalformedURLException;
044: import java.util.logging.Level;
045: import org.tigris.subversion.svnclientadapter.SVNRevision;
046: import org.tigris.subversion.svnclientadapter.SVNUrl;
047:
048: /**
049: *
050: * @author Tomas Stupka
051: */
052: public class RepositoryFile {
053:
054: public static final String[] ROOT_FOLDER_PATH = new String[0];
055:
056: private final SVNUrl repositoryUrl;
057: private final SVNRevision revision;
058: private SVNUrl fileUrl;
059: private String[] pathSegments;
060: private String path;
061: private String toString = null;
062: private boolean repositoryRoot;
063:
064: private String name;
065:
066: public RepositoryFile(SVNUrl repositoryUrl, SVNRevision revision) {
067: assert repositoryUrl != null;
068: assert revision != null;
069:
070: this .repositoryUrl = repositoryUrl;
071: this .revision = revision;
072: repositoryRoot = true;
073: }
074:
075: public RepositoryFile(SVNUrl repositoryUrl, SVNUrl fileUrl,
076: SVNRevision revision) {
077: this (repositoryUrl, revision);
078: this .fileUrl = fileUrl;
079: repositoryRoot = fileUrl == null;
080:
081: if (!repositoryRoot) {
082: String[] fileUrlSegments = fileUrl.getPathSegments();
083: int fileSegmentsLength = fileUrlSegments.length;
084: int repositorySegmentsLength = repositoryUrl
085: .getPathSegments().length;
086: pathSegments = new String[fileSegmentsLength
087: - repositorySegmentsLength];
088: StringBuffer sb = new StringBuffer();
089: for (int i = repositorySegmentsLength; i < fileSegmentsLength; i++) {
090: pathSegments[i - repositorySegmentsLength] = fileUrlSegments[i];
091: sb.append(fileUrlSegments[i]);
092: if (i - repositorySegmentsLength < pathSegments.length - 1) {
093: sb.append("/"); // NOI18N
094: }
095: }
096: path = sb.toString();
097: }
098: }
099:
100: public RepositoryFile(SVNUrl repositoryUrl, String[] pathSegments,
101: SVNRevision revision) throws MalformedURLException {
102: this (repositoryUrl, revision);
103: this .pathSegments = pathSegments;
104: repositoryRoot = pathSegments == null;
105:
106: if (!repositoryRoot) {
107: StringBuffer sb = new StringBuffer();
108: for (int i = 0; i < pathSegments.length; i++) {
109: sb.append(pathSegments[i]);
110: if (i < pathSegments.length - 1) {
111: sb.append("/"); // NOI18N
112: }
113: }
114: path = sb.toString();
115: fileUrl = repositoryUrl.appendPath(path);
116: }
117: }
118:
119: public RepositoryFile(SVNUrl repositoryUrl, String path,
120: SVNRevision revision) throws MalformedURLException {
121: this (repositoryUrl, revision);
122: this .path = path;
123: repositoryRoot = path == null;
124:
125: if (!repositoryRoot) {
126: fileUrl = repositoryUrl.appendPath(path);
127: pathSegments = path.split("/"); // NOI18N
128: }
129: }
130:
131: public SVNUrl getRepositoryUrl() {
132: return repositoryUrl;
133: }
134:
135: public SVNRevision getRevision() {
136: return revision;
137: }
138:
139: public SVNUrl getFileUrl() {
140: if (isRepositoryRoot()) {
141: return getRepositoryUrl();
142: }
143: return fileUrl;
144: }
145:
146: public String[] getPathSegments() {
147: if (isRepositoryRoot()) {
148: return ROOT_FOLDER_PATH;
149: }
150: return pathSegments;
151: }
152:
153: public String getPath() {
154: if (isRepositoryRoot()) {
155: return ""; // NOI18N
156: }
157: return path;
158: }
159:
160: public boolean isRepositoryRoot() {
161: return repositoryRoot;
162: }
163:
164: public String getName() {
165: if (name == null) {
166: if (isRepositoryRoot()) {
167: String url = getRepositoryUrl().toString();
168: int idx = url.indexOf("://"); // NOI18N
169: if (idx >= 0) {
170: url = url.substring(idx + 3);
171: }
172: return url;
173: } else {
174: return getFileUrl().getLastPathSegment();
175: }
176: }
177: return name;
178: }
179:
180: public String toString() {
181: if (toString == null) {
182: StringBuffer sb = new StringBuffer();
183: sb.append(fileUrl);
184: sb.append("@"); // NOI18N
185: sb.append(revision);
186: toString = sb.toString();
187: }
188:
189: return toString;
190: }
191:
192: public RepositoryFile appendPath(String path) {
193: return new RepositoryFile(repositoryUrl, getFileUrl()
194: .appendPath(path), revision);
195: }
196:
197: public RepositoryFile replaceLastSegment(String segment, int level) {
198: assert segment != null && !segment.equals(""); // NOI18N
199: assert level > -1 && level < fileUrl.getPathSegments().length;
200: assert !isRepositoryRoot(); // can't do this
201:
202: String fileUrlString = fileUrl.toString();
203: int fromIdx = fileUrlString.lastIndexOf('/');
204: int toIdx = fileUrlString.length() - 1;
205: for (int i = 0; i < level; i++) {
206: toIdx = fromIdx - 1;
207: fromIdx = fileUrlString.lastIndexOf('/', fromIdx - 1);
208: }
209:
210: assert !(fromIdx < repositoryUrl.toString().length());
211: assert toIdx >= fromIdx && toIdx < fileUrlString.length();
212:
213: SVNUrl newUrl = null;
214: try {
215: newUrl = new SVNUrl(fileUrlString.substring(0, fromIdx + 1)
216: + segment + fileUrlString.substring(toIdx + 1));
217: } catch (MalformedURLException ex) {
218: Subversion.LOG.log(Level.INFO, ex.getMessage(), ex); // should not happen
219: }
220: return new RepositoryFile(repositoryUrl, newUrl, revision);
221: }
222:
223: public RepositoryFile removeLastSegment() {
224: assert !isRepositoryRoot(); // can't do this
225:
226: String fileUrlStrint = fileUrl.toString();
227: int idx = fileUrlStrint.lastIndexOf('/');
228:
229: assert !(idx < repositoryUrl.toString().length());
230:
231: SVNUrl newUrl = null;
232: try {
233: newUrl = new SVNUrl(fileUrlStrint.substring(0, idx));
234: } catch (MalformedURLException ex) {
235: Subversion.LOG.log(Level.INFO, ex.getMessage(), ex); // should not happen
236: }
237: return new RepositoryFile(repositoryUrl, newUrl, revision);
238: }
239:
240: }
|