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.klco.email2html.OutputWriter.java

/**
 * Converts the specified name or arbitrary string into a path by downcasing
 * it and replacing all non-ASCII characters with dashes.
 * /*from w  ww  .j a  v a  2s . c  o m*/
 * @param str
 *            the string to convert
 * @return the path
 */
public static final String toPath(final String str) {
    if (str == null) {
        return null;
    }
    boolean prev = false;
    final StringBuilder sb = new StringBuilder();
    for (char c : Normalizer.normalize(str.toLowerCase(), Normalizer.Form.NFKD).toCharArray()) {
        if (Character.isLetterOrDigit(c)) {
            sb.append(c);
            prev = true;
        } else if (prev) {
            sb.append('-');
            prev = false;
        }
    }
    if (sb.toString().endsWith("-")) {
        return sb.substring(0, sb.length() - 1);
    } else {
        return sb.toString();
    }
}

From source file:com.cloudbees.hudson.plugins.folder.ChildNameGeneratorTest.java

public static String encode(String s) {
    // we want to test that the name can be different from the on-disk name
    return "$$" + Normalizer.normalize(s, Normalizer.Form.NFD);
}

From source file:DataBase.DatabaseManager.java

public static String stripAccents(String s) {
    s = StringUtils.replaceEachRepeatedly(s.toLowerCase(), InputReplace, OutputReplace);
    s = StringEscapeUtils.escapeSql(s);/*w  w w. j av  a2 s.  c om*/
    s = Normalizer.normalize(s.toLowerCase(), Normalizer.Form.NFD);
    s = s.replaceAll("('+|+)", "'");
    s = s.replaceAll("\"+", "");

    //LOG.info("after stripAccents: " + s);
    return s;
}

From source file:de.gbv.ole.Marc21ToOleBulk.java

/**
 * Write data to bib output./*from ww  w. j  av a2 s. c o m*/
 * 
 * Does Unicode NFC normalization.
 * 
 * Masks these four characters: \n \r \t \\
 * This masking is needed for LOAD DATA INFILE.
 * 
 * @param data      data to write
 * @throws SAXException     on write error
 */
private void write(final String data) throws SAXException {
    String normalized = Normalizer.normalize(data, Normalizer.Form.NFC);
    char[] charArray = StringUtils.replaceEach(normalized, new String[] { "\n", "\r", "\t", "\\", },
            new String[] { "\\\n", "\\\r", "\\\t", "\\\\", }).toCharArray();
    handler.characters(charArray, 0, charArray.length);
}

From source file:br.gov.frameworkdemoiselle.behave.integration.alm.ALMIntegration.java

public String convertToIdentificationString(String str) {
    String ret = Normalizer.normalize(str, Normalizer.Form.NFD).replace(" ", "")
            .replaceAll("\\p{InCombiningDiacriticalMarks}+", "");
    ret = ret.replaceAll("[-]", "").replaceAll("[:]", "").replaceAll("[.]", "").replaceAll("[#]", "");
    return ret;//from w  w w  . j  a v a2 s .c om
}

From source file:hd3gtv.as5kpc.Serverchannel.java

private String makeName(String suffix_label, TextField showname) {
    StringBuilder sb = new StringBuilder();
    Calendar c = Calendar.getInstance();
    if (c.get(Calendar.DAY_OF_MONTH) < 10) {
        sb.append("0");
    }//www  .  j  av a  2  s.  c  o m
    sb.append(c.get(Calendar.DAY_OF_MONTH));
    sb.append("-");
    if (c.get(Calendar.MONTH) + 1 < 10) {
        sb.append("0");
    }
    sb.append(c.get(Calendar.MONTH) + 1);
    sb.append(" ");

    String show_name = showname.getText();
    show_name = PATTERN_Combining_Diacritical_Marks
            .matcher(Normalizer.normalize(showname.getText(), Normalizer.Form.NFD)).replaceAll("").trim()
            .toUpperCase();
    showname.setText(show_name);

    sb.append(show_name);

    if (show_name.endsWith(suffix_label) == false) {
        sb.append(" ");
        sb.append(suffix_label);
    }
    return sb.toString();
}

From source file:uk.sipperfly.utils.CommonUtil.java

/**
 * Create xml file to export//from  ww w .  j av  a 2  s.c  om
 *
 * @param recipient
 * @param ftp
 * @param config
 * @param bagInfo
 * @param path
 */
