KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > schema > XPathExpr


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
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.schema;
24
25 public class XPathExpr {
26 private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
27 private static final String JavaDoc RCSName = "$Name: $";
28   private java.util.ArrayList JavaDoc fields = new java.util.ArrayList JavaDoc(4);
29   private boolean anyLevel = false;
30   private boolean isAttribute = false;
31
32   public XPathExpr(boolean anyLevel) {
33     this.anyLevel = anyLevel;
34   }
35
36   public void addStep(String JavaDoc namespaceURI, String JavaDoc localName) {
37     fields.add(new XPathExpr.Step(namespaceURI, localName));
38   }
39
40   public void setAttribute(boolean isAttribute) {
41     this.isAttribute = isAttribute;
42   }
43
44   public int getStepCount() {
45     return fields.size();
46   }
47
48   public boolean matches(String JavaDoc namespaceURI, String JavaDoc localName, int index, boolean attr) {
49     if (index >= fields.size()) return false;
50     if (isAttribute && index == fields.size()-1 && !attr) return false;
51     if (attr && (!isAttribute || index != fields.size()-1)) return false;
52     XPathExpr.Step step = (XPathExpr.Step)fields.get(index);
53     return step.matches(namespaceURI, localName);
54   }
55
56   public boolean isAnyLevel() {
57     return anyLevel;
58   }
59
60   static class Step {
61     private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
62     private static final String JavaDoc RCSName = "$Name: $";
63     String JavaDoc namespaceURI;
64     String JavaDoc localName;
65     boolean isAny = false;
66
67     Step(String JavaDoc namespaceURI, String JavaDoc localName) {
68       this.namespaceURI = namespaceURI;
69       this.localName = localName;
70       if ("*".equals(localName)) {
71         this.localName = null;
72         if (namespaceURI == null)
73           isAny = true;
74       }
75     }
76
77     boolean matches(String JavaDoc ns, String JavaDoc name) {
78       if (isAny)
79     return true;
80       else if (localName == null)
81     return namespaceURI.equals(ns);
82       else
83     return localName.equals(name)
84       && ((namespaceURI == null && ns == null) || (namespaceURI != null && namespaceURI.equals(ns)));
85     }
86   }
87 }
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
Popular Tags