📄 xmputils.java
字号:
* <li>{@link XMPConst#TRUESTR} and {@link XMPConst#FALSESTR} * <li>"t" and "f" * <li>"on" and "off" * <li>"yes" and "no" * <li>"value <> 0" and "value == 0" * </ul> * @throws XMPException If an empty string is passed. */ public static boolean convertToBoolean(String value) throws XMPException { if (value == null || value.length() == 0) { throw new XMPException("Empty convert-string", XMPError.BADVALUE); } value = value.toLowerCase(); try { // First try interpretation as Integer (anything not 0 is true) return Integer.parseInt(value) != 0; } catch (NumberFormatException e) { return "true".equals(value) || "t".equals(value) || "on".equals(value) || "yes".equals(value); } } /** * Convert from boolean to string. * * @param value * a boolean value * @return The XMP string representation of the boolean. The values used are * given by the constnts {@link XMPConst#TRUESTR} and * {@link XMPConst#FALSESTR}. */ public static String convertFromBoolean(boolean value) { return value ? XMPConst.TRUESTR : XMPConst.FALSESTR; } /** * Converts a string value to an <code>int</code>. * * @param rawValue * the string value * @return Returns an int. * @throws XMPException * If the <code>rawValue</code> is <code>null</code> or empty or the * conversion fails. */ public static int convertToInteger(String rawValue) throws XMPException { try { if (rawValue == null || rawValue.length() == 0) { throw new XMPException("Empty convert-string", XMPError.BADVALUE); } if (rawValue.startsWith("0x")) { return Integer.parseInt(rawValue.substring(2), 16); } else { return Integer.parseInt(rawValue); } } catch (NumberFormatException e) { throw new XMPException("Invalid integer string", XMPError.BADVALUE); } } /** * Convert from int to string. * * @param value * an int value * @return The string representation of the int. */ public static String convertFromInteger(int value) { return String.valueOf(value); } /** * Converts a string value to a <code>long</code>. * * @param rawValue * the string value * @return Returns a long. * @throws XMPException * If the <code>rawValue</code> is <code>null</code> or empty or the * conversion fails. */ public static long convertToLong(String rawValue) throws XMPException { try { if (rawValue == null || rawValue.length() == 0) { throw new XMPException("Empty convert-string", XMPError.BADVALUE); } if (rawValue.startsWith("0x")) { return Long.parseLong(rawValue.substring(2), 16); } else { return Long.parseLong(rawValue); } } catch (NumberFormatException e) { throw new XMPException("Invalid long string", XMPError.BADVALUE); } } /** * Convert from long to string. * * @param value * a long value * @return The string representation of the long. */ public static String convertFromLong(long value) { return String.valueOf(value); } /** * Converts a string value to a <code>double</code>. * * @param rawValue * the string value * @return Returns a double. * @throws XMPException * If the <code>rawValue</code> is <code>null</code> or empty or the * conversion fails. */ public static double convertToDouble(String rawValue) throws XMPException { try { if (rawValue == null || rawValue.length() == 0) { throw new XMPException("Empty convert-string", XMPError.BADVALUE); } else { return Double.parseDouble(rawValue); } } catch (NumberFormatException e) { throw new XMPException("Invalid double string", XMPError.BADVALUE); } } /** * Convert from long to string. * * @param value * a long value * @return The string representation of the long. */ public static String convertFromDouble(double value) { return String.valueOf(value); } /** * Converts a string value to an <code>XMPDateTime</code>. * * @param rawValue * the string value * @return Returns an <code>XMPDateTime</code>-object. * @throws XMPException * If the <code>rawValue</code> is <code>null</code> or empty or the * conversion fails. */ public static XMPDateTime convertToDate(String rawValue) throws XMPException { if (rawValue == null || rawValue.length() == 0) { throw new XMPException("Empty convert-string", XMPError.BADVALUE); } else { return ISO8601Converter.parse(rawValue); } } /** * Convert from <code>XMPDateTime</code> to string. * * @param value * an <code>XMPDateTime</code> * @return The string representation of the long. */ public static String convertFromDate(XMPDateTime value) { return ISO8601Converter.render(value); } /** * Convert from a byte array to a base64 encoded string. * * @param buffer * the byte array to be converted * @return Returns the base64 string. */ public static String encodeBase64(byte[] buffer) { return new String(Base64.encode(buffer)); } /** * Decode from Base64 encoded string to raw data. * * @param base64String * a base64 encoded string * @return Returns a byte array containg the decoded string. * @throws XMPException Thrown if the given string is not property base64 encoded */ public static byte[] decodeBase64(String base64String) throws XMPException { try { return Base64.decode(base64String.getBytes()); } catch (Throwable e) { throw new XMPException("Invalid base64 string", XMPError.BADVALUE, e); } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -