ingo@100: /* ingo@100: * Copyright (c) 2010 by Intevation GmbH ingo@100: * ingo@100: * This program is free software under the LGPL (>=v2.1) ingo@100: * Read the file LGPL.txt coming with the software for details ingo@100: * or visit http://www.gnu.org/licenses/ if it does not exist. ingo@100: */ ingo@100: ingo@79: package de.intevation.artifactdatabase; ingo@79: ingo@79: import java.io.UnsupportedEncodingException; ingo@79: ingo@79: import java.util.UUID; ingo@79: ingo@80: import org.apache.commons.codec.DecoderException; ingo@80: ingo@80: import org.apache.commons.codec.binary.Hex; ingo@80: ingo@79: import org.apache.log4j.Logger; ingo@79: ingo@79: /** sascha@87: * Commonly used string functions. sascha@87: * ingo@79: * @author Sascha L. Teichmann ingo@79: */ ingo@79: public final class StringUtils ingo@79: { ingo@79: private static Logger logger = Logger.getLogger(StringUtils.class); ingo@79: sascha@87: /** sascha@87: * Generated a random UUIDv4 in form of a string. sascha@87: * @return the UUID sascha@87: */ ingo@79: public static final String newUUID() { ingo@79: return UUID.randomUUID().toString(); ingo@79: } ingo@79: sascha@87: /** sascha@87: * Checks if a given string is a valid UUID. sascha@87: * @param uuid The string to test. sascha@87: * @return true if the string is a valid UUID else false. sascha@87: */ ingo@79: public static final boolean checkUUID(String uuid) { ingo@79: try { ingo@79: UUID.fromString(uuid); ingo@79: } ingo@79: catch (IllegalArgumentException iae) { ingo@79: logger.warn(iae.getLocalizedMessage()); ingo@79: return false; ingo@79: } ingo@79: return true; ingo@79: } ingo@79: sascha@87: /** sascha@87: * Returns the UTF-8 byte array representation of a given string. sascha@87: * @param s The string to be transformed. sascha@87: * @return The byte array representation. sascha@87: */ ingo@79: public static final byte [] getUTF8Bytes(String s) { ingo@79: try { ingo@79: return s.getBytes("UTF-8"); ingo@79: } ingo@79: catch (UnsupportedEncodingException usee) { ingo@79: logger.error(usee.getLocalizedMessage(), usee); ingo@79: return s.getBytes(); ingo@79: } ingo@79: } ingo@80: sascha@87: /** sascha@87: * Tries to convert a Base64 encoded string into the sascha@87: * corresponing byte array. sascha@87: * @param s The Base64 encoded string sascha@87: * @return The byte array representation or null if sascha@87: * an decoding error occurs. sascha@87: */ ingo@80: public static final byte [] decodeHex(String s) { ingo@80: try { ingo@80: return Hex.decodeHex(s.toCharArray()); ingo@80: } ingo@80: catch (DecoderException de) { ingo@80: return null; ingo@80: } ingo@80: } sascha@231: sascha@231: public static final String repeat(String s, int count, String sep) { sascha@231: if (count <= 0) { sascha@231: return ""; sascha@231: } sascha@231: StringBuilder sb = new StringBuilder(s); sascha@237: for (--count; count > 0; --count) { sascha@231: sb.append(sep).append(s); sascha@231: } sascha@231: return sb.toString(); sascha@231: } sascha@231: sascha@231: public static final String repeat(char c, int count, char sep) { sascha@231: if (count <= 0) { sascha@231: return ""; sascha@231: } sascha@231: StringBuilder sb = new StringBuilder(2*count-1).append(c); sascha@237: for (--count; count > 0; --count) { sascha@231: sb.append(sep).append(c); sascha@231: } sascha@231: return sb.toString(); sascha@231: } ingo@79: } ingo@79: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :