001    /*
002     * Copyright (c) 2000 - 2006 The Legion Of The Bouncy Castle (http://www.bouncycastle.org)
003     * 
004     * Permission is hereby granted, free of charge, to any person obtaining a copy of this
005     * software and associated documentation files (the "Software"), to deal in the Software
006     * without restriction, including without limitation the rights to use, copy, modify,
007     * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
008     * permit persons to whom the Software is furnished to do so, subject to the following
009     * conditions:
010     * 
011     * The above copyright notice and this permission notice shall be included in all copies
012     * or substantial portions of the Software.
013     * 
014     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
015     * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
016     * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
017     * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
018     * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
019     * DEALINGS IN THE SOFTWARE.
020     * 
021     */
022    
023    package org.apache.directory.shared.asn1.der;
024    
025    
026    import java.io.IOException;
027    import java.util.Enumeration;
028    
029    import org.apache.directory.shared.i18n.I18n;
030    
031    
032    /**
033     * BER TaggedObject
034     */
035    public class BERTaggedObject extends DERTaggedObject
036    {
037        /**
038         * @param tag
039         *            the tag number for this object.
040         * @param obj
041         *            the tagged object.
042         */
043        public BERTaggedObject(int tag, DEREncodable obj)
044        {
045            super( tag, obj );
046        }
047    
048    
049        /**
050         * @param explicit true
051         *            if an explicitly tagged object.
052         * @param tag
053         *            the tag number for this object.
054         * @param obj
055         *            the tagged object.
056         */
057        public BERTaggedObject(boolean explicit, int tag, DEREncodable obj)
058        {
059            super( explicit, tag, obj );
060        }
061    
062    
063        public void encode( ASN1OutputStream out ) throws IOException
064        {
065            out.write( DERObject.CONSTRUCTED | DERObject.TAGGED | tag );
066            out.write( DERObject.TAGGED );
067    
068            if ( !empty )
069            {
070                if ( !explicit )
071                {
072                    if ( obj instanceof DEROctetString )
073                    {
074                        Enumeration<DEREncodable> e;
075    
076                        if ( obj instanceof BERConstructedOctetString )
077                        {
078                            e = ( ( BERConstructedOctetString ) obj ).getObjects();
079                        }
080                        else
081                        {
082                            DEROctetString octs = ( DEROctetString ) obj;
083                            BERConstructedOctetString berO = new BERConstructedOctetString( octs.getOctets() );
084    
085                            e = berO.getObjects();
086                        }
087    
088                        while ( e.hasMoreElements() )
089                        {
090                            out.writeObject( e.nextElement() );
091                        }
092                    }
093                    else if ( obj instanceof DERSequence )
094                    {
095                        Enumeration<DEREncodable> e = ( ( DERSequence ) obj ).getObjects();
096    
097                        while ( e.hasMoreElements() )
098                        {
099                            out.writeObject( e.nextElement() );
100                        }
101                    }
102                    else
103                    {
104                        throw new RuntimeException( I18n.err( I18n.ERR_00027, obj.getClass().getName() ) );
105                    }
106                }
107                else
108                {
109                    out.writeObject( obj );
110                }
111            }
112    
113            out.write( DERObject.TERMINATOR );
114            out.write( DERObject.TERMINATOR );
115        }
116    }