KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > search > impl > lucene > LuceneXPathHandler


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.search.impl.lucene;
18
19 import java.util.ArrayList JavaDoc;
20
21 import org.alfresco.repo.search.impl.lucene.analysis.PathTokenFilter;
22 import org.alfresco.repo.search.impl.lucene.query.AbsoluteStructuredFieldPosition;
23 import org.alfresco.repo.search.impl.lucene.query.DescendantAndSelfStructuredFieldPosition;
24 import org.alfresco.repo.search.impl.lucene.query.PathQuery;
25 import org.alfresco.repo.search.impl.lucene.query.RelativeStructuredFieldPosition;
26 import org.alfresco.repo.search.impl.lucene.query.SelfAxisStructuredFieldPosition;
27 import org.alfresco.repo.search.impl.lucene.query.StructuredFieldPosition;
28 import org.alfresco.service.cmr.dictionary.DictionaryService;
29 import org.alfresco.service.namespace.NamespacePrefixResolver;
30 import org.saxpath.Axis;
31 import org.saxpath.Operator;
32 import org.saxpath.SAXPathException;
33 import org.saxpath.XPathHandler;
34
35 public class LuceneXPathHandler implements XPathHandler
36 {
37     private PathQuery query;
38
39     private boolean isAbsolutePath = true;
40
41     int absolutePosition = 0;
42
43     private NamespacePrefixResolver namespacePrefixResolver;
44
45     private DictionaryService dictionaryService;
46
47     public LuceneXPathHandler()
48     {
49         super();
50     }
51
52     public PathQuery getQuery()
53     {
54         return this.query;
55     }
56
57     public void endAbsoluteLocationPath() throws SAXPathException
58     {
59         // No action
60
}
61
62     public void endAdditiveExpr(int op) throws SAXPathException
63     {
64         switch (op)
65         {
66         case Operator.NO_OP:
67             break;
68         case Operator.ADD:
69         case Operator.SUBTRACT:
70             throw new UnsupportedOperationException JavaDoc();
71         default:
72             throw new UnsupportedOperationException JavaDoc("Unknown operation " + op);
73         }
74     }
75
76     public void endAllNodeStep() throws SAXPathException
77     {
78         // Nothing to do
79
// Todo: Predicates
80
}
81
82     public void endAndExpr(boolean create) throws SAXPathException
83     {
84         if (create)
85         {
86             throw new UnsupportedOperationException JavaDoc();
87         }
88     }
89
90     public void endCommentNodeStep() throws SAXPathException
91     {
92         // TODO Auto-generated method stub
93
throw new UnsupportedOperationException JavaDoc();
94     }
95
96     public void endEqualityExpr(int op) throws SAXPathException
97     {
98         switch (op)
99         {
100         case Operator.NO_OP:
101             break;
102         case Operator.EQUALS:
103         case Operator.NOT_EQUALS:
104             throw new UnsupportedOperationException JavaDoc();
105         default:
106             throw new UnsupportedOperationException JavaDoc("Unknown operation " + op);
107         }
108     }
109
110     public void endFilterExpr() throws SAXPathException
111     {
112         // TODO Auto-generated method stub
113
throw new UnsupportedOperationException JavaDoc();
114     }
115
116     public void endFunction() throws SAXPathException
117     {
118         // TODO Auto-generated method stub
119
throw new UnsupportedOperationException JavaDoc();
120     }
121
122     public void endMultiplicativeExpr(int op) throws SAXPathException
123     {
124         switch (op)
125         {
126         case Operator.NO_OP:
127             break;
128         case Operator.MULTIPLY:
129         case Operator.DIV:
130         case Operator.MOD:
131             throw new UnsupportedOperationException JavaDoc();
132         default:
133             throw new UnsupportedOperationException JavaDoc("Unknown operation " + op);
134         }
135     }
136
137     public void endNameStep() throws SAXPathException
138     {
139         // Do nothing at the moment
140
// Could have repdicates
141
}
142
143     public void endOrExpr(boolean create) throws SAXPathException
144     {
145         if (create)
146         {
147             throw new UnsupportedOperationException JavaDoc();
148         }
149     }
150
151     public void endPathExpr() throws SAXPathException
152     {
153         // Already built
154
}
155
156     public void endPredicate() throws SAXPathException
157     {
158         // TODO Auto-generated method stub
159
throw new UnsupportedOperationException JavaDoc();
160     }
161
162     public void endProcessingInstructionNodeStep() throws SAXPathException
163     {
164         // TODO Auto-generated method stub
165
throw new UnsupportedOperationException JavaDoc();
166     }
167
168     public void endRelationalExpr(int op) throws SAXPathException
169     {
170         switch (op)
171         {
172         case Operator.NO_OP:
173             break;
174         case Operator.GREATER_THAN:
175         case Operator.GREATER_THAN_EQUALS:
176         case Operator.LESS_THAN:
177         case Operator.LESS_THAN_EQUALS:
178             throw new UnsupportedOperationException JavaDoc();
179         default:
180             throw new UnsupportedOperationException JavaDoc("Unknown operation " + op);
181         }
182     }
183
184     public void endRelativeLocationPath() throws SAXPathException
185     {
186         // No action
187
}
188
189     public void endTextNodeStep() throws SAXPathException
190     {
191         // TODO Auto-generated method stub
192
throw new UnsupportedOperationException JavaDoc();
193     }
194
195     public void endUnaryExpr(int op) throws SAXPathException
196     {
197         switch (op)
198         {
199         case Operator.NO_OP:
200             break;
201         case Operator.NEGATIVE:
202             throw new UnsupportedOperationException JavaDoc();
203         default:
204             throw new UnsupportedOperationException JavaDoc("Unknown operation " + op);
205         }
206     }
207
208     public void endUnionExpr(boolean create) throws SAXPathException
209     {
210         if (create)
211         {
212             throw new UnsupportedOperationException JavaDoc();
213         }
214     }
215
216     public void endXPath() throws SAXPathException
217     {
218         // Do nothing at the moment
219
}
220
221     public void literal(String JavaDoc arg0) throws SAXPathException
222     {
223         // TODO Auto-generated method stub
224
throw new UnsupportedOperationException JavaDoc();
225     }
226
227     public void number(double arg0) throws SAXPathException
228     {
229         // TODO Auto-generated method stub
230
throw new UnsupportedOperationException JavaDoc();
231     }
232
233     public void number(int arg0) throws SAXPathException
234     {
235         // TODO Auto-generated method stub
236
throw new UnsupportedOperationException JavaDoc();
237     }
238
239     public void startAbsoluteLocationPath() throws SAXPathException
240     {
241         if (!isAbsolutePath)
242         {
243             throw new IllegalStateException JavaDoc();
244         }
245
246     }
247
248     public void startAdditiveExpr() throws SAXPathException
249     {
250         // Do nothing at the moment
251
}
252
253     public void startAllNodeStep(int axis) throws SAXPathException
254     {
255         switch (axis)
256         {
257         case Axis.CHILD:
258             if (isAbsolutePath)
259             {
260                 // addAbsolute(null, null);
261
// We can always do relative stuff
262
addRelative(null, null);
263             }
264             else
265             {
266                 addRelative(null, null);
267             }
268             break;
269         case Axis.DESCENDANT_OR_SELF:
270             query.appendQuery(getArrayList(new DescendantAndSelfStructuredFieldPosition(), new DescendantAndSelfStructuredFieldPosition()));
271             break;
272         case Axis.SELF:
273             query.appendQuery(getArrayList(new SelfAxisStructuredFieldPosition(), new SelfAxisStructuredFieldPosition()));
274             break;
275         default:
276             throw new UnsupportedOperationException JavaDoc();
277         }
278     }
279
280     private ArrayList JavaDoc<StructuredFieldPosition> getArrayList(StructuredFieldPosition one, StructuredFieldPosition two)
281     {
282         ArrayList JavaDoc<StructuredFieldPosition> answer = new ArrayList JavaDoc<StructuredFieldPosition>(2);
283         answer.add(one);
284         answer.add(two);
285         return answer;
286     }
287
288     public void startAndExpr() throws SAXPathException
289     {
290         // Do nothing
291
}
292
293     public void startCommentNodeStep(int arg0) throws SAXPathException
294     {
295         // TODO Auto-generated method stub
296
throw new UnsupportedOperationException JavaDoc();
297     }
298
299     public void startEqualityExpr() throws SAXPathException
300     {
301         // Do nothing
302
}
303
304     public void startFilterExpr() throws SAXPathException
305     {
306         // TODO Auto-generated method stub
307
throw new UnsupportedOperationException JavaDoc();
308     }
309
310     public void startFunction(String JavaDoc arg0, String JavaDoc arg1) throws SAXPathException
311     {
312         // TODO Auto-generated method stub
313
throw new UnsupportedOperationException JavaDoc();
314     }
315
316     public void startMultiplicativeExpr() throws SAXPathException
317     {
318         // Do nothing at the moment
319
}
320
321     public void startNameStep(int axis, String JavaDoc nameSpace, String JavaDoc localName) throws SAXPathException
322     {
323         switch (axis)
324         {
325         case Axis.CHILD:
326             if (isAbsolutePath)
327             {
328                 // addAbsolute(nameSpace, localName);
329
// we can always do relative stuff
330
addRelative(nameSpace, localName);
331             }
332             else
333             {
334                 addRelative(nameSpace, localName);
335             }
336             break;
337         default:
338             throw new UnsupportedOperationException JavaDoc();
339         }
340
341     }
342
343     private void addAbsolute(String JavaDoc nameSpace, String JavaDoc localName)
344     {
345         ArrayList JavaDoc<StructuredFieldPosition> answer = new ArrayList JavaDoc<StructuredFieldPosition>(2);
346         // TODO: Resolve name space
347
absolutePosition++;
348         if ((nameSpace == null) || (nameSpace.length() == 0))
349         {
350
351             if(localName.equals("*"))
352             {
353                 answer.add(new RelativeStructuredFieldPosition("*"));
354             }
355             else if (namespacePrefixResolver.getNamespaceURI("") == null)
356             {
357                 answer.add(new AbsoluteStructuredFieldPosition(PathTokenFilter.NO_NS_TOKEN_TEXT, absolutePosition));
358             }
359             else
360             {
361                 answer.add(new AbsoluteStructuredFieldPosition(namespacePrefixResolver.getNamespaceURI(""), absolutePosition));
362             }
363
364         }
365         else
366         {
367             answer.add(new AbsoluteStructuredFieldPosition(namespacePrefixResolver.getNamespaceURI(nameSpace), absolutePosition));
368         }
369
370         absolutePosition++;
371         if ((localName == null) || (localName.length() == 0))
372         {
373             answer.add(new AbsoluteStructuredFieldPosition("*", absolutePosition));
374         }
375         else
376         {
377             answer.add(new AbsoluteStructuredFieldPosition(localName, absolutePosition));
378         }
379         query.appendQuery(answer);
380
381     }
382
383     private void addRelative(String JavaDoc nameSpace, String JavaDoc localName)
384     {
385         ArrayList JavaDoc<StructuredFieldPosition> answer = new ArrayList JavaDoc<StructuredFieldPosition>(2);
386         if ((nameSpace == null) || (nameSpace.length() == 0))
387         {
388             if(localName.equals("*"))
389             {
390                 answer.add(new RelativeStructuredFieldPosition("*"));
391             }
392             else if (namespacePrefixResolver.getNamespaceURI("") == null)
393             {
394                 answer.add(new RelativeStructuredFieldPosition(PathTokenFilter.NO_NS_TOKEN_TEXT));
395             }
396             else
397             {
398                 answer.add(new RelativeStructuredFieldPosition(namespacePrefixResolver.getNamespaceURI("")));
399             }
400         }
401         else
402         {
403             answer.add(new RelativeStructuredFieldPosition(namespacePrefixResolver.getNamespaceURI(nameSpace)));
404         }
405
406         if ((localName == null) || (localName.length() == 0))
407         {
408             answer.add(new RelativeStructuredFieldPosition("*"));
409         }
410         else
411         {
412             answer.add(new RelativeStructuredFieldPosition(localName));
413         }
414         query.appendQuery(answer);
415     }
416
417     public void startOrExpr() throws SAXPathException
418     {
419         // Do nothing at the moment
420
}
421
422     public void startPathExpr() throws SAXPathException
423     {
424         // Just need one!
425
}
426
427     public void startPredicate() throws SAXPathException
428     {
429         // TODO Auto-generated method stub
430
throw new UnsupportedOperationException JavaDoc();
431     }
432
433     public void startProcessingInstructionNodeStep(int arg0, String JavaDoc arg1) throws SAXPathException
434     {
435         // TODO Auto-generated method stub
436
throw new UnsupportedOperationException JavaDoc();
437     }
438
439     public void startRelationalExpr() throws SAXPathException
440     {
441         // Do nothing at the moment
442
}
443
444     public void startRelativeLocationPath() throws SAXPathException
445     {
446         isAbsolutePath = false;
447     }
448
449     public void startTextNodeStep(int arg0) throws SAXPathException
450     {
451         // TODO Auto-generated method stub
452
throw new UnsupportedOperationException JavaDoc();
453     }
454
455     public void startUnaryExpr() throws SAXPathException
456     {
457         // Do nothing for now
458
}
459
460     public void startUnionExpr() throws SAXPathException
461     {
462         // Do nothing at the moment
463
}
464
465     public void startXPath() throws SAXPathException
466     {
467         query = new PathQuery(dictionaryService);
468     }
469
470     public void variableReference(String JavaDoc uri, String JavaDoc localName) throws SAXPathException
471     {
472        
473     }
474
475     public void setNamespacePrefixResolver(NamespacePrefixResolver namespacePrefixResolver)
476     {
477         this.namespacePrefixResolver = namespacePrefixResolver;
478     }
479
480     public void setDictionaryService(DictionaryService dictionaryService)
481     {
482         this.dictionaryService = dictionaryService;
483     }
484     
485     
486     
487
488 }
489
Popular Tags