001    /*
002     *  Licensed to the Apache Software Foundation (ASF) under one
003     *  or more contributor license agreements.  See the NOTICE file
004     *  distributed with this work for additional information
005     *  regarding copyright ownership.  The ASF licenses this file
006     *  to you under the Apache License, Version 2.0 (the
007     *  "License"); you may not use this file except in compliance
008     *  with the License.  You may obtain a copy of the License at
009     *  
010     *    http://www.apache.org/licenses/LICENSE-2.0
011     *  
012     *  Unless required by applicable law or agreed to in writing,
013     *  software distributed under the License is distributed on an
014     *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     *  KIND, either express or implied.  See the License for the
016     *  specific language governing permissions and limitations
017     *  under the License. 
018     *  
019     */
020    package org.apache.directory.shared.asn1;
021    
022    
023    import java.nio.ByteBuffer;
024    
025    import org.apache.directory.shared.asn1.codec.DecoderException;
026    import org.apache.directory.shared.asn1.codec.EncoderException;
027    import org.apache.directory.shared.i18n.I18n;
028    
029    
030    /**
031     * An abstract class which implements basic TLV operations.
032     * 
033     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
034     * @version $Rev$, $Date$
035     */
036    public abstract class AbstractAsn1Object implements Asn1Object
037    {
038        // ~ Instance fields
039        // ----------------------------------------------------------------------------
040    
041        /** The object's current length. It is used while decoding PDUs */
042        private int currentLength;
043    
044        /** The object's expected length. It is used while decoding PDUs */
045        private int expectedLength;
046    
047        /** The encapsulating Object */
048        protected AbstractAsn1Object parent;
049    
050        
051        /** The identifier of the associated TLV */
052        private int tlvId;
053    
054        // ~ Methods
055        // ------------------------------------------------------------------------------------
056    
057        /**
058         * Constructor associated with a TLV indentifier. Used when 
059         * decoded a TLV, we create an association between the decode
060         * Asn1Object and the TLV which is the encoded form.
061         * 
062         * @param tlvId The TLV Id.
063         */
064        protected AbstractAsn1Object( int tlvId )
065        {
066            this.tlvId = tlvId;
067        }
068    
069        
070        /**
071         * Default constructor. The TLV Id is set to -1. This constructor
072         * is called when an Asn1Object is created to be encoded, not decoded.
073         */
074        protected AbstractAsn1Object()
075        {
076            this.tlvId = -1;
077        }
078        
079        /**
080         * Get the current object length, which is the sum of all inner length
081         * already decoded.
082         * 
083         * @return The current object's length
084         */
085        public int getCurrentLength()
086        {
087            return currentLength;
088        }
089    
090    
091        /**
092         * Compute the object length, which is the sum of all inner length.
093         * 
094         * @return The object's computed length
095         */
096        public abstract int computeLength();
097    
098    
099        /**
100         * Encode the object to a PDU.
101         * 
102         * @param buffer The buffer where to put the PDU
103         * @return The PDU.
104         * @throws EncoderException if the buffer can't be encoded
105         */
106        public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
107        {
108            return null;
109        }
110    
111    
112        /**
113         * Get the expected object length.
114         * 
115         * @return The expected object's length
116         */
117        public int getExpectedLength()
118        {
119            return expectedLength;
120        }
121    
122    
123        /**
124         * Add a length to the object
125         * 
126         * @param length
127         *            The length to add.
128         * @throws DecoderException
129         *             Thrown if the current length exceed the expected length
130         */
131        public void addLength( int length ) throws DecoderException
132        {
133            currentLength += length;
134    
135            if ( currentLength > expectedLength )
136            {
137                throw new DecoderException( I18n.err( I18n.ERR_00041 ) );
138            }
139        }
140    
141    
142        /**
143         * Set the expected length
144         * 
145         * @param expectedLength
146         *            The expectedLength to set.
147         */
148        public void setExpectedLength( int expectedLength )
149        {
150            this.expectedLength = expectedLength;
151        }
152    
153    
154        /**
155         * Set the current length
156         * 
157         * @param currentLength
158         *            The currentLength to set.
159         */
160        public void setCurrentLength( int currentLength )
161        {
162            this.currentLength = currentLength;
163        }
164    
165    
166        /**
167         * Get the parent
168         * 
169         * @return Returns the parent.
170         */
171        public AbstractAsn1Object getParent()
172        {
173            return parent;
174        }
175    
176    
177        /**
178         * Set the parent
179         * 
180         * @param parent
181         *            The parent to set.
182         */
183        public void setParent( AbstractAsn1Object parent )
184        {
185            this.parent = parent;
186        }
187    
188        /**
189         * @return The TLV identifier associated with this object
190         */
191        public int getTlvId()
192        {
193            return tlvId;
194        }
195    }