KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > xml > XPath


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 /* $Id: XPath.java 42598 2004-03-01 16:18:28Z gregor $ */
19
20 package org.apache.lenya.xml;
21
22 import java.util.StringTokenizer JavaDoc;
23
24 import org.w3c.dom.Node JavaDoc;
25
26 public class XPath {
27     String JavaDoc xpath = null;
28     String JavaDoc[] parts = null;
29
30     /**
31      *
32      */

33     public XPath(String JavaDoc xpath) {
34         this.xpath = xpath;
35
36         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(xpath, "/");
37         int length = st.countTokens();
38         parts = new String JavaDoc[length];
39
40         for (int i = 0; i < length; i++) {
41             parts[i] = st.nextToken();
42         }
43     }
44
45     /**
46      *
47      */

48     public XPath getParent() {
49         String JavaDoc parentXPath = "";
50
51         for (int i = 0; i < (parts.length - 1); i++) {
52             parentXPath = parentXPath + "/" + parts[i];
53         }
54
55         return new XPath(parentXPath);
56     }
57
58     /**
59      *
60      */

61     public short getType() {
62         if (parts[parts.length - 1].indexOf("@") == 0) {
63             return Node.ATTRIBUTE_NODE;
64         }
65
66         return Node.ELEMENT_NODE;
67     }
68
69     /**
70      *
71      */

72     public String JavaDoc toString() {
73         return xpath;
74     }
75
76     /**
77      *
78      */

79     public String JavaDoc getName() {
80         if (getType() == Node.ATTRIBUTE_NODE) {
81             return parts[parts.length - 1].substring(1);
82         }
83
84         return parts[parts.length - 1];
85     }
86
87     /**
88      * Describe 'getName' method here.
89      *
90      * @return a value of type 'String'
91      */

92     public String JavaDoc getElementName() {
93         if (getType() == Node.ATTRIBUTE_NODE) {
94             return parts[parts.length - 2];
95         }
96
97         return parts[parts.length - 1];
98     }
99
100     /**
101      *
102      */

103     public String JavaDoc getNameWithoutPredicates() {
104         return removePredicates(getName());
105     }
106
107     /**
108      * Remove predicates (square brackets), http://www.w3.org/TR/xpath
109      */

110     public String JavaDoc removePredicates(String JavaDoc s) {
111         int index = s.indexOf("[");
112
113         if (index >= 0) {
114             return s.substring(0, index);
115         }
116
117         return s;
118     }
119 }
120
Popular Tags