001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    
018    package org.apache.geronimo.util.asn1;
019    
020    import java.io.IOException;
021    
022    /**
023     * DER VisibleString object.
024     */
025    public class DERVisibleString
026        extends DERObject
027        implements DERString
028    {
029        String  string;
030    
031        /**
032         * return a Visible String from the passed in object.
033         *
034         * @exception IllegalArgumentException if the object cannot be converted.
035         */
036        public static DERVisibleString getInstance(
037            Object  obj)
038        {
039            if (obj == null || obj instanceof DERVisibleString)
040            {
041                return (DERVisibleString)obj;
042            }
043    
044            if (obj instanceof ASN1OctetString)
045            {
046                return new DERVisibleString(((ASN1OctetString)obj).getOctets());
047            }
048    
049            if (obj instanceof ASN1TaggedObject)
050            {
051                return getInstance(((ASN1TaggedObject)obj).getObject());
052            }
053    
054            throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
055        }
056    
057        /**
058         * return a Visible String from a tagged object.
059         *
060         * @param obj the tagged object holding the object we want
061         * @param explicit true if the object is meant to be explicitly
062         *              tagged false otherwise.
063         * @exception IllegalArgumentException if the tagged object cannot
064         *               be converted.
065         */
066        public static DERVisibleString getInstance(
067            ASN1TaggedObject obj,
068            boolean          explicit)
069        {
070            return getInstance(obj.getObject());
071        }
072    
073        /**
074         * basic constructor - byte encoded string.
075         */
076        public DERVisibleString(
077            byte[]   string)
078        {
079            char[]  cs = new char[string.length];
080    
081            for (int i = 0; i != cs.length; i++)
082            {
083                cs[i] = (char)(string[i] & 0xff);
084            }
085    
086            this.string = new String(cs);
087        }
088    
089        /**
090         * basic constructor
091         */
092        public DERVisibleString(
093            String   string)
094        {
095            this.string = string;
096        }
097    
098        public String getString()
099        {
100            return string;
101        }
102    
103        public byte[] getOctets()
104        {
105            char[]  cs = string.toCharArray();
106            byte[]  bs = new byte[cs.length];
107    
108            for (int i = 0; i != cs.length; i++)
109            {
110                bs[i] = (byte)cs[i];
111            }
112    
113            return bs;
114        }
115    
116        void encode(
117            DEROutputStream  out)
118            throws IOException
119        {
120            out.writeEncoded(VISIBLE_STRING, this.getOctets());
121        }
122    
123        public boolean equals(
124            Object  o)
125        {
126            if ((o == null) || !(o instanceof DERVisibleString))
127            {
128                return false;
129            }
130    
131            return this.getString().equals(((DERVisibleString)o).getString());
132        }
133    
134        public int hashCode()
135        {
136            return this.getString().hashCode();
137        }
138    }