Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: dd65a9b2812378bd49ecc065aac3746b540c6141 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*******************************************************************************
 * Copyright (c) 2000, 2006 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.team.internal.ccvs.core.client;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.team.internal.ccvs.core.CVSException;

public class MTHandler extends ResponseHandler {

	private String nextLine;
	private boolean isLineAvailable;
	
	/* (non-Javadoc)
	 * @see org.eclipse.team.internal.ccvs.core.client.ResponseHandler#getInstance()
	 */
	ResponseHandler getInstance() {
		return new MTHandler();
	}

	/**
	 * @see ResponseHandler#getResponseID()
	 */
	public String getResponseID() {
		return "MT"; //$NON-NLS-1$
	}

	/**
	 * @see ResponseHandler#handle(Session, String, IProgressMonitor)
	 */
	public void handle(Session session, String argument, IProgressMonitor monitor)
		throws CVSException {
		
		// If there was a line available from before, clear it
		if (isLineAvailable()) {
			startNextLine();
		}
		
		if (argument.charAt(0) == '+') {
			// Reset any previously accumulated text
			startNextLine();
		} else if (argument.charAt(0) == '-') {
			// Mark the line as available in case there was no trailing newline
			if (nextLine != null) {
				isLineAvailable = true;
			}
		} else {
			// Extract the tag and text from the line
			String tag;
			String text;
			int spaceIndex = argument.indexOf(' ');
			if (spaceIndex == -1) {
				tag = argument;
				text = null;
			} else {
				tag = argument.substring(0, spaceIndex);
				text = argument.substring(spaceIndex + 1);
			}
			
			// Accumulate the line and indicate if its available for use
			if (tag.equals("newline")) { //$NON-NLS-1$
				isLineAvailable = true;
			} else if (text != null) {
				// Reset the previous line if required
				if (isLineAvailable()) {
					startNextLine();
				}
				// Accumulate the line
				if (nextLine == null) {
					nextLine = text;
				} else {
					// The text from the sevrver contains spaces when appropriate so just append
					nextLine = nextLine + text;
				}
			}
		}
	}
	
	/**
	 * Check if there is a line available. If there is, it should be fetched with
	 * getLine() immediatly before the next MT response is processed.
	 */
	public boolean isLineAvailable() {
		return isLineAvailable;
	}
	
	/**
	 * Get the available line. This purges the line from the handler
	 */
	public String getLine() {
		return nextLine;
	}
	
	private void startNextLine() {
		isLineAvailable = false;
		nextLine = null;
	}
}

Back to the top