Java Java String Format convertToJavaClassname(final String classname, final Map replacement)

Here you can find the source of convertToJavaClassname(final String classname, final Map replacement)

Description

convert To Java Classname

License

Open Source License

Declaration

public static String convertToJavaClassname(final String classname, final Map<Character, String> replacement) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.text.CharacterIterator;
import java.text.StringCharacterIterator;
import java.util.Map;

public class Main {
    public static String convertToJavaClassname(final String classname, final Map<Character, String> replacement) {
        if (classname == null) {
            return null;
        }// www .  ja v  a  2 s. c o  m

        final StringBuilder sb = new StringBuilder();

        final CharacterIterator iter = new StringCharacterIterator(classname);
        char c = iter.first();
        if (c == CharacterIterator.DONE) {
            return null;
        }
        if (!Character.isJavaIdentifierStart(c) && !Character.isIdentifierIgnorable(c)) {
            String repl = replacement.get(c);
            if (repl == null) {
                repl = "_";
                System.err.println("no replacement for illegal character found: " + c);
            }
            sb.append(repl);
        } else {
            sb.append(c);
        }
        c = iter.next();
        while (c != CharacterIterator.DONE) {
            if (!Character.isJavaIdentifierPart(c) && !Character.isIdentifierIgnorable(c)) {
                String repl = replacement.get(c);
                if (repl == null) {
                    repl = "_";
                    System.err.println("no replacement for illegal character found: " + c);
                }
                sb.append(repl);
            } else {
                sb.append(c);
            }
            c = iter.next();
        }
        return sb.toString();
    }
}

Related

  1. convertToJavaIdentity(String original, boolean firstCharacterUpperCase, boolean normalize)
  2. convertToJavaUnicode(String input, Character start1, Character end1, Character start2, Character end2)
  3. toJava(CharSequence str)
  4. toJava(final double[] doubles)