Example usage for java.text Normalizer normalize

List of usage examples for java.text Normalizer normalize

Introduction

In this page you can find the example usage for java.text Normalizer normalize.

Prototype

public static String normalize(CharSequence src, Form form) 

Source Link

Document

Normalize a sequence of char values.

Usage

From source file:org.sonar.core.permission.PermissionDao.java

private String generateTemplateKee(String name, Date timeStamp) {
    if (PermissionTemplateDto.DEFAULT.getName().equals(name)) {
        return PermissionTemplateDto.DEFAULT.getKee();
    }/*from  w  w w .  j av a2 s  . c om*/
    String normalizedName = Normalizer.normalize(name, Normalizer.Form.NFD).replaceAll("[^\\p{ASCII}]", "")
            .replace(" ", "_");
    return normalizedName.toLowerCase() + "_" + DateFormatUtils.format(timeStamp, "yyyyMMdd_HHmmss");
}

From source file:jp.furplag.util.commons.StringUtils.java

/**
 * normalize a String./*from   w ww. j a v  a 2s. com*/
 *
 * <pre>
 * StringUtils.normalize(null, false) = null
 * StringUtils.normalize(null, true) = ""
 * StringUtils.normalize("", false) = null
 * StringUtils.normalize("", true) = ""
 * StringUtils.normalize("abc123", {@code true}/{@code false}) = "abcdef123456"
 * StringUtils.normalize("ABC123", {@code true}/{@code false}) = "ABCDEF123456"
 * StringUtils.normalize("?????", {@code true}/{@code false}) = "?????"
 * </pre>
 *
 * @param str the string, may be null.
 * @param emptyToBlank if true, return empty String ("") if the String is null, whitespace, full-width space, empty, newline or whitespace
 * @return normalized string.
 */
public static String normalize(final String str, final boolean emptyToBlank) {
    if (isSimilarToBlank(str))
        return emptyToBlank ? EMPTY : null;

    return Normalizer.normalize(str, Normalizer.Form.NFKC).replaceAll("[\\s\\t]+", " ")
            .replaceAll("[??]", "-").trim();
}

From source file:cz.zcu.kiv.eegdatabase.data.service.HibernatePersonService.java

private String createUniqueUsername(String firstName, String surname) {
    log.debug("Generating unique username");
    String username = firstName.toLowerCase() + "-" + surname.toLowerCase();

    // Removing the diacritical marks
    String decomposed = Normalizer.normalize(username, Normalizer.Form.NFD);
    username = decomposed.replaceAll("[^\\p{ASCII}]", "");

    String tempUsername = username;
    // if the username already exists generate random numbers until some combination is free
    // if the username does not exist, we can use it (so the while is jumped over)
    while (personDao.usernameExists(tempUsername)) {
        Random random = new Random();
        int number = random.nextInt(999) + 1; // not many users are expected to have the same name and surname, so 1000 numbers is enough
        tempUsername = username + "-" + number;
    }//  w w w. ja v  a 2s  . c  o  m
    return tempUsername;
}

From source file:com.money.manager.ex.core.Core.java

/**
 * This method allows to highlight in bold the content of a search string
 * @param search       string//  www.  ja v a 2 s . c om
 * @param originalText string where to find
 * @return CharSequence modified
 */
public CharSequence highlight(String search, String originalText) {
    if (TextUtils.isEmpty(search))
        return originalText;
    // ignore case and accents
    // the same thing should have been done for the search text
    String normalizedText = Normalizer.normalize(originalText, Normalizer.Form.NFD)
            .replaceAll("\\p{InCombiningDiacriticalMarks}+", "").toLowerCase();

    int start = normalizedText.indexOf(search.toLowerCase());
    if (start < 0) {
        // not found, nothing to to
        return originalText;
    } else {
        // highlight each appearance in the original text
        // while searching in normalized text
        Spannable highlighted = new SpannableString(originalText);
        while (start >= 0) {
            int spanStart = Math.min(start, originalText.length());
            int spanEnd = Math.min(start + search.length(), originalText.length());

            highlighted.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), spanStart, spanEnd,
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

            start = normalizedText.indexOf(search, spanEnd);
        }
        return highlighted;
    }
}

From source file:org.nuxeo.ecm.core.storage.dbs.DBSSession.java

protected String normalize(String path) {
    return Normalizer.normalize(path, Normalizer.Form.NFC);
}

From source file:com.dsh105.commodus.StringUtil.java

/**
 * Strips all diacritics (special characters) from the given string
 * <p/>//  w ww .  j  a v a  2  s  .  c  o  m
 * From http://stackoverflow.com/a/1453284
 *
 * @param input string to remove diacritics from
 * @return the stripped string
 */
public static String stripDiacritics(String input) {
    input = Normalizer.normalize(input, Normalizer.Form.NFD);
    input = DIACRITICS_AND_FRIENDS.matcher(input).replaceAll("");
    return input;
}

From source file:org.cryptomator.ui.model.Vault.java

/**
 * Tries to form a similar string using the regular latin alphabet.
 * /*from  www . j ava  2s .c om*/
 * @param string
 * @return a string composed of a-z, A-Z, 0-9, and _.
 */
