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.commons.logging.Log; |
23 |
| import org.apache.commons.logging.LogFactory; |
24 |
| import org.apache.xindice.core.DBException; |
25 |
| import org.apache.xindice.core.data.Key; |
26 |
| import org.apache.xindice.util.ObjectStack; |
27 |
| import org.apache.xindice.util.ReadOnlyException; |
28 |
| import org.apache.xindice.xml.SymbolTable; |
29 |
| import org.apache.xindice.xml.sax.CompressionHandler; |
30 |
| import org.apache.xindice.xml.sax.SAXEventGenerator; |
31 |
| import org.w3c.dom.Document; |
32 |
| import org.xml.sax.Attributes; |
33 |
| import org.xml.sax.ContentHandler; |
34 |
| import org.xml.sax.Locator; |
35 |
| import org.xml.sax.SAXException; |
36 |
| |
37 |
| import java.util.Iterator; |
38 |
| import java.util.LinkedList; |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
| |
46 |
| class DocumentHandler implements ContentHandler, CompressionHandler { |
47 |
| |
48 |
| private static final Log log = LogFactory.getLog(DocumentHandler.class); |
49 |
| |
50 |
| static final int ACTION_CREATE = 0; |
51 |
| static final int ACTION_DELETE = 1; |
52 |
| |
53 |
| private ObjectStack stack = new ObjectStack(); |
54 |
| private Indexer[] indexers; |
55 |
| private IndexerEventHandler[] handlers; |
56 |
| private LinkedList path = new LinkedList(); |
57 |
| |
58 |
| public Key key; |
59 |
| public Document doc; |
60 |
| public int action; |
61 |
| private SymbolTable symbols; |
62 |
| |
63 |
| public StackInfo info; |
64 |
| |
65 |
11100
| public DocumentHandler(SymbolTable symbols, Key key, Document doc, int action, Indexer[] list) {
|
66 |
11100
| this.symbols = symbols;
|
67 |
11100
| this.key = key;
|
68 |
11100
| this.doc = doc;
|
69 |
11100
| this.action = action;
|
70 |
11100
| this.indexers = list;
|
71 |
| |
72 |
11100
| handlers = new IndexerEventHandler[list.length];
|
73 |
11100
| for (int i = 0; i < list.length; i++) {
|
74 |
11110
| handlers[i] = list[i].getIndexerEventHandler();
|
75 |
| } |
76 |
| |
77 |
11100
| try {
|
78 |
11100
| SAXEventGenerator events = new SAXEventGenerator(symbols, doc);
|
79 |
11100
| events.setContentHandler(this);
|
80 |
11100
| events.setProperty(HANDLER, this);
|
81 |
11100
| events.start();
|
82 |
| } catch (Exception e) { |
83 |
0
| if (log.isWarnEnabled()) {
|
84 |
0
| log.warn("ignored exception", e);
|
85 |
| } |
86 |
| } |
87 |
| } |
88 |
| |
89 |
| |
90 |
0
| public void setDocumentLocator(Locator locator) {
|
91 |
| } |
92 |
| |
93 |
0
| public void startPrefixMapping(String prefix, String uri) {
|
94 |
| } |
95 |
| |
96 |
0
| public void endPrefixMapping(String prefix) {
|
97 |
| } |
98 |
| |
99 |
0
| public void ignorableWhitespace(char ch[], int start, int length) {
|
100 |
| } |
101 |
| |
102 |
0
| public void processingInstruction(String target, String data) {
|
103 |
| } |
104 |
| |
105 |
0
| public void skippedEntity(String name) {
|
106 |
| } |
107 |
| |
108 |
11100
| public void symbols(SymbolTable symbols) {
|
109 |
| } |
110 |
| |
111 |
11100
| public void dataBytes(byte[] data) {
|
112 |
| } |
113 |
| |
114 |
11100
| public void startDocument() {
|
115 |
| } |
116 |
| |
117 |
11100
| public void endDocument() {
|
118 |
11100
| for (int i = 0; i < handlers.length; i++ ) {
|
119 |
11110
| try {
|
120 |
11110
| switch (action) {
|
121 |
10981
| case ACTION_CREATE:
|
122 |
10981
| handlers[i].onDocumentAdded(key);
|
123 |
10981
| break;
|
124 |
129
| case ACTION_DELETE:
|
125 |
129
| handlers[i].onDocumentDeleted(key);
|
126 |
129
| break;
|
127 |
0
| default:
|
128 |
0
| if (log.isWarnEnabled()) {
|
129 |
0
| log.warn("invalid action : " + action);
|
130 |
| } |
131 |
| } |
132 |
| } catch (Exception e) { |
133 |
0
| if (log.isWarnEnabled()) {
|
134 |
0
| log.warn("ignored exception", e);
|
135 |
| } |
136 |
| } |
137 |
| } |
138 |
| } |
139 |
| |
140 |
65640
| private void processEntry(LazyIndexPattern pattern, String value, int pos, int len) {
|
141 |
65640
| for (int i = 0; i < handlers.length; i++ ) {
|
142 |
65680
| try {
|
143 |
65680
| switch (action) {
|
144 |
65106
| case ACTION_CREATE:
|
145 |
65106
| add(i, pattern, value, key, pos, len);
|
146 |
65106
| break;
|
147 |
574
| case ACTION_DELETE:
|
148 |
574
| delete(i, pattern, value, key, pos, len);
|
149 |
574
| break;
|
150 |
0
| default:
|
151 |
0
| if (log.isWarnEnabled()) {
|
152 |
0
| log.warn("invalid action : " + action);
|
153 |
| } |
154 |
| } |
155 |
| } catch (Exception e) { |
156 |
0
| if (log.isWarnEnabled()) {
|
157 |
0
| log.warn("ignored exception", e);
|
158 |
| } |
159 |
| } |
160 |
| } |
161 |
| } |
162 |
| |
163 |
65106
| private void add(int n, LazyIndexPattern ptrn, String value, Key key, int pos, int len) throws DBException {
|
164 |
65106
| IndexPattern[] patterns = indexers[n].getPatterns();
|
165 |
65106
| for (int i = 0; i < patterns.length; i++) {
|
166 |
65763
| if (ptrn.getMatchLevel(patterns[i]) > 0) {
|
167 |
4478
| handlers[n].onNameAdded(patterns[i], key, ptrn.getElementID(), ptrn.getAttributeID());
|
168 |
4478
| handlers[n].onValueAdded(patterns[i], value, key, pos, len, ptrn.getElementID(), ptrn.getAttributeID());
|
169 |
| } |
170 |
| } |
171 |
| } |
172 |
| |
173 |
574
| private void delete(int n, LazyIndexPattern ptrn, String value, Key key, int pos, int len) throws DBException {
|
174 |
574
| IndexPattern[] patterns = indexers[n].getPatterns();
|
175 |
574
| for (int i = 0; i < patterns.length; i++) {
|
176 |
1144
| if (ptrn.getMatchLevel(patterns[i]) > 0) {
|
177 |
298
| handlers[n].onNameDeleted(patterns[i], key, ptrn.getElementID(), ptrn.getAttributeID());
|
178 |
298
| handlers[n].onValueDeleted(patterns[i], value, key, pos, len, ptrn.getElementID(), ptrn.getAttributeID());
|
179 |
| } |
180 |
| } |
181 |
| } |
182 |
| |
183 |
43888
| public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
|
184 |
43888
| try {
|
185 |
| |
186 |
43888
| if (namespaceURI != null && namespaceURI.length() > 0) {
|
187 |
0
| info.symbolID = symbols.getNormalizedSymbol(localName, namespaceURI, true);
|
188 |
| } |
189 |
| |
190 |
43888
| int size = atts.getLength();
|
191 |
43888
| for (int i = 0; i < size; i++) {
|
192 |
21752
| String nsURI = atts.getURI(i);
|
193 |
21752
| short id;
|
194 |
21752
| if (nsURI != null && nsURI.length() > 0) {
|
195 |
0
| id = symbols.getNormalizedSymbol(atts.getLocalName(i), nsURI, true);
|
196 |
| } else { |
197 |
21752
| id = symbols.getSymbol(atts.getQName(i), true);
|
198 |
| } |
199 |
| |
200 |
21752
| processEntry(new LazyIndexPattern(id), atts.getValue(i), info.pos, info.len);
|
201 |
| } |
202 |
| } catch (ReadOnlyException e) { |
203 |
0
| throw new SAXException(e);
|
204 |
| } |
205 |
| } |
206 |
| |
207 |
43888
| public void endElement(String namespaceURI, String localName, String qName) {
|
208 |
43888
| StringBuffer sb = info.sb;
|
209 |
43888
| processEntry(new LazyIndexPattern(), sb.toString(), info.pos, info.len);
|
210 |
43888
| info = (StackInfo) stack.pop();
|
211 |
43888
| if (info != null) {
|
212 |
32788
| info.sb.append(sb);
|
213 |
| } |
214 |
43888
| path.removeLast();
|
215 |
| } |
216 |
| |
217 |
65235
| public void characters(char ch[], int start, int length) {
|
218 |
65235
| info.sb.append(ch, start, length);
|
219 |
| } |
220 |
| |
221 |
43888
| public void symbolID(short symbolID) {
|
222 |
43888
| if (info != null) {
|
223 |
32788
| stack.push(info);
|
224 |
| } |
225 |
43888
| info = new StackInfo(symbolID);
|
226 |
43888
| path.add(new Short(symbolID));
|
227 |
| } |
228 |
| |
229 |
43888
| public void dataLocation(int pos, int len) {
|
230 |
43888
| info.pos = pos;
|
231 |
43888
| info.len = len;
|
232 |
| } |
233 |
| |
234 |
| |
235 |
| |
236 |
| |
237 |
| private class StackInfo { |
238 |
| public short symbolID; |
239 |
| public StringBuffer sb = new StringBuffer(); |
240 |
| public int pos = -1; |
241 |
| public int len = -1; |
242 |
| |
243 |
43888
| public StackInfo(short symbolID) {
|
244 |
43888
| this.symbolID = symbolID;
|
245 |
| } |
246 |
| } |
247 |
| |
248 |
| private class LazyIndexPattern { |
249 |
| private short attrID = IndexPattern.PATTERN_NONE; |
250 |
| private IndexPattern pattern; |
251 |
| |
252 |
43888
| public LazyIndexPattern() {
|
253 |
| } |
254 |
| |
255 |
21752
| public LazyIndexPattern(short attrID) {
|
256 |
21752
| this.attrID = attrID;
|
257 |
| } |
258 |
| |
259 |
66907
| public int getMatchLevel(IndexPattern p) {
|
260 |
66907
| if (pattern == null) {
|
261 |
66530
| if (p.isAbsolute() && p.getElementIDs().length != path.size()) {
|
262 |
1526
| return 0;
|
263 |
| } |
264 |
| |
265 |
65004
| if (p.getElementIDs().length > path.size()) {
|
266 |
0
| return 0;
|
267 |
| } |
268 |
| |
269 |
65004
| short elemID = p.getElementID();
|
270 |
65004
| if (elemID != ((Short) path.getLast()).shortValue() && elemID != IndexPattern.PATTERN_WILDCARD) {
|
271 |
38800
| return 0;
|
272 |
| } |
273 |
| |
274 |
26204
| short attrID = p.getAttributeID();
|
275 |
26204
| if (this.attrID != attrID && (attrID != IndexPattern.PATTERN_WILDCARD || this.attrID == IndexPattern.PATTERN_NONE)) {
|
276 |
21427
| return 0;
|
277 |
| } |
278 |
| |
279 |
4777
| pattern = new IndexPattern(symbols, pathToArray(), this.attrID);
|
280 |
| } |
281 |
| |
282 |
5154
| return pattern.getMatchLevel(p);
|
283 |
| } |
284 |
| |
285 |
9552
| public short getElementID() {
|
286 |
9552
| return pattern.getElementID();
|
287 |
| } |
288 |
| |
289 |
9552
| public short getAttributeID() {
|
290 |
9552
| return pattern.getAttributeID();
|
291 |
| } |
292 |
| |
293 |
4777
| private short[] pathToArray() {
|
294 |
4777
| short[] array = new short[path.size()];
|
295 |
| |
296 |
4777
| int j = 0;
|
297 |
4777
| for (Iterator i = path.iterator(); i.hasNext(); ) {
|
298 |
7860
| array[j++] = ((Short) i.next()).shortValue();
|
299 |
| } |
300 |
| |
301 |
4777
| return array;
|
302 |
| } |
303 |
| } |
304 |
| } |