FileDocCategorySizeDatePackage
JspHelper.javaAPI DocApache Tomcat 6.0.148341Fri Jul 20 04:20:34 BST 2007org.apache.catalina.manager

JspHelper

public class JspHelper extends Object
Helper JavaBean for JSPs, because JSTL 1.1/EL 2.0 is too dumb to to what I need (call methods with parameters), or I am too dumb to use it correctly. :)
author
Cédrik LIME

Fields Summary
private static final String
DATE_TIME_FORMAT
private static final String
DATE_FORMAT
private static final String
TIME_FORMAT
private static final int
HIGHEST_SPECIAL
private static char[]
specialCharactersRepresentation
Constructors Summary
private JspHelper()
Public constructor, so that this class can be considered a JavaBean


                    
      
        super();
    
Methods Summary
public static java.lang.StringescapeXml(java.lang.Object obj)

     
        specialCharactersRepresentation['&"] = "&".toCharArray();
        specialCharactersRepresentation['<"] = "<".toCharArray();
        specialCharactersRepresentation['>"] = ">".toCharArray();
        specialCharactersRepresentation['""] = """.toCharArray();
        specialCharactersRepresentation['\'"] = "'".toCharArray();
    
        String value = null;
        try {
            value = (obj == null) ? null : String.valueOf(obj);
        } catch (Exception e) {
            // Ignore
        }
        return escapeXml(value);
    
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().

    	if (buffer == null) {
			return "";
		}
        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.StringformatNumber(long number)

    	return NumberFormat.getNumberInstance().format(number);
    
public static java.lang.StringgetDisplayCreationTimeForSession(org.apache.catalina.Session in_session)

        try {
			DateFormat formatter = new SimpleDateFormat(DATE_TIME_FORMAT);
			return formatter.format(new Date(in_session.getCreationTime()));
        } catch (IllegalStateException ise) {
        	//ignore: invalidated session
        	return "";
		}
    
public static java.lang.StringgetDisplayInactiveTimeForSession(org.apache.catalina.Session in_session)

        return secondsToTimeString(SessionUtils.getInactiveTimeForSession(in_session)/1000);
    
public static java.lang.StringgetDisplayLastAccessedTimeForSession(org.apache.catalina.Session in_session)

        try {
			DateFormat formatter = new SimpleDateFormat(DATE_TIME_FORMAT);
			return formatter.format(new Date(in_session.getLastAccessedTime()));
        } catch (IllegalStateException ise) {
        	//ignore: invalidated session
        	return "";
		}
    
public static java.lang.StringgetDisplayTTLForSession(org.apache.catalina.Session in_session)

        return secondsToTimeString(SessionUtils.getTTLForSession(in_session)/1000);
    
public static java.lang.StringgetDisplayUsedTimeForSession(org.apache.catalina.Session in_session)

        return secondsToTimeString(SessionUtils.getUsedTimeForSession(in_session)/1000);
    
public static java.lang.StringguessDisplayLocaleFromSession(org.apache.catalina.Session in_session)
Try to get user locale from the session, if possible. IMPLEMENTATION NOTE: this method has explicit support for Tapestry 3 and Struts 1.x

param
in_session
return
String

        return localeToString(SessionUtils.guessLocaleFromSession(in_session));
    
public static java.lang.StringguessDisplayUserFromSession(org.apache.catalina.Session in_session)
Try to get user name from the session, if possible.

param
in_session
return
String

        Object user = SessionUtils.guessUserFromSession(in_session);
        return escapeXml(user);
    
private static java.lang.StringlocaleToString(java.util.Locale locale)

        if (locale != null) {
            return locale.toString();//locale.getDisplayName();
        } else {
            return "";
        }
    
public static java.lang.StringsecondsToTimeString(long in_seconds)

        StringBuffer buff = new StringBuffer(9);
        long rest = in_seconds;
        long hour = rest / 3600;
        rest = rest % 3600;
        long minute = rest / 60;
        rest = rest % 60;
        long second = rest;
        if (hour < 10) {
            buff.append('0");
        }
        buff.append(hour);
        buff.append(':");
        if (minute < 10) {
            buff.append('0");
        }
        buff.append(minute);
        buff.append(':");
        if (second < 10) {
            buff.append('0");
        }
        buff.append(second);
        return buff.toString();
    
private static voidwriteEscapedXml(char[] buffer, int length, java.io.Writer w)
Following copied from org.apache.taglibs.standard.tag.common.core.OutSupport v1.1.2 Optimized to create no extra objects and write directly to the JspWriter using blocks of escaped and unescaped characters

       int start = 0;

       for (int i = 0; i < length; i++) {
           char c = buffer[i];
           if (c <= HIGHEST_SPECIAL) {
               char[] escaped = specialCharactersRepresentation[c];
               if (escaped != null) {
                   // add unescaped portion
                   if (start < i) {
                       w.write(buffer,start,i-start);
                   }
                   // add escaped xml
                   w.write(escaped);
                   start = i + 1;
               }
           }
       }
       // add rest of unescaped portion
       if (start < length) {
           w.write(buffer,start,length-start);
       }