view artifact-database/src/main/java/de/intevation/artifactdatabase/StringUtils.java @ 184:a22b7e367b25

Added Backend code to list the collection items in a collection. artifacts/trunk@1410 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 07 Mar 2011 11:37:49 +0000
parents 933bbc9fc11f
children 1ea35226a6de
line wrap: on
line source
/*
 * Copyright (c) 2010 by Intevation GmbH
 *
 * This program is free software under the LGPL (>=v2.1)
 * Read the file LGPL.txt coming with the software for details
 * or visit http://www.gnu.org/licenses/ if it does not exist.
 */

package de.intevation.artifactdatabase;

import java.io.UnsupportedEncodingException;

import java.util.UUID;

import org.apache.commons.codec.DecoderException;

import org.apache.commons.codec.binary.Hex;

import org.apache.log4j.Logger;

/**
 * Commonly used string functions.
 *
 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a>
 */
public final class StringUtils
{
    private static Logger logger = Logger.getLogger(StringUtils.class);

    /**
     * Generated a random UUIDv4 in form of a string.
     * @return the UUID
     */
    public static final String newUUID() {
        return UUID.randomUUID().toString();
    }

    /**
     * Checks if a given string is a valid UUID.
     * @param uuid The string to test.
     * @return true if the string is a valid UUID else false.
     */
    public static final boolean checkUUID(String uuid) {
        try {
            UUID.fromString(uuid);
        }
        catch (IllegalArgumentException iae) {
            logger.warn(iae.getLocalizedMessage());
            return false;
        }
        return true;
    }

    /**
     * Returns the UTF-8 byte array representation of a given string.
     * @param s The string to be transformed.
     * @return The byte array representation.
     */
    public static final byte [] getUTF8Bytes(String s) {
        try {
            return s.getBytes("UTF-8");
        }
        catch (UnsupportedEncodingException usee) {
            logger.error(usee.getLocalizedMessage(), usee);
            return s.getBytes();
        }
    }

    /**
     * Tries to convert a Base64 encoded string into the
     * corresponing byte array.
     * @param s The Base64 encoded string
     * @return The byte array representation or null if
     * an decoding error occurs.
     */
    public static final byte [] decodeHex(String s) {
        try {
            return Hex.decodeHex(s.toCharArray());
        }
        catch (DecoderException de) {
            return null;
        }
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org