FileDocCategorySizeDatePackage
RFC2253Parser.javaAPI DocJava SE 6 API12785Tue Jun 10 00:23:04 BST 2008com.sun.org.apache.xml.internal.security.utils

RFC2253Parser

public class RFC2253Parser extends Object
author
$Author: raul $

Fields Summary
static boolean
_TOXML
{@link java.util.logging} logging facility
static int
counter
Field i
Constructors Summary
Methods Summary
static java.lang.StringchangeLess32toRFC(java.lang.String string)
Method changeLess32toRFC

param
string
return
normalized string
throws
IOException


      StringBuffer sb = new StringBuffer();
      StringReader sr = new StringReader(string);
      int i = 0;
      char c;

      for (; (i = sr.read()) > -1; ) {
         c = (char) i;

         if (c == '\\") {
            sb.append(c);

            char c1 = (char) sr.read();
            char c2 = (char) sr.read();

            //65 (A) 97 (a)
            if ((((c1 >= 48) && (c1 <= 57)) || ((c1 >= 65) && (c1 <= 70)) || ((c1 >= 97) && (c1 <= 102)))
                    && (((c2 >= 48) && (c2 <= 57))
                        || ((c2 >= 65) && (c2 <= 70))
                        || ((c2 >= 97) && (c2 <= 102)))) {
               char ch = (char) Byte.parseByte("" + c1 + c2, 16);

               sb.append(ch);
            } else {
               sb.append(c1);
               sb.append(c2);
            }
         } else {
            sb.append(c);
         }
      }

      return sb.toString();
   
static java.lang.StringchangeLess32toXML(java.lang.String string)
Method changeLess32toXML