public String createXMLExport(List<Recipients> recipient, FTP ftp, Configurations config, List<BagInfo> bagInfo,
        String path, Boolean template) {
    try {
        char[] charArray = { '<', '>', '&', '"', '\\', '!', '#', '$', '%', '\'', '(', ')', '*', '+', ',', '/',
                ':', ';', '=', '?', '@', '[', ']', '^', '`', '{', '|', '}', '~' };
        String name = "Exactly_Configuration_" + System.currentTimeMillis() + ".xml";
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        Document doc = docBuilder.newDocument();
        Element temElement = doc.createElement("Exactly");
        doc.appendChild(temElement);
        //////bag info
        Element bagElement = doc.createElement("Metadata");
        temElement.appendChild(bagElement);

        for (BagInfo b : bagInfo) {
            StringBuilder stringBuilder = new StringBuilder();
            char[] txt = Normalizer.normalize(b.getLabel(), Normalizer.Form.NFD).toCharArray();
            for (int i = 0; i < b.getLabel().length(); i++) {
                int check = 0;
                for (int j = 0; j < charArray.length; j++) {
                    if (txt[i] == charArray[j]) {
                        check = 1;

                    }
                }
                if (check == 0) {
                    stringBuilder.append(txt[i]);
                }
            }
            Element firstname = doc.createElement(stringBuilder.toString().replace(" ", "-"));
            firstname.appendChild(doc.createTextNode(b.getValue()));
            Attr attr = doc.createAttribute("label");
            attr.setValue(Normalizer.normalize(b.getLabel(), Normalizer.Form.NFD).toString());
            firstname.setAttributeNode(attr);
            bagElement.appendChild(firstname);
        }
        //////emails
        Element recipientElement = doc.createElement("Recipients");
        temElement.appendChild(recipientElement);

        for (Recipients r : recipient) {
            if (r.getEmail() != null) {
                Element mail = doc.createElement("Email");
                mail.appendChild(doc.createTextNode(r.getEmail()));
                recipientElement.appendChild(mail);
            }
        }
        //////////ftp
        Element ftpElement = doc.createElement("FTP");
        temElement.appendChild(ftpElement);

        Element server = doc.createElement("Host");
        if (ftp.getHostName() != null) {
            server.appendChild(doc.createTextNode(ftp.getHostName()));
        } else {
            server.appendChild(doc.createTextNode(""));
        }
        ftpElement.appendChild(server);

        Element user = doc.createElement("Username");
        if (ftp.getUsername() != null) {
            user.appendChild(doc.createTextNode(ftp.getUsername()));
        } else {
            user.appendChild(doc.createTextNode(""));
        }
        ftpElement.appendChild(user);

        Element password1 = doc.createElement("Password");

        if (ftp.getPassword() == null || ftp.getPassword() == "") {
            password1.appendChild(doc.createTextNode(""));
        } else {
            password1.appendChild(doc.createTextNode(EncryptDecryptUtil.encrypt(ftp.getPassword())));
        }
        ftpElement.appendChild(password1);

        Element port = doc.createElement("Port");
        port.appendChild(doc.createTextNode(String.valueOf(ftp.getPort())));
        ftpElement.appendChild(port);

        Element protocol = doc.createElement("Mode");
        protocol.appendChild(doc.createTextNode(ftp.getMode()));
        ftpElement.appendChild(protocol);

        Element email = doc.createElement("Destination");
        if (ftp.getDestination() != null) {
            email.appendChild(doc.createTextNode(ftp.getDestination()));
        } else {
            email.appendChild(doc.createTextNode(""));
        }
        ftpElement.appendChild(email);
        //////configurations
        Element configElement = doc.createElement("configurations");
        temElement.appendChild(configElement);

        Element server1 = doc.createElement("Server-Name");
        if (config.getServerName() != null) {
            server1.appendChild(doc.createTextNode(config.getServerName()));
        } else {
            server1.appendChild(doc.createTextNode(""));
        }
        configElement.appendChild(server1);

        Element user1 = doc.createElement("Username");
        if (config.getUsername() != null) {
            user1.appendChild(doc.createTextNode(config.getUsername()));
        } else {
            user1.appendChild(doc.createTextNode(""));
        }
        configElement.appendChild(user1);

        Element password = doc.createElement("Password");

        if (config.getPassword() == null || config.getPassword() == "") {
            password.appendChild(doc.createTextNode(""));
        } else {
            password.appendChild(doc.createTextNode(EncryptDecryptUtil.encrypt(config.getPassword())));
        }
        configElement.appendChild(password);

        Element port1 = doc.createElement("Port");
        port1.appendChild(doc.createTextNode(config.getServerPort()));
        configElement.appendChild(port1);

        Element protocol1 = doc.createElement("Protocol");
        protocol1.appendChild(doc.createTextNode(config.getServerProtocol()));
        configElement.appendChild(protocol1);

        Element email1 = doc.createElement("Email-Notification");
        if (config.getEmailNotifications()) {
            email1.appendChild(doc.createTextNode("true"));
        } else {
            email1.appendChild(doc.createTextNode("false"));
        }

        configElement.appendChild(email1);
        // write the content into xml file
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        if (template) {
            StringWriter writer = new StringWriter();
            transformer.transform(source, new StreamResult(writer));
            String output = writer.getBuffer().toString();
            return output;
        } else {
            StreamResult result = new StreamResult(new File(path + File.separator + name));
            transformer.transform(source, result);
        }
    } catch (ParserConfigurationException ex) {
        Logger.getLogger(CommonUtil.class.getName()).log(Level.SEVERE, null, ex);
    } catch (Exception ex) {
        Logger.getLogger(CommonUtil.class.getName()).log(Level.SEVERE, null, ex);
    }
    return "XML exported";
}

From source file:com.healthmarketscience.jackcess.impl.OleUtil.java

private static byte[] getZeroTermStrBytes(String str) {
    // since we are converting to ascii, try to make "nicer" versions of crazy
    // chars (e.g. convert "u with an umlaut" to just "u").  this may not
    // ultimately help anything but it is what ms access does.

    // decompose complex chars into combos of char and accent
    str = Normalizer.normalize(str, Normalizer.Form.NFD);
    // strip the accents
    str = UNICODE_ACCENT_PATTERN.matcher(str).replaceAll("");
    // (re)normalize what is left
    str = Normalizer.normalize(str, Normalizer.Form.NFC);

    return (str + '\0').getBytes(OLE_CHARSET);
}

From source file:org.iavante.uploader.ifaces.impl.UploadServiceImpl.java

/**
 * Save to disk and extract metadata.// www  . j av a  2s .  c  o m
 * 
 * @param request
 * @param response
 * @return 200 (OK), 404 (Not saved)
 * @throws Exception
 */
private int saveAndExtract(HttpServletRequest request, HttpServletResponse response) throws Exception {

    if (out.isInfoEnabled())
        out.info("saveAndExtract, ini");

    int code = 0;

    Enumeration en2 = request.getParameterNames();
    while (en2.hasMoreElements()) {
        String cad = (String) en2.nextElement();
        if (out.isInfoEnabled())
            out.info("saveAndExtract, Parameter: " + cad);
    }

    dirUploadFiles = Constants.DIRUPLOADFILES;
    paramData = Constants.PARAMDATA;
    paramNotification = Constants.PARAMNOTIFICATION;
    paramFilename = Constants.PARAMFILENAME;

    if (ServletFileUpload.isMultipartContent(request)) {

        if (out.isInfoEnabled())
            out.info("saveAndExtract, isMultipartContent");
        if (out.isInfoEnabled())
            out.info("saveAndExtract, Constants.PARAMDATA=" + Constants.PARAMDATA);

        // Notification url
        String urlString = Constants.BASICURL;
        String param = request.getParameter("coreurl");
        if (param != null) {
            urlString = param;
        }
        param = request.getParameter(Constants.PARAMNOTIFICATION);
        if (param != null) {
            urlString += param;
        }

        Constants.URLPOST = urlString;
        if (out.isInfoEnabled())
            out.info("saveAndExtract, Constants.URLPOST=" + Constants.URLPOST);

        // Getting file bytes.
        SlingHttpServletRequest req = (SlingHttpServletRequest) request;
        byte[] data = req.getRequestParameter(Constants.PARAMDATA).get();

        if (out.isInfoEnabled())
            out.info("saveAndExtract, data.length=" + data.length);

        if (data.length > 0) {

            // Getting file
            Date today = new Date();
            if (request.getParameter(Constants.PARAMFILENAME) == null) {
                String[] ne = request.getParameter(Constants.PARAMDATA).split("\\.");
                props.put("nombre", ne[ne.length - 2].replaceAll("[ ?]+", "_") + "_" + today.getTime() + "."
                        + ne[ne.length - 1]);
            } else {
                String[] ne = request.getParameter(Constants.PARAMFILENAME).split("\\.");
                props.put("nombre", ne[ne.length - 2].replaceAll("[ ?]+", "_") + "_" + today.getTime() + "."
                        + ne[ne.length - 1]);
            }

            props.put("nombre", Normalizer.normalize((String) props.get("nombre"), Normalizer.Form.NFC));

            props.put("size", String.valueOf(data.length));

            // Save the file
            File archivo = new File(dirUploadFiles, (String) props.get("nombre"));
            FileOutputStream fout;
            fout = new FileOutputStream(archivo);
            // fout.write(request.getParameter(Constants.PARAMDATA).getBytes(
            // "ISO-8859-1"));
            fout.write(data);

            if (archivo.exists()) {
                results += "SAVED " + archivo.getAbsolutePath() + "</p>";
                if (out.isInfoEnabled())
                    out.info("saveAndExtract, SAVED " + archivo.getAbsolutePath());

                // Extract mimetype
                List cadenas0 = new ArrayList();
                cadenas0.add(props.get("nombre"));
                SystemProcess p0 = new SystemProcess();
                String orden0 = Constants.PATHFILE + "/file -i " + dirUploadFiles + "/" + props.get("nombre"); // command to execute
                Map aux0 = p0.start(orden0, cadenas0);

                for (int i = 0; i < cadenas0.size(); i++) {
                    // only look for the first match of the searched string ->
                    // (String) ((List) aux.get(cadenas.get(i))).get(0)
                    if (((List) aux0.get(cadenas0.get(i))).size() > 0) {
                        String linea = (String) ((List) aux0.get(cadenas0.get(i))).get(0);
                        String regex = cadenas0.get(i) + "(\\:)(\\s+)[\\S]+";
                        String res = "";

                        Pattern p = Pattern.compile(regex);
                        Matcher m = p.matcher(linea);
                        if (m.find()) {
                            res = m.group();
                            String[] aux2 = res.split("[\\s]+");
                            aux2[1] = aux2[1].replaceAll(";$", "");
                            props.put("mime", aux2[1]);
                        }
                        if (out.isInfoEnabled())
                            out.info(res);
                    }
                }

                // Setting 'type' param
                String mime = (String) props.get("mime");
                String[] mimeSplit = mime.split("/");
                if ("application/ogg".compareTo(mime) == 0 || "application/octet-stream".compareTo(mime) == 0) {
                    props.put("type", "video");
                } else {
                    props.put("type", mimeSplit[0]);
                }

                if (out.isInfoEnabled())
                    out.info("extracted mimetype, extracting metadata...");

                Document doc = null; // parse the document
                synchronized (contentParserAccess) {
                    doc = contentParser.getDocument(archivo, (String) props.get("mime"));
                }
                if (out.isInfoEnabled())
                    out.info("lucene doc.body -> " + doc.getField("body").stringValue());

                List<String> tags = new ArrayList<String>();
                tags.add("duration");
                tags.add("bitrate");
                tags.add("video");
                tags.add("audio");

                for (String tag : tags) {
                    Field field = doc.getField(tag);
                    if (field != null) {
                        props.put(tag.toLowerCase(), field.stringValue());
                    }
                }

                String[] ne = ((String) props.get("nombre")).split("\\.");
                bodyFilename = dirUploadFiles + "/" + ne[0] + ".txt";

                FileWriter fw = new FileWriter(bodyFilename);
                doc2txt(doc, fw);
                fw.flush();
                fw.close();

                extracted = true;
                code = 200;

            } else {
                // if save failed
                results += "ERROR SAVING FILE. NOT EXISTS " + archivo.getAbsolutePath() + "</p>";
                if (out.isInfoEnabled())
                    out.info("saveAndExtract, ERROR SAVING FILE. NOT EXISTS " + archivo.getAbsolutePath());
                code = 404;
            }

            results += "Props: " + props + "<br>";

        } else {
            results += "Size: 0 <br>";
        }
    }

    if (out.isInfoEnabled())
        out.info("saveAndExtract, end");

    return code;

}