org.apache.myfaces.renderkit.html.util
Class DefaultAddResource

java.lang.Object
  extended by org.apache.myfaces.renderkit.html.util.NonBufferingAddResource
      extended by org.apache.myfaces.renderkit.html.util.DefaultAddResource
All Implemented Interfaces:
AddResource

public class DefaultAddResource
extends NonBufferingAddResource

This is a utility class to render link to resources used by custom components. Mostly used to avoid having to include [script src="..."][/script] in the head of the pages before using a component.

When used together with the ExtensionsFilter, this class can allow components in the body of a page to emit script and stylesheet references into the page head section. The relevant methods on this object simply queue the changes, and when the page is complete the ExtensionsFilter calls back into this class to allow it to insert the commands into the buffered response.

This class also works with the ExtensionsFilter to allow components to emit references to javascript/css/etc which are bundled in the component's jar file. Special URLs are generated which the ExtensionsFilter will later handle by retrieving the specified resource from the classpath.

The special URL format is:

 {contextPath}/faces/myFacesExtensionResource/
    {resourceLoaderName}/{cacheKey}/{resourceURI}
 
Where:

Version:
$Revision: 371739 373827 $ $Date: 2006-01-31 14:50:35 +0000 (Tue, 31 Jan 2006) $
Author:
Sylvain Vieujot (latest modification by $Author: mmarinschek $)

Nested Class Summary
protected static class DefaultAddResource.AttributeInfo
           
protected static class DefaultAddResource.ParseCallbackListener
           
protected static interface DefaultAddResource.PositionedInfo
           
protected static interface DefaultAddResource.WritablePositionedInfo
           
 
Field Summary
protected  int afterBodyContentInsertPosition
           
protected  int beforeBodyEndPosition
           
protected  int beforeBodyPosition
           
protected  int bodyInsertPosition
           
protected  int headerInsertPosition
           
protected  org.apache.commons.logging.Log log
           
protected  StringBuffer originalResponse
           
protected  boolean parserCalled
           
 
Fields inherited from class org.apache.myfaces.renderkit.html.util.NonBufferingAddResource
_contextPath, PATH_SEPARATOR
 
Fields inherited from interface org.apache.myfaces.renderkit.html.util.AddResource
BODY_END, BODY_ONLOAD, HEADER_BEGIN
 
Constructor Summary
protected DefaultAddResource()
           
 
Method Summary
 void addInlineScriptAtPosition(javax.faces.context.FacesContext context, ResourcePosition position, String inlineScript)
          Adds the given Inline Script at the specified document position.
 void addInlineStyleAtPosition(javax.faces.context.FacesContext context, ResourcePosition position, String inlineStyle)
          Adds the given Inline Style at the specified document position.
 void addJavaScriptAtPosition(javax.faces.context.FacesContext context, ResourcePosition position, Class myfacesCustomComponent, String resourceName)
          Insert a [script src="url"] entry into the document header at the specified document position.
 void addJavaScriptAtPosition(javax.faces.context.FacesContext context, ResourcePosition position, Class myfacesCustomComponent, String resourceName, boolean defer)
          Insert a [script src="url"] entry into the document header at the specified document position.
 void addJavaScriptAtPosition(javax.faces.context.FacesContext context, ResourcePosition position, ResourceHandler resourceHandler)
          Adds the given Javascript resource to the document header at the specified document positioy by supplying a resourcehandler instance.
 void addJavaScriptAtPosition(javax.faces.context.FacesContext context, ResourcePosition position, ResourceHandler resourceHandler, boolean defer)
          Adds the given Javascript resource at the specified document position.
 void addJavaScriptAtPosition(javax.faces.context.FacesContext context, ResourcePosition position, String uri)
          Insert a [script src="url"] entry into the document header at the specified document position.
 void addJavaScriptAtPosition(javax.faces.context.FacesContext context, ResourcePosition position, String uri, boolean defer)
          Adds the given Javascript resource at the specified document position.
 void addJavaScriptAtPositionPlain(javax.faces.context.FacesContext context, ResourcePosition position, Class myfacesCustomComponent, String resourceName)
          Adds the given Javascript resource at the specified document position.
 void addJavaScriptToBodyTag(javax.faces.context.FacesContext context, String javascriptEventName, String addedJavaScript)
           
 void addStyleSheet(javax.faces.context.FacesContext context, ResourcePosition position, Class myfacesCustomComponent, String resourceName)
          Adds the given Style Sheet at the specified document position.
 void addStyleSheet(javax.faces.context.FacesContext context, ResourcePosition position, ResourceHandler resourceHandler)
          Adds the given Style Sheet at the specified document position.
 void addStyleSheet(javax.faces.context.FacesContext context, ResourcePosition position, String uri)
          Adds the given Style Sheet at the specified document position.