public static String normalize(String string) {
    String normalized = Normalizer.normalize(string, Form.NFD);
    StringBuilder builder = new StringBuilder();
    for (int i = 0; i < normalized.length(); i++) {
        char c = normalized.charAt(i);
        if (Character.isWhitespace(c)) {
            if (builder.length() == 0 || builder.charAt(builder.length() - 1) != '_') {
                builder.append('_');
            }
        } else if (c < 127 && Character.isLetterOrDigit(c)) {
            builder.append(c);
        } else if (c < 127) {
            if (builder.length() == 0 || builder.charAt(builder.length() - 1) != '_') {
                builder.append('_');
            }
        }
    }
    return builder.toString();
}

From source file:TreeColapse.java

/**
 * @throws IOException/*from www  .j a v  a  2s  .c  o  m*/
 *
 */
private void createTree() throws IOException {
    int nivelTree = 0;
    FileReader txtFile = new FileReader(
            "C:/Users/Vinicius/Desktop/fantinatto/academico/TCC/alg/dataminingSE/taxonomia_materias.txt");
    BufferedReader txtBuffer = new BufferedReader(txtFile);
    String curLine = txtBuffer.readLine();

    Node root = new Node();
    root.setVal("Root", 0);
    graph.addVertex(root);
    Node aux = root;

    while (curLine != null) {
        String minusculo = curLine.toLowerCase(); // normaliza em minusculo
        // e sem acento
        minusculo = Normalizer.normalize(minusculo, Normalizer.Form.NFD);
        minusculo = minusculo.replaceAll("[^\\p{ASCII}]", "");
        String[] linhaSplit = minusculo.split("\t");

        // tamanho do indice
        nivelTree = linhaSplit[0].length();
        // encontra pai para o novo n
        while (nivelTree <= aux.getNivel()) {
            // pega n pai, se for root retorna null
            if (graph.getParent(aux) != null) {
                aux = graph.getParent(aux);
            }
        }
        if (nivelTree == aux.getNivel()) {
            Node newNode = new Node();
            newNode.setVal(linhaSplit[1], nivelTree);
            graph.addEdge(edgeFactory.create(), graph.getParent(aux), newNode);
            aux = graph.getParent(newNode);
        } else {
            Node newNode = new Node();
            newNode.setVal(linhaSplit[1], nivelTree);
            graph.addEdge(edgeFactory.create(), aux, newNode);
            aux = newNode;

        }

        curLine = txtBuffer.readLine();

    }

}

From source file:org.sonar.plugins.visualstudio.VisualStudioProjectBuilder.java

@VisibleForTesting
static String escapeProjectName(String projectName) {
    String escaped = Normalizer.normalize(projectName, Normalizer.Form.NFD);
    escaped = escaped.replaceAll("\\p{M}", "");
    escaped = escaped.replace(' ', '_');
    escaped = escaped.replace('+', '_');
    return escaped;
}

From source file:be.wolkmaan.klimtoren.security.digest.StandardStringDigester.java

@Override
public boolean matches(final String message, final String digest) {

    String processedDigest = digest;

    if (processedDigest != null) {
        if (this.prefix != null) {
            if (!processedDigest.startsWith(this.prefix)) {
                throw new EncryptionOperationNotPossibleException(
                        "Digest does not start with required prefix \"" + this.prefix + "\"");
            }// w  w w.ja  va 2s  .  c om
            processedDigest = processedDigest.substring(this.prefix.length());
        }
        if (this.suffix != null) {
            if (!processedDigest.endsWith(this.suffix)) {
                throw new EncryptionOperationNotPossibleException(
                        "Digest does not end with required suffix \"" + this.suffix + "\"");
            }
            processedDigest = processedDigest.substring(0, processedDigest.length() - this.suffix.length());
        }
    }

    if (message == null) {
        return (processedDigest == null);
    } else if (processedDigest == null) {
        return false;
    }

    // Check initialization
    if (!isInitialized()) {
        initialize();
    }

    try {

        // Normalize Unicode message to NFC form
        String normalizedMessage;
        if (!this.unicodeNormalizationIgnored) {
            normalizedMessage = Normalizer.normalize(message, Normalizer.Form.NFC);
        } else {
            normalizedMessage = message;
        }

        // We get a valid byte array from the message, in the 
        // fixed MESSAGE_CHARSET that the digest operations use.
        final byte[] messageBytes = normalizedMessage.getBytes(MESSAGE_CHARSET);

        // The BASE64 or HEXADECIMAL encoding is reversed and the digest
        // is converted into a byte array.
        byte[] digestBytes;
        if (this.stringOutputTypeBase64) {
            // The digest must be a US-ASCII String BASE64-encoded
            digestBytes = processedDigest.getBytes(DIGEST_CHARSET);
            digestBytes = this.base64.decode(digestBytes);
        } else {
            digestBytes = CommonUtils.fromHexadecimal(processedDigest);
        }

        // The StandardByteDigester is asked to match message to digest.
        return this.byteDigester.matches(messageBytes, digestBytes);

    } catch (EncryptionInitializationException | EncryptionOperationNotPossibleException e) {
        throw e;
    } catch (UnsupportedEncodingException e) {
        // If digest fails, it is more secure not to return any information
        // about the cause in nested exceptions. Simply fail.
        throw new EncryptionOperationNotPossibleException();
    }

}