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;
021:
022: /**
023: * Represents a node test on type.
024: *
025: * @author Enrico Lelina
026: * @author nk160297
027: * @version
028: */
029: public class StepNodeTypeTest extends StepNodeTest {
030:
031: private static StepNodeTestType[] int2Type = new StepNodeTestType[] {
032: StepNodeTestType.NODETYPE_NODE,
033: StepNodeTestType.NODETYPE_TEXT,
034: StepNodeTestType.NODETYPE_COMMENT,
035: StepNodeTestType.NODETYPE_PI };
036:
037: /** The node type. */
038: private StepNodeTestType mNodeType;
039:
040: // The instruciton relates to the processing_instruction() only!
041: private String mInstruction;
042:
043: /**
044: * Constructor.
045: * @param nodeType the node type
046: */
047: public StepNodeTypeTest(int intNodeType, String instruction) {
048: super ();
049: assert intNodeType <= int2Type.length : "The index of node test type "
050: + intNodeType + " is out of possible values"; // NOI18N
051: mNodeType = int2Type[intNodeType - 1];
052: mInstruction = instruction;
053: }
054:
055: /**
056: * Constructor.
057: * @param nodeType the node type
058: */
059: public StepNodeTypeTest(StepNodeTestType nodeType,
060: String instruction) {
061: super ();
062: mNodeType = nodeType;
063: mInstruction = instruction;
064: }
065:
066: /**
067: * Gets the node type.
068: * @return the node type
069: */
070: public StepNodeTestType getNodeType() {
071: return mNodeType;
072: }
073:
074: public String getInstruction() {
075: return mInstruction;
076: }
077:
078: @Override
079: public boolean equals(Object obj) {
080: if (obj instanceof StepNodeTypeTest) {
081: StepNodeTypeTest otherStep = (StepNodeTypeTest) obj;
082: if (otherStep.mNodeType == mNodeType) {
083: if (mNodeType == StepNodeTestType.NODETYPE_PI) {
084: if (otherStep.mInstruction == null) {
085: return mInstruction == null;
086: } else {
087: return otherStep.mInstruction
088: .equals(mInstruction);
089: }
090: } else {
091: // Ignore the instruction if another type is used.
092: return true;
093: }
094: }
095: }
096: return false;
097: }
098:
099: public String getXPathText() {
100: StepNodeTestType type = getNodeType();
101: if (type == StepNodeTestType.NODETYPE_PI
102: && mInstruction != null && mInstruction.length() != 0) {
103: return type.getXPathText() + "(" + mInstruction + ")";
104: }
105: return type.getXPathText() + "()";
106: }
107:
108: @Override
109: public String toString() {
110: return getXPathText();
111: }
112: }
|