Source for gnu.java.security.util.FormatUtil

   1: /* FormatUtil.java -- Encoding and decoding format utility methods
   2:    Copyright (C) 2006 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package gnu.java.security.util;
  40: 
  41: import gnu.java.security.Registry;
  42: 
  43: /**
  44:  * Encoding and decoding format utility methods.
  45:  */
  46: public class FormatUtil
  47: {
  48:   /** Trivial constructor to enforce Singleton pattern. */
  49:   private FormatUtil()
  50:   {
  51:     super();
  52:   }
  53: 
  54:   /**
  55:    * Returns the fully qualified name of the designated encoding ID.
  56:    *
  57:    * @param formatID the unique identifier of the encoding format.
  58:    * @return the fully qualified name of the designated format. Returns
  59:    *         <code>null</code> if no such encoding format is known.
  60:    */
  61:   public static final String getEncodingName(int formatID)
  62:   {
  63:     String result = null;
  64:     switch (formatID)
  65:       {
  66:       case Registry.RAW_ENCODING_ID:
  67:         result = Registry.RAW_ENCODING;
  68:         break;
  69:       case Registry.X509_ENCODING_ID:
  70:         result = Registry.X509_ENCODING;
  71:         break;
  72:       case Registry.PKCS8_ENCODING_ID:
  73:         result = Registry.PKCS8_ENCODING;
  74:         break;
  75:       case Registry.ASN1_ENCODING_ID:
  76:         result = Registry.ASN1_ENCODING;
  77:         break;
  78:       }
  79: 
  80:     return result;
  81:   }
  82: 
  83:   /**
  84:    * Returns the short name of the designated encoding ID. This is used by the
  85:    * JCE Adapters.
  86:    *
  87:    * @param formatID the unique identifier of the encoding format.
  88:    * @return the short name of the designated format. Returns <code>null</code>
  89:    *         if no such encoding format is known.
  90:    */
  91:   public static final String getEncodingShortName(int formatID)
  92:   {
  93:     String result = null;
  94:     switch (formatID)
  95:       {
  96:       case Registry.RAW_ENCODING_ID:
  97:         result = Registry.RAW_ENCODING_SHORT_NAME;
  98:         break;
  99:       case Registry.X509_ENCODING_ID:
 100:         result = Registry.X509_ENCODING_SORT_NAME;
 101:         break;
 102:       case Registry.PKCS8_ENCODING_ID:
 103:         result = Registry.PKCS8_ENCODING_SHORT_NAME;
 104:         break;
 105:       case Registry.ASN1_ENCODING_ID:
 106:         result = Registry.ASN1_ENCODING_SHORT_NAME;
 107:         break;
 108:       }
 109: 
 110:     return result;
 111:   }
 112: 
 113:   /**
 114:    * Returns the identifier of the encoding format given its short name.
 115:    *
 116:    * @param name the case-insensitive canonical short name of an encoding
 117:    *          format.
 118:    * @return the identifier of the designated encoding format, or <code>0</code>
 119:    *         if the name does not correspond to any known format.
 120:    */
 121:   public static final int getFormatID(String name)
 122:   {
 123:     if (name == null)
 124:       return 0;
 125: 
 126:     name = name.trim();
 127:     if (name.length() == 0)
 128:       return 0;
 129: 
 130:     int result = 0;
 131:     if (name.equalsIgnoreCase(Registry.RAW_ENCODING_SHORT_NAME))
 132:       result = Registry.RAW_ENCODING_ID;
 133:     else if (name.equalsIgnoreCase(Registry.X509_ENCODING_SORT_NAME))
 134:       result = Registry.X509_ENCODING_ID;
 135:     else if (name.equalsIgnoreCase(Registry.PKCS8_ENCODING_SHORT_NAME))
 136:       result = Registry.PKCS8_ENCODING_ID;
 137: 
 138:     return result;
 139:   }
 140: }