001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019:
020: package org.netbeans.modules.xml.xpath.ext.impl;
021:
022: import javax.xml.namespace.QName;
023: import org.netbeans.modules.xml.xpath.ext.LocationStep;
024: import org.netbeans.modules.xml.xpath.ext.StepNodeTest;
025: import org.netbeans.modules.xml.xpath.ext.StepNodeNameTest;
026: import org.netbeans.modules.xml.xpath.ext.StepNodeTypeTest;
027: import org.netbeans.modules.xml.xpath.ext.XPathUtils;
028: import org.netbeans.modules.xml.xpath.ext.XPathAxis;
029: import org.netbeans.modules.xml.xpath.ext.XPathModel;
030: import org.netbeans.modules.xml.xpath.ext.XPathPredicateExpression;
031: import org.netbeans.modules.xml.xpath.ext.XPathSchemaContext;
032: import org.netbeans.modules.xml.xpath.ext.visitor.XPathVisitor;
033:
034: /**
035: * Represents a location path step.
036: *
037: * @author Enrico Lelina
038: * @author nk160297
039: * @version
040: */
041: public class LocationStepImpl extends XPathExpressionImpl implements
042: LocationStep {
043:
044: private static XPathAxis[] int2Axis = new XPathAxis[] {
045: XPathAxis.SELF, XPathAxis.CHILD, XPathAxis.PARENT,
046: XPathAxis.ANCESTOR, XPathAxis.ATTRIBUTE,
047: XPathAxis.NAMESPACE, XPathAxis.PRECEDING,
048: XPathAxis.FOLLOWING, XPathAxis.DESCENDANT,
049: XPathAxis.ANCESTOR_OR_SELF, XPathAxis.DESCENDANT_OR_SELF,
050: XPathAxis.FOLLOWING_SIBLING, XPathAxis.PRECEDING_SIBLING };
051:
052: /** The axis. */
053: private XPathAxis mAxis;
054:
055: /** The node test. */
056: private StepNodeTest mNodeTest;
057:
058: /** predicates */
059: private XPathPredicateExpression[] mPredicates = null;
060:
061: private XPathSchemaContext mSchemaContext;
062:
063: /** Constructor. */
064: public LocationStepImpl(XPathModel model) {
065: this (model, 0, null, null);
066: }
067:
068: /**
069: * Constructor.
070: * @param axis the axis
071: * @param nodeTest the node test
072: */
073: public LocationStepImpl(XPathModel model, int axis,
074: StepNodeTest nodeTest, XPathPredicateExpression[] predicates) {
075: super (model);
076: assert axis < int2Axis.length : "The index of axis " + axis
077: + " is out of possible values"; // NOI18N
078: //
079: setAxis(int2Axis[axis - 1]);
080: setNodeTest(nodeTest);
081: setPredicates(predicates);
082: }
083:
084: /**
085: * Constructor.
086: * @param axis the axis
087: * @param nodeTest the node test
088: */
089: public LocationStepImpl(XPathModel model, XPathAxis axis,
090: StepNodeTest nodeTest, XPathPredicateExpression[] predicates) {
091: super (model);
092: //
093: if (axis == null) {
094: axis = XPathAxis.CHILD;
095: }
096: //
097: setAxis(axis);
098: setNodeTest(nodeTest);
099: setPredicates(predicates);
100: }
101:
102: /**
103: * Gets the axis.
104: * @return the axis
105: */
106: public XPathAxis getAxis() {
107: return mAxis;
108: }
109:
110: /**
111: * Sets the axis.
112: * @param axis the axis
113: */
114: public void setAxis(XPathAxis axis) {
115: mAxis = axis;
116: }
117:
118: /**
119: * Gets the node test.
120: * @return the node test
121: */
122: public StepNodeTest getNodeTest() {
123: return mNodeTest;
124: }
125:
126: /**
127: * Sets the node test.
128: * @param nodeTest the node test
129: */
130: public void setNodeTest(StepNodeTest nodeTest) {
131: mNodeTest = nodeTest;
132: }
133:
134: /**
135: * Gets the Predicates
136: * @return the predicates
137: */
138: public XPathPredicateExpression[] getPredicates() {
139: return mPredicates;
140: }
141:
142: /**
143: * Sets the Predicates
144: * @param predicates list of predicates
145: */
146: public void setPredicates(XPathPredicateExpression[] predicates) {
147: mPredicates = predicates;
148: }
149:
150: /**
151: * Gets the string representation.
152: * @return the string representation
153: */
154: public String getString() {
155: StringBuilder sb = new StringBuilder();
156: //
157: StepNodeTest nodeTest = getNodeTest();
158: if (nodeTest instanceof StepNodeNameTest) {
159: sb.append(getAxis().getShortForm());
160: //
161: StepNodeNameTest snnt = (StepNodeNameTest) nodeTest;
162: if (snnt.isWildcard()) {
163: sb.append("*"); // NOI18N
164: } else {
165: QName nodeName = ((StepNodeNameTest) nodeTest)
166: .getNodeName();
167: sb.append(XPathUtils.qNameObjectToString(nodeName));
168: }
169: } else if (nodeTest instanceof StepNodeTypeTest) {
170: StepNodeTypeTest sntt = (StepNodeTypeTest) nodeTest;
171: switch (sntt.getNodeType()) {
172: case NODETYPE_NODE:
173: switch (getAxis()) {
174: case CHILD: // it means that the location step is "node()"
175: sb.append(sntt.getXPathText());
176: break;
177: case SELF: // it means that the location step is abbreviated step "."
178: sb.append("."); // NOI18N
179: break;
180: case PARENT: // it means that the location step is abbreviated step ".."
181: sb.append(".."); // NOI18N
182: break;
183: case DESCENDANT_OR_SELF: // it means that the location step is abbreviated step "//"
184: // It doesn't necessary to append anything here because
185: // the double slash "//" abbreviated step is a kind of "empty" step.
186: // It is a step without a content between two slashes.
187: break;
188: default:
189: // other axis are not supported here
190: }
191: break;
192: case NODETYPE_COMMENT:
193: case NODETYPE_PI:
194: case NODETYPE_TEXT:
195: sb.append(sntt.getXPathText());
196: break;
197: }
198: }
199: //
200: return sb.toString();
201: }
202:
203: @Override
204: public void accept(XPathVisitor visitor) {
205: visitor.visit(this );
206: }
207:
208: public XPathSchemaContext getSchemaContext() {
209: if (mSchemaContext == null) {
210: if (myModel.getRootExpression() != null) {
211: myModel.resolveExtReferences(false);
212: } else {
213: myModel.resolveExpressionExtReferences(this );
214: }
215: }
216: return mSchemaContext;
217: }
218:
219: public void setSchemaContext(XPathSchemaContext newContext) {
220: mSchemaContext = newContext;
221: }
222:
223: @Override
224: public String toString() {
225: return getString();
226: }
227:
228: @Override
229: public boolean equals(Object obj) {
230: if (obj instanceof LocationStep) {
231: //
232: // Compare Node Test
233: LocationStep step2 = (LocationStep) obj;
234: StepNodeTest snt2 = step2.getNodeTest();
235: if (!snt2.equals(mNodeTest)) {
236: return false;
237: }
238: //
239: // Compare Axis
240: if (step2.getAxis() != mAxis) {
241: return false;
242: }
243: //
244: // Compare predicates
245: XPathPredicateExpression[] predicates2 = step2
246: .getPredicates();
247: if (!XPathUtils.samePredicatesArr(mPredicates, predicates2)) {
248: return false;
249: }
250: //
251: return true;
252: }
253: return false;
254: }
255: }
|