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
030 /**
031 * BER TaggedObject
032 */
033 public class BERTaggedObject extends DERTaggedObject
034 {
035 /**
036 * @param tag
037 * the tag number for this object.
038 * @param obj
039 * the tagged object.
040 */
041 public BERTaggedObject(int tag, DEREncodable obj)
042 {
043 super( tag, obj );
044 }
045
046
047 /**
048 * @param explicit true
049 * if an explicitly tagged object.
050 * @param tag
051 * the tag number for this object.
052 * @param obj
053 * the tagged object.
054 */
055 public BERTaggedObject(boolean explicit, int tag, DEREncodable obj)
056 {
057 super( explicit, tag, obj );
058 }
059
060
061 public void encode( ASN1OutputStream out ) throws IOException
062 {
063 out.write( DERObject.CONSTRUCTED | DERObject.TAGGED | tag );
064 out.write( DERObject.TAGGED );
065
066 if ( !empty )
067 {
068 if ( !explicit )
069 {
070 if ( obj instanceof DEROctetString )
071 {
072 Enumeration<DEREncodable> e;
073
074 if ( obj instanceof BERConstructedOctetString )
075 {
076 e = ( ( BERConstructedOctetString ) obj ).getObjects();
077 }
078 else
079 {
080 DEROctetString octs = ( DEROctetString ) obj;
081 BERConstructedOctetString berO = new BERConstructedOctetString( octs.getOctets() );
082
083 e = berO.getObjects();
084 }
085
086 while ( e.hasMoreElements() )
087 {
088 out.writeObject( e.nextElement() );
089 }
090 }
091 else if ( obj instanceof DERSequence )
092 {
093 Enumeration<DEREncodable> e = ( ( DERSequence ) obj ).getObjects();
094
095 while ( e.hasMoreElements() )
096 {
097 out.writeObject( e.nextElement() );
098 }
099 }
100 else
101 {
102 throw new RuntimeException( "Not implemented: " + obj.getClass().getName() );
103 }
104 }
105 else
106 {
107 out.writeObject( obj );
108 }
109 }
110
111 out.write( DERObject.TERMINATOR );
112 out.write( DERObject.TERMINATOR );
113 }
114 }