001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: /* $Id: XPath.java 473861 2006-11-12 03:51:14Z gregor $ */
020:
021: package org.apache.lenya.xml;
022:
023: import java.util.StringTokenizer;
024:
025: import org.w3c.dom.Node;
026:
027: /**
028: * Helper class for XPath operations
029: */
030: public class XPath {
031: String xpath = null;
032: String[] parts = null;
033:
034: /**
035: * Constructor
036: * @param _xpath The Xpath
037: */
038: public XPath(String _xpath) {
039: this .xpath = _xpath;
040:
041: StringTokenizer st = new StringTokenizer(_xpath, "/");
042: int length = st.countTokens();
043: this .parts = new String[length];
044:
045: for (int i = 0; i < length; i++) {
046: this .parts[i] = st.nextToken();
047: }
048: }
049:
050: /**
051: * Get the parent path
052: * @return The parent path
053: */
054: public XPath getParent() {
055: StringBuffer buf = new StringBuffer();
056:
057: for (int i = 0; i < (this .parts.length - 1); i++) {
058: buf.append("/" + this .parts[i]);
059: }
060:
061: return new XPath(buf.toString());
062: }
063:
064: /**
065: * Get the type of a node. Only supports attribute and element nodes
066: * @return The node type
067: */
068: public short getType() {
069: if (this .parts[this .parts.length - 1].indexOf("@") == 0) {
070: return Node.ATTRIBUTE_NODE;
071: }
072: return Node.ELEMENT_NODE;
073: }
074:
075: /**
076: * Return a string representation of the XPath
077: * @return The Xpath
078: */
079: public String toString() {
080: return this .xpath;
081: }
082:
083: /**
084: * Get the name
085: * @return The name
086: */
087: public String getName() {
088: if (getType() == Node.ATTRIBUTE_NODE) {
089: return this .parts[this .parts.length - 1].substring(1);
090: }
091: return this .parts[this .parts.length - 1];
092: }
093:
094: /**
095: * Return the name of the element
096: * @return the name of the element
097: */
098: public String getElementName() {
099: if (getType() == Node.ATTRIBUTE_NODE) {
100: return this .parts[this .parts.length - 2];
101: }
102: return this .parts[this .parts.length - 1];
103: }
104:
105: /**
106: * Get the name without predicates
107: * @return The name without predicates
108: */
109: public String getNameWithoutPredicates() {
110: return removePredicates(getName());
111: }
112:
113: /**
114: * Remove predicates (square brackets), http://www.w3.org/TR/xpath
115: * @param s The string to remove predicates from
116: * @return The string without predicates
117: */
118: public String removePredicates(String s) {
119: int index = s.indexOf("[");
120:
121: if (index >= 0) {
122: return s.substring(0, index);
123: }
124:
125: return s;
126: }
127: }
|