comparison test/com/tmate/hgkit/StatusOutputParser.java @ 61:fac8e7fcc8b0

Simple test framework - capable of parsing Hg cmdline output to compare with Java result
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Tue, 18 Jan 2011 18:32:49 +0100
parents
children
comparison
equal deleted inserted replaced
60:613c936d74e4 61:fac8e7fcc8b0
1 /*
2 * Copyright (c) 2011 Artem Tikhomirov
3 */
4 package com.tmate.hgkit;
5
6 import java.io.File;
7 import java.util.Collections;
8 import java.util.LinkedList;
9 import java.util.List;
10 import java.util.Map;
11 import java.util.TreeMap;
12 import java.util.regex.Matcher;
13 import java.util.regex.Pattern;
14
15 /**
16 *
17 * @author artem
18 */
19 public class StatusOutputParser implements OutputParser {
20
21 private final Pattern pattern;
22 private List<String> modified, added, removed, clean, missing, unknown, ignored;
23 private Map<String, String> copied;
24 private final boolean winPathSeparator;
25
26 public StatusOutputParser() {
27 // pattern = Pattern.compile("^([MAR?IC! ]) ([\\w \\.-/\\\\]+)$", Pattern.MULTILINE);
28 pattern = Pattern.compile("^([MAR?IC! ]) (.+)$", Pattern.MULTILINE);
29 winPathSeparator = File.separatorChar == '\\';
30 }
31
32 public void reset() {
33 modified = added = removed = clean = missing = unknown = ignored = null;
34 copied = null;
35 }
36
37 public void parse(CharSequence seq) {
38 Matcher m = pattern.matcher(seq);
39 while (m.find()) {
40 String fname = m.group(2);
41 switch ((int) m.group(1).charAt(0)) {
42 case (int) 'M' : {
43 modified = doAdd(modified, fname);
44 break;
45 }
46 case (int) 'A' : {
47 added = doAdd(added, fname);
48 break;
49 }
50 case (int) 'R' : {
51 removed = doAdd(removed, fname);
52 break;
53 }
54 case (int) '?' : {
55 unknown = doAdd(unknown, fname);
56 break;
57 }
58 case (int) 'I' : {
59 ignored = doAdd(ignored, fname);
60 break;
61 }
62 case (int) 'C' : {
63 clean = doAdd(clean, fname);
64 break;
65 }
66 case (int) '!' : {
67 missing = doAdd(missing, fname);
68 break;
69 }
70 case (int) ' ' : {
71 if (copied == null) {
72 copied = new TreeMap<String, String>();
73 }
74 // last added is copy destination
75 // to get or to remove it - depends on what StatusCollector does in this case
76 copied.put(fname, added.get(added.size() - 1));
77 break;
78 }
79 }
80 }
81 }
82
83 //
84 public List<String> getModified() {
85 return proper(modified);
86 }
87
88 public List<String> getAdded() {
89 return proper(added);
90 }
91
92 public List<String> getRemoved() {
93 return proper(removed);
94 }
95
96 public Map<String,String> getCopied() {
97 if (copied == null) {
98 return Collections.emptyMap();
99 }
100 return Collections.unmodifiableMap(copied);
101 }
102
103 public List<String> getClean() {
104 return proper(clean);
105 }
106
107 public List<String> getMissing() {
108 return proper(missing);
109 }
110
111 public List<String> getUnknown() {
112 return proper(unknown);
113 }
114
115 public List<String> getIgnored() {
116 return proper(ignored);
117 }
118
119 private List<String> proper(List<String> l) {
120 if (l == null) {
121 return Collections.emptyList();
122 }
123 return Collections.unmodifiableList(l);
124 }
125
126 private List<String> doAdd(List<String> l, String s) {
127 if (l == null) {
128 l = new LinkedList<String>();
129 }
130 if (winPathSeparator) {
131 // Java impl always give slashed path, while Hg uses local, os-specific convention
132 s = s.replace('\\', '/');
133 }
134 l.add(s);
135 return l;
136 }
137 }