KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > beans > factory > parsing > ParseState


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.beans.factory.parsing;
18
19 import java.util.Stack JavaDoc;
20
21 /**
22  * Simple {@link Stack}-based structure for tracking the logical position during
23  * a parsing process. {@link Entry entries} are added to the stack at
24  * each point during the parse phase in a reader-specific manner.
25  *
26  * <p>Calling {@link #toString()} will render a tree-style view of the current logical
27  * position in the parse phase. This representation is intended for use in
28  * error messages.
29  *
30  * @author Rob Harrop
31  * @since 2.0
32  */

33 public final class ParseState {
34
35     /**
36      * Tab character used when rendering the tree-style representation.
37      */

38     private static final char TAB = '\t';
39
40     /**
41      * Internal {@link Stack} storage.
42      */

43     private final Stack JavaDoc state;
44
45
46     /**
47      * Create a new <code>ParseState</code> with an empty {@link Stack}.
48      */

49     public ParseState() {
50         this.state = new Stack JavaDoc();
51     }
52
53     /**
54      * Create a new <code>ParseState</code> whose {@link Stack} is a {@link Object#clone clone}
55      * of that of the passed in <code>ParseState</code>.
56      */

57     private ParseState(ParseState other) {
58         this.state = (Stack JavaDoc) other.state.clone();
59     }
60
61
62     /**
63      * Add a new {@link Entry} to the {@link Stack}.
64      */

65     public void push(Entry entry) {
66         this.state.push(entry);
67     }
68
69     /**
70      * Remove an {@link Entry} from the {@link Stack}.
71      */

72     public void pop() {
73         this.state.pop();
74     }
75
76     /**
77      * Return the {@link Entry} currently at the top of the {@link Stack} or
78      * <code>null</code> if the {@link Stack} is empty.
79      */

80     public Entry peek() {
81         return (Entry) (this.state.empty() ? null : this.state.peek());
82     }
83
84     /**
85      * Create a new instance of {@link ParseState} which is an independent snapshot
86      * of this instance.
87      */

88     public ParseState snapshot() {
89         return new ParseState(this);
90     }
91
92
93     /**
94      * Returns a tree-style representation of the current <code>ParseState</code>.
95      */

96     public String JavaDoc toString() {
97         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
98         for (int x = 0; x < this.state.size(); x++) {
99             if (x > 0) {
100                 sb.append('\n');
101                 for (int y = 0; y < x; y++) {
102                     sb.append(TAB);
103                 }
104                 sb.append("-> ");
105             }
106             sb.append(this.state.get(x));
107         }
108         return sb.toString();
109     }
110
111
112     /**
113      * Marker interface for entries into the {@link ParseState}.
114      */

115     public interface Entry {
116
117     }
118
119 }
120
Popular Tags