param
string
return
normalized string
throws
IOException


      StringBuffer sb = new StringBuffer();
      StringReader sr = new StringReader(string);
      int i = 0;

      for (; (i = sr.read()) > -1; ) {
         if (i < 32) {
            sb.append('\\");
            sb.append(Integer.toHexString(i));
         } else {
            sb.append((char) i);
         }
      }

      return sb.toString();
   
static java.lang.StringchangeWStoRFC(java.lang.String string)
Method changeWStoRFC

param
string
return
normalized string


      StringBuffer sb = new StringBuffer();
      int i = 0;
      int k;

      for (int j = 0; (k = string.indexOf("\\20", j)) >= 0; j = k + 3) {
         sb.append(trim(string.substring(i, k)) + "\\ ");

         i = k + 3;
      }

      sb.append(string.substring(i));

      return sb.toString();
   
static java.lang.StringchangeWStoXML(java.lang.String string)
Method changeWStoXML

param
string
return
normalized string
throws
IOException


      StringBuffer sb = new StringBuffer();
      StringReader sr = new StringReader(string);
      int i = 0;
      char c;

      for (; (i = sr.read()) > -1; ) {
         c = (char) i;

         if (c == '\\") {
            char c1 = (char) sr.read();

            if (c1 == ' ") {
               sb.append('\\");

               String s = "20";

               sb.append(s);
            } else {
               sb.append('\\");
               sb.append(c1);
            }
         } else {
            sb.append(c);
         }
      }

      return sb.toString();
   
private static intcountQuotes(java.lang.String s, int i, int j)
Returns the number of Quotation from i to j

param
s
param
i
param
j
return
number of quotes


      int k = 0;

      for (int l = i; l < j; l++) {
         if (s.charAt(l) == '"") {
            k++;
         }
      }

      return k;
   
public static voidmain(java.lang.String[] args)
Method main

param
args
throws
Exception


      testToXML("CN=\"Steve, Kille\",  O=Isode Limited, C=GB");
      testToXML("CN=Steve Kille    ,   O=Isode Limited,C=GB");
      testToXML("\\ OU=Sales+CN=J. Smith,O=Widget Inc.,C=US\\ \\ ");
      testToXML("CN=L. Eagle,O=Sue\\, Grabbit and Runn,C=GB");
      testToXML("CN=Before\\0DAfter,O=Test,C=GB");
      testToXML("CN=\"L. Eagle,O=Sue, = + < > # ;Grabbit and Runn\",C=GB");
      testToXML("1.3.6.1.4.1.1466.0=#04024869,O=Test,C=GB");

      {
         StringBuffer sb = new StringBuffer();

         sb.append('L");
         sb.append('u");
         sb.append('\uc48d");
         sb.append('i");
         sb.append('\uc487");

         String test7 = "SN=" + sb.toString();

         testToXML(test7);
      }

      testToRFC("CN=\"Steve, Kille\",  O=Isode Limited, C=GB");
      testToRFC("CN=Steve Kille    ,   O=Isode Limited,C=GB");
      testToRFC("\\20OU=Sales+CN=J. Smith,O=Widget Inc.,C=US\\20\\20 ");
      testToRFC("CN=L. Eagle,O=Sue\\, Grabbit and Runn,C=GB");
      testToRFC("CN=Before\\12After,O=Test,C=GB");
      testToRFC("CN=\"L. Eagle,O=Sue, = + < > # ;Grabbit and Runn\",C=GB");
      testToRFC("1.3.6.1.4.1.1466.0=\\#04024869,O=Test,C=GB");

      {
         StringBuffer sb = new StringBuffer();

         sb.append('L");
         sb.append('u");
         sb.append('\uc48d");
         sb.append('i");
         sb.append('\uc487");

         String test7 = "SN=" + sb.toString();

         testToRFC(test7);
      }
   
public static java.lang.Stringnormalize(java.lang.String dn)
Method normalize

param
dn
return
normalized string


      //if empty string
      if ((dn == null) || dn.equals("")) {
         return "";
      }

      try {
         String _DN = semicolonToComma(dn);
         StringBuffer sb = new StringBuffer();
         int i = 0;
         int l = 0;
         int k;

         //for name component
         for (int j = 0; (k = _DN.indexOf(",", j)) >= 0; j = k + 1) {
            l += countQuotes(_DN, j, k);

            if ((k > 0) && (_DN.charAt(k - 1) != '\\") && (l % 2) != 1) {
               sb.append(parseRDN(_DN.substring(i, k).trim()) + ",");

               i = k + 1;
               l = 0;
            }
         }

         sb.append(parseRDN(trim(_DN.substring(i))));

         return sb.toString();
      } catch (IOException ex) {
         return dn;
      }
   
static java.lang.StringnormalizeAT(java.lang.String str)
Method normalizeAT

param
str
return
normalized string


      String at = str.toUpperCase().trim();

      if (at.startsWith("OID")) {
         at = at.substring(3);
      }

      return at;
   
static java.lang.StringnormalizeV(java.lang.String str)
Method normalizeV

param
str
return
normalized string
throws
IOException


      String value = trim(str);

      if (value.startsWith("\"")) {
         StringBuffer sb = new StringBuffer();
         StringReader sr = new StringReader(value.substring(1,
                              value.length() - 1));
         int i = 0;
         char c;

         for (; (i = sr.read()) > -1; ) {
            c = (char) i;

            //the following char is defined at 4.Relationship with RFC1779 and LDAPv2 inrfc2253
            if ((c == ',") || (c == '=") || (c == '+") || (c == '<")
                    || (c == '>") || (c == '#") || (c == ';")) {
               sb.append('\\");
            }

            sb.append(c);
         }

         value = trim(sb.toString());
      }

      if (_TOXML == true) {
         if (value.startsWith("#")) {
            value = '\\" + value;
         }
      } else {
         if (value.startsWith("\\#")) {
            value = value.substring(1);
         }
      }

      return value;
   
static java.lang.StringparseATAV(java.lang.String str)
Method parseATAV

param
str
return
normalized string
throws
IOException


      int i = str.indexOf("=");

      if ((i == -1) || ((i > 0) && (str.charAt(i - 1) == '\\"))) {
         return str;
      } 
      String attrType = normalizeAT(str.substring(0, i));
      String attrValue = normalizeV(str.substring(i + 1));

      return attrType + "=" + attrValue;
      
   
static java.lang.StringparseRDN(java.lang.String str)
Method parseRDN

param
str
return
normalized string
throws
IOException


      StringBuffer sb = new StringBuffer();
      int i = 0;
      int l = 0;
      int k;

      for (int j = 0; (k = str.indexOf("+", j)) >= 0; j = k + 1) {
         l += countQuotes(str, j, k);

         if ((k > 0) && (str.charAt(k - 1) != '\\") && (l % 2) != 1) {
            sb.append(parseATAV(trim(str.substring(i, k))) + "+");

            i = k + 1;
            l = 0;
         }
      }

      sb.append(parseATAV(trim(str.substring(i))));

      return sb.toString();
   
static java.lang.StringremoveWSandReplace(java.lang.String str, java.lang.String symbol, java.lang.String replace)
Method removeWSandReplace

param
str
param
symbol
param
replace
return
normalized string


      StringBuffer sb = new StringBuffer();
      int i = 0;
      int l = 0;
      int k;

      for (int j = 0; (k = str.indexOf(symbol, j)) >= 0; j = k + 1) {
         l += countQuotes(str, j, k);

         if ((k > 0) && (str.charAt(k - 1) != '\\") && (l % 2) != 1) {
            sb.append(trim(str.substring(i, k)) + replace);

            i = k + 1;
            l = 0;
         }
      }

      sb.append(trim(str.substring(i)));

      return sb.toString();
   
static java.lang.StringremoveWhiteSpace(java.lang.String str, java.lang.String symbol)
Method removeWhiteSpace

param
str
param
symbol
return
normalized string

      return removeWSandReplace(str, symbol, symbol);
   
public static java.lang.Stringrfc2253toXMLdsig(java.lang.String dn)
Method rfc2253toXMLdsig

param
dn
return
normalized string


              
        

      _TOXML = true;

      // Transform from RFC1779 to RFC2253
      String normalized = normalize(dn);

      return rfctoXML(normalized);
   
static java.lang.StringrfctoXML(java.lang.String string)
Method rfctoXML

param
string
return
normalized string


      try {
         String s = changeLess32toXML(string);

         return changeWStoXML(s);
      } catch (Exception e) {
         return string;
      }
   
static java.lang.StringsemicolonToComma(java.lang.String str)
Method semicolonToComma

param
str
return
normalized string

      return removeWSandReplace(str, ";", ",");
   
static voidtestToRFC(java.lang.String st)
Method testToRFC

param
st


      System.out.println("start " + counter++ + ": " + st);
      System.out.println("         " + xmldsigtoRFC2253(st));
      System.out.println("");
   
static voidtestToXML(java.lang.String st)
Method test

param
st


           
       

      System.out.println("start " + counter++ + ": " + st);
      System.out.println("         " + rfc2253toXMLdsig(st));
      System.out.println("");
   
static java.lang.Stringtrim(java.lang.String str)
Method trim

param
str
return
the string


      String trimed = str.trim();
      int i = str.indexOf(trimed.substring(0)) + trimed.length();

      if ((str.length() > i) && trimed.endsWith("\\")
              &&!trimed.endsWith("\\\\")) {
         if (str.charAt(i) == ' ") {
            trimed = trimed + " ";
         }
      }

      return trimed;
   
public static java.lang.StringxmldsigtoRFC2253(java.lang.String dn)
Method xmldsigtoRFC2253

param
dn
return
normalized string


      _TOXML = false;

      // Transform from RFC1779 to RFC2253
      String normalized = normalize(dn);

      return xmltoRFC(normalized);
   
static java.lang.StringxmltoRFC(java.lang.String string)
Method xmltoRFC

param
string
return
normalized string


      try {
         String s = changeLess32toRFC(string);

         return changeWStoRFC(s);
      } catch (Exception e) {
         return string;
      }