KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Map JavaDoc;
18
19 import java.util.regex.Matcher JavaDoc;
20 import java.util.regex.Pattern JavaDoc;
21
22 import org.eclipse.core.runtime.Assert;
23 import org.eclipse.core.runtime.IStatus;
24 import org.eclipse.osgi.util.NLS;
25 import org.eclipse.team.internal.ccvs.core.*;
26
27 /**
28  * This class extracts matches server lines to expected patterns and extracts
29  * required information from the line.
30  */

31 public class ServerMessageLineMatcher {
32
33     protected static final Pattern JavaDoc VARIABLE_MATCHING_PATTERN = Pattern.compile("(\\((\\w*):.*:\\2\\))"); //$NON-NLS-1$
34

35     Pattern JavaDoc pattern;
36     String JavaDoc[] variables;
37
38     public ServerMessageLineMatcher(String JavaDoc template, String JavaDoc[] expectedVariables) throws CVSException {
39         // Extract the variable names from the template
40
Matcher JavaDoc matcher = VARIABLE_MATCHING_PATTERN.matcher(template);
41         List JavaDoc variables = new ArrayList JavaDoc();
42         while (matcher.find()) {
43             if (matcher.groupCount() != 2) {
44                 IStatus status = new CVSStatus(IStatus.ERROR,NLS.bind(CVSMessages.ServerMessageLineMatcher_5, new String JavaDoc[] { template }));
45                 throw new CVSException(status);
46             }
47             variables.add(matcher.group(2));
48         }
49         ensureMatches(template, variables, expectedVariables);
50         this.variables = (String JavaDoc[]) variables.toArray(new String JavaDoc[variables.size()]);
51
52         // Modify the template so it can be used to match message lines from the server
53
// (i.e. remove the variable markup)
54
for (Iterator JavaDoc iter = variables.iterator(); iter.hasNext();) {
55                 String JavaDoc element = (String JavaDoc) iter.next();
56                 template = template.replaceAll(element + ":", ""); //$NON-NLS-1$ //$NON-NLS-2$
57
template = template.replaceAll(":" + element, ""); //$NON-NLS-1$ //$NON-NLS-2$
58
}
59
60         // Ensure that the number of groups in the pattern match the number of variables
61
int count = 0;
62         int start = -1;
63         while ((start = template.indexOf('(', start + 1)) != -1) {
64             count++;
65         }
66         if (count != variables.size()) {
67             IStatus status = new CVSStatus(IStatus.ERROR,NLS.bind(CVSMessages.ServerMessageLineMatcher_6, new String JavaDoc[] { template }));
68             throw new CVSException(status);
69         }
70
71         // Create the pattern fir matching lines from the server
72
this.pattern = Pattern.compile(template);
73     }
74
75     /*
76      * Throw an exception if the found variables do not match the expected variables
77      */

78     private void ensureMatches(String JavaDoc template, List JavaDoc variables, String JavaDoc[] expectedVariables) throws CVSException {
79         for (int i = 0; i < expectedVariables.length; i++) {
80             String JavaDoc expected = expectedVariables[i];
81             if (!variables.contains(expected)) {
82                 IStatus status = new CVSStatus(IStatus.ERROR,NLS.bind(CVSMessages.ServerMessageLineMatcher_7, new String JavaDoc[] { expected, template }));
83                 throw new CVSException(status);
84             }
85         }
86     }
87
88     public Map JavaDoc processServerMessage(String JavaDoc line) {
89         Matcher JavaDoc matcher = pattern.matcher(line);
90         if (!matcher.find()) return null;
91         Assert.isTrue(matcher.groupCount() == variables.length);
92         Map JavaDoc result = new HashMap JavaDoc();
93         for (int i = 1; i <= matcher.groupCount(); i++) {
94             result.put(variables[i - 1], matcher.group(i));
95         }
96         return result;
97     }
98
99 }
100
Popular Tags