KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > util > asn1 > x509 > DisplayText


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18
19 package org.apache.geronimo.util.asn1.x509;
20
21 import org.apache.geronimo.util.asn1.ASN1Choice;
22 import org.apache.geronimo.util.asn1.ASN1Encodable;
23 import org.apache.geronimo.util.asn1.ASN1TaggedObject;
24 import org.apache.geronimo.util.asn1.DERObject;
25 import org.apache.geronimo.util.asn1.DERBMPString;
26 import org.apache.geronimo.util.asn1.DERIA5String;
27 import org.apache.geronimo.util.asn1.DERUTF8String;
28 import org.apache.geronimo.util.asn1.DERVisibleString;
29 import org.apache.geronimo.util.asn1.DERString;
30
31 /**
32  * <code>DisplayText</code> class, used in
33  * <code>CertificatePolicies</code> X509 V3 extensions (in policy qualifiers).
34  *
35  * <p>It stores a string in a chosen encoding.
36  * <pre>
37  * DisplayText ::= CHOICE {
38  * ia5String IA5String (SIZE (1..200)),
39  * visibleString VisibleString (SIZE (1..200)),
40  * bmpString BMPString (SIZE (1..200)),
41  * utf8String UTF8String (SIZE (1..200)) }
42  * </pre>
43  * @see PolicyQualifierInfo
44  * @see PolicyInformation
45  */

46 public class DisplayText
47     extends ASN1Encodable
48     implements ASN1Choice
49 {
50    /**
51     * Constant corresponding to ia5String encoding.
52     *
53     */

54    public static final int CONTENT_TYPE_IA5STRING = 0;
55    /**
56     * Constant corresponding to bmpString encoding.
57     *
58     */

59    public static final int CONTENT_TYPE_BMPSTRING = 1;
60    /**
61     * Constant corresponding to utf8String encoding.
62     *
63     */

64    public static final int CONTENT_TYPE_UTF8STRING = 2;
65    /**
66     * Constant corresponding to visibleString encoding.
67     *
68     */

69    public static final int CONTENT_TYPE_VISIBLESTRING = 3;
70
71    /**
72     * Describe constant <code>DISPLAY_TEXT_MAXIMUM_SIZE</code> here.
73     *
74     */

75    public static final int DISPLAY_TEXT_MAXIMUM_SIZE = 200;
76
77    int contentType;
78    DERString contents;
79
80    /**
81     * Creates a new <code>DisplayText</code> instance.
82     *
83     * @param type the desired encoding type for the text.
84     * @param text the text to store. Strings longer than 200
85     * characters are truncated.
86     */

87    public DisplayText (int type, String JavaDoc text)
88    {
89       if (text.length() > DISPLAY_TEXT_MAXIMUM_SIZE) {
90          // RFC3280 limits these strings to 200 chars
91
// truncate the string
92
text = text.substring (0, DISPLAY_TEXT_MAXIMUM_SIZE);
93       }
94
95       contentType = type;
96       switch (type) {
97          case CONTENT_TYPE_IA5STRING:
98             contents = (DERString)new DERIA5String (text);
99             break;
100          case CONTENT_TYPE_UTF8STRING:
101             contents = (DERString)new DERUTF8String(text);
102             break;
103          case CONTENT_TYPE_VISIBLESTRING:
104             contents = (DERString)new DERVisibleString(text);
105             break;
106          case CONTENT_TYPE_BMPSTRING:
107             contents = (DERString)new DERBMPString(text);
108             break;
109          default:
110             contents = (DERString)new DERUTF8String(text);
111             break;
112       }
113    }
114
115    /**
116     * return true if the passed in String can be represented without
117     * loss as a UTF8String, false otherwise.
118     */

119    private boolean canBeUTF8(
120        String JavaDoc str)
121    {
122        for (int i = str.length() - 1; i >= 0; i--)
123        {
124            if (str.charAt(i) > 0x00ff)
125            {
126                return false;
127            }
128        }
129
130        return true;
131    }
132
133    /**
134     * Creates a new <code>DisplayText</code> instance.
135     *
136     * @param text the text to encapsulate. Strings longer than 200
137     * characters are truncated.
138     */

139    public DisplayText (String JavaDoc text)
140    {
141       // by default use UTF8String
142
if (text.length() > DISPLAY_TEXT_MAXIMUM_SIZE) {
143          text = text.substring(0, DISPLAY_TEXT_MAXIMUM_SIZE);
144       }
145
146       if (canBeUTF8(text))
147       {
148           contentType = CONTENT_TYPE_UTF8STRING;
149           contents = new DERUTF8String(text);
150       }
151       else
152       {
153           contentType = CONTENT_TYPE_BMPSTRING;
154           contents = new DERBMPString(text);
155       }
156    }
157
158    /**
159     * Creates a new <code>DisplayText</code> instance.
160     * <p>Useful when reading back a <code>DisplayText</code> class
161     * from it's ASN1Encodable/DEREncodable form.
162     *
163     * @param de a <code>DEREncodable</code> instance.
164     */

165    public DisplayText(DERString de)
166    {
167       contents = de;
168    }
169
170    public static DisplayText getInstance(Object JavaDoc de)
171    {
172       if (de instanceof DERString)
173       {
174           return new DisplayText((DERString)de);
175       }
176       else if (de instanceof DisplayText)
177       {
178           return (DisplayText)de;
179       }
180
181       throw new IllegalArgumentException JavaDoc("illegal object in getInstance");
182    }
183
184    public static DisplayText getInstance(
185        ASN1TaggedObject obj,
186        boolean explicit)
187    {
188        return getInstance(obj.getObject()); // must be explicitly tagged
189
}
190
191    public DERObject toASN1Object()
192    {
193       return (DERObject)contents;
194    }
195
196    /**
197     * Returns the stored <code>String</code> object.
198     *
199     * @return the stored text as a <code>String</code>.
200     */

201    public String JavaDoc getString()
202    {
203       return contents.getString();
204    }
205 }
206
Popular Tags