Created
October 16, 2012 14:04
-
-
Save anonymous/3899465 to your computer and use it in GitHub Desktop.
Custom h:head renderer to enable facets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import java.util.ArrayList; | |
import java.util.List; | |
import javax.faces.component.UIComponent; | |
import javax.faces.context.FacesContext; | |
import javax.faces.context.ResponseWriter; | |
import javax.faces.render.Renderer; | |
/** | |
* Renders head content based on the following order: <br/> | |
* Begin facet<br/> | |
* Unknown resources (like richfaces specific .ecss, .rflib)<br/> | |
* CSS resources (resources that end with .css)<br/> | |
* Middle facet<br/> | |
* JavaScript resources (resources that end with .js)<br/> | |
* h:head content encoded by | |
* {@link Renderer#encodeChildren(FacesContext, UIComponent)}<br/> | |
* End facet<br/> | |
* | |
* Usage - add the following in faces-config.xml: | |
* | |
* <renderer> | |
* <component-family>javax.faces.Output</component-family> | |
* <renderer-type>javax.faces.Head</renderer-type> | |
* <renderer-class>test.HeadRenderer</renderer-class> | |
* </renderer> | |
*/ | |
public class HeadRenderer extends Renderer { | |
@Override | |
public void encodeBegin(FacesContext context, UIComponent component) | |
throws IOException { | |
ResponseWriter writer = context.getResponseWriter(); | |
writer.startElement("head", component); | |
// "begin" facet | |
UIComponent beginFacet = component.getFacet("begin"); | |
if (beginFacet != null) { | |
beginFacet.encodeAll(context); | |
} | |
// Split resources managed by JSF and encode them | |
List<UIComponent> styles = new ArrayList<UIComponent>(); | |
List<UIComponent> scripts = new ArrayList<UIComponent>(); | |
List<UIComponent> others = new ArrayList<UIComponent>(); | |
for (UIComponent current : context.getViewRoot().getComponentResources( | |
context, "head")) { | |
String name = (String) current.getAttributes().get("name"); | |
if (name != null) { | |
if (name.endsWith(".css")) { | |
styles.add(current); | |
} else if (name.endsWith(".js")) { | |
scripts.add(current); | |
} else { | |
others.add(current); | |
} | |
} | |
} | |
// Non JavaScript or css sources | |
for (UIComponent other : others) { | |
other.encodeAll(context); | |
} | |
// Styles | |
for (UIComponent style : styles) { | |
style.encodeAll(context); | |
} | |
// "middle" facet | |
UIComponent middle = component.getFacet("middle"); | |
if (middle != null) { | |
middle.encodeAll(context); | |
} | |
// Scripts | |
for (UIComponent script : scripts) { | |
script.encodeAll(context); | |
} | |
} | |
@Override | |
public void encodeEnd(FacesContext context, UIComponent component) | |
throws IOException { | |
ResponseWriter writer = context.getResponseWriter(); | |
// "end" facet | |
UIComponent endFacet = component.getFacet("end"); | |
if (endFacet != null) { | |
endFacet.encodeAll(context); | |
} | |
writer.endElement("head"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment