FileDocCategorySizeDatePackage
Util.javaAPI DocGlassfish v2 API10993Sat May 05 19:17:52 BST 2007org.apache.taglibs.standard.tag.common.core

Util

public class Util extends Object

Utilities in support of tag-handler classes.

author
Jan Luehe

Fields Summary
private static final String
REQUEST
private static final String
SESSION
private static final String
APPLICATION
private static final String
DEFAULT
private static final String
SHORT
private static final String
MEDIUM
private static final String
LONG
private static final String
FULL
public static final int
HIGHEST_SPECIAL
public static char[]
specialCharactersRepresentation
Constructors Summary
Methods Summary
public static java.lang.StringURLEncode(java.lang.String s, java.lang.String enc)
URL encodes a string, based on the supplied character encoding. This performs the same function as java.next.URLEncode.encode in J2SDK1.4, and should be removed if the only platform supported is 1.4 or higher.

param
s The String to be URL encoded.
param
enc The character encoding
return
The URL encoded String [taken from jakarta-tomcat-jasper/jasper2 org.apache.jasper.runtime.JspRuntimeLibrary.java]


	if (s == null) {
	    return "null";
	}

	if (enc == null) {
	    enc = "UTF-8";	// Is this right?
	}

	StringBuffer out = new StringBuffer(s.length());
	ByteArrayOutputStream buf = new ByteArrayOutputStream();
	OutputStreamWriter writer = null;
	try {
	    writer = new OutputStreamWriter(buf, enc);
	} catch (java.io.UnsupportedEncodingException ex) {
	    // Use the default encoding?
	    writer = new OutputStreamWriter(buf);
	}
	
	for (int i = 0; i < s.length(); i++) {
	    int c = s.charAt(i);
	    if (c == ' ") {
		out.append('+");
	    } else if (isSafeChar(c)) {
		out.append((char)c);
	    } else {
		// convert to external encoding before hex conversion
		try {
		    writer.write(c);
		    writer.flush();
		} catch(IOException e) {
		    buf.reset();
		    continue;
		}
		byte[] ba = buf.toByteArray();
		for (int j = 0; j < ba.length; j++) {
		    out.append('%");
		    // Converting each byte in the buffer
		    out.append(Character.forDigit((ba[j]>>4) & 0xf, 16));
		    out.append(Character.forDigit(ba[j] & 0xf, 16));
		}
		buf.reset();
	    }
	}
	return out.toString();
    
public static java.lang.StringescapeXml(java.lang.String buffer)
Performs the following substring replacements (to facilitate output to XML/HTML pages): & -> & < -> < > -> > " -> " ' -> ' See also OutSupport.writeEscapedXml().

        int start = 0;
        int length = buffer.length();
        char[] arrayBuffer = buffer.toCharArray();
        StringBuffer escapedBuffer = null;

        for (int i = 0; i < length; i++) {
            char c = arrayBuffer[i];
            if (c <= HIGHEST_SPECIAL) {
                char[] escaped = specialCharactersRepresentation[c];
                if (escaped != null) {
                    // create StringBuffer to hold escaped xml string
                    if (start == 0) {
                        escapedBuffer = new StringBuffer(length + 5);
                    }
                    // add unescaped portion
                    if (start < i) {
                        escapedBuffer.append(arrayBuffer,start,i-start);
                    }
                    start = i + 1;
                    // add escaped xml
                    escapedBuffer.append(escaped);
                }
            }
        }
        // no xml escaping was necessary
        if (start == 0) {
            return buffer;
        }
        // add rest of unescaped portion
        if (start < length) {
            escapedBuffer.append(arrayBuffer,start,length-start);
        }
        return escapedBuffer.toString();
    
public static java.lang.StringgetContentTypeAttribute(java.lang.String input, java.lang.String name)
Get the value associated with a content-type attribute. Syntax defined in RFC 2045, section 5.1.

	int begin;
	int end;
        int index = input.toUpperCase().indexOf(name.toUpperCase());
        if (index == -1) return null;
        index = index + name.length(); // positioned after the attribute name
        index = input.indexOf('=", index); // positioned at the '='
        if (index == -1) return null;
        index += 1; // positioned after the '='
        input = input.substring(index).trim();
        
        if (input.charAt(0) == '"") {
            // attribute value is a quoted string
            begin = 1;
            end = input.indexOf('"", begin);
            if (end == -1) return null;
        } else {
            begin = 0;
            end = input.indexOf(';");
            if (end == -1) end = input.indexOf(' ");
            if (end == -1) end = input.length();
        }
        return input.substring(begin, end).trim();
    
public static java.util.EnumerationgetRequestLocales(javax.servlet.http.HttpServletRequest request)
HttpServletRequest.getLocales() returns the server's default locale if the request did not specify a preferred language. We do not want this behavior, because it prevents us from using the fallback locale. We therefore need to return an empty Enumeration if no preferred locale has been specified. This way, the logic for the fallback locale will be able to kick in.

        
        Enumeration values = request.getHeaders("accept-language");
        if (values.hasMoreElements()) {
            // At least one "accept-language". Simply return
            // the enumeration returned by request.getLocales().
            // System.out.println("At least one accept-language");
            return request.getLocales();
        } else {
            // No header for "accept-language". Simply return
            // the empty enumeration.
            // System.out.println("No accept-language");
            return values;
        }
    
public static intgetScope(java.lang.String scope)

     
        specialCharactersRepresentation['&"] = "&".toCharArray();
        specialCharactersRepresentation['<"] = "<".toCharArray();
        specialCharactersRepresentation['>"] = ">".toCharArray();
        specialCharactersRepresentation['""] = """.toCharArray();
        specialCharactersRepresentation['\'"] = "'".toCharArray();
    
	int ret = PageContext.PAGE_SCOPE; // default

	if (REQUEST.equalsIgnoreCase(scope))
	    ret = PageContext.REQUEST_SCOPE;
	else if (SESSION.equalsIgnoreCase(scope))
	    ret = PageContext.SESSION_SCOPE;
	else if (APPLICATION.equalsIgnoreCase(scope))
	    ret = PageContext.APPLICATION_SCOPE;

	return ret;
    
public static intgetStyle(java.lang.String style, java.lang.String errCode)

	int ret = DateFormat.DEFAULT;

	if (style != null) {
	    if (DEFAULT.equalsIgnoreCase(style)) {
		ret = DateFormat.DEFAULT;
	    } else if (SHORT.equalsIgnoreCase(style)) {
		ret = DateFormat.SHORT;
	    } else if (MEDIUM.equalsIgnoreCase(style)) {
		ret = DateFormat.MEDIUM;
	    } else if (LONG.equalsIgnoreCase(style)) {
		ret = DateFormat.LONG;
	    } else if (FULL.equalsIgnoreCase(style)) {
		ret = DateFormat.FULL;
	    } else {
		throw new JspException(Resources.getMessage(errCode, style));
	    }
	}

	return ret;
    
private static booleanisSafeChar(int c)

	if (c >= 'a" && c <= 'z") {
	    return true;
	}
	if (c >= 'A" && c <= 'Z") {
	    return true;
	}
	if (c >= '0" && c <= '9") {
	    return true;
	}
	if (c == '-" || c == '_" || c == '." || c == '!" ||
	    c == '~" || c == '*" || c == '\'" || c == '(" || c == ')") {
	    return true;
	}
	return false;