Java String Escape escapeASN1(String str)

Here you can find the source of escapeASN1(String str)

Description

Escapes Strings to be used in ASN.1 attributes (like LDAP attributes)

License

Apache License

Parameter

Parameter Description
str The string to escape

Return

The escaped string

Declaration

public static String escapeASN1(String str) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2009, 2010 Innovation Gate GmbH
 * //from ww  w  .  j  av a  2 s.c  om
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

import java.util.ArrayList;

import java.util.List;

public class Main {
    private static final String ASN1_ESCAPE_CHARS = ",+\"\\<>;";
    private static final String ASN1_FIRSTCHAR_ESCAPE_CHARS = " #";

    /**
     * Escapes Strings to be used in ASN.1 attributes (like LDAP attributes)
     * @param str The string to escape
     * @return The escaped string
     */
    public static String escapeASN1(String str) {

        StringBuffer out = new StringBuffer();
        char c;
        boolean escape;
        for (int i = 0; i < str.length(); i++) {
            c = str.charAt(i);

            escape = false;
            if (i == 0 && ASN1_FIRSTCHAR_ESCAPE_CHARS.indexOf(c) != -1) {
                escape = true;
            }

            if (escape == false && (i == str.length() - 1) && c == ' ') {
                escape = true;
            }

            if (escape == false && ASN1_ESCAPE_CHARS.indexOf(c) != -1) {
                escape = true;
            }

            if (escape) {
                out.append("\\");
            }
            out.append(c);

        }

        return out.toString();

    }

    /**
     * Creates a new list that contains the string representations
     * of all elements of the original list
     * null values are preserved.
     * 
     * @param listOriginal
     * @return The list with all elements converted to their string representation
     */
    public static List<String> toString(List<Object> listOriginal) {

        List<String> list = new ArrayList<String>();
        Object elem;
        for (int i = 0; i < listOriginal.size(); i++) {
            elem = listOriginal.get(i);
            if (elem != null) {
                list.add(String.valueOf(elem));
            } else {
                list.add(null);
            }
        }

        return list;

    }
}

Related

  1. escape(String original)
  2. escape(String s)
  3. escape(String s)
  4. escape(String str)
  5. escape(String string, String chars)
  6. escapeCellText(String text, boolean wrap, boolean multiline)
  7. escapeCharacters(String string)
  8. escapeHTML(String s)
  9. escapeHTML(String source)