KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > core > client > listeners > AdminKSubstListener


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.team.internal.ccvs.core.client.listeners;
12
13
14 import org.eclipse.core.runtime.*;
15 import org.eclipse.osgi.util.NLS;
16 import org.eclipse.team.internal.ccvs.core.*;
17 import org.eclipse.team.internal.ccvs.core.client.CommandOutputListener;
18 import org.eclipse.team.internal.ccvs.core.client.Command.KSubstOption;
19 import org.eclipse.team.internal.ccvs.core.syncinfo.FolderSyncInfo;
20 import org.eclipse.team.internal.ccvs.core.syncinfo.ResourceSyncInfo;
21 import org.eclipse.team.internal.ccvs.core.util.Util;
22
23 /**
24  * Used with 'admin -ksubst' to capture lines of text that are issued
25  * as confirmation that the remote keyword substitution mode has been
26  * changed. When encountered, updates the local ResourceSyncInfo for
27  * the file in question to reflect
28  *
29  * e.g.
30  * RCS file: path/filename,v
31  * done
32  *
33  * We don't expect to see anything special on stderr if the command succeeds.
34  */

35 public class AdminKSubstListener extends CommandOutputListener {
36     private KSubstOption ksubstMode;
37     
38     public AdminKSubstListener(KSubstOption ksubstMode) {
39         this.ksubstMode = ksubstMode;
40     }
41     
42     public IStatus messageLine(String JavaDoc line, ICVSRepositoryLocation location, ICVSFolder commandRoot,
43         IProgressMonitor monitor) {
44         if (line.startsWith("RCS file:")) { //$NON-NLS-1$
45
String JavaDoc rcsFile = line.substring(10).trim();
46             if (! rcsFile.endsWith(",v")) { //$NON-NLS-1$
47
return new CVSStatus(IStatus.ERROR,
48                     NLS.bind(CVSMessages.AdminKSubstListener_expectedRCSFile, new String JavaDoc[] { rcsFile }));
49             }
50             IPath rcsFilePath = new Path(null, Util.removeAtticSegment(rcsFile.substring(0, rcsFile.length() - 2)));
51             try {
52                 ICVSFile file = findLocalFileFor(commandRoot, rcsFilePath);
53                 //ResourceSyncInfo info = file.getSyncInfo();
54
byte[] syncBytes = file.getSyncBytes();
55                 if (syncBytes != null) {
56                     // only update sync info if we have it locally
57
file.setSyncBytes(ResourceSyncInfo.setKeywordMode(syncBytes, ksubstMode), ICVSFile.UNKNOWN);
58                 }
59             } catch (CVSException e) {
60                 return e.getStatus();
61             }
62         }
63         return OK;
64     }
65     
66     private ICVSFile findLocalFileFor(ICVSFolder commandRoot, IPath rcsFilePath) throws CVSException {
67         
68         // First, look for the local file by following the remote path
69
FolderSyncInfo info = commandRoot.getFolderSyncInfo();
70         String JavaDoc remoteRootLocation = info.getRemoteLocation();
71         if (remoteRootLocation == null) {
72             throw new CVSException(new CVSStatus(IStatus.ERROR,
73                 CVSMessages.AdminKSubstListener_commandRootNotManaged));
74         }
75         IPath remoteRootPath = new Path(null, remoteRootLocation);
76         if (remoteRootPath.isPrefixOf(rcsFilePath)) {
77             IPath relativeFilePath = rcsFilePath.removeFirstSegments(remoteRootPath.segmentCount());
78             ICVSFile file = commandRoot.getFile(relativeFilePath.toString());
79             if (file.isManaged() && isMatchingPath(file, rcsFilePath)) {
80                 return file;
81             }
82         }
83         
84         // We couldn't find the file that way which means we're working in a defined module.
85
// Scan all folders looking for a match
86
ICVSFolder parent = findFolder(commandRoot, rcsFilePath.removeLastSegments(1));
87         if (parent != null) {
88             ICVSFile file = parent.getFile(rcsFilePath.lastSegment());
89             if (file.isManaged()) {
90                 return file;
91             }
92         }
93         
94         // No file was found so return null;
95
throw new CVSException(new CVSStatus(IStatus.ERROR,
96                 NLS.bind(CVSMessages.AdminKSubstListener_expectedChildOfCommandRoot, new String JavaDoc[] { rcsFilePath.toString(), remoteRootPath.toString() })));
97     }
98
99     private ICVSFolder findFolder(ICVSFolder commandRoot, IPath path) throws CVSException {
100         final String JavaDoc remotePath = path.toString();
101         final ICVSFolder[] result = new ICVSFolder[] { null };
102         commandRoot.accept(new ICVSResourceVisitor() {
103             public void visitFile(ICVSFile file) throws CVSException {
104                 // Nothing to do for files
105
}
106             public void visitFolder(ICVSFolder folder) throws CVSException {
107                 FolderSyncInfo info = folder.getFolderSyncInfo();
108                 if (info != null && info.getRemoteLocation().equals(remotePath)) {
109                     // We found the folder we're looking for
110
result[0] = folder;
111                 }
112                 if (result[0] == null) {
113                     folder.acceptChildren(this);
114                 }
115             }
116         });
117         return result[0];
118     }
119
120     private boolean isMatchingPath(ICVSFile file, IPath rcsFilePath) throws CVSException {
121         FolderSyncInfo info = file.getParent().getFolderSyncInfo();
122         return info != null
123            && info.getRemoteLocation().equals(rcsFilePath.removeLastSegments(1).toString());
124     }
125 }
126
Popular Tags