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

com.sun.jsftemplating.handlers.UtilHandlers Maven / Gradle / Ivy

/*
 * The contents of this file are subject to the terms 
 * of the Common Development and Distribution License 
 * (the License).  You may not use this file except in
 * compliance with the License.
 * 
 * You can obtain a copy of the license at 
 * https://jsftemplating.dev.java.net/cddl1.html or
 * jsftemplating/cddl1.txt.
 * See the License for the specific language governing 
 * permissions and limitations under the License.
 * 
 * When distributing Covered Code, include this CDDL 
 * Header Notice in each file and include the License file 
 * at jsftemplating/cddl1.txt.  
 * If applicable, add the following below the CDDL Header, 
 * with the fields enclosed by brackets [] replaced by
 * you own identifying information: 
 * "Portions Copyrighted [year] [name of copyright owner]"
 * 
 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
 */
/*
 *  UtilHandlers.java
 *
 *  Created on December 2, 2004, 3:06 AM
 */
package com.sun.jsftemplating.handlers;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.Serializable;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.faces.component.UIComponent;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;

import com.sun.jsftemplating.annotation.Handler;
import com.sun.jsftemplating.annotation.HandlerInput;
import com.sun.jsftemplating.annotation.HandlerOutput;
import com.sun.jsftemplating.el.PageSessionResolver;
import com.sun.jsftemplating.layout.LayoutViewHandler;
import com.sun.jsftemplating.layout.descriptors.LayoutElement;
import com.sun.jsftemplating.layout.descriptors.handler.HandlerContext;
import com.sun.jsftemplating.util.Util;


