2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
6 * The Java Pathfinder core (jpf-core) platform is licensed under the
7 * Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0.
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 package gov.nasa.jpf.util.script;
21 import java.io.PrintWriter;
22 import java.util.Iterator;
24 public abstract class ScriptElementContainer extends ScriptElement implements Iterable <ScriptElement> {
26 protected class SECIterator implements Iterator<ScriptElement>, Cloneable {
28 SECIterator prev; // to build ad hoc stacks
36 public boolean hasNext() {
41 public ScriptElement next() {
43 ScriptElement ret = cur;
44 cur = cur.nextSibling;
52 public void remove() {
53 throw new UnsupportedOperationException("no ScriptElement removal supported");
56 public SECIterator getPrev() {
60 public void setPrev (SECIterator it) {
65 public Object clone() {
67 // need to deep copy iterators
68 SECIterator it = (SECIterator)super.clone();
70 it.prev = (SECIterator) prev.clone();
73 } catch (CloneNotSupportedException cnsx) {
74 return null; // can't happen, just compiler pleasing
80 ScriptElement firstChild;
82 ScriptElementContainer (ScriptElement parent, int line) {
86 public ScriptElement getFirstChild () {
91 * beware, this sets the nextSibling
93 public void add (ScriptElement e) {
96 if (firstChild == null) {
99 ScriptElement p=firstChild;
100 while (p.nextSibling != null) p=p.nextSibling;
105 public int getNumberOfChildren() {
107 ScriptElement e = firstChild;
110 e = e.getNextSibling();
116 public SECIterator iterator () {
117 return new SECIterator();
120 void dump (PrintWriter w, int level, ScriptElement elem) {
123 while (elem != null) {
124 for (int i=0; i<level; i++) {
129 if (elem instanceof ScriptElementContainer) {
130 ScriptElementContainer c = (ScriptElementContainer) elem;
132 dump(w, level+1, c.getFirstChild());
133 for (int i=0; i<level; i++) {
141 elem = elem.getNextSibling();
144 catch (Throwable t) {
149 protected String toString (String type) {
150 StringBuilder sb = new StringBuilder();
159 for (ScriptElement e = firstChild; e != null; e = e.nextSibling) {
166 return sb.toString();
170 public String toString() {
171 return toString(null);
174 public void dump (PrintWriter w) {
175 dump(w, 0, firstChild);
178 public void processChildren(ElementProcessor p) {
179 for (ScriptElement e = firstChild; e != null; e = e.nextSibling) {