KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > xml > xqueryevaluator > eval > XPathFilter


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 XQuark Group.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 package org.xquark.xml.xqueryevaluator.eval;
24
25
26 public class XPathFilter implements Cloneable JavaDoc {
27     private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
28     private static final String JavaDoc RCSName = "$Name: $";
29     private int index = 0;
30     private short[] skipCounts;
31     XPathExpr xpath;
32     
33     public XPathFilter(XPathExpr xpath) {
34       this.xpath = xpath;
35       skipCounts = new short[xpath.getStepCount()];
36     }
37     
38     public boolean isDone() {
39       return index == xpath.getStepCount();
40     }
41     
42     protected Object JavaDoc clone() {
43         XPathFilter result = null;
44         try {
45             result = (XPathFilter) super.clone();
46             result.skipCounts = new short[skipCounts.length];
47             for (int i = 0; i < skipCounts.length; i++) {
48                 result.skipCounts[i] = skipCounts[i];
49             }
50         } catch (CloneNotSupportedException JavaDoc ex) {}
51         return result;
52     }
53     
54     public boolean pushElement(String JavaDoc namespaceURI, String JavaDoc localName) {
55       if (isDone()) return false;
56       if (xpath.isAnyLevel(index)) {
57         skipCounts[index]++;
58         return true;
59       }
60       if (xpath.isSelf(index)) return false;
61       if (xpath.matchElement(namespaceURI, localName, index)) {
62         index++;
63         skipSelfStep(namespaceURI, localName);
64         return true;
65       }
66       return false;
67     }
68     
69     public boolean matchDescendantOrSelfStep(String JavaDoc namespaceURI, String JavaDoc localName) {
70         return xpath.isAnyLevel(index) && (xpath.isSelf(index) || skipCounts[index] > 0) && xpath.matchElement(namespaceURI, localName, index);
71     }
72     
73     public XPathFilter fork(String JavaDoc namespaceURI, String JavaDoc localName) {
74         XPathFilter next = (XPathFilter) clone();
75         if (next.skipCounts[index] > 0) next.skipCounts[index]--;
76         next.index++;
77         next.skipSelfStep(namespaceURI, localName);
78         return next;
79     }
80     
81     public void skipSelfStep(String JavaDoc namespaceURI, String JavaDoc localName) {
82         while (!xpath.isAnyLevel(index) && xpath.isSelfElement(index) && xpath.matchElement(namespaceURI, localName, index)) {
83             index++;
84         }
85     }
86     
87     public boolean matchSelf(String JavaDoc uri, String JavaDoc localName) {
88         if (isDone()) return false;
89         for (int i = index; i < xpath.getStepCount(); i++) {
90             if (!xpath.isSelf(i) || !xpath.matchElement(uri, localName, i))
91                 return false;
92         }
93         return true;
94     }
95     
96     public boolean matchAttribute(String JavaDoc namespaceURI, String JavaDoc localName) {
97       if (isDone()) return false;
98       if (xpath.matchAttribute(namespaceURI, localName, index)) {
99         return true;
100       }
101       return false;
102     }
103     
104     public boolean matchText() {
105         if (isDone()) return false;
106         if (!xpath.isAnyLevel(index) && xpath.isSelf(index)) return false;
107         if (xpath.matchText(index)) {
108             for (int i = index+1; i < xpath.getStepCount(); i++) {
109                 if (!xpath.isSelf(i) || !xpath.matchText(i))
110                     return false;
111             }
112             return true;
113         }
114         return false;
115     }
116     
117     public boolean matchSelfText() {
118         if (isDone()) return false;
119         for (int i = index; i < xpath.getStepCount(); i++) {
120             if (!xpath.isSelf(i) || !xpath.matchText(i))
121                 return false;
122         }
123         return true;
124     }
125     
126     public boolean matchSelfNode() {
127         if (isDone()) return false;
128         for (int i = index; i < xpath.getStepCount(); i++) {
129             if (!xpath.isSelf(i) || !xpath.isSelfNode(i))
130                 return false;
131         }
132         return true;
133     }
134     
135     public void popElement() {
136         if (!isDone() && skipCounts[index] > 0) skipCounts[index]--;
137         else if (index > 0) index--;
138     }
139     
140   }
141
Popular Tags