src/java.xml.crypto/share/classes/com/sun/org/apache/xml/internal/security/utils/RFC2253Parser.java

Print this page
rev 10521 : 8055723[core]: Replace concat String to append in StringBuilder parameters
Contributed-by: Otavio Santana <otaviojava@java.net>


  71      * @return normalized string
  72      */
  73     public static String normalize(String dn, boolean toXml) {
  74         //if empty string
  75         if ((dn == null) || dn.equals("")) {
  76             return "";
  77         }
  78 
  79         try {
  80             String DN = semicolonToComma(dn);
  81             StringBuilder sb = new StringBuilder();
  82             int i = 0;
  83             int l = 0;
  84             int k;
  85 
  86             //for name component
  87             for (int j = 0; (k = DN.indexOf(',', j)) >= 0; j = k + 1) {
  88                 l += countQuotes(DN, j, k);
  89 
  90                 if ((k > 0) && (DN.charAt(k - 1) != '\\') && (l % 2) == 0) {
  91                     sb.append(parseRDN(DN.substring(i, k).trim(), toXml) + ",");
  92 
  93                     i = k + 1;
  94                     l = 0;
  95                 }
  96             }
  97 
  98             sb.append(parseRDN(trim(DN.substring(i)), toXml));
  99 
 100             return sb.toString();
 101         } catch (IOException ex) {
 102             return dn;
 103         }
 104     }
 105 
 106     /**
 107      * Method parseRDN
 108      *
 109      * @param str
 110      * @param toXml
 111      * @return normalized string
 112      * @throws IOException
 113      */
 114     static String parseRDN(String str, boolean toXml) throws IOException {
 115         StringBuilder sb = new StringBuilder();
 116         int i = 0;
 117         int l = 0;
 118         int k;
 119 
 120         for (int j = 0; (k = str.indexOf('+', j)) >= 0; j = k + 1) {
 121             l += countQuotes(str, j, k);
 122 
 123             if ((k > 0) && (str.charAt(k - 1) != '\\') && (l % 2) == 0) {
 124                 sb.append(parseATAV(trim(str.substring(i, k)), toXml) + "+");
 125 
 126                 i = k + 1;
 127                 l = 0;
 128             }
 129         }
 130 
 131         sb.append(parseATAV(trim(str.substring(i)), toXml));
 132 
 133         return sb.toString();
 134     }
 135 
 136     /**
 137      * Method parseATAV
 138      *
 139      * @param str
 140      * @param toXml
 141      * @return normalized string
 142      * @throws IOException
 143      */
 144     static String parseATAV(String str, boolean toXml) throws IOException {


 352             } else {
 353                 sb.append(c);
 354             }
 355         }
 356 
 357         return sb.toString();
 358     }
 359 
 360     /**
 361      * Method changeWStoRFC
 362      *
 363      * @param string
 364      * @return normalized string
 365      */
 366     static String changeWStoRFC(String string) {
 367         StringBuilder sb = new StringBuilder();
 368         int i = 0;
 369         int k;
 370 
 371         for (int j = 0; (k = string.indexOf("\\20", j)) >= 0; j = k + 3) {
 372             sb.append(trim(string.substring(i, k)) + "\\ ");
 373 
 374             i = k + 3;
 375         }
 376 
 377         sb.append(string.substring(i));
 378 
 379         return sb.toString();
 380     }
 381 
 382     /**
 383      * Method semicolonToComma
 384      *
 385      * @param str
 386      * @return normalized string
 387      */
 388     static String semicolonToComma(String str) {
 389         return removeWSandReplace(str, ";", ",");
 390     }
 391 
 392     /**


 401     }
 402 
 403     /**
 404      * Method removeWSandReplace
 405      *
 406      * @param str
 407      * @param symbol
 408      * @param replace
 409      * @return normalized string
 410      */
 411     static String removeWSandReplace(String str, String symbol, String replace) {
 412         StringBuilder sb = new StringBuilder();
 413         int i = 0;
 414         int l = 0;
 415         int k;
 416 
 417         for (int j = 0; (k = str.indexOf(symbol, j)) >= 0; j = k + 1) {
 418             l += countQuotes(str, j, k);
 419 
 420             if ((k > 0) && (str.charAt(k - 1) != '\\') && (l % 2) == 0) {
 421                 sb.append(trim(str.substring(i, k)) + replace);
 422 
 423                 i = k + 1;
 424                 l = 0;
 425             }
 426         }
 427 
 428         sb.append(trim(str.substring(i)));
 429 
 430         return sb.toString();
 431     }
 432 
 433     /**
 434      * Returns the number of Quotation from i to j
 435      *
 436      * @param s
 437      * @param i
 438      * @param j
 439      * @return number of quotes
 440      */
 441     private static int countQuotes(String s, int i, int j) {




  71      * @return normalized string
  72      */
  73     public static String normalize(String dn, boolean toXml) {
  74         //if empty string
  75         if ((dn == null) || dn.equals("")) {
  76             return "";
  77         }
  78 
  79         try {
  80             String DN = semicolonToComma(dn);
  81             StringBuilder sb = new StringBuilder();
  82             int i = 0;
  83             int l = 0;
  84             int k;
  85 
  86             //for name component
  87             for (int j = 0; (k = DN.indexOf(',', j)) >= 0; j = k + 1) {
  88                 l += countQuotes(DN, j, k);
  89 
  90                 if ((k > 0) && (DN.charAt(k - 1) != '\\') && (l % 2) == 0) {
  91                     sb.append(parseRDN(DN.substring(i, k).trim(), toXml)).append(',');
  92 
  93                     i = k + 1;
  94                     l = 0;
  95                 }
  96             }
  97 
  98             sb.append(parseRDN(trim(DN.substring(i)), toXml));
  99 
 100             return sb.toString();
 101         } catch (IOException ex) {
 102             return dn;
 103         }
 104     }
 105 
 106     /**
 107      * Method parseRDN
 108      *
 109      * @param str
 110      * @param toXml
 111      * @return normalized string
 112      * @throws IOException
 113      */
 114     static String parseRDN(String str, boolean toXml) throws IOException {
 115         StringBuilder sb = new StringBuilder();
 116         int i = 0;
 117         int l = 0;
 118         int k;
 119 
 120         for (int j = 0; (k = str.indexOf('+', j)) >= 0; j = k + 1) {
 121             l += countQuotes(str, j, k);
 122 
 123             if ((k > 0) && (str.charAt(k - 1) != '\\') && (l % 2) == 0) {
 124                 sb.append(parseATAV(trim(str.substring(i, k)), toXml)).append('+');
 125 
 126                 i = k + 1;
 127                 l = 0;
 128             }
 129         }
 130 
 131         sb.append(parseATAV(trim(str.substring(i)), toXml));
 132 
 133         return sb.toString();
 134     }
 135 
 136     /**
 137      * Method parseATAV
 138      *
 139      * @param str
 140      * @param toXml
 141      * @return normalized string
 142      * @throws IOException
 143      */
 144     static String parseATAV(String str, boolean toXml) throws IOException {


 352             } else {
 353                 sb.append(c);
 354             }
 355         }
 356 
 357         return sb.toString();
 358     }
 359 
 360     /**
 361      * Method changeWStoRFC
 362      *
 363      * @param string
 364      * @return normalized string
 365      */
 366     static String changeWStoRFC(String string) {
 367         StringBuilder sb = new StringBuilder();
 368         int i = 0;
 369         int k;
 370 
 371         for (int j = 0; (k = string.indexOf("\\20", j)) >= 0; j = k + 3) {
 372             sb.append(trim(string.substring(i, k))).append("\\ ");
 373 
 374             i = k + 3;
 375         }
 376 
 377         sb.append(string.substring(i));
 378 
 379         return sb.toString();
 380     }
 381 
 382     /**
 383      * Method semicolonToComma
 384      *
 385      * @param str
 386      * @return normalized string
 387      */
 388     static String semicolonToComma(String str) {
 389         return removeWSandReplace(str, ";", ",");
 390     }
 391 
 392     /**


 401     }
 402 
 403     /**
 404      * Method removeWSandReplace
 405      *
 406      * @param str
 407      * @param symbol
 408      * @param replace
 409      * @return normalized string
 410      */
 411     static String removeWSandReplace(String str, String symbol, String replace) {
 412         StringBuilder sb = new StringBuilder();
 413         int i = 0;
 414         int l = 0;
 415         int k;
 416 
 417         for (int j = 0; (k = str.indexOf(symbol, j)) >= 0; j = k + 1) {
 418             l += countQuotes(str, j, k);
 419 
 420             if ((k > 0) && (str.charAt(k - 1) != '\\') && (l % 2) == 0) {
 421                 sb.append(trim(str.substring(i, k))).append(replace);
 422 
 423                 i = k + 1;
 424                 l = 0;
 425             }
 426         }
 427 
 428         sb.append(trim(str.substring(i)));
 429 
 430         return sb.toString();
 431     }
 432 
 433     /**
 434      * Returns the number of Quotation from i to j
 435      *
 436      * @param s
 437      * @param i
 438      * @param j
 439      * @return number of quotes
 440      */
 441     private static int countQuotes(String s, int i, int j) {