DisplayText.cs :  » PDF » iTextSharp » Org » BouncyCastle » Asn1 » X509 » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » PDF » iTextSharp 
iTextSharp » Org » BouncyCastle » Asn1 » X509 » DisplayText.cs
using System;

namespace Org.BouncyCastle.Asn1.X509{
  /**
   * <code>DisplayText</code> class, used in
   * <code>CertificatePolicies</code> X509 V3 extensions (in policy qualifiers).
   *
   * <p>It stores a string in a chosen encoding.
   * <pre>
   * DisplayText ::= CHOICE {
   *      ia5String        IA5String      (SIZE (1..200)),
   *      visibleString    VisibleString  (SIZE (1..200)),
   *      bmpString        BMPString      (SIZE (1..200)),
   *      utf8String       UTF8String     (SIZE (1..200)) }
   * </pre></p>
   * @see PolicyQualifierInfo
   * @see PolicyInformation
   */
  public class DisplayText
    : Asn1Encodable, IAsn1Choice
  {
    /**
     * Constant corresponding to ia5String encoding.
     *
     */
    public const int ContentTypeIA5String = 0;
    /**
     * Constant corresponding to bmpString encoding.
     *
     */
    public const int ContentTypeBmpString = 1;
    /**
     * Constant corresponding to utf8String encoding.
     *
     */
    public const int ContentTypeUtf8String = 2;
    /**
     * Constant corresponding to visibleString encoding.
     *
     */
    public const int ContentTypeVisibleString = 3;
    /**
     * Describe constant <code>DisplayTextMaximumSize</code> here.
     *
     */
    public const int DisplayTextMaximumSize = 200;

    internal readonly int contentType;
    internal readonly IAsn1String contents;

    /**
     * Creates a new <code>DisplayText</code> instance.
     *
     * @param type the desired encoding type for the text.
     * @param text the text to store. Strings longer than 200
     * characters are truncated.
     */
    public DisplayText(
      int    type,
      string  text)
    {
      if (text.Length > DisplayTextMaximumSize)
      {
        // RFC3280 limits these strings to 200 chars
        // truncate the string
        text = text.Substring(0, DisplayTextMaximumSize);
      }

      contentType = type;
      switch (type)
      {
        case ContentTypeIA5String:
          contents = (IAsn1String)new DerIA5String (text);
          break;
        case ContentTypeUtf8String:
          contents = (IAsn1String)new DerUtf8String(text);
          break;
        case ContentTypeVisibleString:
          contents = (IAsn1String)new DerVisibleString(text);
          break;
        case ContentTypeBmpString:
          contents = (IAsn1String)new DerBmpString(text);
          break;
        default:
          contents = (IAsn1String)new DerUtf8String(text);
          break;
      }
    }

//    /**
//     * return true if the passed in string can be represented without
//     * loss as a PrintableString, false otherwise.
//     */
//    private bool CanBePrintable(
//      string str)
//    {
//      for (int i = str.Length - 1; i >= 0; i--)
//      {
//        if (str[i] > 0x007f)
//        {
//          return false;
//        }
//      }
//
//      return true;
//    }

    /**
     * Creates a new <code>DisplayText</code> instance.
     *
     * @param text the text to encapsulate. Strings longer than 200
     * characters are truncated.
     */
    public DisplayText(
      string text)
    {
      // by default use UTF8String
      if (text.Length > DisplayTextMaximumSize)
      {
        text = text.Substring(0, DisplayTextMaximumSize);
      }

      contentType = ContentTypeUtf8String;
      contents = new DerUtf8String(text);
    }

    /**
     * Creates a new <code>DisplayText</code> instance.
     * <p>Useful when reading back a <code>DisplayText</code> class
     * from it's Asn1Encodable form.</p>
     *
     * @param contents an <code>Asn1Encodable</code> instance.
     */
    public DisplayText(
      IAsn1String contents)
    {
      this.contents = contents;
    }

    public static DisplayText GetInstance(
      object obj)
    {
      if (obj is IAsn1String)
      {
        return new DisplayText((IAsn1String) obj);
      }

      if (obj is DisplayText)
      {
        return (DisplayText) obj;
      }

      throw new ArgumentException("unknown object in factory: " + obj.GetType().Name, "obj");
    }

    public override Asn1Object ToAsn1Object()
    {
      return (Asn1Object) contents;
    }

    /**
     * Returns the stored <code>string</code> object.
     *
     * @return the stored text as a <code>string</code>.
     */
    public string GetString()
    {
      return contents.GetString();
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.