1 11 package org.eclipse.team.internal.ccvs.core.client.listeners; 12 13 import java.util.ArrayList ; 14 import java.util.HashMap ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 import java.util.Map ; 18 19 import java.util.regex.Matcher ; 20 import java.util.regex.Pattern ; 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 31 public class ServerMessageLineMatcher { 32 33 protected static final Pattern VARIABLE_MATCHING_PATTERN = Pattern.compile("(\\((\\w*):.*:\\2\\))"); 35 Pattern pattern; 36 String [] variables; 37 38 public ServerMessageLineMatcher(String template, String [] expectedVariables) throws CVSException { 39 Matcher matcher = VARIABLE_MATCHING_PATTERN.matcher(template); 41 List variables = new ArrayList (); 42 while (matcher.find()) { 43 if (matcher.groupCount() != 2) { 44 IStatus status = new CVSStatus(IStatus.ERROR,NLS.bind(CVSMessages.ServerMessageLineMatcher_5, new String [] { template })); 45 throw new CVSException(status); 46 } 47 variables.add(matcher.group(2)); 48 } 49 ensureMatches(template, variables, expectedVariables); 50 this.variables = (String []) variables.toArray(new String [variables.size()]); 51 52 for (Iterator iter = variables.iterator(); iter.hasNext();) { 55 String element = (String ) iter.next(); 56 template = template.replaceAll(element + ":", ""); template = template.replaceAll(":" + element, ""); } 59 60 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 [] { template })); 68 throw new CVSException(status); 69 } 70 71 this.pattern = Pattern.compile(template); 73 } 74 75 78 private void ensureMatches(String template, List variables, String [] expectedVariables) throws CVSException { 79 for (int i = 0; i < expectedVariables.length; i++) { 80 String expected = expectedVariables[i]; 81 if (!variables.contains(expected)) { 82 IStatus status = new CVSStatus(IStatus.ERROR,NLS.bind(CVSMessages.ServerMessageLineMatcher_7, new String [] { expected, template })); 83 throw new CVSException(status); 84 } 85 } 86 } 87 88 public Map processServerMessage(String line) { 89 Matcher matcher = pattern.matcher(line); 90 if (!matcher.find()) return null; 91 Assert.isTrue(matcher.groupCount() == variables.length); 92 Map result = new HashMap (); 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 |