Debian Codegraph @

rhino/src/org/mozilla/javascript/ast/XmlLiteral.java

raw symbols SCIP
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 import java.util.ArrayList;
12 import java.util.List;
13
14 /**
15 * AST node for an E4X (Ecma-357) embedded XML literal. Node type is
16 * {@link Token#XML}. The parser generates a simple list of strings and
17 * expressions. In the future we may parse the XML and produce a richer set of
18 * nodes, but for now it's just a set of expressions evaluated to produce a
19 * string to pass to the {@code XML} constructor function.<p>
20 */
21 public class XmlLiteral extends AstNode {
22
23 private List<XmlFragment> fragments = new ArrayList<XmlFragment>();
24
25 {
26 type = Token.XML;
27 }
28
29 public XmlLiteral() {
30 }
31
32 public XmlLiteral(int pos) {
33 super(pos);
34 }
35
36 public XmlLiteral(int pos, int len) {
37 super(pos, len);
38 }
39
40 /**
41 * Returns fragment list - a list of expression nodes.
42 */
43 public List<XmlFragment> getFragments() {
44 return fragments;
45 }
46
47 /**
48 * Sets fragment list, removing any existing fragments first.
49 * Sets the parent pointer for each fragment in the list to this node.
50 * @param fragments fragment list. Replaces any existing fragments.
51 * @throws IllegalArgumentException} if {@code fragments} is {@code null}
52 */
53 public void setFragments(List<XmlFragment> fragments) {
54 assertNotNull(fragments);
55 this.fragments.clear();
56 for (XmlFragment fragment : fragments)
57 addFragment(fragment);
58 }
59
60 /**
61 * Adds a fragment to the fragment list. Sets its parent to this node.
62 * @throws IllegalArgumentException} if {@code fragment} is {@code null}
63 */
64 public void addFragment(XmlFragment fragment) {
65 assertNotNull(fragment);
66 fragments.add(fragment);
67 fragment.setParent(this);
68 }
69
70 @Override
71 public String toSource(int depth) {
72 StringBuilder sb = new StringBuilder(250);
73 for (XmlFragment frag : fragments) {
74 sb.append(frag.toSource(0));
75 }
76 return sb.toString();
77 }
78
79 /**
80 * Visits this node, then visits each child fragment in lexical order.
81 */
82 @Override
83 public void visit(NodeVisitor v) {
84 if (v.visit(this)) {
85 for (XmlFragment frag : fragments) {
86 frag.visit(v);
87 }
88 }
89 }
90 }