All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.yestech.lib.util.EncoderUtil Maven / Gradle / Ivy

Go to download

A collection of classes that can be used across yestech artifacts/components, but must not be dependant on any yestech component. Most of the code is utility type code. When more than a few classes are found to be in a package or the package start to handle more that a few reposibilities then a new independant component is created and the existing code in yeslib is ported to the new component.

There is a newer version: 1.2.0
Show newest version
/*
 * Copyright LGPL3
 * YES Technology Association
 * http://yestech.org
 *
 * http://www.opensource.org/licenses/lgpl-3.0.html
 */

/*
 *
 * Author:  Artie Copeland
 * Last Modified Date: $DateTime: $
 */
package org.yestech.lib.util;

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

/**
 * A utility to encode to multiple formats.
 * 
 * @author Artie Copeland
 * @version $Revision: $
 */
final public class EncoderUtil {
    final private static Logger logger = LoggerFactory.getLogger(EncoderUtil.class);
    final private static String DEFAULT_STRING_ENCODING = "UTF-8";

    private EncoderUtil() {
    }

    /**
     * URL Encodes the string
     *
     * @param value
     * @return
     */
    public static String urlEncode(String value) {
        return urlEncode(value, DEFAULT_STRING_ENCODING);
    }

    /**
     * URL Encodes the string using a charset.
     *
     * @param value
     * @param encoding
     * @return
     */
    public static String urlEncode(String value, String encoding) {
        try {
            if (value == null) {
                return "";
            }
            if (StringUtils.isBlank(value)) {
                return value;
            }
            return URLEncoder.encode(value, encoding);
        }
        catch (UnsupportedEncodingException e) {
            logger.error(e.getMessage(), e);
            return null;
        }
    }

    /**
     * Encodes 's' by converting each character to its equivalent HTML entity,
     * where one exists.
     *
     * @param s the string to convert.
     * @return a string with entities encoded.
     */
    public static String htmlEncode(String s) {
        return StringEscapeUtils.escapeHtml(s);
    }

    /**
     * Escape HTTP URI fragment per RFC2396
     *
     * @param uri
     * @return
     */
    public static String uriEncode(String uri) {
        int length = uri.length();

        if (length == 0)
            return uri;

        StringBuffer results = new StringBuffer();

        for (int i = 0;
             i < length;
             i++) {
            char c = uri.charAt(i);
            switch (c) {
                case ';':
                case '/':
                case '?':
                case ':':
                case '@':
                case '&':
                case '=':
                case '+':
                case '$':
                case ',':
                case '%':
                    // Note:  Space and double quote are not specified by
                    //        RFC2396..., but we need 'em anyway...
                case ' ':
                case '"':
                    // TODO:  getBytes() should specify encoding???
                    try {
                        byte[] octets;
                        octets = uri.substring(i, i + 1).getBytes();
                        for (int j = 0;
                             j < octets.length;
                             j++) {
                            String hexVal = Integer.toHexString(octets[j]).toUpperCase();
                            if (hexVal.length() == 2) {
                                results.append("%").append(hexVal);
                            } else {
                                results.append("%0").append(hexVal);
                            }
                        }
                    } catch (Exception e) {
                    }

                    break;
                default:
                    results.append(c);
                    break;
            }
        }

        return results.toString();
    }

    /**
     * Encodes 's' by converting each character to its equivalent XML entity,
     * where one exists.
     *
     * @param s the string to convert.
     * @return a string with entities encoded.
     */
    public static String xmlEncode(String s) {
        return StringEscapeUtils.escapeXml(s);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy