KickJava   Java API By Example, From Geeks To Geeks.

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


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;
12
13 import org.eclipse.core.runtime.IProgressMonitor;
14 import org.eclipse.team.internal.ccvs.core.CVSException;
15
16 public class MTHandler extends ResponseHandler {
17
18     private String JavaDoc nextLine;
19     private boolean isLineAvailable;
20     
21     /* (non-Javadoc)
22      * @see org.eclipse.team.internal.ccvs.core.client.ResponseHandler#getInstance()
23      */

24     ResponseHandler getInstance() {
25         return new MTHandler();
26     }
27
28     /**
29      * @see ResponseHandler#getResponseID()
30      */

31     public String JavaDoc getResponseID() {
32         return "MT"; //$NON-NLS-1$
33
}
34
35     /**
36      * @see ResponseHandler#handle(Session, String, IProgressMonitor)
37      */

38     public void handle(Session session, String JavaDoc argument, IProgressMonitor monitor)
39         throws CVSException {
40         
41         // If there was a line available from before, clear it
42
if (isLineAvailable()) {
43             startNextLine();
44         }
45         
46         if (argument.charAt(0) == '+') {
47             // Reset any previously accumulated text
48
startNextLine();
49         } else if (argument.charAt(0) == '-') {
50             // Mark the line as available in case there was no trailing newline
51
if (nextLine != null) {
52                 isLineAvailable = true;
53             }
54         } else {
55             // Extract the tag and text from the line
56
String JavaDoc tag;
57             String JavaDoc text;
58             int spaceIndex = argument.indexOf(' ');
59             if (spaceIndex == -1) {
60                 tag = argument;
61                 text = null;
62             } else {
63                 tag = argument.substring(0, spaceIndex);
64                 text = argument.substring(spaceIndex + 1);
65             }
66             
67             // Accumulate the line and indicate if its available for use
68
if (tag.equals("newline")) { //$NON-NLS-1$
69
isLineAvailable = true;
70             } else if (text != null) {
71                 // Reset the previous line if required
72
if (isLineAvailable()) {
73                     startNextLine();
74                 }
75                 // Accumulate the line
76
if (nextLine == null) {
77                     nextLine = text;
78                 } else {
79                     // The text from the sevrver contains spaces when appropriate so just append
80
nextLine = nextLine + text;
81                 }
82             }
83         }
84     }
85     
86     /**
87      * Check if there is a line available. If there is, it should be fetched with
88      * getLine() immediatly before the next MT response is processed.
89      */

90     public boolean isLineAvailable() {
91         return isLineAvailable;
92     }
93     
94     /**
95      * Get the available line. This purges the line from the handler
96      */

97     public String JavaDoc getLine() {
98         return nextLine;
99     }
100     
101     private void startNextLine() {
102         isLineAvailable = false;
103         nextLine = null;
104     }
105 }
106
Popular Tags