com.sun.jsftemplating.component.factory.ri.LoadBundleFactory 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.
*/
package com.sun.jsftemplating.component.factory.ri;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import com.sun.jsftemplating.annotation.UIComponentFactory;
import com.sun.jsftemplating.component.EventComponent;
import com.sun.jsftemplating.component.factory.ComponentFactoryBase;
import com.sun.jsftemplating.layout.LayoutDefinitionManager;
import com.sun.jsftemplating.layout.descriptors.LayoutComponent;
import com.sun.jsftemplating.layout.descriptors.handler.Handler;
import com.sun.jsftemplating.layout.descriptors.handler.HandlerDefinition;
/**
* When using JSP as the view technology for JSF, you not only have
* components but also JSP tags that interact with JSF. In JSFTemplating
* the recommended approach to doing this is to use handlers. This
* offers a clean way to execute arbitrary Java code. While that is still
* the recommendation, this class is provided for added flexibility. The
* purpose of this class is to read a ResourceBundle and make it available
* to the page. The better approach would be to use the
* {@link com.sun.jsftemplating.handlers.ScopeHandlers#setResourceBundle}
* handler.
*
* The >f:loadBundle< tag does not represent a component, so this
* handler does not create a component, it returns the parent
* (which is passed in) after configuring the
* ResourceBundle
.
*
* The {@link com.sun.jsftemplating.layout.descriptors.ComponentType}
* id for this factory is: "f:loadBundle". It requires a
* basename
and a var
property to be passed
* in. Optionally the locale
can be passed in (this is not
* a feature of the JSF version, but an extra feature supported by the
* handler in JSFTemplating.
*
* @author Ken Paulsen ([email protected])
*/
@UIComponentFactory("f:loadBundle")
public class LoadBundleFactory extends ComponentFactoryBase {
/**
* This is the factory method loads a ResourceBundle
.
*
* @param context The FacesContext
* @param descriptor The {@link LayoutComponent} descriptor.
* @param parent The parent UIComponent
(not used)
*
* @return parent
.
*/
public UIComponent create(FacesContext context, LayoutComponent descriptor, UIComponent parent) {
// Create an Event component for this
EventComponent event = new EventComponent();
if (parent != null) {
addChild(context, descriptor, parent, event);
}
// Get the inputs
String baseName = (String) descriptor.getEvaluatedOption(context, "basename", parent);
String var = (String) descriptor.getEvaluatedOption(context, "var", parent);
Locale locale = (Locale) descriptor.getEvaluatedOption(context, "locale", parent);
// Create a handler (needed to execute code each time displayed)...
HandlerDefinition def = LayoutDefinitionManager.
getGlobalHandlerDefinition("setResourceBundle");
Handler handler = new Handler(def);
handler.setInputValue("bundle", baseName);
handler.setInputValue("key", var);
handler.setInputValue("locale", locale);
List handlers = new ArrayList();
handlers.add(handler);
event.getAttributes().put("beforeEncode", handlers);
// Return (parent)
return event;
}
}