com.sun.faces.renderkit.html_basic.ScriptStyleBaseRenderer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jsf-impl Show documentation
Show all versions of jsf-impl Show documentation
This is the master POM file for Oracle's Implementation of the JSF 2.2 Specification.
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
* or packager/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at packager/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* Oracle designates this particular file as subject to the "Classpath"
* exception as provided by Oracle in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package com.sun.faces.renderkit.html_basic;
import com.sun.faces.util.FacesLogger;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.application.ProjectStage;
import javax.faces.component.UIComponent;
import javax.faces.component.UINamingContainer;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.faces.render.Renderer;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.PostAddToViewEvent;
import javax.faces.event.ComponentSystemEvent;
import javax.faces.event.ComponentSystemEventListener;
import javax.faces.event.ListenerFor;
/**
* Base class for shared behavior between Script and Stylesheet renderers.
* Maybe composition would be better, but inheritance is easier
*/
@ListenerFor(systemEventClass=PostAddToViewEvent.class)
public abstract class ScriptStyleBaseRenderer extends Renderer implements ComponentSystemEventListener {
private static final String COMP_KEY =
ScriptStyleBaseRenderer.class.getName() + "_COMPOSITE_COMPONENT";
// Log instance for this class
protected static final Logger logger = FacesLogger.RENDERKIT.getLogger();
/*
* Indicates that the component associated with this Renderer has already
* been added to the facet in the view.
*/
/* When this method is called, we know that there is a component
* with a script renderer somewhere in the view. We need to make it
* so that when an element with a name given by the value of the optional
* "target" component attribute is encountered, this component
* can be called upon to render itself.
* This method will add the component (associated with this Renderer)
* to a facet in the view only if a "target" component attribute is set.
*
*/
public void processEvent(ComponentSystemEvent event) throws AbortProcessingException {
UIComponent component = event.getComponent();
FacesContext context = FacesContext.getCurrentInstance();
String target = verifyTarget((String) component.getAttributes().get("target"));
if (target != null) {
// We're checking for a composite component here as if the resource
// is relocated, it may still require it's composite component context
// in order to properly render. Store it for later use by
// encodeBegin() and encodeEnd().
UIComponent cc = UIComponent.getCurrentCompositeComponent(context);
if (cc != null) {
component.getAttributes().put(COMP_KEY, cc.getClientId(context));
}
context.getViewRoot().addComponentResource(context, component, target);
}
}
@Override
public final void decode(FacesContext context, UIComponent component) {
// no-op
}
@Override
public final boolean getRendersChildren() {
return true;
}
/**
* If overridden, this method (i.e. super.encodeEnd) should be called
* last within the overridding implementation.
*/
@Override
public void encodeEnd(FacesContext context, UIComponent component)
throws IOException {
// Remove the key to prevent issues with state saving...
String ccID = (String) component.getAttributes().get(COMP_KEY);
if (ccID != null) {
// the first pop maps to the component we're rendering.
// the second pop maps to the composite component that was pushed
// in this renderer's encodeBegin implementation.
// re-push the current component to reset the original context
component.popComponentFromEL(context);
component.popComponentFromEL(context);
component.pushComponentToEL(context, component);
}
}
/**
* If overridden, this method (i.e. super.encodeBegin) should be called
* first within the overridding implementation.
*/
@Override
public void encodeBegin(FacesContext context, UIComponent component)
throws IOException {
String ccID = (String) component.getAttributes().get(COMP_KEY);
if (null != ccID) {
char sep = UINamingContainer.getSeparatorChar(context);
UIComponent cc;
// If the composite component id includes the separator char...
if (-1 != ccID.indexOf(sep)) {
// use the UIViewRoot's findComponent.
cc = context.getViewRoot().findComponent(':' + ccID);
} else {
// ... otherwise use our special findComponent.
cc = findComponentIgnoringNamingContainers(context.getViewRoot(), ccID, true);
}
UIComponent curCC = UIComponent.getCurrentCompositeComponent(context);
if (cc != curCC) {
// the first pop maps to the component we're rendering.
// push the composite component to the 'stack' and then re-push
// the component we're rendering so the current component is
// correct.
component.popComponentFromEL(context);
component.pushComponentToEL(context, cc);
component.pushComponentToEL(context, component);
}
}
}
private static UIComponent findComponentIgnoringNamingContainers(UIComponent base,
String id,
boolean checkId) {
if (checkId && id.equals(base.getId())) {
return base;
}
// Search through our facets and children
UIComponent result = null;
for (Iterator i = base.getFacetsAndChildren(); i.hasNext();) {
UIComponent kid = (UIComponent) i.next();
if (checkId && id.equals(kid.getId())) {
result = kid;
break;
}
result = findComponentIgnoringNamingContainers(kid, id, true);
if (result != null) {
break;
} else if (id.equals(kid.getId())) {
result = kid;
break;
}
}
return (result);
}
@Override
public final void encodeChildren(FacesContext context, UIComponent component)
throws IOException {
Map attributes = component.getAttributes();
String name = (String) attributes.get("name");
int childCount = component.getChildCount();
boolean renderChildren = (0 < childCount);
// If we have no "name" attribute...
if (null == name) {
// and no child content...
if (0 == childCount) {
// this is user error, so put up a message if desired
if (context.isProjectStage(ProjectStage.Development)) {
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_WARN,
"outputScript with no library, no name, and no body content",
"Is body content intended?");
context.addMessage(component.getClientId(context), message);
}
// We have no children, but don't bother with the method
// invocation anyway.
renderChildren = false;
}
} else if (0 < childCount) {
// If we have a "name" and also have child content, ignore
// the child content and log a message.
if (logger.isLoggable(Level.INFO)) {
logger.info("outputScript with \"name\" attribute and nested content. Ignoring nested content.");
}
renderChildren = false;
}
if (renderChildren) {
ResponseWriter writer = context.getResponseWriter();
startElement(writer, component);
super.encodeChildren(context, component);
endElement(writer);
}
}
// ------------------------------------------------------- Protected Methods
/**
* Allow the subclass to customize the start element content
*/
protected abstract void startElement(ResponseWriter writer,
UIComponent component) throws IOException;
/**
* Allow the subclass to customize the start element content
*/
protected abstract void endElement(ResponseWriter writer) throws IOException;
/**
* Allow a subclass to control what's a valid value for "target".
*/
protected String verifyTarget(String toVerify) {
return toVerify;
}
}