| 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
| 2 | * |
| 3 | * This Source Code Form is subject to the terms of the Mozilla Public |
| 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 | |
| 7 | package org.mozilla.javascript.ast; |
| 8 | |
| 9 | import org.mozilla.javascript.Token; |
| 10 | |
| 11 | /** |
| 12 | * AST node for an E4X XML {@code [expr]} property-ref expression. |
| 13 | * The node type is {@link Token#REF_NAME}.<p> |
| 14 | * |
| 15 | * Syntax:<p> |
| 16 | * |
| 17 | * <pre> @<i><sub>opt</sub></i> ns:: <i><sub>opt</sub></i> name</pre> |
| 18 | * |
| 19 | * Examples include {@code name}, {@code ns::name}, {@code ns::*}, |
| 20 | * {@code *::name}, {@code *::*}, {@code @attr}, {@code @ns::attr}, |
| 21 | * {@code @ns::*}, {@code @*::attr}, {@code @*::*} and {@code @*}.<p> |
| 22 | * |
| 23 | * The node starts at the {@code @} token, if present. Otherwise it starts |
| 24 | * at the namespace name. The node bounds extend through the closing |
| 25 | * right-bracket, or if it is missing due to a syntax error, through the |
| 26 | * end of the index expression.<p> |
| 27 | */ |
| 28 | public class XmlPropRef extends XmlRef { |
| 29 | |
| 30 | private Name propName; |
| 31 | |
| 32 | { |
| 33 | type = Token.REF_NAME; |
| 34 | } |
| 35 | |
| 36 | public XmlPropRef() { |
| 37 | } |
| 38 | |
| 39 | public XmlPropRef(int pos) { |
| 40 | super(pos); |
| 41 | } |
| 42 | |
| 43 | public XmlPropRef(int pos, int len) { |
| 44 | super(pos, len); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Returns property name. |
| 49 | */ |
| 50 | public Name getPropName() { |
| 51 | return propName; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Sets property name, and sets its parent to this node. |
| 56 | * @throws IllegalArgumentException if {@code propName} is {@code null} |
| 57 | */ |
| 58 | public void setPropName(Name propName) { |
| 59 | assertNotNull(propName); |
| 60 | this.propName = propName; |
| 61 | propName.setParent(this); |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public String toSource(int depth) { |
| 66 | StringBuilder sb = new StringBuilder(); |
| 67 | sb.append(makeIndent(depth)); |
| 68 | if (isAttributeAccess()) { |
| 69 | sb.append("@"); |
| 70 | } |
| 71 | if (namespace != null) { |
| 72 | sb.append(namespace.toSource(0)); |
| 73 | sb.append("::"); |
| 74 | } |
| 75 | sb.append(propName.toSource(0)); |
| 76 | return sb.toString(); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Visits this node, then the namespace if present, then the property name. |
| 81 | */ |
| 82 | @Override |
| 83 | public void visit(NodeVisitor v) { |
| 84 | if (v.visit(this)) { |
| 85 | if (namespace != null) { |
| 86 | namespace.visit(v); |
| 87 | } |
| 88 | propName.visit(v); |
| 89 | } |
| 90 | } |
| 91 | } |