1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| package org.apache.xindice.core.indexer; |
21 |
| |
22 |
| import org.apache.xindice.core.FaultCodes; |
23 |
| import org.apache.xindice.util.ReadOnlyException; |
24 |
| import org.apache.xindice.xml.NamespaceMap; |
25 |
| import org.apache.xindice.xml.SymbolTable; |
26 |
| |
27 |
| import java.util.Arrays; |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| public final class IndexPattern { |
36 |
| |
37 |
| public static final int PATTERN_NONE = -1; |
38 |
| public static final int PATTERN_WILDCARD = -2; |
39 |
| |
40 |
| public static final int SCORE_NONE = 0; |
41 |
| public static final int SCORE_WILDCARD = 1; |
42 |
| public static final int SCORE_NATURAL = 3; |
43 |
| |
44 |
| |
45 |
| private SymbolTable symbols; |
46 |
| private boolean absolute; |
47 |
| private String[] elemName; |
48 |
| private short elemID[]; |
49 |
| private String attrName; |
50 |
| private short attrID = PATTERN_NONE; |
51 |
| |
52 |
| |
53 |
| |
54 |
| |
55 |
| |
56 |
| |
57 |
| |
58 |
| |
59 |
| |
60 |
| |
61 |
| |
62 |
| |
63 |
| |
64 |
| |
65 |
| |
66 |
| |
67 |
| |
68 |
| |
69 |
| |
70 |
| |
71 |
| |
72 |
| |
73 |
| |
74 |
| |
75 |
| |
76 |
1478
| public IndexPattern(SymbolTable symbols, String pattern, NamespaceMap nsMap) throws IndexerException {
|
77 |
1478
| this.symbols = symbols;
|
78 |
| |
79 |
1478
| String ptrn = pattern.trim();
|
80 |
1478
| if (ptrn.charAt(0) == '/') {
|
81 |
415
| absolute = true;
|
82 |
415
| ptrn = ptrn.substring(1);
|
83 |
| } |
84 |
| |
85 |
1478
| if (ptrn.length() == 0) {
|
86 |
0
| throw new IndexerException(FaultCodes.IDX_CANNOT_CREATE, "Invalid pattern '" + pattern + "'");
|
87 |
| } |
88 |
| |
89 |
1478
| elemName = ptrn.split("/", -1);
|
90 |
1478
| elemID = new short[elemName.length];
|
91 |
| |
92 |
1478
| try {
|
93 |
1478
| for (int i = 0; i < elemName.length; i++) {
|
94 |
| |
95 |
2237
| if ("".equals(elemName[i])) {
|
96 |
0
| throw new IndexerException(FaultCodes.IDX_CANNOT_CREATE, "Invalid pattern '" + pattern + "'");
|
97 |
| } |
98 |
| |
99 |
2237
| int idx = elemName[i].indexOf('@');
|
100 |
2237
| if (idx >= 0) {
|
101 |
| |
102 |
607
| if (i == elemName.length - 1) {
|
103 |
607
| attrName = elemName[i].substring(idx + 1);
|
104 |
607
| if (attrName.equals("*")) {
|
105 |
43
| attrID = PATTERN_WILDCARD;
|
106 |
| } else { |
107 |
564
| attrID = symbols.getNormalizedSymbol(attrName, nsMap, true);
|
108 |
| } |
109 |
| } else { |
110 |
0
| throw new IndexerException(FaultCodes.IDX_CANNOT_CREATE, "Invalid pattern '" + pattern + "'");
|
111 |
| } |
112 |
| |
113 |
607
| elemName[i] = elemName[i].substring(0, idx);
|
114 |
| } |
115 |
| |
116 |
2237
| if (elemName[i].equals("*")) {
|
117 |
186
| elemID[i] = PATTERN_WILDCARD;
|
118 |
| } else { |
119 |
2051
| elemID[i] = symbols.getNormalizedSymbol(elemName[i], nsMap, true);
|
120 |
| } |
121 |
| } |
122 |
| } catch (ReadOnlyException e) { |
123 |
0
| throw new IndexerException(FaultCodes.IDX_NOT_SUPPORTED,
|
124 |
| "Pattern '" + pattern + "' is not allowed in this collection.", e); |
125 |
| } |
126 |
| } |
127 |
| |
128 |
| |
129 |
| |
130 |
| |
131 |
| |
132 |
| |
133 |
| |
134 |
0
| public IndexPattern(SymbolTable symbols, short[] elemID) {
|
135 |
0
| this(symbols, elemID, false);
|
136 |
| } |
137 |
| |
138 |
| |
139 |
| |
140 |
| |
141 |
| |
142 |
| |
143 |
| |
144 |
| |
145 |
4777
| public IndexPattern(SymbolTable symbols, short[] elemID, short attrID) {
|
146 |
4777
| this(symbols, elemID, attrID, true);
|
147 |
| } |
148 |
| |
149 |
| |
150 |
| |
151 |
| |
152 |
| |
153 |
| |
154 |
| |
155 |
| |
156 |
17414
| public IndexPattern(SymbolTable symbols, short elemID, short attrID) {
|
157 |
17414
| this(symbols, new short[] { elemID }, attrID, false);
|
158 |
| } |
159 |
| |
160 |
| |
161 |
| |
162 |
| |
163 |
| |
164 |
| |
165 |
| |
166 |
| |
167 |
0
| public IndexPattern(SymbolTable symbols, short[] elemID, boolean absolute) {
|
168 |
0
| this(symbols, elemID, (short) PATTERN_NONE, absolute);
|
169 |
| } |
170 |
| |
171 |
| |
172 |
| |
173 |
| |
174 |
| |
175 |
| |
176 |
| |
177 |
| |
178 |
| |
179 |
22191
| public IndexPattern(SymbolTable symbols, short[] elemID, short attrID, boolean absolute) {
|
180 |
22191
| this.symbols = symbols;
|
181 |
| |
182 |
22191
| this.elemID = elemID;
|
183 |
22191
| this.elemName = new String[elemID.length];
|
184 |
22191
| for (int i = 0; i < elemID.length; i++) {
|
185 |
25274
| this.elemName[i] = symbols.getName(elemID[i]);
|
186 |
| } |
187 |
| |
188 |
22191
| this.attrID = attrID;
|
189 |
22191
| this.attrName = symbols.getName(attrID);
|
190 |
| |
191 |
22191
| this.absolute = absolute;
|
192 |
| } |
193 |
| |
194 |
| |
195 |
| |
196 |
| |
197 |
| |
198 |
| |
199 |
| |
200 |
| |
201 |
| |
202 |
| |
203 |
| |
204 |
| |
205 |
| |
206 |
| |
207 |
| |
208 |
14141
| public int getMatchLevel(IndexPattern p) {
|
209 |
| |
210 |
14141
| if ((p.absolute && !absolute) || (p.elemID.length > elemID.length) ||
|
211 |
| (p.absolute && absolute && p.elemID.length != elemID.length)) { |
212 |
64
| return 0;
|
213 |
| } |
214 |
| |
215 |
14077
| int result;
|
216 |
| |
217 |
14077
| if (attrID != PATTERN_NONE && p.attrID == PATTERN_WILDCARD) {
|
218 |
3116
| result = SCORE_WILDCARD;
|
219 |
10961
| } else if (attrID == p.attrID) {
|
220 |
2318
| result = SCORE_NATURAL;
|
221 |
| } else { |
222 |
8643
| return 0;
|
223 |
| } |
224 |
| |
225 |
5434
| int i = elemID.length - 1;
|
226 |
5434
| int j = p.elemID.length - 1;
|
227 |
5434
| while (i >= 0 && j >=0) {
|
228 |
5483
| if (p.elemID[j] == PATTERN_WILDCARD) {
|
229 |
3198
| result += (SCORE_WILDCARD << 2);
|
230 |
2285
| } else if (elemID[i] == p.elemID[j]) {
|
231 |
1870
| result += (SCORE_NATURAL << 2);
|
232 |
| } else { |
233 |
415
| result = 0;
|
234 |
415
| break;
|
235 |
| } |
236 |
5068
| i--;
|
237 |
5068
| j--;
|
238 |
| } |
239 |
| |
240 |
5434
| return result;
|
241 |
| } |
242 |
| |
243 |
66534
| public boolean isAbsolute() {
|
244 |
66534
| return absolute;
|
245 |
| } |
246 |
| |
247 |
| |
248 |
| |
249 |
| |
250 |
| |
251 |
| |
252 |
| |
253 |
85770
| public short getElementID() {
|
254 |
85770
| return elemID.length > 0 ? elemID[elemID.length - 1] : PATTERN_NONE;
|
255 |
| } |
256 |
| |
257 |
| |
258 |
| |
259 |
| |
260 |
| |
261 |
| |
262 |
69581
| public short[] getElementIDs() {
|
263 |
69581
| return elemID;
|
264 |
| } |
265 |
| |
266 |
| |
267 |
| |
268 |
| |
269 |
| |
270 |
| |
271 |
| |
272 |
44467
| public short getAttributeID() {
|
273 |
44467
| return attrID;
|
274 |
| } |
275 |
| |
276 |
| |
277 |
| |
278 |
| |
279 |
| |
280 |
| |
281 |
0
| public String[] getElementNames() {
|
282 |
0
| return elemName;
|
283 |
| } |
284 |
| |
285 |
| |
286 |
| |
287 |
| |
288 |
| |
289 |
| |
290 |
| |
291 |
0
| public String getAttributeName() {
|
292 |
0
| return attrName;
|
293 |
| } |
294 |
| |
295 |
2507
| public int hashCode() {
|
296 |
2507
| return (getElementID() << 16) + attrID;
|
297 |
| } |
298 |
| |
299 |
813
| public boolean equals(Object obj) {
|
300 |
813
| if (obj instanceof IndexPattern) {
|
301 |
813
| IndexPattern p = (IndexPattern) obj;
|
302 |
813
| boolean eq = Arrays.equals(elemID, p.elemID) && attrID == p.attrID && absolute == p.absolute;
|
303 |
813
| if (eq && (elemName != null || p.elemName != null)) {
|
304 |
644
| eq = elemName != null && p.elemName != null && Arrays.equals(elemName, p.elemName);
|
305 |
| } |
306 |
813
| if (eq && (attrName != null || p.attrName != null)) {
|
307 |
232
| eq = attrName != null && p.attrName != null && attrName.equals(p.attrName);
|
308 |
| } |
309 |
813
| return eq;
|
310 |
| } else { |
311 |
0
| return false;
|
312 |
| } |
313 |
| } |
314 |
| |
315 |
9
| public String toString() {
|
316 |
9
| StringBuffer pattern = new StringBuffer();
|
317 |
9
| if (absolute) {
|
318 |
4
| pattern.append('/');
|
319 |
| } |
320 |
| |
321 |
9
| for (int i = 0; i < elemID.length; i++) {
|
322 |
17
| pattern.append(elemName[i]);
|
323 |
17
| if (i < elemID.length - 1) {
|
324 |
8
| pattern.append('/');
|
325 |
| } |
326 |
| } |
327 |
| |
328 |
9
| if (attrID != PATTERN_NONE) {
|
329 |
9
| pattern.append('@').append(attrName);
|
330 |
| } |
331 |
| |
332 |
9
| return pattern.toString();
|
333 |
| } |
334 |
| } |