annotate src/org/tmatesoft/hg/internal/KeywordFilter.java @ 599:55b7987c1796

Do not instantiate intermediate arrays
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Fri, 03 May 2013 15:29:26 +0200
parents b3c16d1aede0
children cf200271439a
rev   line source
112
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
1 /*
418
528b6780a8bd A bit of FIXME cleanup (mostly degraded to TODO post 1.0), comments and javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 367
diff changeset
2 * Copyright (c) 2011-2012 TMate Software Ltd
112
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
3 *
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
4 * This program is free software; you can redistribute it and/or modify
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
5 * it under the terms of the GNU General Public License as published by
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
6 * the Free Software Foundation; version 2 of the License.
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
7 *
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
8 * This program is distributed in the hope that it will be useful,
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
11 * GNU General Public License for more details.
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
12 *
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
13 * For information on how to redistribute this software under
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
14 * the terms of a license other than GNU General Public License
130
7567f4a42fe5 Correct contact address
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 129
diff changeset
15 * contact TMate Software at support@hg4j.com
112
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
16 */
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
17 package org.tmatesoft.hg.internal;
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
18
456
909306e412e2 Refactor LogFacility and SessionContext, better API for both
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 423
diff changeset
19 import static org.tmatesoft.hg.util.LogFacility.Severity.Error;
909306e412e2 Refactor LogFacility and SessionContext, better API for both
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 423
diff changeset
20
112
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
21 import java.nio.ByteBuffer;
114
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
22 import java.util.ArrayList;
354
5f9073eabf06 Propagate errors with exceptions up to a end client
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
23 import java.util.Date;
112
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
24 import java.util.TreeMap;
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
25
354
5f9073eabf06 Propagate errors with exceptions up to a end client
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
26 import org.tmatesoft.hg.core.Nodeid;
154
ba2bf656f00f Changeset => RawChangeset
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 133
diff changeset
27 import org.tmatesoft.hg.repo.HgChangelog.RawChangeset;
112
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
28 import org.tmatesoft.hg.repo.HgRepository;
423
9c9c442b5f2e Major refactoring of exception handling. Low-level API uses RuntimeExceptions, while checked are left for higher level
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 418
diff changeset
29 import org.tmatesoft.hg.repo.HgRuntimeException;
331
a37ce7145c3f Access to repository configuration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 154
diff changeset
30 import org.tmatesoft.hg.util.Pair;
133
4a948ec83980 core.Path to util.Path as it's not Hg repo dependant
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 130
diff changeset
31 import org.tmatesoft.hg.util.Path;
112
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
32
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
33 /**
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
34 *
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
35 * @author Artem Tikhomirov
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
36 * @author TMate Software Ltd.
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
37 */
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
38 public class KeywordFilter implements Filter {
113
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 112
diff changeset
39 // present implementation is stateless, however, filter use pattern shall not assume that. In fact, Factory may us that
114
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
40 private final HgRepository repo;
112
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
41 private final boolean isExpanding;
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
42 private final TreeMap<String,String> keywords;
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
43 private final int minBufferLen;
114
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
44 private final Path path;
154
ba2bf656f00f Changeset => RawChangeset
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 133
diff changeset
45 private RawChangeset latestFileCset;
112
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
46
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
47 /**
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
48 *
114
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
49 * @param hgRepo
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
50 * @param path
112
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
51 * @param expand <code>true</code> to expand keywords, <code>false</code> to shrink
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
52 */
114
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
53 private KeywordFilter(HgRepository hgRepo, Path p, boolean expand) {
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
54 repo = hgRepo;
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
55 path = p;
112
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
56 isExpanding = expand;
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
57 keywords = new TreeMap<String,String>();
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
58 keywords.put("Id", "Id");
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
59 keywords.put("Revision", "Revision");
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
60 keywords.put("Author", "Author");
113
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 112
diff changeset
61 keywords.put("Date", "Date");
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 112
diff changeset
62 keywords.put("LastChangedRevision", "LastChangedRevision");
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 112
diff changeset
63 keywords.put("LastChangedBy", "LastChangedBy");
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 112
diff changeset
64 keywords.put("LastChangedDate", "LastChangedDate");
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 112
diff changeset
65 keywords.put("Source", "Source");
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 112
diff changeset
66 keywords.put("Header", "Header");
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 112
diff changeset
67
112
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
68 int l = 0;
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
69 for (String s : keywords.keySet()) {
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
70 if (s.length() > l) {
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
71 l = s.length();
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
72 }
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
73 }
418
528b6780a8bd A bit of FIXME cleanup (mostly degraded to TODO post 1.0), comments and javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 367
diff changeset
74 // TODO post-1.0 later may implement #filter() not to read full kw value (just "$kw:"). However, limit of maxLen + 2 would keep valid.
112
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
75 // for buffers less then minBufferLen, there are chances #filter() implementation would never end
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
76 // (i.e. for input "$LongestKey"$
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
77 minBufferLen = l + 2 + (isExpanding ? 0 : 120 /*any reasonable constant for max possible kw value length*/);
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
78 }
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
79
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
80 /**
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
81 * @param src buffer ready to be read
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
82 * @return buffer ready to be read and original buffer's position modified to reflect consumed bytes. IOW, if source buffer
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
83 * on return has remaining bytes, they are assumed not-read (not processed) and next chunk passed to filter is supposed to
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
84 * start with them
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
85 */
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
86 public ByteBuffer filter(ByteBuffer src) {
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
87 if (src.capacity() < minBufferLen) {
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
88 throw new IllegalStateException(String.format("Need buffer of at least %d bytes to ensure filter won't hang", minBufferLen));
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
89 }
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
90 ByteBuffer rv = null;
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
91 int keywordStart = -1;
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
92 int x = src.position();
119
ed2b4adac51c Index and start of the copy are distinct
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 118
diff changeset
93 int copyFrom = x; // needs to be updated each time we copy a slice, but not each time we modify source index (x)
112
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
94 while (x < src.limit()) {
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
95 if (keywordStart == -1) {
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
96 int i = indexOf(src, '$', x, false);
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
97 if (i == -1) {
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
98 if (rv == null) {
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
99 return src;
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
100 } else {
119
ed2b4adac51c Index and start of the copy are distinct
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 118
diff changeset
101 copySlice(src, copyFrom, src.limit(), rv);
112
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
102 rv.flip();
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
103 src.position(src.limit());
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
104 return rv;
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
105 }
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
106 }
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
107 keywordStart = i;
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
108 // fall-through
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
109 }
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
110 if (keywordStart >= 0) {
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
111 int i = indexOf(src, '$', keywordStart+1, true);
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
112 if (i == -1) {
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
113 // end of buffer reached
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
114 if (rv == null) {
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
115 if (keywordStart == x) {
418
528b6780a8bd A bit of FIXME cleanup (mostly degraded to TODO post 1.0), comments and javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 367
diff changeset
116 // TODO post-1.0 in fact, x might be equal to keywordStart and to src.position() here ('$' is first character in the buffer,
112
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
117 // and there are no other '$' not eols till the end of the buffer). This would lead to deadlock (filter won't consume any
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
118 // bytes). To prevent this, either shall copy bytes [keywordStart..buffer.limit()) to local buffer and use it on the next invocation,
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
119 // or add lookup of the keywords right after first '$' is found (do not wait for closing '$'). For now, large enough src buffer would be sufficient
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
120 // not to run into such situation
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
121 throw new IllegalStateException("Try src buffer of a greater size");
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
122 }
121
b1d6208fb517 Conditionally apply filters to file content
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 119
diff changeset
123 rv = ByteBuffer.allocate(keywordStart - copyFrom);
112
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
124 }
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
125 // copy all from source till latest possible kw start
119
ed2b4adac51c Index and start of the copy are distinct
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 118
diff changeset
126 copySlice(src, copyFrom, keywordStart, rv);
112
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
127 rv.flip();
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
128 // and tell caller we've consumed only to the potential kw start
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
129 src.position(keywordStart);
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
130 return rv;
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
131 } else if (src.get(i) == '$') {
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
132 // end of keyword, or start of a new one.
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
133 String keyword;
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
134 if ((keyword = matchKeyword(src, keywordStart, i)) != null) {
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
135 if (rv == null) {
113
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 112
diff changeset
136 // src.remaining(), not .capacity because src is not read, and remaining represents
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 112
diff changeset
137 // actual bytes count, while capacity - potential.
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 112
diff changeset
138 // Factor of 4 is pure guess and a HACK, need to be fixed with re-expanding buffer on demand
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 112
diff changeset
139 rv = ByteBuffer.allocate(isExpanding ? src.remaining() * 4 : src.remaining());
112
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
140 }
119
ed2b4adac51c Index and start of the copy are distinct
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 118
diff changeset
141 copySlice(src, copyFrom, keywordStart+1, rv);
112
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
142 rv.put(keyword.getBytes());
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
143 if (isExpanding) {
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
144 rv.put((byte) ':');
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
145 rv.put((byte) ' ');
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
146 expandKeywordValue(keyword, rv);
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
147 rv.put((byte) ' ');
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
148 }
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
149 rv.put((byte) '$');
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
150 keywordStart = -1;
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
151 x = i+1;
119
ed2b4adac51c Index and start of the copy are distinct
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 118
diff changeset
152 copyFrom = x;
112
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
153 continue;
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
154 } else {
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
155 if (rv != null) {
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
156 // we've already did some substitution, thus need to copy bytes we've scanned.
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
157 copySlice(src, x, i, rv);
119
ed2b4adac51c Index and start of the copy are distinct
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 118
diff changeset
158 copyFrom = i;
112
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
159 } // no else in attempt to avoid rv creation if no real kw would be found
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
160 keywordStart = i;
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
161 x = i; // '$' at i wasn't consumed, hence x points to i, not i+1. This is to avoid problems with case: "sdfsd $ asdfs $Id$ sdf"
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
162 continue;
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
163 }
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
164 } else {
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
165 assert src.get(i) == '\n' || src.get(i) == '\r';
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
166 // line break
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
167 if (rv != null) {
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
168 copySlice(src, x, i+1, rv);
119
ed2b4adac51c Index and start of the copy are distinct
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 118
diff changeset
169 copyFrom = i+1;
112
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
170 }
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
171 x = i+1;
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
172 keywordStart = -1; // Wasn't keyword, really
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
173 continue; // try once again
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
174 }
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
175 }
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
176 }
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
177 if (keywordStart != -1) {
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
178 if (rv == null) {
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
179 // no expansion happened yet, and we have potential kw start
113
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 112
diff changeset
180 rv = ByteBuffer.allocate(keywordStart - src.position());
112
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
181 copySlice(src, src.position(), keywordStart, rv);
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
182 }
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
183 src.position(keywordStart);
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
184 }
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
185 if (rv != null) {
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
186 rv.flip();
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
187 return rv;
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
188 }
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
189 return src;
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
190 }
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
191
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
192 /**
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
193 * @param keyword
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
194 * @param rv
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
195 */
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
196 private void expandKeywordValue(String keyword, ByteBuffer rv) {
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
197 if ("Id".equals(keyword)) {
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
198 rv.put(identityString().getBytes());
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
199 } else if ("Revision".equals(keyword)) {
114
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
200 rv.put(revision().getBytes());
112
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
201 } else if ("Author".equals(keyword)) {
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
202 rv.put(username().getBytes());
114
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
203 } else if ("Date".equals(keyword)) {
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
204 rv.put(date().getBytes());
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
205 } else {
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
206 throw new IllegalStateException(String.format("Keyword %s is not yet supported", keyword));
112
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
207 }
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
208 }
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
209
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
210 private String matchKeyword(ByteBuffer src, int kwStart, int kwEnd) {
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
211 assert kwEnd - kwStart - 1 > 0;
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
212 assert src.get(kwStart) == src.get(kwEnd) && src.get(kwEnd) == '$';
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
213 char[] chars = new char[kwEnd - kwStart - 1];
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
214 int i;
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
215 for (i = 0; i < chars.length; i++) {
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
216 char c = (char) src.get(kwStart + 1 + i);
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
217 if (c == ':') {
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
218 break;
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
219 }
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
220 chars[i] = c;
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
221 }
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
222 String kw = new String(chars, 0, i);
114
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
223 // XXX may use subMap to look up keywords based on few available characters (not waiting till closing $)
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
224 // System.out.println(keywords.subMap("I", "J"));
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
225 // System.out.println(keywords.subMap("A", "B"));
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
226 // System.out.println(keywords.subMap("Au", "B"));
112
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
227 return keywords.get(kw);
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
228 }
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
229
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
230 // copies part of the src buffer, [from..to). doesn't modify src position
113
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 112
diff changeset
231 static void copySlice(ByteBuffer src, int from, int to, ByteBuffer dst) {
112
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
232 if (to > src.limit()) {
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
233 throw new IllegalArgumentException("Bad right boundary");
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
234 }
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
235 if (dst.remaining() < to - from) {
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
236 throw new IllegalArgumentException("Not enough room in the destination buffer");
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
237 }
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
238 for (int i = from; i < to; i++) {
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
239 dst.put(src.get(i));
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
240 }
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
241 }
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
242
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
243 private static int indexOf(ByteBuffer b, char ch, int from, boolean newlineBreaks) {
113
67ae317408c9 Filter implementation for newline translation
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 112
diff changeset
244 for (int i = from; i < b.limit(); i++) {
112
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
245 byte c = b.get(i);
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
246 if (ch == c) {
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
247 return i;
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
248 }
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
249 if (newlineBreaks && (c == '\n' || c == '\r')) {
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
250 return i;
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
251 }
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
252 }
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
253 return -1;
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
254 }
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
255
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
256 private String identityString() {
114
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
257 return String.format("%s,v %s %s %s", path, revision(), date(), username());
112
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
258 }
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
259
114
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
260 private String revision() {
354
5f9073eabf06 Propagate errors with exceptions up to a end client
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
261 try {
418
528b6780a8bd A bit of FIXME cleanup (mostly degraded to TODO post 1.0), comments and javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 367
diff changeset
262 // TODO post-1.0 Either add cset's nodeid into Changeset class or use own inspector
528b6780a8bd A bit of FIXME cleanup (mostly degraded to TODO post 1.0), comments and javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 367
diff changeset
263 // when accessing changelog, see below, #getChangeset
367
2fadf8695f8a Use 'revision index' instead of the vague 'local revision number' concept in the API
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 354
diff changeset
264 int csetRev = repo.getFileNode(path).getChangesetRevisionIndex(HgRepository.TIP);
354
5f9073eabf06 Propagate errors with exceptions up to a end client
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
265 return repo.getChangelog().getRevision(csetRev).shortNotation();
423
9c9c442b5f2e Major refactoring of exception handling. Low-level API uses RuntimeExceptions, while checked are left for higher level
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 418
diff changeset
266 } catch (HgRuntimeException ex) {
490
b3c16d1aede0 Refactoring: move HgRepository's implementation aspects to Internals (which is now its imlementation counterpart and primary repository class to be used by other parts of the library)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 456
diff changeset
267 repo.getSessionContext().getLog().dump(getClass(), Error, ex, null);
354
5f9073eabf06 Propagate errors with exceptions up to a end client
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
268 return Nodeid.NULL.shortNotation(); // XXX perhaps, might return anything better? Not sure how hg approaches this.
5f9073eabf06 Propagate errors with exceptions up to a end client
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
269 }
112
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
270 }
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
271
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
272 private String username() {
354
5f9073eabf06 Propagate errors with exceptions up to a end client
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
273 try {
5f9073eabf06 Propagate errors with exceptions up to a end client
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
274 return getChangeset().user();
423
9c9c442b5f2e Major refactoring of exception handling. Low-level API uses RuntimeExceptions, while checked are left for higher level
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 418
diff changeset
275 } catch (HgRuntimeException ex) {
490
b3c16d1aede0 Refactoring: move HgRepository's implementation aspects to Internals (which is now its imlementation counterpart and primary repository class to be used by other parts of the library)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 456
diff changeset
276 repo.getSessionContext().getLog().dump(getClass(), Error, ex, null);
354
5f9073eabf06 Propagate errors with exceptions up to a end client
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
277 return "";
5f9073eabf06 Propagate errors with exceptions up to a end client
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
278 }
114
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
279 }
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
280
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
281 private String date() {
354
5f9073eabf06 Propagate errors with exceptions up to a end client
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
282 Date d;
5f9073eabf06 Propagate errors with exceptions up to a end client
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
283 try {
5f9073eabf06 Propagate errors with exceptions up to a end client
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
284 d = getChangeset().date();
423
9c9c442b5f2e Major refactoring of exception handling. Low-level API uses RuntimeExceptions, while checked are left for higher level
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 418
diff changeset
285 } catch (HgRuntimeException ex) {
490
b3c16d1aede0 Refactoring: move HgRepository's implementation aspects to Internals (which is now its imlementation counterpart and primary repository class to be used by other parts of the library)
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 456
diff changeset
286 repo.getSessionContext().getLog().dump(getClass(), Error, ex, null);
354
5f9073eabf06 Propagate errors with exceptions up to a end client
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
287 d = new Date(0l);
5f9073eabf06 Propagate errors with exceptions up to a end client
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
288 }
5f9073eabf06 Propagate errors with exceptions up to a end client
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 331
diff changeset
289 return String.format("%tY/%<tm/%<td %<tH:%<tM:%<tS", d);
114
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
290 }
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
291
423
9c9c442b5f2e Major refactoring of exception handling. Low-level API uses RuntimeExceptions, while checked are left for higher level
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 418
diff changeset
292 private RawChangeset getChangeset() throws HgRuntimeException {
114
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
293 if (latestFileCset == null) {
418
528b6780a8bd A bit of FIXME cleanup (mostly degraded to TODO post 1.0), comments and javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 367
diff changeset
294 // TODO post-1.0 Use of TIP is likely incorrect in cases when working copy is not based
528b6780a8bd A bit of FIXME cleanup (mostly degraded to TODO post 1.0), comments and javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 367
diff changeset
295 // on latest revision. Perhaps, a constant like HgRepository.DIRSTATE_PARENT may come handy
528b6780a8bd A bit of FIXME cleanup (mostly degraded to TODO post 1.0), comments and javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 367
diff changeset
296 // Besides, it's reasonable to pass own inspector instead of implicit use of RawCsetCollector
528b6780a8bd A bit of FIXME cleanup (mostly degraded to TODO post 1.0), comments and javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 367
diff changeset
297 // to get changeset nodeid/index right away. Also check ChangelogHelper if may be of any use
367
2fadf8695f8a Use 'revision index' instead of the vague 'local revision number' concept in the API
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 354
diff changeset
298 int csetRev = repo.getFileNode(path).getChangesetRevisionIndex(HgRepository.TIP);
114
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
299 latestFileCset = repo.getChangelog().range(csetRev, csetRev).get(0);
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
300 }
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
301 return latestFileCset;
112
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
302 }
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
303
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
304 public static class Factory implements Filter.Factory {
114
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
305
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
306 private HgRepository repo;
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
307 private Path.Matcher matcher;
112
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
308
331
a37ce7145c3f Access to repository configuration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 154
diff changeset
309 public void initialize(HgRepository hgRepo) {
114
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
310 repo = hgRepo;
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
311 ArrayList<String> patterns = new ArrayList<String>();
331
a37ce7145c3f Access to repository configuration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 154
diff changeset
312 for (Pair<String,String> e : hgRepo.getConfiguration().getSection("keyword")) {
a37ce7145c3f Access to repository configuration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 154
diff changeset
313 if (!"ignore".equalsIgnoreCase(e.second())) {
a37ce7145c3f Access to repository configuration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 154
diff changeset
314 patterns.add(e.first());
114
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
315 }
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
316 }
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
317 matcher = new PathGlobMatcher(patterns.toArray(new String[patterns.size()]));
423
9c9c442b5f2e Major refactoring of exception handling. Low-level API uses RuntimeExceptions, while checked are left for higher level
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 418
diff changeset
318 // TODO post-1.0 read and respect keyword patterns from [keywordmaps]
114
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
319 }
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
320
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
321 public Filter create(Path path, Options opts) {
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
322 if (matcher.accept(path)) {
118
68ba22a2133a Defects in the filter initialization
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 114
diff changeset
323 return new KeywordFilter(repo, path, opts.getDirection() == Filter.Direction.FromRepo);
114
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
324 }
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
325 return null;
112
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
326 }
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
327 }
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
328
114
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
329 //
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
330 // public static void main(String[] args) throws Exception {
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
331 // FileInputStream fis = new FileInputStream(new File("/temp/kwoutput.txt"));
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
332 // FileOutputStream fos = new FileOutputStream(new File("/temp/kwoutput2.txt"));
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
333 // ByteBuffer b = ByteBuffer.allocate(256);
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
334 // KeywordFilter kwFilter = new KeywordFilter(false);
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
335 // while (fis.getChannel().read(b) != -1) {
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
336 // b.flip(); // get ready to be read
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
337 // ByteBuffer f = kwFilter.filter(b);
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
338 // fos.getChannel().write(f); // XXX in fact, f may not be fully consumed
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
339 // if (b.hasRemaining()) {
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
340 // b.compact();
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
341 // } else {
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
342 // b.clear();
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
343 // }
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
344 // }
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
345 // fis.close();
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
346 // fos.flush();
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
347 // fos.close();
46291ec605a0 Filters to read and initialize according to configuration files
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 113
diff changeset
348 // }
112
d488c7638b87 Prototype Filter support with keyword filter as a playground
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
349 }