KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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 import java.io.*;
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.core.runtime.IStatus;
19 import org.eclipse.team.internal.ccvs.core.*;
20 import org.eclipse.team.internal.ccvs.core.client.CommandOutputListener;
21
22 public class AnnotateListener extends CommandOutputListener {
23
24 /**
25  * Handle output from the CVS Annotate command.
26  */

27     ByteArrayOutputStream aStream = new ByteArrayOutputStream();
28     List JavaDoc blocks = new ArrayList JavaDoc();
29     int lineNumber;
30     
31     public IStatus messageLine(String JavaDoc line, ICVSRepositoryLocation location, ICVSFolder commandRoot, IProgressMonitor monitor) {
32         String JavaDoc error = null;
33         CVSAnnotateBlock aBlock = new CVSAnnotateBlock(line, lineNumber++);
34         if (!aBlock.isValid()) {
35             error = line;
36         }
37         
38         /**
39          * Make sure all lines have a line terminator.
40          */

41         try {
42             aStream.write(line.substring(aBlock.getSourceOffset()).getBytes());
43             if (!(line.endsWith("\r") || line.endsWith("\r\n"))) { //$NON-NLS-1$ //$NON-NLS-2$
44
aStream.write(System.getProperty("line.separator").getBytes()); //$NON-NLS-1$
45
}
46         } catch (IOException e) {
47         }
48         add(aBlock);
49         if (error != null)
50             return new CVSStatus(IStatus.ERROR, CVSStatus.ERROR_LINE_PARSE_FAILURE, error, commandRoot);
51         return OK;
52     }
53     
54     public InputStream getContents() {
55         return new ByteArrayInputStream(aStream.toByteArray());
56     }
57     
58     public List JavaDoc getCvsAnnotateBlocks() {
59         return blocks;
60     }
61     /**
62      * Add an annotate block to the receiver merging this block with the
63      * previous block if it is part of the same change.
64      * @param aBlock
65      */

66     private void add(CVSAnnotateBlock aBlock) {
67         
68         int size = blocks.size();
69         if (size == 0) {
70             blocks.add(aBlock);
71         } else {
72             CVSAnnotateBlock lastBlock = (CVSAnnotateBlock) blocks.get(size - 1);
73             if (lastBlock.getRevision().equals(aBlock.getRevision())) {
74                 lastBlock.setEndLine(aBlock.getStartLine());
75             } else {
76                 blocks.add(aBlock);
77             }
78         }
79     }
80
81     /* (non-Javadoc)
82      * @see org.eclipse.team.internal.ccvs.core.client.listeners.ICommandOutputListener#errorLine(java.lang.String, org.eclipse.team.internal.ccvs.core.ICVSRepositoryLocation, org.eclipse.team.internal.ccvs.core.ICVSFolder, org.eclipse.core.runtime.IProgressMonitor)
83      */

84     public IStatus errorLine(String JavaDoc line, ICVSRepositoryLocation location, ICVSFolder commandRoot, IProgressMonitor monitor) {
85         if(line.startsWith(CVSMessages.AnnotateListener_3)) {
86             String JavaDoc error = CVSMessages.AnnotateListener_4;
87             return new CVSStatus(IStatus.ERROR, CVSStatus.SERVER_ERROR, error, commandRoot);
88         }
89         return super.errorLine(line, location, commandRoot, monitor);
90     }
91
92     /**
93      * Set the contents of the listener to the provided contents.
94      * This is done if the contents fetched by the annotate command
95      * has a charater set that may have been mangled by the transfer
96      * @param remoteContents the actual contens of the file
97      */

98     public void setContents(InputStream remoteContents) {
99         try {
100             ByteArrayOutputStream stream = new ByteArrayOutputStream();
101             byte[] buffer = new byte[1024];
102             int n = remoteContents.read(buffer);
103             while (n != -1) {
104                 stream.write(buffer, 0, n);
105                 n = remoteContents.read(buffer);
106             }
107             aStream = stream;
108         } catch (IOException e) {
109             // Log and continue
110
CVSProviderPlugin.log(CVSException.wrapException(e));
111         }
112     }
113 }
114
Popular Tags