1 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 46 public class DisplayText 47 extends ASN1Encodable 48 implements ASN1Choice 49 { 50 54 public static final int CONTENT_TYPE_IA5STRING = 0; 55 59 public static final int CONTENT_TYPE_BMPSTRING = 1; 60 64 public static final int CONTENT_TYPE_UTF8STRING = 2; 65 69 public static final int CONTENT_TYPE_VISIBLESTRING = 3; 70 71 75 public static final int DISPLAY_TEXT_MAXIMUM_SIZE = 200; 76 77 int contentType; 78 DERString contents; 79 80 87 public DisplayText (int type, String text) 88 { 89 if (text.length() > DISPLAY_TEXT_MAXIMUM_SIZE) { 90 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 119 private boolean canBeUTF8( 120 String 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 139 public DisplayText (String text) 140 { 141 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 165 public DisplayText(DERString de) 166 { 167 contents = de; 168 } 169 170 public static DisplayText getInstance(Object 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 ("illegal object in getInstance"); 182 } 183 184 public static DisplayText getInstance( 185 ASN1TaggedObject obj, 186 boolean explicit) 187 { 188 return getInstance(obj.getObject()); } 190 191 public DERObject toASN1Object() 192 { 193 return (DERObject)contents; 194 } 195 196 201 public String getString() 202 { 203 return contents.getString(); 204 } 205 } 206 | Popular Tags |