001
002package org.apache.commons.ssl.org.bouncycastle.asn1.x509;
003
004import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Choice;
005import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Object;
006import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1Primitive;
007import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1String;
008import org.apache.commons.ssl.org.bouncycastle.asn1.ASN1TaggedObject;
009import org.apache.commons.ssl.org.bouncycastle.asn1.DERBMPString;
010import org.apache.commons.ssl.org.bouncycastle.asn1.DERIA5String;
011import org.apache.commons.ssl.org.bouncycastle.asn1.DERUTF8String;
012import org.apache.commons.ssl.org.bouncycastle.asn1.DERVisibleString;
013
014/**
015 * <code>DisplayText</code> class, used in
016 * <code>CertificatePolicies</code> X509 V3 extensions (in policy qualifiers).
017 *
018 * <p>It stores a string in a chosen encoding. 
019 * <pre>
020 * DisplayText ::= CHOICE {
021 *      ia5String        IA5String      (SIZE (1..200)),
022 *      visibleString    VisibleString  (SIZE (1..200)),
023 *      bmpString        BMPString      (SIZE (1..200)),
024 *      utf8String       UTF8String     (SIZE (1..200)) }
025 * </pre>
026 * @see PolicyQualifierInfo
027 * @see PolicyInformation
028 */
029public class DisplayText 
030    extends ASN1Object
031    implements ASN1Choice
032{
033   /**
034    * Constant corresponding to ia5String encoding. 
035    *
036    */
037   public static final int CONTENT_TYPE_IA5STRING = 0;
038   /**
039    * Constant corresponding to bmpString encoding. 
040    *
041    */
042   public static final int CONTENT_TYPE_BMPSTRING = 1;
043   /**
044    * Constant corresponding to utf8String encoding. 
045    *
046    */
047   public static final int CONTENT_TYPE_UTF8STRING = 2;
048   /**
049    * Constant corresponding to visibleString encoding. 
050    *
051    */
052   public static final int CONTENT_TYPE_VISIBLESTRING = 3;
053
054   /**
055    * Describe constant <code>DISPLAY_TEXT_MAXIMUM_SIZE</code> here.
056    *
057    */
058   public static final int DISPLAY_TEXT_MAXIMUM_SIZE = 200;
059   
060   int contentType;
061   ASN1String contents;
062   
063   /**
064    * Creates a new <code>DisplayText</code> instance.
065    *
066    * @param type the desired encoding type for the text. 
067    * @param text the text to store. Strings longer than 200
068    * characters are truncated. 
069    */
070   public DisplayText(int type, String text)
071   {
072      if (text.length() > DISPLAY_TEXT_MAXIMUM_SIZE)
073      {
074         // RFC3280 limits these strings to 200 chars
075         // truncate the string
076         text = text.substring (0, DISPLAY_TEXT_MAXIMUM_SIZE);
077      }
078     
079      contentType = type;
080      switch (type)
081      {
082         case CONTENT_TYPE_IA5STRING:
083            contents = new DERIA5String(text);
084            break;
085         case CONTENT_TYPE_UTF8STRING:
086            contents = new DERUTF8String(text);
087            break;
088         case CONTENT_TYPE_VISIBLESTRING:
089            contents = new DERVisibleString(text);
090            break;
091         case CONTENT_TYPE_BMPSTRING:
092            contents = new DERBMPString(text);
093            break;
094         default:
095            contents = new DERUTF8String(text);
096            break;
097      }
098   }
099   
100   /**
101    * Creates a new <code>DisplayText</code> instance.
102    *
103    * @param text the text to encapsulate. Strings longer than 200
104    * characters are truncated. 
105    */
106   public DisplayText(String text) 
107   {
108      // by default use UTF8String
109      if (text.length() > DISPLAY_TEXT_MAXIMUM_SIZE)
110      {
111         text = text.substring(0, DISPLAY_TEXT_MAXIMUM_SIZE);
112      }
113      
114      contentType = CONTENT_TYPE_UTF8STRING;
115      contents = new DERUTF8String(text);
116   }
117
118   /**
119    * Creates a new <code>DisplayText</code> instance.
120    * <p>Useful when reading back a <code>DisplayText</code> class
121    * from it's ASN1Encodable/DEREncodable form. 
122    *
123    * @param de a <code>DEREncodable</code> instance. 
124    */
125   private DisplayText(ASN1String de)
126   {
127      contents = de;
128   }
129
130   public static DisplayText getInstance(Object obj) 
131   {
132      if  (obj instanceof ASN1String)
133      {
134          return new DisplayText((ASN1String)obj);
135      }
136      else if (obj == null || obj instanceof DisplayText)
137      {
138          return (DisplayText)obj;
139      }
140
141      throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
142   }
143
144   public static DisplayText getInstance(
145       ASN1TaggedObject obj,
146       boolean          explicit)
147   {
148       return getInstance(obj.getObject()); // must be explicitly tagged
149   }
150   
151   public ASN1Primitive toASN1Primitive()
152   {
153      return (ASN1Primitive)contents;
154   }
155
156   /**
157    * Returns the stored <code>String</code> object. 
158    *
159    * @return the stored text as a <code>String</code>. 
160    */
161   public String getString() 
162   {
163      return contents.getString();
164   }   
165}