com.google.clearsilver.jsilver.autoescape
Class AutoEscapeContext

java.lang.Object
  extended by com.google.clearsilver.jsilver.autoescape.AutoEscapeContext

public class AutoEscapeContext
extends Object

Encapsulates auto escaping logic.


Nested Class Summary
static class AutoEscapeContext.AutoEscapeState
          Enum representing states of the data being parsed.
 
Field Summary
static Map<String,HtmlParser.Mode> CONTENT_TYPE_LIST
          Map of content-type to corresponding HtmlParser.Mode, used by setContentType to specify the content type of provided input.
 
Constructor Summary
AutoEscapeContext()
           
AutoEscapeContext(EscapeMode mode)
          Create a new context in the state represented by mode.
AutoEscapeContext(EscapeMode mode, String resourceName)
          Create a new context in the state represented by mode.
 
Method Summary
 AutoEscapeContext cloneCurrentEscapeContext()
          Create a new context that is a copy of the current state of this context.
 int getColumnNumber()
          Returns the current column number within the resource being auto escaped.
 AutoEscapeContext.AutoEscapeState getCurrentState()
          Calls the HtmlParser API to determine current state.
 EscapeMode getEscapeModeForCurrentState()
          Returns the EscapeMode which will bring AutoEscapeContext into this state.
 String getEscapingFunctionForCurrentState()
          Determine the correct escaping to apply for a variable.
 int getLineNumber()
          Returns the current line number within the resource being auto escaped.
 String getResourceName()
          Returns the name of the resource currently being auto escaped.
 void insertText()
          Lets the AutoEscapeContext know that some input was skipped.
 boolean isPermittedStateChangeForIncludes(AutoEscapeContext.AutoEscapeState start, AutoEscapeContext.AutoEscapeState end)
          Determines whether an included template that begins in state start is allowed to end in state end.
 void parseData(String data)
          Parse the given data and update internal state accordingly.
 void setContentType(String contentType)
          Resets the state of the underlying html parser to a state consistent with the contentType provided.
 void setCurrentPosition(int line, int column)
          Sets the current position in the resource being auto escaped.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CONTENT_TYPE_LIST

public static final Map<String,HtmlParser.Mode> CONTENT_TYPE_LIST
Map of content-type to corresponding HtmlParser.Mode, used by setContentType to specify the content type of provided input. Valid values and the corresponding mode are:
text/html HtmlParser.Mode.HTML
text/plain HtmlParser.Mode.HTML
application/javascript HtmlParser.Mode.JS
application/json HtmlParser.Mode.JS
text/javascript HtmlParser.Mode.JS
text/css HtmlParser.Mode.CSS

See Also:
setContentType(java.lang.String)
Constructor Detail

AutoEscapeContext

public AutoEscapeContext()

AutoEscapeContext

public AutoEscapeContext(EscapeMode mode)
Create a new context in the state represented by mode.

Parameters:
mode - EscapeMode object.

AutoEscapeContext

public AutoEscapeContext(EscapeMode mode,
                         String resourceName)
Create a new context in the state represented by mode. If a non-null resourceName is provided, it will be used in displaying error messages.

Parameters:
mode - The initial EscapeMode for this context
resourceName - Name of the resource being auto escaped.
Method Detail

cloneCurrentEscapeContext

public AutoEscapeContext cloneCurrentEscapeContext()
Create a new context that is a copy of the current state of this context.

Returns:
New AutoEscapeContext that is a snapshot of the current state of this context.

setCurrentPosition

public void setCurrentPosition(int line,
                               int column)
Sets the current position in the resource being auto escaped. Useful for generating detailed error messages.

Parameters:
line - line number.
column - column number within line.

getResourceName

public String getResourceName()
Returns the name of the resource currently being auto escaped.


getLineNumber

public int getLineNumber()
Returns the current line number within the resource being auto escaped.


getColumnNumber

public int getColumnNumber()
Returns the current column number within the resource being auto escaped.


parseData

public void parseData(String data)
Parse the given data and update internal state accordingly.

Parameters:
data - Input to parse, usually the contents of a template.

insertText

public void insertText()
Lets the AutoEscapeContext know that some input was skipped. This method will usually be called for variables in the input stream. The AutoEscapeContext is told that the input stream contained some additional data but does not get to see the data. It can adjust its internal state accordingly.


isPermittedStateChangeForIncludes

public boolean isPermittedStateChangeForIncludes(AutoEscapeContext.AutoEscapeState start,
                                                 AutoEscapeContext.AutoEscapeState end)
Determines whether an included template that begins in state start is allowed to end in state end. Usually included templates are only allowed to end in the same context they begin in. This lets auto escaping parse the remainder of the parent template without needing to know the ending context of the included template. However, there is one exception where auto escaping will allow a different ending context: if the included template is a URI attribute value, it is allowed to change context from ATTR_URI_START to ATTR_URI. This does not cause any issues because the including template will call insertText when it encounters the include command, and insertText will cause the HTML parser to switch its internal state in the same way.


getEscapingFunctionForCurrentState

public String getEscapingFunctionForCurrentState()
Determine the correct escaping to apply for a variable. Looks at the current state of the htmlParser, and determines what escaping to apply to a variable in this state.

Returns:
Name of escaping function to use in this state.

getEscapeModeForCurrentState

public EscapeMode getEscapeModeForCurrentState()
Returns the EscapeMode which will bring AutoEscapeContext into this state. Initializing a new AutoEscapeContext with this EscapeMode will bring it into the state that the current AutoEscapeContext object is in.

Returns:
An EscapeMode object.

getCurrentState

public AutoEscapeContext.AutoEscapeState getCurrentState()
Calls the HtmlParser API to determine current state. This function is mostly a wrapper around the HtmlParser API. It gathers all the necessary information using that API and returns a single enum representing the current state.

Returns:
AutoEscapeState enum representing the current state.

setContentType

public void setContentType(String contentType)
Resets the state of the underlying html parser to a state consistent with the contentType provided. This method should be used when the starting auto escaping context of a resource cannot be determined from its contents - for example, a CSS stylesheet or a javascript source file.

Parameters:
contentType - MIME type header representing the content being parsed.
See Also:
CONTENT_TYPE_LIST


Copyright © 2010-2012 Google. All Rights Reserved.