protected  Set getBodyEndInfos()
           
protected  Set getBodyOnloadInfos()
           
protected  Set getHeaderBeginInfos()
           
protected  DefaultAddResource.PositionedInfo getScriptInstance(javax.faces.context.FacesContext context, String uri, boolean defer)
           
 boolean hasHeaderBeginInfos()
          check there is something to write to the header
 void parseResponse(javax.servlet.http.HttpServletRequest request, String bufferedResponse, javax.servlet.http.HttpServletResponse response)
          Parses the response to mark the positions where code will be inserted
 boolean requiresBuffer()
          return true if you require the complete response buffered
 void responseFinished()
          called when the response has finished
 void responseStarted()
          called when the response start
 void writeMyFacesJavascriptBeforeBodyEnd(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
          Writes the javascript code necessary for myfaces in every page, just befode the closing </body> tag
 void writeResponse(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
          Writes the response
 void writeWithFullHeader(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
          Add the resources to the <head> of the page.
 
Methods inherited from class org.apache.myfaces.renderkit.html.util.NonBufferingAddResource
addJavaScriptHere, addJavaScriptHere, addJavaScriptHere, addJavaScriptHerePlain, addResourceHere, getCacheKey, getResourceUri, getResourceUri, getResourceUri, getResourceUri, getResourceUri, getResourceUri, getResourceUri, isResourceUri, serveResource, setContextPath, validateResourceHandler, validateResourceLoader, writeInlineScript, writeInlineStylesheet, writeJavaScriptReference, writeJavaScriptReference, writeStyleReference, writeStyleReference
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

protected org.apache.commons.logging.Log log

originalResponse

protected StringBuffer originalResponse

parserCalled

protected boolean parserCalled

headerInsertPosition

protected int headerInsertPosition

bodyInsertPosition

protected int bodyInsertPosition

beforeBodyPosition

protected int beforeBodyPosition

afterBodyContentInsertPosition

protected int afterBodyContentInsertPosition

beforeBodyEndPosition

protected int beforeBodyEndPosition
Constructor Detail

DefaultAddResource

protected DefaultAddResource()
Method Detail

addJavaScriptAtPosition

public void addJavaScriptAtPosition(javax.faces.context.FacesContext context,
                                    ResourcePosition position,
                                    ResourceHandler resourceHandler)
Adds the given Javascript resource to the document header at the specified document positioy by supplying a resourcehandler instance.

Use this method to have full control about building the reference url to identify the resource and to customize how the resource is written to the response. In most cases, however, one of the convenience methods on this class can be used without requiring a custom ResourceHandler to be provided.

If the script has already been referenced, it's added only once.

Note that this method queues the javascript for insertion, and that the script is inserted into the buffered response by the ExtensionsFilter after the page is complete.

Specified by:
addJavaScriptAtPosition in interface AddResource
Overrides:
addJavaScriptAtPosition in class NonBufferingAddResource

addJavaScriptAtPosition

public void addJavaScriptAtPosition(javax.faces.context.FacesContext context,
                                    ResourcePosition position,
                                    Class myfacesCustomComponent,
                                    String resourceName)
Insert a [script src="url"] entry into the document header at the specified document position. If the script has already been referenced, it's added only once.

The resource is expected to be in the classpath, at the same location as the specified component + "/resource".

Example: when customComponent is class example.Widget, and resourceName is script.js, the resource will be retrieved from "example/Widget/resource/script.js" in the classpath.

Specified by:
addJavaScriptAtPosition in interface AddResource
Overrides:
addJavaScriptAtPosition in class NonBufferingAddResource

addJavaScriptAtPositionPlain

public void addJavaScriptAtPositionPlain(javax.faces.context.FacesContext context,
                                         ResourcePosition position,
                                         Class myfacesCustomComponent,
                                         String resourceName)
Description copied from interface: AddResource
Adds the given Javascript resource at the specified document position. If the script has already been referenced, it's added only once.
In constrast to the other methods this will not encode the url. So ,depending on the use case, it wont work in cookie-only environments.

Specified by:
addJavaScriptAtPositionPlain in interface AddResource
Overrides:
addJavaScriptAtPositionPlain in class NonBufferingAddResource

addJavaScriptAtPosition

public void addJavaScriptAtPosition(javax.faces.context.FacesContext context,
                                    ResourcePosition position,
                                    Class myfacesCustomComponent,
                                    String resourceName,
                                    boolean defer)
Insert a [script src="url"] entry into the document header at the specified document position. If the script has already been referenced, it's added only once.

Specified by:
addJavaScriptAtPosition in interface AddResource
Overrides:
addJavaScriptAtPosition in class NonBufferingAddResource
Parameters:
defer - specifies whether the html attribute "defer" is set on the generated script tag. If this is true then the browser will continue processing the html page without waiting for the specified script to load and be run.

addJavaScriptAtPosition

public void addJavaScriptAtPosition(javax.faces.context.FacesContext context,
                                    ResourcePosition position,
                                    String uri)
Insert a [script src="url"] entry into the document header at the specified document position. If the script has already been referenced, it's added only once.

Specified by:
addJavaScriptAtPosition in interface AddResource
Overrides:
addJavaScriptAtPosition in class NonBufferingAddResource
Parameters:
uri - is the location of the desired resource, relative to the base directory of the webapp (ie its contextPath).

addJavaScriptAtPosition

public void addJavaScriptAtPosition(javax.faces.context.FacesContext context,
                                    ResourcePosition position,
                                    String uri,
                                    boolean defer)
Adds the given Javascript resource at the specified document position. If the script has already been referenced, it's added only once.

Specified by:
addJavaScriptAtPosition in interface AddResource
Overrides:
addJavaScriptAtPosition in class NonBufferingAddResource

addJavaScriptToBodyTag

public void addJavaScriptToBodyTag(javax.faces.context.FacesContext context,
                                   String javascriptEventName,
                                   String addedJavaScript)
Specified by:
addJavaScriptToBodyTag in interface AddResource
Overrides:
addJavaScriptToBodyTag in class NonBufferingAddResource

addJavaScriptAtPosition

public void addJavaScriptAtPosition(javax.faces.context.FacesContext context,
                                    ResourcePosition position,
                                    ResourceHandler resourceHandler,
                                    boolean defer)
Adds the given Javascript resource at the specified document position. If the script has already been referenced, it's added only once.

Specified by:
addJavaScriptAtPosition in interface AddResource
Overrides:
addJavaScriptAtPosition in class NonBufferingAddResource

addStyleSheet

public void addStyleSheet(javax.faces.context.FacesContext context,
                          ResourcePosition position,
                          Class myfacesCustomComponent,
                          String resourceName)
Adds the given Style Sheet at the specified document position. If the style sheet has already been referenced, it's added only once.

Specified by:
addStyleSheet in interface AddResource
Overrides:
addStyleSheet in class NonBufferingAddResource

addStyleSheet

public void addStyleSheet(javax.faces.context.FacesContext context,
                          ResourcePosition position,
                          String uri)
Adds the given Style Sheet at the specified document position. If the style sheet has already been referenced, it's added only once.

Specified by:
addStyleSheet in interface AddResource
Overrides:
addStyleSheet in class NonBufferingAddResource

addStyleSheet

public void addStyleSheet(javax.faces.context.FacesContext context,
                          ResourcePosition position,
                          ResourceHandler resourceHandler)
Adds the given Style Sheet at the specified document position. If the style sheet has already been referenced, it's added only once.

Specified by:
addStyleSheet in interface AddResource
Overrides:
addStyleSheet in class NonBufferingAddResource

addInlineStyleAtPosition

public void addInlineStyleAtPosition(javax.faces.context.FacesContext context,
                                     ResourcePosition position,
                                     String inlineStyle)
Adds the given Inline Style at the specified document position.

Specified by:
addInlineStyleAtPosition in interface AddResource
Overrides:
addInlineStyleAtPosition in class NonBufferingAddResource

addInlineScriptAtPosition

public void addInlineScriptAtPosition(javax.faces.context.FacesContext context,
                                      ResourcePosition position,
                                      String inlineScript)
Adds the given Inline Script at the specified document position.

Specified by:
addInlineScriptAtPosition in interface AddResource
Overrides:
addInlineScriptAtPosition in class NonBufferingAddResource

getHeaderBeginInfos

protected Set getHeaderBeginInfos()

getBodyEndInfos

protected Set getBodyEndInfos()

getBodyOnloadInfos

protected Set getBodyOnloadInfos()

hasHeaderBeginInfos

public boolean hasHeaderBeginInfos()
Description copied from interface: AddResource
check there is something to write to the header

Specified by:
hasHeaderBeginInfos in interface AddResource
Overrides:
hasHeaderBeginInfos in class NonBufferingAddResource

parseResponse

public void parseResponse(javax.servlet.http.HttpServletRequest request,
                          String bufferedResponse,
                          javax.servlet.http.HttpServletResponse response)
Parses the response to mark the positions where code will be inserted

Specified by:
parseResponse in interface AddResource
Overrides:
parseResponse in class NonBufferingAddResource

writeMyFacesJavascriptBeforeBodyEnd

public void writeMyFacesJavascriptBeforeBodyEnd(javax.servlet.http.HttpServletRequest request,
                                                javax.servlet.http.HttpServletResponse response)
                                         throws IOException
Writes the javascript code necessary for myfaces in every page, just befode the closing </body> tag

Specified by:
writeMyFacesJavascriptBeforeBodyEnd in interface AddResource
Overrides:
writeMyFacesJavascriptBeforeBodyEnd in class NonBufferingAddResource
Throws:
IOException

writeWithFullHeader

public void writeWithFullHeader(javax.servlet.http.HttpServletRequest request,
                                javax.servlet.http.HttpServletResponse response)
                         throws IOException
Add the resources to the <head> of the page. If the head tag is missing, but the <body> tag is present, the head tag is added. If both are missing, no resource is added.

The ordering is such that the user header CSS & JS override the MyFaces' ones.

Specified by:
writeWithFullHeader in interface AddResource
Overrides:
writeWithFullHeader in class NonBufferingAddResource
Throws:
IOException

writeResponse

public void writeResponse(javax.servlet.http.HttpServletRequest request,
                          javax.servlet.http.HttpServletResponse response)
                   throws IOException
Writes the response

Specified by:
writeResponse in interface AddResource
Overrides:
writeResponse in class NonBufferingAddResource
Throws:
IOException

getScriptInstance

protected DefaultAddResource.PositionedInfo getScriptInstance(javax.faces.context.FacesContext context,
                                                              String uri,
                                                              boolean defer)

requiresBuffer

public boolean requiresBuffer()
Description copied from interface: AddResource
return true if you require the complete response buffered

Specified by:
requiresBuffer in interface AddResource
Overrides:
requiresBuffer in class NonBufferingAddResource

responseStarted

public void responseStarted()
Description copied from interface: AddResource
called when the response start

Specified by:
responseStarted in interface AddResource
Overrides:
responseStarted in class NonBufferingAddResource

responseFinished

public void responseFinished()
Description copied from interface: AddResource
called when the response has finished

Specified by:
responseFinished in interface AddResource
Overrides:
responseFinished in class NonBufferingAddResource


Copyright © 2012 The Apache Software Foundation. All Rights Reserved.