1 /*
2 * Copyright 2005 Philipp Erlacher
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 package org.exolab.castor.xml.parsing;
17
18 import java.util.Stack;
19
20 import org.exolab.castor.xml.UnmarshalState;
21
22 /**
23 * This class helps to access a stack in that {@link UnmarshalState}s are stored.
24 *
25 * @author <a href="mailto:philipp DOT erlacher AT gmail DOT com">Philipp
26 * Erlacher</a>
27 *
28 */
29 public class UnmarshalStateStack {
30
31 /**
32 * The stack to store {@link UnmarshalState}s.
33 */
34 private Stack<UnmarshalState> _unmarshalStates = new Stack<UnmarshalState>();
35
36 /**
37 * This index denotes the position of the parent state.
38 */
39 private Integer parentStateIndex = null;
40
41 /**
42 * Peeks the stack for the top {@link UnmarshalState}, without removing
43 * it.
44 *
45 * @return Top {@link UnmarshalState}, without removing it.
46 */
47 public UnmarshalState getLastState() {
48 return _unmarshalStates.peek();
49 }
50
51 /**
52 * Pops the top {@link UnmarshalState} off the stack.
53 *
54 * @return Top {@link UnmarshalState} instance, removing it from the stack as well.
55 */
56 public UnmarshalState removeLastState() {
57 return _unmarshalStates.pop();
58 }
59
60 /**
61 * Checks if the stack is empty.
62 *
63 * @return True if there is no element on the stack.
64 */
65 public boolean isEmpty() {
66 return _unmarshalStates.empty();
67 }
68
69 /**
70 * Pushes a {@link UnmarshalState} instance onto the stack-
71 *
72 * @param state The {@link UnmarshalState} instance to be pushed onto the stack.
73 */
74 public void pushState(UnmarshalState state) {
75 _unmarshalStates.push(state);
76 }
77
78 /**
79 * Checks if there is a parent state on the stack.
80 *
81 * @return True of there's a parent state.
82 */
83 public boolean hasAnotherParentState() {
84 if (parentStateIndex == null) {
85 parentStateIndex = _unmarshalStates.size() - 2;
86 }
87 return parentStateIndex >= 0;
88 }
89
90 /**
91 * Removes a parent state from the stack.
92 *
93 * @return UnmarshalState that is a parent state
94 */
95 public UnmarshalState removeParentState() {
96 Integer tmpParentIndex = parentStateIndex;
97 parentStateIndex--;
98 return _unmarshalStates.elementAt(tmpParentIndex);
99 }
100
101 public UnmarshalState peekAtState(Integer index) {
102 return _unmarshalStates.elementAt(index);
103 }
104
105 public UnmarshalState getFirstParentState() {
106 return _unmarshalStates.elementAt(getFirstParentStateIndex());
107 }
108
109 public Integer getFirstParentStateIndex() {
110 return (parentStateIndex == null) ? _unmarshalStates.size() -2 : parentStateIndex;
111 }
112
113 public Integer size() {
114 return _unmarshalStates.size();
115 }
116
117 public void resetParentState() {
118 this.parentStateIndex = null;
119 }
120
121 }