comparison test/org/tmatesoft/hg/test/StatusOutputParser.java @ 66:52dc3f4cfc76

Primitive test suite in org.tmatesoft
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Fri, 21 Jan 2011 18:20:05 +0100
parents test/com/tmate/hgkit/StatusOutputParser.java@fac8e7fcc8b0
children e62f9638fb27
comparison
equal deleted inserted replaced
65:e21df6259f83 66:52dc3f4cfc76
1 /*
2 * Copyright (c) 2011 TMate Software Ltd
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * For information on how to redistribute this software under
14 * the terms of a license other than GNU General Public License
15 * contact TMate Software at support@svnkit.com
16 */
17 package org.tmatesoft.hg.test;
18
19 import java.io.File;
20 import java.util.Collections;
21 import java.util.LinkedList;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.TreeMap;
25 import java.util.regex.Matcher;
26 import java.util.regex.Pattern;
27
28 /**
29 *
30 * @author Artem Tikhomirov
31 * @author TMate Software Ltd.
32 */
33 public class StatusOutputParser implements OutputParser {
34
35 private final Pattern pattern;
36 private List<String> modified, added, removed, clean, missing, unknown, ignored;
37 private Map<String, String> copied;
38 private final boolean winPathSeparator;
39
40 public StatusOutputParser() {
41 // pattern = Pattern.compile("^([MAR?IC! ]) ([\\w \\.-/\\\\]+)$", Pattern.MULTILINE);
42 pattern = Pattern.compile("^([MAR?IC! ]) (.+)$", Pattern.MULTILINE);
43 winPathSeparator = File.separatorChar == '\\';
44 }
45
46 public void reset() {
47 modified = added = removed = clean = missing = unknown = ignored = null;
48 copied = null;
49 }
50
51 public void parse(CharSequence seq) {
52 Matcher m = pattern.matcher(seq);
53 while (m.find()) {
54 String fname = m.group(2);
55 switch ((int) m.group(1).charAt(0)) {
56 case (int) 'M' : {
57 modified = doAdd(modified, fname);
58 break;
59 }
60 case (int) 'A' : {
61 added = doAdd(added, fname);
62 break;
63 }
64 case (int) 'R' : {
65 removed = doAdd(removed, fname);
66 break;
67 }
68 case (int) '?' : {
69 unknown = doAdd(unknown, fname);
70 break;
71 }
72 case (int) 'I' : {
73 ignored = doAdd(ignored, fname);
74 break;
75 }
76 case (int) 'C' : {
77 clean = doAdd(clean, fname);
78 break;
79 }
80 case (int) '!' : {
81 missing = doAdd(missing, fname);
82 break;
83 }
84 case (int) ' ' : {
85 if (copied == null) {
86 copied = new TreeMap<String, String>();
87 }
88 // last added is copy destination
89 // to get or to remove it - depends on what StatusCollector does in this case
90 copied.put(fname, added.get(added.size() - 1));
91 break;
92 }
93 }
94 }
95 }
96
97 //
98 public List<String> getModified() {
99 return proper(modified);
100 }
101
102 public List<String> getAdded() {
103 return proper(added);
104 }
105
106 public List<String> getRemoved() {
107 return proper(removed);
108 }
109
110 public Map<String,String> getCopied() {
111 if (copied == null) {
112 return Collections.emptyMap();
113 }
114 return Collections.unmodifiableMap(copied);
115 }
116
117 public List<String> getClean() {
118 return proper(clean);
119 }
120
121 public List<String> getMissing() {
122 return proper(missing);
123 }
124
125 public List<String> getUnknown() {
126 return proper(unknown);
127 }
128
129 public List<String> getIgnored() {
130 return proper(ignored);
131 }
132
133 private List<String> proper(List<String> l) {
134 if (l == null) {
135 return Collections.emptyList();
136 }
137 return Collections.unmodifiableList(l);
138 }
139
140 private List<String> doAdd(List<String> l, String s) {
141 if (l == null) {
142 l = new LinkedList<String>();
143 }
144 if (winPathSeparator) {
145 // Java impl always give slashed path, while Hg uses local, os-specific convention
146 s = s.replace('\\', '/');
147 }
148 l.add(s);
149 return l;
150 }
151 }