1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.scm.provider.hg.command;
20
21 import java.util.ArrayList;
22 import java.util.HashMap;
23 import java.util.Iterator;
24 import java.util.List;
25 import java.util.Map;
26
27 import org.apache.maven.scm.ScmFileStatus;
28 import org.apache.maven.scm.util.AbstractConsumer;
29
30
31
32
33
34
35
36
37
38
39 public class HgConsumer extends AbstractConsumer {
40
41
42
43
44 private static final Map<String, ScmFileStatus> IDENTIFIERS = new HashMap<>();
45
46
47
48
49 private static final Map<String, String> MESSAGES = new HashMap<>();
50
51
52
53
54
55
56 private static final int MAX_STDERR_SIZE = 10;
57
58
59
60
61 private final List<String> stderr = new ArrayList<>();
62
63 static {
64
65
66 IDENTIFIERS.put("adding", ScmFileStatus.ADDED);
67 IDENTIFIERS.put("unknown", ScmFileStatus.UNKNOWN);
68 IDENTIFIERS.put("modified", ScmFileStatus.MODIFIED);
69 IDENTIFIERS.put("removed", ScmFileStatus.DELETED);
70 IDENTIFIERS.put("renamed", ScmFileStatus.MODIFIED);
71
72
73
74 IDENTIFIERS.put("A", ScmFileStatus.ADDED);
75 IDENTIFIERS.put("?", ScmFileStatus.UNKNOWN);
76 IDENTIFIERS.put("M", ScmFileStatus.MODIFIED);
77 IDENTIFIERS.put("R", ScmFileStatus.DELETED);
78 IDENTIFIERS.put("C", ScmFileStatus.CHECKED_IN);
79 IDENTIFIERS.put("!", ScmFileStatus.MISSING);
80 IDENTIFIERS.put("I", ScmFileStatus.UNKNOWN);
81
82 MESSAGES.put("hg: WARNING:", "WARNING");
83 MESSAGES.put("hg: ERROR:", "ERROR");
84 MESSAGES.put("'hg' ", "ERROR");
85 }
86
87 public void doConsume(ScmFileStatus status, String trimmedLine) {
88
89 }
90
91
92 public void consumeLine(String line) {
93 if (logger.isDebugEnabled()) {
94 logger.debug(line);
95 }
96 String trimmedLine = line.trim();
97
98 String statusStr = processInputForKnownIdentifiers(trimmedLine);
99
100
101 if (statusStr == null) {
102 boolean isMessage = processInputForKnownMessages(trimmedLine);
103
104 if (isMessage) {
105 return;
106 }
107 } else {
108
109 trimmedLine = trimmedLine.substring(statusStr.length());
110 trimmedLine = trimmedLine.trim();
111 }
112
113 ScmFileStatus status = statusStr != null ? (IDENTIFIERS.get(statusStr.intern())) : null;
114 doConsume(status, trimmedLine);
115 }
116
117
118
119
120
121
122
123 public String getStdErr() {
124 StringBuilder str = new StringBuilder();
125 for (Iterator<String> it = stderr.iterator(); it.hasNext(); ) {
126 str.append(it.next());
127 }
128 return str.toString();
129 }
130
131 private static String processInputForKnownIdentifiers(String line) {
132 for (Iterator<String> it = IDENTIFIERS.keySet().iterator(); it.hasNext(); ) {
133 String id = it.next();
134 if (line.startsWith(id)) {
135 return id;
136 }
137 }
138 return null;
139 }
140
141 private boolean processInputForKnownMessages(String line) {
142 for (Iterator<String> it = MESSAGES.keySet().iterator(); it.hasNext(); ) {
143 String prefix = it.next();
144 if (line.startsWith(prefix)) {
145 stderr.add(line);
146 if (stderr.size() > MAX_STDERR_SIZE) {
147 stderr.remove(0);
148 }
149 String message = line.substring(prefix.length());
150 if (MESSAGES.get(prefix).equals("WARNING")) {
151 if (logger.isWarnEnabled()) {
152 logger.warn(message);
153 }
154 } else {
155 if (logger.isErrorEnabled()) {
156 logger.error(message);
157 }
158 }
159 return true;
160 }
161 }
162 return false;
163 }
164 }