/**
 *  

This class contains * {@link com.sun.jsftemplating.layout.descriptors.handler.Handler} * methods that perform common utility-type functions.

* * @author Ken Paulsen ([email protected]) */ public class UtilHandlers { /** *

Default Constructor.

*/ public UtilHandlers() { } /** *

This handler uses the special "condition" attribute to determine if * it should execute (and therefor any of its child handlers. So the * implementation itself does nothing.

*/ @Handler(id="if", input={@HandlerInput(name="condition", type=String.class)}) public static void ifHandler(HandlerContext context) { // Do nothing, the purpose of this handler is to provide condition // support which is handled by the parser / runtime. } /** *

A utility handler that resembles the for() method in Java. Handlers * inside the for loop will be executed in a loop. The starting index * is specified by start. The index will increase * sequentially untill it is equal to end. * var will be a request attribute that is set to the * current index value as the loop iterates.

*

For example:

* * forLoop(start="1" end="3" var="foo") {...} * *

The handlers inside the {...} will be executed 2 times * (with foo=1 and foo=2).

* *
  • start -- type: Integer Starting * index, defaults to zero if not specified.
  • *
  • end -- type: Integer; Ending index. * Required.
  • *
  • var -- type: String; Request * attribute to be set in the for loop to the value of the * index.
* * @param handlerCtx The {@link HandlerContext}. */ @Handler(id="for", input={ @HandlerInput(name="start", type=Integer.class), @HandlerInput(name="end", type=Integer.class, required=true), @HandlerInput(name="var", type=String.class, required=true)}) public static boolean forLoop(HandlerContext handlerCtx) { Integer startInt = (Integer) handlerCtx.getInputValue("start"); int start = (startInt == null) ? 0 : startInt; int end = (Integer) handlerCtx.getInputValue("end"); String var = (String) handlerCtx.getInputValue("var"); List handlers = handlerCtx.getHandler().getChildHandlers(); if (handlers.size() > 0) { // We have child handlers in the loop... execute while we iterate LayoutElement elt = handlerCtx.getLayoutElement(); Map requestMap = handlerCtx.getFacesContext(). getExternalContext().getRequestMap(); for (int idx=start; idx < end; idx++) { requestMap.put(var, idx); // Ignore whats returned by the handler... we need to return // false anyway to prevent children from being executed again elt.dispatchHandlers(handlerCtx, handlers); } } // This will prevent the child handlers from executing again return false; } /** *

This handler writes using System.out.println. It * requires that value be supplied as a String input * parameter.

* * @param context The HandlerContext. */ @Handler(id="println", input={@HandlerInput(name="value", type=String.class, required=true)}) public static void println(HandlerContext context) { String value = (String) context.getInputValue("value"); System.out.println(value); } /** *

This handler writes using * FacesContext.getResponseWriter().

* * @param context The HandlerContext. */ @Handler(id="write", input={@HandlerInput(name="value", type=String.class, required=true)}) public static void write(HandlerContext context) { String text = (String) context.getInputValue("value"); if (text == null) { // Even though this is required, an expression can evaluate to null text = ""; } try { context.getFacesContext().getResponseWriter().write(text); } catch (IOException ex) { throw new RuntimeException(ex); } } /** *

This handler decrements a number by 1. This handler requires * "number" to be supplied as an Integer input value. It sets an * output value "value" to number-1.

* * @param context The HandlerContext. */ @Handler(id="dec", input={ @HandlerInput(name="number", type=Integer.class, required=true)}, output={ @HandlerOutput(name="value", type=Integer.class)}) public static void dec(HandlerContext context) { Integer value = (Integer) context.getInputValue("number"); context.setOutputValue("value", new Integer(value.intValue() - 1)); } /** *

This handler increments a number by 1. This handler requires * "number" to be supplied as an Integer input value. It sets an * output value "value" to number+1.

* * @param context The HandlerContext. */ @Handler(id="inc", input={ @HandlerInput(name="number", type=Integer.class, required=true)}, output={ @HandlerOutput(name="value", type=Integer.class)}) public static void inc(HandlerContext context) { Integer value = (Integer) context.getInputValue("number"); context.setOutputValue("value", new Integer(value.intValue() + 1)); } /** *

This method returns an Iterator for the given * List. The List input value key is: * "list". The output value key for the Iterator is: * "iterator".

* * @param context The HandlerContext. */ @Handler(id="getIterator", input={ @HandlerInput(name="list", type=List.class, required=true)}, output={ @HandlerOutput(name="iterator", type=Iterator.class)}) public static void getIterator(HandlerContext context) { List list = (List) context.getInputValue("list"); context.setOutputValue("iterator", list.iterator()); } /** *

This method returns a Boolean value representing * whether another value exists for the given Iterator. * The Iterator input value key is: "iterator". The * output value key is "hasNext".

* * @param context The HandlerContext. */ @Handler(id="iteratorHasNext", input={ @HandlerInput(name="iterator", type=Iterator.class, required=true)}, output={ @HandlerOutput(name="hasNext", type=Boolean.class)}) public static void iteratorHasNext(HandlerContext context) { Iterator it = (Iterator) context.getInputValue("iterator"); context.setOutputValue("hasNext", Boolean.valueOf(it.hasNext())); } /** *

This method returns the next object in the List that * the given Iterator is iterating over. The * Iterator input value key is: "iterator". The * output value key is "next".

* * @param context The HandlerContext. */ @Handler(id="iteratorNext", input={ @HandlerInput(name="iterator", type=Iterator.class, required=true)}, output={ @HandlerOutput(name="next")}) public static void iteratorNext(HandlerContext context) { Iterator it = (Iterator) context.getInputValue("iterator"); context.setOutputValue("next", it.next()); } /** *

This method creates a List. Optionally you may supply "size" to * create a List of blank "" values of the specified size. The * output value from this handler is "result".

* * @param context The HandlerContext */ @Handler(id="createList", input={ @HandlerInput(name="size", type=Integer.class, required=true)}, output={ @HandlerOutput(name="result", type=List.class)}) public static void createList(HandlerContext context) { int size = ((Integer) context.getInputValue("size")).intValue(); List list = new ArrayList(size); for (int count = 0; count < size; count++) { list.add(""); } context.setOutputValue("result", list); } /** *

This method creates a Map (HashMap). * The output value from this handler is "result".

* * @param context The HandlerContext */ @Handler(id="createMap", output={ @HandlerOutput(name="result", type=Map.class)}) public static void createMap(HandlerContext context) { Map map = new HashMap(); context.setOutputValue("result", map); } /** *

This method adds a value to a Map. You must supply * map to use as well as the key and * value to add.

* * @param context The HandlerContext */ @Handler(id="mapPut", input={ @HandlerInput(name="map", type=Map.class, required=true), @HandlerInput(name="key", type=Object.class, required=true), @HandlerInput(name="value", type=Object.class, required=true)} ) public static void mapPut(HandlerContext context) { Map map = (Map) context.getInputValue("map"); Object key = context.getInputValue("key"); Object value = context.getInputValue("value"); map.put(key, value); } /** *

This method returns true. It does not take any input or provide * any output values.

* * @param context The {@link HandlerContext} */ @Handler(id="returnTrue") public static boolean returnTrue(HandlerContext context) { return true; } /** *

This method returns false. It does not take any input or provide * any output values.

* * @param context The {@link HandlerContext} */ @Handler(id="returnFalse") public static boolean returnFalse(HandlerContext context) { return false; } /** *

This method enables you to retrieve the clientId for the given * UIComponent.

* * @param context The {@link HandlerContext} */ @Handler(id="getClientId", input={ @HandlerInput(name="component", type=UIComponent.class, required=true)}, output={ @HandlerOutput(name="clientId", type=String.class)}) public static void getClientId(HandlerContext context) { UIComponent comp = (UIComponent) context.getInputValue("component"); context.setOutputValue("clientId", comp.getClientId(context.getFacesContext())); } /** *

This method enables you to retrieve the id or clientId for the given * Object which is expected to be a * UIComponent or a String that already * represents the clientId.

* * @param context The {@link HandlerContext} */ @Handler(id="getId", input={ @HandlerInput(name="object", required=true)}, output={ @HandlerOutput(name="id", type=String.class), @HandlerOutput(name="clientId", type=String.class)}) public static void getId(HandlerContext context) { Object obj = context.getInputValue("object"); if (obj == null) { return; } String clientId = null; String id = null; if (obj instanceof UIComponent) { clientId = ((UIComponent) obj).getClientId(context.getFacesContext()); id = ((UIComponent) obj).getId(); } else { clientId = obj.toString(); id = clientId.substring(clientId.lastIndexOf(':') + 1); } context.setOutputValue("id", id); context.setOutputValue("clientId", clientId); } /** *

This handler provides a way to see the call stack by printing a * stack trace. The output will go to stderr and will also be * returned in the output value "stackTrace". An optional message * may be provided to be included in the trace.

* * @param context The HandlerContext. */ @Handler(id="printStackTrace", input={ @HandlerInput(name="msg", type=String.class)}, output={ @HandlerOutput(name="stackTrace", type=String.class)}) public static void printStackTrace(HandlerContext context) { // See if we have a message to print w/ it String msg = (String) context.getInputValue("msg"); if (msg == null) { msg = ""; } // Get the StackTrace StringWriter strWriter = new StringWriter(); new RuntimeException(msg).printStackTrace(new PrintWriter(strWriter)); String trace = strWriter.toString(); // Print it to stderr and return it System.err.println(trace); context.setOutputValue("stackTrace", trace); } /** *

This handler prints out the contents of the given UIComponent's * attribute map.

* * @param context The HandlerContext. */ @Handler(id="dumpAttributeMap", input={ @HandlerInput(name="component", type=UIComponent.class)}) public static void dumpAttributeMap(HandlerContext context) { UIComponent comp = (UIComponent) context.getInputValue("component"); if (comp != null) { Map map = comp.getAttributes(); for (Iterator iter = map.entrySet().iterator(); iter.hasNext();) { Map.Entry me = (Map.Entry)iter.next(); System.out.println("key="+ me.getKey()+"'"+"value="+ me.getValue()); } } else { System.out.println("UIComponent is null"); } } /** *

This handler sets the encoding type of the given UIViewRoot's * attribute map.

* * @param context The HandlerContext. */ @Handler(id="setEncoding", input={ @HandlerInput(name="value", type=String.class) }) public static void setEncoding(HandlerContext context) { String value = (String) context.getInputValue("value"); FacesContext fctxt = context.getFacesContext(); if(fctxt != null) { UIViewRoot root = fctxt.getViewRoot(); // Get the Page Session Map Map map = PageSessionResolver.getPageSession(fctxt, root); if (map == null) { map = PageSessionResolver.createPageSession(fctxt, root); } // Set the page session value map.put(LayoutViewHandler.ENCODING_TYPE , value); } } /** *

This handler url encodes the given String. It will return null if * null is given and it will use a default encoding of "UTF-8" if no * encoding is specified.

* * @param context The HandlerContext. */ @Handler(id="urlencode", input={ @HandlerInput(name="value", type=String.class, required=true), @HandlerInput(name="encoding", type=String.class) }, output={ @HandlerOutput(name="result", type=String.class)}) public static void urlencode(HandlerContext context) { String value = (String) context.getInputValue("value"); String encoding = (String) context.getInputValue("encoding"); if (encoding == null) { encoding = "UTF-8"; } // The value could be null if an EL expression maps to null if (value != null) { try { value = URLEncoder.encode(value, encoding); } catch (UnsupportedEncodingException ex) { throw new IllegalArgumentException(ex); } } context.setOutputValue("result", value); } /** *

This handler marks the response complete. This means that no * additional response will be sent. This is useful if you've * provided a response already and you don't want JSF to do it again * (it may cause problems to do it 2x).

* * @param context The HandlerContext. */ @Handler(id="responseComplete") public static void responseComplete(HandlerContext context) { context.getFacesContext().responseComplete(); } /** *

This handler indicates to JSF that the request should proceed * immediately to the render response phase. It will be ignored if * rendering has already begun. This is useful if you want to stop * processing and jump to the response. This is often the case when * an error ocurrs or validation fails. Typically the page the user * is on will be reshown (although if navigation has already * occurred, the new page will be shown.

* * @param context The HandlerContext. */ @Handler(id="renderResponse") public static void renderResponse(HandlerContext context) { context.getFacesContext().renderResponse(); } /** *

This handler gets the current system time in milliseconds. It may * be used to time things.

*/ @Handler(id="getDate", output={ @HandlerOutput(name="time", type=Long.class) }) public static void getDate(HandlerContext context) { context.setOutputValue("time", new java.util.Date().getTime()); } /** *

This method converts '<' and '>' characters into "&lt;" * and "&gt;" in an effort to avoid HTML from being processed. * This can be used to avoid <script> tags, or to show code * examples which might include HTML characters. '&' characters * will also be converted to "&amp;".

*/ @Handler(id="htmlEscape", input={ @HandlerInput(name="value", type=String.class, required=true) }, output={ @HandlerOutput(name="result", type=String.class)}) public static void htmlEscape(HandlerContext context) { String value = (String) context.getInputValue("value"); value = Util.htmlEscape(value); context.setOutputValue("result", value); } }