📄 text.java
字号:
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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. */package org.apache.jackrabbit.util;import java.io.UnsupportedEncodingException;import java.io.ByteArrayOutputStream;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;import java.util.ArrayList;import java.util.BitSet;import java.util.Properties;/** * This Class provides some text related utilities */public class Text { /** * Hidden constructor. */ private Text() { } /** * used for the md5 */ public static final char[] hexTable = "0123456789abcdef".toCharArray(); /** * Calculate an MD5 hash of the string given. * * @param data the data to encode * @param enc the character encoding to use * @return a hex encoded string of the md5 digested input */ public static String md5(String data, String enc) throws UnsupportedEncodingException { try { return digest("MD5", data.getBytes(enc)); } catch (NoSuchAlgorithmException e) { throw new InternalError("MD5 digest not available???"); } } /** * Calculate an MD5 hash of the string given using 'utf-8' encoding. * * @param data the data to encode * @return a hex encoded string of the md5 digested input */ public static String md5(String data) { try { return md5(data, "utf-8"); } catch (UnsupportedEncodingException e) { throw new InternalError("UTF8 digest not available???"); } } /** * Digest the plain string using the given algorithm. * * @param algorithm The alogrithm for the digest. This algorithm must be * supported by the MessageDigest class. * @param data The plain text String to be digested. * @param enc The character encoding to use * @return The digested plain text String represented as Hex digits. * @throws java.security.NoSuchAlgorithmException if the desired algorithm is not supported by * the MessageDigest class. * @throws java.io.UnsupportedEncodingException if the encoding is not supported */ public static String digest(String algorithm, String data, String enc) throws NoSuchAlgorithmException, UnsupportedEncodingException { return digest(algorithm, data.getBytes(enc)); } /** * Digest the plain string using the given algorithm. * * @param algorithm The alogrithm for the digest. This algorithm must be * supported by the MessageDigest class. * @param data the data to digest with the given algorithm * @return The digested plain text String represented as Hex digits. * @throws java.security.NoSuchAlgorithmException if the desired algorithm is not supported by * the MessageDigest class. */ public static String digest(String algorithm, byte[] data) throws NoSuchAlgorithmException { MessageDigest md = MessageDigest.getInstance(algorithm); byte[] digest = md.digest(data); StringBuffer res = new StringBuffer(digest.length * 2); for (int i = 0; i < digest.length; i++) { byte b = digest[i]; res.append(hexTable[(b >> 4) & 15]); res.append(hexTable[b & 15]); } return res.toString(); } /** * returns an array of strings decomposed of the original string, split at * every occurance of 'ch'. if 2 'ch' follow each other with no intermediate * characters, empty "" entries are avoided. * * @param str the string to decompose * @param ch the character to use a split pattern * @return an array of strings */ public static String[] explode(String str, int ch) { return explode(str, ch, false); } /** * returns an array of strings decomposed of the original string, split at * every occurance of 'ch'. * * @param str the string to decompose * @param ch the character to use a split pattern * @param respectEmpty if <code>true</code>, empty elements are generated * @return an array of strings */ public static String[] explode(String str, int ch, boolean respectEmpty) { if (str == null || str.length() == 0) { return new String[0]; } ArrayList strings = new ArrayList(); int pos; int lastpos = 0; // add snipples while ((pos = str.indexOf(ch, lastpos)) >= 0) { if (pos - lastpos > 0 || respectEmpty) { strings.add(str.substring(lastpos, pos)); } lastpos = pos + 1; } // add rest if (lastpos < str.length()) { strings.add(str.substring(lastpos)); } else if (respectEmpty && lastpos == str.length()) { strings.add(""); } // return stringarray return (String[]) strings.toArray(new String[strings.size()]); } /** * Concatenates all strings in the string array using the specified delimiter. * @param arr * @param delim * @return the concatenated string */ public static String implode(String[] arr, String delim) { StringBuffer buf = new StringBuffer(); for (int i = 0; i < arr.length; i++) { if (i > 0) { buf.append(delim); } buf.append(arr[i]); } return buf.toString(); } /** * Replaces all occurences of <code>oldString</code> in <code>text</code> * with <code>newString</code>. * * @param text * @param oldString old substring to be replaced with <code>newString</code> * @param newString new substring to replace occurences of <code>oldString</code> * @return a string */ public static String replace(String text, String oldString, String newString) { if (text == null || oldString == null || newString == null) { throw new IllegalArgumentException("null argument"); } int pos = text.indexOf(oldString); if (pos == -1) { return text; } int lastPos = 0; StringBuffer sb = new StringBuffer(text.length()); while (pos != -1) { sb.append(text.substring(lastPos, pos)); sb.append(newString); lastPos = pos + oldString.length(); pos = text.indexOf(oldString, lastPos); } if (lastPos < text.length()) { sb.append(text.substring(lastPos)); } return sb.toString(); } /** * Replaces illegal XML characters in the given string by their corresponding * predefined entity references. * * @param text text to be escaped * @return a string */ public static String encodeIllegalXMLCharacters(String text) { if (text == null) { throw new IllegalArgumentException("null argument"); } StringBuffer buf = null; int length = text.length(); int pos = 0; for (int i = 0; i < length; i++) { int ch = text.charAt(i); switch (ch) { case '<': case '>': case '&': case '"': case '\'': if (buf == null) { buf = new StringBuffer(); } if (i > 0) { buf.append(text.substring(pos, i)); } pos = i + 1; break; default: continue; } if (ch == '<') { buf.append("<"); } else if (ch == '>') { buf.append(">"); } else if (ch == '&') { buf.append("&"); } else if (ch == '"') { buf.append("""); } else if (ch == '\'') { buf.append("'"); } } if (buf == null) { return text; } else { if (pos < length) { buf.append(text.substring(pos)); } return buf.toString(); } } /** * The list of characters that are not encoded by the <code>escape()</code> * and <code>unescape()</code> METHODS. They contains the characters as * defined 'unreserved' in section 2.3 of the RFC 2396 'URI generic syntax': * <p/> * <pre> * unreserved = alphanum | mark * mark = "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")" * </pre> */ public static BitSet URISave; /** * Same as {@link #URISave} but also contains the '/' */ public static BitSet URISaveEx; static { URISave = new BitSet(256); int i; for (i = 'a'; i <= 'z'; i++) { URISave.set(i); } for (i = 'A'; i <= 'Z'; i++) { URISave.set(i); } for (i = '0'; i <= '9'; i++) { URISave.set(i); } URISave.set('-'); URISave.set('_'); URISave.set('.'); URISave.set('!'); URISave.set('~'); URISave.set('*'); URISave.set('\''); URISave.set('('); URISave.set(')'); URISaveEx = (BitSet) URISave.clone(); URISaveEx.set('/'); } /** * Does an URL encoding of the <code>string</code> using the * <code>escape</code> character. The characters that don't need encoding * are those defined 'unreserved' in section 2.3 of the 'URI generic syntax' * RFC 2396, but without the escape character. * * @param string the string to encode. * @param escape the escape character. * @return the escaped string * @throws NullPointerException if <code>string</code> is <code>null</code>. */ public static String escape(String string, char escape) { return escape(string, escape, false); } /** * Does an URL encoding of the <code>string</code> using the * <code>escape</code> character. The characters that don't need encoding * are those defined 'unreserved' in section 2.3 of the 'URI generic syntax' * RFC 2396, but without the escape character. If <code>isPath</code> is * <code>true</code>, additionally the slash '/' is ignored, too. * * @param string the string to encode. * @param escape the escape character. * @param isPath if <code>true</code>, the string is treated as path * @return the escaped string * @throws NullPointerException if <code>string</code> is <code>null</code>. */ public static String escape(String string, char escape, boolean isPath) { try { BitSet validChars = isPath ? URISaveEx : URISave; byte[] bytes = string.getBytes("utf-8"); StringBuffer out = new StringBuffer(bytes.length); for (int i = 0; i < bytes.length; i++) { int c = bytes[i] & 0xff; if (validChars.get(c) && c != escape) { out.append((char) c); } else { out.append(escape); out.append(hexTable[(c >> 4) & 0x0f]); out.append(hexTable[(c) & 0x0f]); } } return out.toString(); } catch (UnsupportedEncodingException e) { throw new InternalError(e.toString()); } } /** * Does a URL encoding of the <code>string</code>. The characters that * don't need encoding are those defined 'unreserved' in section 2.3 of * the 'URI generic syntax' RFC 2396. * * @param string the string to encode * @return the escaped string * @throws NullPointerException if <code>string</code> is <code>null</code>. */ public static String escape(String string) { return escape(string, '%'); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -