Java Encode encodeWikiTag(String s)

Here you can find the source of encodeWikiTag(String s)

Description

This is a variant of the encoder which employs the naming convention of Wikimedia, where every character but the first one is case sensitive

License

Open Source License

Declaration

public static String encodeWikiTag(String s) throws IllegalArgumentException 

Method Source Code


//package com.java2s;
//License from project: GNU General Public License 

import java.io.IOException;

public class Main {
    /**/*ww w  .ja v a  2s  .  c  o m*/
     * This is a variant of the encoder which employs the naming convention of Wikimedia,
     * where every character but the first one is case sensitive
     */
    public static String encodeWikiTag(String s) throws IllegalArgumentException {
        StringBuilder sb = null;
        try {
            for (int i = 0; i < s.length(); i++) {
                char c = s.charAt(i);
                if (isUnreserved(c)) {
                    if (isLowecaseAlphabet(c) && i == 0) {
                        c = capitalize(c);
                        sb = new StringBuilder(s.length()); // minimum length of a new string
                    }
                    if (sb != null)
                        sb.append(c);
                } else {
                    if (sb == null) {
                        sb = new StringBuilder(s.length() + 3); // minimum length of a new string
                        for (int j = 0; j < i; j++) {
                            sb.append(s.charAt(j));
                        }
                    }
                    encodeTagChar(c, sb);
                }
            }
        } catch (IOException e) {
            throw new IllegalArgumentException("Error encode the tag value " + s, e);
        }
        return (sb != null ? sb.toString() : s);
    }

    /** OpenTSDB only accepts alphanumeric values and "-", "_", ".", "/", but
     * we will reserve "-" for our encoding purpose */
    public static boolean isUnreserved(char c) {
        return (in(c, 'a', 'z') || in(c, 'A', 'Z') || in(c, '0', '9') || (c == '/') || (c == '_') || (c == '.'));
    }

    protected static boolean isLowecaseAlphabet(char c) {
        return (in(c, 'a', 'z'));
    }

    private static char capitalize(char c) {
        return (char) (c - 32);
    }

    /** encode a char in TSDB format and append it to a buffer 
     * @throws IOException */
    private static void encodeTagChar(char c, Appendable buffer) throws IOException {
        String hex = Integer.toHexString(c);
        buffer.append('-');
        for (int len = hex.length(); len < 4; len++) {
            buffer.append('0');
        }
        buffer.append(hex);
    }

    /** Tells whether a char is in a range */
    public static boolean in(char c, char a, char b) {
        return (c >= a && c <= b);
    }
}

Related

  1. encodeUnknownString(String in, OutputStream os)
  2. encodeUTCTime(long time, OutputStream os)
  3. encodeUtf8AsBase64(String data)
  4. encodeVarUInt32(int unsignedValue, OutputStream outputStream)
  5. encodeVarUInt64(long unsignedValue, OutputStream outputStream)
  6. encodeXML(String input)
  7. encodeXMLString(String aString, PrintWriter aWriter)