de.thischwa.pmcms.tool.Utils.java Source code

Java tutorial

Introduction

Here is the source code for de.thischwa.pmcms.tool.Utils.java

Source

/*******************************************************************************
 * Poor Man's CMS (pmcms) - A very basic CMS generating static html pages.
 * http://poormans.sourceforge.net
 * Copyright (C) 2004-2013 by Thilo Schwarz
 * 
 * == BEGIN LICENSE ==
 * 
 * Licensed under the terms of any of the following licenses at your
 * choice:
 * 
 *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
 *    http://www.gnu.org/licenses/lgpl.html
 * 
 *  - Mozilla Public License Version 1.1 or later (the "MPL")
 *    http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * == END LICENSE ==
 ******************************************************************************/
package de.thischwa.pmcms.tool;

import org.apache.commons.lang.StringUtils;

/**
 * Collection of static helper methods I didn't find a nicer place.
 *
 * @author Thilo Schwarz
 */
public class Utils {

    /**
     * Joins variable numbers of string.
     * 
     * @param strings
     * @return joined string
     */
    public static String join(String... strings) {
        StringBuilder sb = new StringBuilder();
        for (String string : strings)
            sb.append(string);
        return sb.toString();
    }

    public static String getExtension(String path) {
        if (StringUtils.isBlank(path))
            return null;
        int lastExtPos = path.lastIndexOf(".");
        return (lastExtPos != -1) ? path.substring(lastExtPos + 1) : null;
    }

    public static boolean quietlyDelay(long millis) {
        try {
            Thread.sleep(millis);
            return true;
        } catch (InterruptedException e) {
            return false;
        }
    }

    /**
      * This method ensures that the output String has only
      * valid XML unicode characters as specified by the
      * XML 1.0 standard. For reference, please see
      * <a href="http://www.w3.org/TR/2000/REC-xml-20001006#NT-Char">the
      * standard</a>. 
      *
      * @param in The String whose non-valid characters we want to remove.
      * @return The in String, stripped of non-valid characters.
      */
    public static String stripNonValidXMLCharacters(String in) {
        if (in == null)
            return null;
        if (in.isEmpty())
            return "";

        StringBuffer out = new StringBuffer();
        char current;
        for (int i = 0; i < in.length(); i++) {
            current = in.charAt(i);
            if ((current == 0x9) || (current == 0xA) || (current == 0xD)
                    || ((current >= 0x20) && (current <= 0xD7FF)) || ((current >= 0xE000) && (current <= 0xFFFD))
                    || ((current >= 0x10000) && (current <= 0x10FFFF)))
                out.append(current);
        }
        return out.toString();
    }
}