001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.tools.validator.internal.model;
019:
020: import java.util.HashMap;
021: import java.util.Map;
022: import java.util.Stack;
023: import javax.xml.namespace.QName;
024:
025: import org.apache.cxf.common.util.StringUtils;
026:
027: public class XNode {
028:
029: private String prefix;
030: private QName name;
031: private String attributeName;
032: private String attributeValue;
033: private XNode parentNode;
034:
035: private XNode failurePoint;
036:
037: private Map<String, String> nsMap = new HashMap<String, String>();
038:
039: public void setFailurePoint(XNode point) {
040: this .failurePoint = point;
041: }
042:
043: public XNode getFailurePoint() {
044: return this .failurePoint;
045: }
046:
047: public String getPrefix() {
048: return prefix;
049: }
050:
051: public void setPrefix(final String newPrefix) {
052: this .prefix = newPrefix;
053: }
054:
055: public QName getQName() {
056: return name;
057: }
058:
059: public void setQName(final QName newName) {
060: this .name = newName;
061: }
062:
063: public String getAttributeName() {
064: return attributeName;
065: }
066:
067: public void setAttributeName(final String newAttributeName) {
068: this .attributeName = newAttributeName;
069: }
070:
071: public String getAttributeValue() {
072: return attributeValue;
073: }
074:
075: public void setAttributeValue(final String newAttributeValue) {
076: this .attributeValue = newAttributeValue;
077: }
078:
079: public XNode getParentNode() {
080: return parentNode;
081: }
082:
083: public void setParentNode(final XNode newParentNode) {
084: this .parentNode = newParentNode;
085: }
086:
087: public Map<String, String> getNSMap() {
088: return nsMap;
089: }
090:
091: public String getText() {
092: StringBuffer sb = new StringBuffer();
093: sb.append("[");
094: sb.append(name.getLocalPart());
095: sb.append(":");
096: sb.append(getAttributeValue());
097: sb.append("]");
098: return sb.toString();
099: }
100:
101: public String getPlainText() {
102: StringBuffer sb = new StringBuffer();
103: Stack<XNode> parentNodes = getParentNodes();
104: while (!parentNodes.empty()) {
105: sb.append(parentNodes.pop().getText());
106: }
107: sb.append(getText());
108: nsMap.put(prefix, name.getNamespaceURI());
109: return sb.toString();
110: }
111:
112: public String getXPath() {
113: StringBuffer sb = new StringBuffer();
114: sb.append("/");
115: sb.append(prefix);
116: sb.append(":");
117: sb.append(name.getLocalPart());
118: if (!StringUtils.isEmpty(attributeName)
119: && !StringUtils.isEmpty(attributeValue)) {
120: sb.append("[@");
121: sb.append(attributeName);
122: sb.append("='");
123: sb.append(attributeValue);
124: sb.append("']");
125: }
126: return sb.toString();
127: }
128:
129: private Stack<XNode> getParentNodes() {
130: Stack<XNode> parentNodes = new Stack<XNode>();
131:
132: XNode pNode = getParentNode();
133: while (pNode != null) {
134: nsMap.put(pNode.getPrefix(), pNode.getQName()
135: .getNamespaceURI());
136: parentNodes.push(pNode);
137: pNode = pNode.getParentNode();
138: }
139: return parentNodes;
140: }
141:
142: public String toString() {
143: Stack<XNode> parentNodes = getParentNodes();
144: StringBuffer sb = new StringBuffer();
145: while (!parentNodes.empty()) {
146: sb.append(parentNodes.pop().getXPath());
147: }
148: sb.append(getXPath());
149: nsMap.put(prefix, name.getNamespaceURI());
150: return sb.toString();
151: }
152: }
|