KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > search > impl > lucene > query > AbsoluteStructuredFieldPosition


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.query;
18
19 import java.io.IOException JavaDoc;
20
21 /**
22  * This class patches a term at a specified location.
23  *
24  * @author andyh
25  */

26 public class AbsoluteStructuredFieldPosition extends AbstractStructuredFieldPosition
27 {
28
29     int requiredPosition;
30
31     /**
32      * Search for a term at the specified position.
33      */

34
35     public AbsoluteStructuredFieldPosition(String JavaDoc termText, int position)
36     {
37         super(termText, true, true);
38         this.requiredPosition = position;
39     }
40
41     /*
42      * (non-Javadoc)
43      *
44      * @see org.alfresco.lucene.extensions.StructuredFieldPosition#matches(int,
45      * org.apache.lucene.index.TermPositions)
46      */

47     public int matches(int start, int end, int offset) throws IOException JavaDoc
48     {
49         if (offset >= requiredPosition)
50         {
51             return -1;
52         }
53
54         if (getCachingTermPositions() != null)
55         {
56             // Doing "termText"
57
getCachingTermPositions().reset();
58             int count = getCachingTermPositions().freq();
59             int realPosition = 0;
60             int adjustedPosition = 0;
61             for (int i = 0; i < count; i++)
62             {
63                 realPosition = getCachingTermPositions().nextPosition();
64                 adjustedPosition = realPosition - start;
65                 if ((end != -1) && (realPosition > end))
66                 {
67                     return -1;
68                 }
69                 if (adjustedPosition > requiredPosition)
70                 {
71                     return -1;
72                 }
73                 if (adjustedPosition == requiredPosition)
74                 {
75                     return adjustedPosition;
76                 }
77
78             }
79         }
80         else
81         {
82             // Doing "*"
83
if ((offset + 1) == requiredPosition)
84             {
85                 return offset + 1;
86             }
87         }
88         return -1;
89
90     }
91
92     /*
93      * (non-Javadoc)
94      *
95      * @see org.alfresco.lucene.extensions.StructuredFieldPosition#getPosition()
96      */

97     public int getPosition()
98     {
99         return requiredPosition;
100     }
101
102     public String JavaDoc getDescription()
103     {
104         return "Absolute Named child";
105     }
106 }
Popular Tags