View Javadoc

1   /*
2    *   Copyright 2005 The Apache Software Foundation
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.apache.asn1new.ber;
18  
19  import org.apache.asn1new.ber.containers.IAsn1Container;
20  import org.apache.asn1new.ber.grammar.IGrammar;
21  import org.apache.asn1new.ber.grammar.IStates;
22  import org.apache.asn1new.ber.tlv.TLV;
23  import org.apache.asn1new.ber.tlv.TLVStateEnum;
24  
25  
26  /***
27   * This class is the abstract container used to store the current state
28   * of a PDU being decoded. It also stores the grammars used to decode
29   * the PDU, and zll the informations needed to decode a PDU. 
30   * 
31   *
32   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
33   */
34  public class AbstractContainer implements IAsn1Container
35  {
36      //~ Instance fields ----------------------------------------------------------------------------
37  
38      /*** The grammars that are used.
39       * It's a stack as we can switch grammars */
40      protected IGrammar[] grammarStack;
41  
42      /*** All the possible grammars */
43      protected IGrammar[] grammars;
44  
45      /*** Store a stack of the current states used when switching grammars */
46      protected int[] stateStack;
47  
48      /*** The number of stored grammars */
49      protected int nbGrammars;
50  
51      /*** The current grammar */
52      protected int currentGrammar;
53  
54      /*** The current state of the decoding */
55      protected int state;
56  
57      /*** The current transition */
58      protected int transition;
59  
60      /*** The current TLV */
61      protected TLV tlv;
62  
63      /*** Store the different states for debug purpose */
64      protected IStates states;
65      
66      /*** The parent TLV */
67      protected TLV parentTLV;
68      
69      //~ Methods ------------------------------------------------------------------------------------
70  
71      /***
72       * DOCUMENT ME!
73       *
74       * @return Returns the grammar used to decode a LdapMessage.
75       */
76      public IGrammar getGrammar()
77      {
78          return grammarStack[currentGrammar];
79      }
80  
81      /***
82       * Add a IGrammar to use
83       *
84       * @param grammar The grammar to add.
85       */
86      public void addGrammar( IGrammar grammar )
87      {
88          grammars[nbGrammars++] = grammar;
89      }
90  
91      /***
92       * Switch to another grammar
93       *
94       * @param currentState DOCUMENT ME!
95       * @param grammar The grammar to add.
96       */
97      public void switchGrammar( int currentState, int grammar )
98      {
99          stateStack[currentGrammar] = currentState;
100         currentGrammar++;
101         grammarStack[currentGrammar] = grammars[( grammar >> 8 ) - 1];
102     }
103 
104     /***
105      * restore the previous grammar (the one before a switch has occured)
106      *
107      * @return The previous current state, if any.
108      */
109     public int restoreGrammar()
110     {
111         grammarStack[currentGrammar] = null;
112         currentGrammar--;
113 
114         if ( currentGrammar >= 0 )
115         {
116             return stateStack[currentGrammar];
117         }
118         else
119         {
120             return -1;
121         }
122 
123     }
124 
125     /***
126      * Get the current grammar state
127      *
128      * @return Returns the current grammar state
129      */
130     public int getState()
131     {
132         return state;
133     }
134 
135     /***
136      * Set the new current state
137      *
138      * @param state The new state
139      */
140     public void setState( int state )
141     {
142         this.state = state;
143     }
144 
145     /***
146      * Get the transition
147      *
148      * @return Returns the transition from the previous state to the new 
149      * state
150      */
151     public int getTransition()
152     {
153         return transition;
154     }
155 
156     /***
157      * Update the transition from a state to another
158      *
159      * @param transition The transition to set
160      */
161     public void setTransition( int transition )
162     {
163         this.transition = transition;
164     }
165 
166     /***
167      * Gert the current grammar number
168      *
169      * @return Returns the currentGrammar.
170      */
171     public int getCurrentGrammar()
172     {
173         return currentGrammar;
174     }
175 
176     /***
177      * Get the current grammar type.
178      *
179      * @return Returns the current Grammar type, or -1 if not found.
180      */
181     public int getCurrentGrammarType()
182     {
183 
184         for ( int i = 0; i < grammars.length; i++ )
185         {
186 
187             if ( grammars[i] == grammarStack[currentGrammar] )
188             {
189                 return i;
190             }
191         }
192 
193         return -1;
194     }
195 
196     /***
197      * Initialize the grammar stack
198      *
199      * @param grammar Set the initial grammar
200     */
201     public void setInitGrammar( int grammar )
202     {
203         currentGrammar++;
204         grammarStack[currentGrammar] = grammars[grammar];
205         stateStack[currentGrammar]   = 0;
206     }
207 
208     /***
209      * Set the current TLV
210      *
211      * @param tlv The current TLV
212      */
213     public void setCurrentTLV( TLV tlv )
214     {
215         this.tlv = tlv;
216     }
217 
218     /***
219      * Get the current TLV
220      *
221      * @return Returns the current TLV being decoded
222      */
223     public TLV getCurrentTLV()
224     {
225         return this.tlv;
226     }
227     
228     /***
229      * Get the states for this container's grammars
230      * 
231      * @return Returns the states.
232      */
233     public IStates getStates() {
234         return states;
235     }
236     
237     /***
238      * Get the parent TLV;
239      * 
240      * @return Returns the parent TLV, if any.
241      */
242     public TLV getParentTLV()
243     {
244         return parentTLV;
245     }
246 
247     /***
248      * Set the parent TLV.
249      * 
250      * @param The parent TLV to set.
251      */
252     public void setParentTLV(TLV parentTLV)
253     {
254         this.parentTLV = parentTLV;
255     }
256     
257     /***
258      * Clean the container for the next usage.
259      *
260      */
261     public void clean()
262     {
263         currentGrammar = 0;
264         tlv = null;
265         parentTLV = null;
266         transition = 0;
267         state = TLVStateEnum.TAG_STATE_START;
268     }
269 }