View Javadoc
1   /*
2    * Copyright 2005 Philipp Erlacher
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5    * in compliance with the License. You may obtain a copy of the License at
6    *
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software distributed under the License
10   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11   * or implied. See the License for the specific language governing permissions and limitations under
12   * the License.
13   */
14  package org.exolab.castor.xml.parsing;
15  
16  import java.util.Stack;
17  
18  import org.exolab.castor.xml.UnmarshalState;
19  
20  /**
21   * This class helps to access a stack in that {@link UnmarshalState}s are stored.
22   * 
23   * @author <a href="mailto:philipp DOT erlacher AT gmail DOT com">Philipp Erlacher</a>
24   * 
25   */
26  public class UnmarshalStateStack {
27  
28    /**
29     * The stack to store {@link UnmarshalState}s.
30     */
31    private Stack<UnmarshalState> _unmarshalStates = new Stack<UnmarshalState>();
32  
33    /**
34     * This index denotes the position of the parent state.
35     */
36    private Integer parentStateIndex = null;
37  
38    /**
39     * Peeks the stack for the top {@link UnmarshalState}, without removing it.
40     * 
41     * @return Top {@link UnmarshalState}, without removing it.
42     */
43    public UnmarshalState getLastState() {
44      return _unmarshalStates.peek();
45    }
46  
47    /**
48     * Pops the top {@link UnmarshalState} off the stack.
49     * 
50     * @return Top {@link UnmarshalState} instance, removing it from the stack as well.
51     */
52    public UnmarshalState removeLastState() {
53      return _unmarshalStates.pop();
54    }
55  
56    /**
57     * Checks if the stack is empty.
58     * 
59     * @return True if there is no element on the stack.
60     */
61    public boolean isEmpty() {
62      return _unmarshalStates.empty();
63    }
64  
65    /**
66     * Pushes a {@link UnmarshalState} instance onto the stack-
67     * 
68     * @param state The {@link UnmarshalState} instance to be pushed onto the stack.
69     */
70    public void pushState(UnmarshalState state) {
71      _unmarshalStates.push(state);
72    }
73  
74    /**
75     * Checks if there is a parent state on the stack.
76     * 
77     * @return True of there's a parent state.
78     */
79    public boolean hasAnotherParentState() {
80      if (parentStateIndex == null) {
81        parentStateIndex = _unmarshalStates.size() - 2;
82      }
83      return parentStateIndex >= 0;
84    }
85  
86    /**
87     * Removes a parent state from the stack.
88     * 
89     * @return UnmarshalState that is a parent state
90     */
91    public UnmarshalState removeParentState() {
92      Integer tmpParentIndex = parentStateIndex;
93      parentStateIndex--;
94      return _unmarshalStates.elementAt(tmpParentIndex);
95    }
96  
97    public UnmarshalState peekAtState(Integer index) {
98      return _unmarshalStates.elementAt(index);
99    }
100 
101   public UnmarshalState getFirstParentState() {
102     return _unmarshalStates.elementAt(getFirstParentStateIndex());
103   }
104 
105   public Integer getFirstParentStateIndex() {
106     return (parentStateIndex == null) ? _unmarshalStates.size() - 2 : parentStateIndex;
107   }
108 
109   public Integer size() {
110     return _unmarshalStates.size();
111   }
112 
113   public void resetParentState() {
114     this.parentStateIndex = null;
115   }
116 
117 }