eu.scape_project.hawarp.utils.StringUtils.java Source code

Java tutorial

Introduction

Here is the source code for eu.scape_project.hawarp.utils.StringUtils.java

Source

/*
 * Copyright 2012 The SCAPE Project Consortium.
 *
 * 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.
 * under the License.
 */
package eu.scape_project.hawarp.utils;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import javax.ws.rs.core.UriBuilder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * String utils
 *
 * @author Sven Schlarb https://github.com/shsdev
 */
public class StringUtils {

    private static final Log LOG = LogFactory.getLog(StringUtils.class);

    /**
     * Creates a normalised directory string. Parts will be normalised to a
     * directory string which only has a trailing file path separator. The parts
     * are then concatenated to the final output string.
     *
     * @param dirs Directory strings
     * @return
     */
    public static String normdir(String... dirs) {
        String outDir = "";
        for (String dir : dirs) {
            String dirPart = (dir.startsWith(File.separator) ? dir.substring(1) : dir);
            outDir += ((dirPart.endsWith(File.separator)) ? dirPart : dirPart + File.separator);
        }
        return outDir;
    }

    public static String ensureTrailSep(String path) {
        if (path.charAt(path.length() - 1) != File.separatorChar) {
            path += File.separator;
        }
        return path;
    }

    public static String formatCommandOutput(String pattern, String inlist, String outlist) {
        String ptmapred_cmd = String.format(pattern, inlist, outlist);
        return ptmapred_cmd;
    }

    public static String getStrUntilChar(String str, String c) {
        int index = str.indexOf(c);
        if (str.contains(c)) {
            return str.substring(0, index);
        } else {
            return str;
        }
    }

    public static String normaliseMimetype(String mime) {
        String normalised = getStrUntilChar(mime, ";");
        HashMap<String, String> replMap = new HashMap<String, String>();
        replMap.put("no-type", "application/octet-stream");
        for (String key : replMap.keySet()) {
            if (normalised.contains(key)) {
                normalised = normalised.replace(key, replMap.get(key));
            }
        }
        return normalised.toLowerCase();
    }

}