comparison src/org/tmatesoft/hg/internal/KeywordFilter.java @ 114:46291ec605a0

Filters to read and initialize according to configuration files
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 03 Feb 2011 22:13:55 +0100
parents 67ae317408c9
children 68ba22a2133a
comparison
equal deleted inserted replaced
113:67ae317408c9 114:46291ec605a0
14 * the terms of a license other than GNU General Public License 14 * the terms of a license other than GNU General Public License
15 * contact TMate Software at support@svnkit.com 15 * contact TMate Software at support@svnkit.com
16 */ 16 */
17 package org.tmatesoft.hg.internal; 17 package org.tmatesoft.hg.internal;
18 18
19 import java.io.File;
20 import java.io.FileInputStream;
21 import java.io.FileOutputStream;
22 import java.nio.ByteBuffer; 19 import java.nio.ByteBuffer;
20 import java.util.ArrayList;
21 import java.util.Map;
23 import java.util.TreeMap; 22 import java.util.TreeMap;
24 23
25 import javax.swing.text.html.Option;
26
27 import org.tmatesoft.hg.core.Path; 24 import org.tmatesoft.hg.core.Path;
25 import org.tmatesoft.hg.repo.Changeset;
28 import org.tmatesoft.hg.repo.HgRepository; 26 import org.tmatesoft.hg.repo.HgRepository;
29 27
30 /** 28 /**
31 * 29 *
32 * @author Artem Tikhomirov 30 * @author Artem Tikhomirov
33 * @author TMate Software Ltd. 31 * @author TMate Software Ltd.
34 */ 32 */
35 public class KeywordFilter implements Filter { 33 public class KeywordFilter implements Filter {
36 // present implementation is stateless, however, filter use pattern shall not assume that. In fact, Factory may us that 34 // present implementation is stateless, however, filter use pattern shall not assume that. In fact, Factory may us that
35 private final HgRepository repo;
37 private final boolean isExpanding; 36 private final boolean isExpanding;
38 private final TreeMap<String,String> keywords; 37 private final TreeMap<String,String> keywords;
39 private final int minBufferLen; 38 private final int minBufferLen;
39 private final Path path;
40 private Changeset latestFileCset;
40 41
41 /** 42 /**
42 * 43 *
44 * @param hgRepo
45 * @param path
43 * @param expand <code>true</code> to expand keywords, <code>false</code> to shrink 46 * @param expand <code>true</code> to expand keywords, <code>false</code> to shrink
44 */ 47 */
45 private KeywordFilter(boolean expand) { 48 private KeywordFilter(HgRepository hgRepo, Path p, boolean expand) {
49 repo = hgRepo;
50 path = p;
46 isExpanding = expand; 51 isExpanding = expand;
47 keywords = new TreeMap<String,String>(); 52 keywords = new TreeMap<String,String>();
48 keywords.put("Id", "Id"); 53 keywords.put("Id", "Id");
49 keywords.put("Revision", "Revision"); 54 keywords.put("Revision", "Revision");
50 keywords.put("Author", "Author"); 55 keywords.put("Author", "Author");
181 */ 186 */
182 private void expandKeywordValue(String keyword, ByteBuffer rv) { 187 private void expandKeywordValue(String keyword, ByteBuffer rv) {
183 if ("Id".equals(keyword)) { 188 if ("Id".equals(keyword)) {
184 rv.put(identityString().getBytes()); 189 rv.put(identityString().getBytes());
185 } else if ("Revision".equals(keyword)) { 190 } else if ("Revision".equals(keyword)) {
186 rv.put(revision()); 191 rv.put(revision().getBytes());
187 } else if ("Author".equals(keyword)) { 192 } else if ("Author".equals(keyword)) {
188 rv.put(username().getBytes()); 193 rv.put(username().getBytes());
194 } else if ("Date".equals(keyword)) {
195 rv.put(date().getBytes());
196 } else {
197 throw new IllegalStateException(String.format("Keyword %s is not yet supported", keyword));
189 } 198 }
190 } 199 }
191 200
192 private String matchKeyword(ByteBuffer src, int kwStart, int kwEnd) { 201 private String matchKeyword(ByteBuffer src, int kwStart, int kwEnd) {
193 assert kwEnd - kwStart - 1 > 0; 202 assert kwEnd - kwStart - 1 > 0;
200 break; 209 break;
201 } 210 }
202 chars[i] = c; 211 chars[i] = c;
203 } 212 }
204 String kw = new String(chars, 0, i); 213 String kw = new String(chars, 0, i);
205 System.out.println(keywords.subMap("I", "J")); 214 // XXX may use subMap to look up keywords based on few available characters (not waiting till closing $)
206 System.out.println(keywords.subMap("A", "B")); 215 // System.out.println(keywords.subMap("I", "J"));
207 System.out.println(keywords.subMap("Au", "B")); 216 // System.out.println(keywords.subMap("A", "B"));
217 // System.out.println(keywords.subMap("Au", "B"));
208 return keywords.get(kw); 218 return keywords.get(kw);
209 } 219 }
210 220
211 // copies part of the src buffer, [from..to). doesn't modify src position 221 // copies part of the src buffer, [from..to). doesn't modify src position
212 static void copySlice(ByteBuffer src, int from, int to, ByteBuffer dst) { 222 static void copySlice(ByteBuffer src, int from, int to, ByteBuffer dst) {
233 } 243 }
234 return -1; 244 return -1;
235 } 245 }
236 246
237 private String identityString() { 247 private String identityString() {
238 return "sample/file.txt, asd"; 248 return String.format("%s,v %s %s %s", path, revision(), date(), username());
239 } 249 }
240 250
241 private byte[] revision() { 251 private String revision() {
242 return "1234567890ab".getBytes(); 252 // FIXME add cset's nodeid into Changeset class
253 int csetRev = repo.getFileNode(path).getChangesetLocalRevision(HgRepository.TIP);
254 return repo.getChangelog().getRevision(csetRev).shortNotation();
243 } 255 }
244 256
245 private String username() { 257 private String username() {
246 /* ui.py: username() 258 return getChangeset().user();
247 Searched in this order: $HGUSER, [ui] section of hgrcs, $EMAIL 259 }
248 and stop searching if one of these is set. 260
249 If not found and ui.askusername is True, ask the user, else use 261 private String date() {
250 ($LOGNAME or $USER or $LNAME or $USERNAME) + "@full.hostname". 262 return String.format("%tY/%<tm/%<td %<tH:%<tM:%<tS", getChangeset().date());
251 */ 263 }
252 return "<Sample> sample@sample.org"; 264
265 private Changeset getChangeset() {
266 if (latestFileCset == null) {
267 int csetRev = repo.getFileNode(path).getChangesetLocalRevision(HgRepository.TIP);
268 latestFileCset = repo.getChangelog().range(csetRev, csetRev).get(0);
269 }
270 return latestFileCset;
253 } 271 }
254 272
255 public static class Factory implements Filter.Factory { 273 public static class Factory implements Filter.Factory {
256 274
257 public Filter create(HgRepository hgRepo, Path path, Options opts) { 275 private HgRepository repo;
258 return new KeywordFilter(true); 276 private Path.Matcher matcher;
259 } 277
260 } 278 public void initialize(HgRepository hgRepo, ConfigFile cfg) {
261 279 repo = hgRepo;
262 280 ArrayList<String> patterns = new ArrayList<String>();
263 public static void main(String[] args) throws Exception { 281 for (Map.Entry<String,String> e : cfg.getSection("keyword").entrySet()) {
264 FileInputStream fis = new FileInputStream(new File("/temp/kwoutput.txt")); 282 if (!"ignore".equalsIgnoreCase(e.getValue())) {
265 FileOutputStream fos = new FileOutputStream(new File("/temp/kwoutput2.txt")); 283 patterns.add(e.getKey());
266 ByteBuffer b = ByteBuffer.allocate(256); 284 }
267 KeywordFilter kwFilter = new KeywordFilter(false); 285 }
268 while (fis.getChannel().read(b) != -1) { 286 matcher = new PathGlobMatcher(patterns.toArray(new String[patterns.size()]));
269 b.flip(); // get ready to be read 287 // TODO read and respect keyword patterns from [keywordmaps]
270 ByteBuffer f = kwFilter.filter(b); 288 }
271 fos.getChannel().write(f); // XXX in fact, f may not be fully consumed 289
272 if (b.hasRemaining()) { 290 public Filter create(Path path, Options opts) {
273 b.compact(); 291 if (matcher.accept(path)) {
274 } else { 292 return new KeywordFilter(repo, path, true);
275 b.clear(); 293 }
276 } 294 return null;
277 } 295 }
278 fis.close(); 296 }
279 fos.flush(); 297
280 fos.close(); 298 //
281 } 299 // public static void main(String[] args) throws Exception {
300 // FileInputStream fis = new FileInputStream(new File("/temp/kwoutput.txt"));
301 // FileOutputStream fos = new FileOutputStream(new File("/temp/kwoutput2.txt"));
302 // ByteBuffer b = ByteBuffer.allocate(256);
303 // KeywordFilter kwFilter = new KeywordFilter(false);
304 // while (fis.getChannel().read(b) != -1) {
305 // b.flip(); // get ready to be read
306 // ByteBuffer f = kwFilter.filter(b);
307 // fos.getChannel().write(f); // XXX in fact, f may not be fully consumed
308 // if (b.hasRemaining()) {
309 // b.compact();
310 // } else {
311 // b.clear();
312 // }
313 // }
314 // fis.close();
315 // fos.flush();
316 // fos.close();
317 // }
282 } 318 }