KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-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 import java.util.Map JavaDoc;
14
15 import org.eclipse.core.runtime.IProgressMonitor;
16 import org.eclipse.core.runtime.IStatus;
17 import org.eclipse.team.internal.ccvs.core.CVSException;
18 import org.eclipse.team.internal.ccvs.core.CVSProviderPlugin;
19 import org.eclipse.team.internal.ccvs.core.CVSStatus;
20 import org.eclipse.team.internal.ccvs.core.ICVSFolder;
21 import org.eclipse.team.internal.ccvs.core.ICVSRepositoryLocation;
22 import org.eclipse.team.internal.ccvs.core.client.CommandOutputListener;
23 import org.eclipse.team.internal.ccvs.core.Policy;
24
25 /**
26  * This class interprets the output of "cvs diff --brief ..." in order to get the revisions
27  * of two compared versions of a project or folder.
28  */

29 public class CompareDiffListener extends CommandOutputListener {
30     
31     private static ServerMessageLineMatcher LOCAL_FILE_MATCHER;
32     private static ServerMessageLineMatcher REMOTE_FILE_MATCHER;
33     private static ServerMessageLineMatcher REVISION_LINE_MATCHER;
34     
35     static {
36         try {
37             LOCAL_FILE_MATCHER = new ServerMessageLineMatcher(
38                 "Index: (localFile:.*:localFile)", new String JavaDoc[] {"localFile"}); //$NON-NLS-1$ //$NON-NLS-2$
39
REMOTE_FILE_MATCHER = new ServerMessageLineMatcher(
40                 "RCS file: (remoteFile:.*:remoteFile),v", new String JavaDoc[] {"remoteFile"}); //$NON-NLS-1$ //$NON-NLS-2$
41
REVISION_LINE_MATCHER = new ServerMessageLineMatcher(
42                 "diff .* -r(leftRevision:.*:leftRevision) -r(rightRevision:.*:rightRevision)", new String JavaDoc[] {"leftRevision", "rightRevision"}); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
43
} catch (CVSException e) {
44             // This is serious as the listener will not function properly
45
CVSProviderPlugin.log(e);
46             LOCAL_FILE_MATCHER = null;
47             REMOTE_FILE_MATCHER = null;
48             REVISION_LINE_MATCHER = null;
49         }
50     }
51     
52     private String JavaDoc localFilePath, remoteFilePath, leftRevision, rightRevision;
53     
54     private IFileDiffListener listener;
55     
56     public interface IFileDiffListener {
57         public void fileDiff(
58                 String JavaDoc localFilePath,
59                 String JavaDoc remoteFilePath,
60                 String JavaDoc leftRevision,
61                 String JavaDoc rightRevision);
62     }
63     
64     public CompareDiffListener(IFileDiffListener listener) {
65         this.listener = listener;
66     }
67     
68     public IStatus messageLine(
69             String JavaDoc line,
70             ICVSRepositoryLocation location,
71             ICVSFolder commandRoot,
72             IProgressMonitor monitor) {
73         // ignore any server messages
74
if (getServerMessage(line, location) != null) {
75             return OK;
76         }
77         Map JavaDoc map = LOCAL_FILE_MATCHER.processServerMessage(line);
78         if (map != null) {
79             localFilePath = (String JavaDoc)map.get("localFile"); //$NON-NLS-1$
80
return OK;
81         }
82         map = REMOTE_FILE_MATCHER.processServerMessage(line);
83         if (map != null) {
84             remoteFilePath = (String JavaDoc)map.get("remoteFile"); //$NON-NLS-1$
85
return OK;
86         }
87         map = REVISION_LINE_MATCHER.processServerMessage(line);
88         if (map != null) {
89             leftRevision = (String JavaDoc)map.get("leftRevision"); //$NON-NLS-1$
90
rightRevision = (String JavaDoc)map.get("rightRevision"); //$NON-NLS-1$
91
if (localFilePath == null || remoteFilePath == null) {
92                 return new CVSStatus(IStatus.ERROR, Policy.bind("CompareDiffListener.11")); //$NON-NLS-1$
93
}
94             listener.fileDiff(localFilePath, remoteFilePath, leftRevision, rightRevision);
95             localFilePath = remoteFilePath = leftRevision = rightRevision = null;
96             return OK;
97         }
98         // Ignore all other lines
99
return OK;
100     }
101
102     private IStatus handleUnknownDiffFormat(String JavaDoc line) {
103         return new CVSStatus(IStatus.ERROR, Policy.bind("CompareDiffListener.12", line)); //$NON-NLS-1$
104
}
105
106     public IStatus errorLine(
107             String JavaDoc line,
108             ICVSRepositoryLocation location,
109             ICVSFolder commandRoot,
110             IProgressMonitor monitor) {
111         // ignore server messages for now - this is used only with the diff
112
// request and the errors can be safely ignored.
113
if (getServerMessage(line, location) != null) {
114             return OK;
115         }
116         return super.errorLine(line, location, commandRoot, monitor);
117     }
118
119 }
120
Popular Tags