com.google.gwt.uibinder.rebind
Class UiBinderWriter

java.lang.Object
  extended by com.google.gwt.uibinder.rebind.UiBinderWriter
All Implemented Interfaces:
com.google.gwt.uibinder.rebind.Statements

public class UiBinderWriter
extends Object
implements com.google.gwt.uibinder.rebind.Statements

Writer for UiBinder generated classes. TODO(rdamazio): Refactor this, extract model classes, improve ordering guarantees, etc. TODO(rjrjr): Line numbers in error messages.


Nested Class Summary
 
Nested classes/interfaces inherited from interface com.google.gwt.uibinder.rebind.Statements
com.google.gwt.uibinder.rebind.Statements.Empty
 
Field Summary
 
Fields inherited from interface com.google.gwt.uibinder.rebind.Statements
EMPTY
 
Constructor Summary
UiBinderWriter(com.google.gwt.core.ext.typeinfo.JClassType baseClass, String implClassName, String templatePath, com.google.gwt.core.ext.typeinfo.TypeOracle oracle, com.google.gwt.uibinder.rebind.MortalLogger logger, com.google.gwt.uibinder.rebind.FieldManager fieldManager, com.google.gwt.uibinder.rebind.messages.MessagesWriter messagesWriter, com.google.gwt.uibinder.rebind.DesignTimeUtils designTime, com.google.gwt.uibinder.rebind.UiBinderContext uiBinderCtx, boolean useSafeHtmlTemplates, boolean useLazyWidgetBuilders, String binderUri)
           
 
Method Summary
 void addDetachStatement(String format, Object... args)
          Add a statement to be executed right after the current attached element is detached.
 void addInitStatement(String format, Object... params)
          Add a statement to be run after everything has been instantiated, in the style of String.format(java.lang.String, java.lang.Object...).
 void addStatement(String format, Object... args)
          Adds a statement to the block run after fields are declared, in the style of String.format(java.lang.String, java.lang.Object...).
static String asCommaSeparatedList(String... args)
           
 void beginAttachedSection(String element)
          Begin a section where a new attachable element is being parsed--that is, one that will be constructed as a big innerHTML string, and then briefly attached to the dom to allow fields accessing its to be filled (at the moment, HasHTMLParser, HTMLPanelParser, and DomElementParser.).
 String declareDomField(String fieldName, String ancestorField)
          Declare a field that will hold an Element instance.
 String declareDomIdHolder()
          Declare a variable that will be filled at runtime with a unique id, safe for use as a dom element's id attribute.
 String declareField(String typeName, XMLElement elem)
          Declares a field of the given type name, returning the name of the declared field.
 String declareFieldIfNeeded(XMLElement elem)
          If this element has a gwt:field attribute, create a field for it of the appropriate type, and return the field name.
 String declareTemplateCall(String html)
          Writes a new SafeHtml template to the generated BinderImpl.
 String detokenate(String betokened)
          Given a string containing tokens returned by tokenForStringExpression(java.lang.String), tokenForSafeHtmlExpression(java.lang.String) or declareDomField(java.lang.String, java.lang.String), return a string with those tokens replaced by the appropriate expressions.
 void die(String message)
          Post an error message and halt processing.
 void die(String message, Object... params)
          Post an error message and halt processing.
 void die(XMLElement context, String message, Object... params)
          Post an error message about a specific XMLElement and halt processing.
 void endAttachedSection()
          End the current attachable section.
 void ensureAttached()
          Ensure that the specified element is attached to the DOM.
 void ensureCurrentFieldAttached()
          Ensure that the specified field is attached to the DOM.
static String escapeAttributeText(String text)
          Escape text that will be part of a string literal to be interpreted at runtime as an HTML attribute value.
static String escapeText(String text, boolean preserveWhitespace)
          Escape text that will be part of a string literal to be interpreted at runtime as HTML, optionally preserving whitespace.
static String escapeTextForJavaStringLiteral(String text)
          Escape characters that would mess up interpretation of this string as a string literal in generated code (that is, protect \, \n and " ).
 com.google.gwt.core.ext.typeinfo.JClassType findFieldType(XMLElement elem)
          Finds the JClassType that corresponds to this XMLElement, which must be a Widget or an Element.
 void genPropertySet(String fieldName, String propName, String value)
          Generates the code to set a property value (assumes that 'value' is a valid Java expression).
 void genStringPropertySet(String fieldName, String propName, String value)
          Generates the code to set a string property.
 com.google.gwt.uibinder.attributeparsers.AttributeParser getBundleAttributeParser(com.google.gwt.uibinder.rebind.XMLAttribute attribute)
          Deprecated. exists only to support BundleAttributeParser, which will be leaving us soon.
 com.google.gwt.uibinder.rebind.model.ImplicitClientBundle getBundleClass()
           
 com.google.gwt.uibinder.rebind.DesignTimeUtils getDesignTime()
          Returns the DesignTimeUtils, not null.
 com.google.gwt.uibinder.rebind.FieldManager getFieldManager()
           
 com.google.gwt.uibinder.rebind.MortalLogger getLogger()
          Returns the logger, at least until we get get it handed off to parsers via constructor args.
 com.google.gwt.uibinder.rebind.messages.MessagesWriter getMessages()
          Get the MessagesWriter for this UI, generating it if necessary.
 com.google.gwt.core.ext.typeinfo.TypeOracle getOracle()
          Gets the type oracle.
 com.google.gwt.uibinder.rebind.model.OwnerClass getOwnerClass()
           
 String getUiFieldAttributeName()
           
 boolean isBinderElement(XMLElement elem)
           
 boolean isElementAssignableTo(XMLElement elem, Class<?> possibleSuperclass)
           
 boolean isElementAssignableTo(XMLElement elem, com.google.gwt.core.ext.typeinfo.JClassType possibleSupertype)
           
 boolean isImportedElement(XMLElement elem)
           
 boolean isOwnerFieldLazyDomElement(String fieldName)
          Checks whether the given owner field name is a LazyDomElement or not.
 boolean isRenderableElement(XMLElement elem)
           
 boolean isWidgetElement(XMLElement elem)
           
 String parseElementToField(XMLElement elem)
          Parses the object associated with the specified element, and returns the name of the field (possibly private) that will hold it.
 com.google.gwt.uibinder.rebind.FieldWriter parseElementToFieldWriter(XMLElement elem)
          Parses the object associated with the specified element, and returns the field writer that will hold it.
 void setFieldInitializer(String fieldName, String factoryMethod)
          Gives the writer the initializer to use for this field instead of the default GWT.create call.
 void setFieldInitializerAsConstructor(String fieldName, com.google.gwt.core.ext.typeinfo.JClassType type, String... args)
          Instructs the writer to initialize the field with a specific constructor invocation, instead of the default GWT.create call.
 String tokenForSafeConstant(String expression)
          Like tokenForStringExpression(java.lang.String), but used for runtime expressions that we trust to be safe to interpret at runtime as HTML without escaping, like translated messages with simple formatting.
 String tokenForSafeHtmlExpression(String expression)
          Like tokenForStringExpression(java.lang.String), but used for runtime SafeHtml instances.
 String tokenForStringExpression(String expression)
          Returns a string token that can be used in place the given expression inside any string literals.
 boolean useLazyWidgetBuilders()
           
 boolean useSafeHtmlTemplates()
           
 void warn(String message)
          Post a warning message.
 void warn(String message, Object... params)
          Post a warning message.
 void warn(XMLElement context, String message, Object... params)
          Post a warning message.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

UiBinderWriter

public UiBinderWriter(com.google.gwt.core.ext.typeinfo.JClassType baseClass,
                      String implClassName,
                      String templatePath,
                      com.google.gwt.core.ext.typeinfo.TypeOracle oracle,
                      com.google.gwt.uibinder.rebind.MortalLogger logger,
                      com.google.gwt.uibinder.rebind.FieldManager fieldManager,
                      com.google.gwt.uibinder.rebind.messages.MessagesWriter messagesWriter,
                      com.google.gwt.uibinder.rebind.DesignTimeUtils designTime,
                      com.google.gwt.uibinder.rebind.UiBinderContext uiBinderCtx,
                      boolean useSafeHtmlTemplates,
                      boolean useLazyWidgetBuilders,
                      String binderUri)
               throws com.google.gwt.core.ext.UnableToCompleteException
Throws:
com.google.gwt.core.ext.UnableToCompleteException
Method Detail

asCommaSeparatedList

public static String asCommaSeparatedList(String... args)

escapeAttributeText

public static String escapeAttributeText(String text)
Escape text that will be part of a string literal to be interpreted at runtime as an HTML attribute value.


escapeText

public static String escapeText(String text,
                                boolean preserveWhitespace)
Escape text that will be part of a string literal to be interpreted at runtime as HTML, optionally preserving whitespace.


escapeTextForJavaStringLiteral

public static String escapeTextForJavaStringLiteral(String text)
Escape characters that would mess up interpretation of this string as a string literal in generated code (that is, protect \, \n and " ).


addDetachStatement

public void addDetachStatement(String format,
                               Object... args)
Add a statement to be executed right after the current attached element is detached. This is useful for doing things that might be expensive while the element is attached to the DOM.

Specified by:
addDetachStatement in interface com.google.gwt.uibinder.rebind.Statements
Parameters:
format -
args -
See Also:
beginAttachedSection(String)

addInitStatement

public void addInitStatement(String format,
                             Object... params)
Add a statement to be run after everything has been instantiated, in the style of String.format(java.lang.String, java.lang.Object...).

Specified by:
addInitStatement in interface com.google.gwt.uibinder.rebind.Statements

addStatement

public void addStatement(String format,
                         Object... args)
Adds a statement to the block run after fields are declared, in the style of String.format(java.lang.String, java.lang.Object...).

Specified by:
addStatement in interface com.google.gwt.uibinder.rebind.Statements

beginAttachedSection

public void beginAttachedSection(String element)
Begin a section where a new attachable element is being parsed--that is, one that will be constructed as a big innerHTML string, and then briefly attached to the dom to allow fields accessing its to be filled (at the moment, HasHTMLParser, HTMLPanelParser, and DomElementParser.).

Succeeding calls made to ensureAttached() and ensureCurrentFieldAttached() must refer to children of this element, until endAttachedSection() is called.

Parameters:
element - Java expression for the generated code that will return the dom element to be attached.

declareDomField

public String declareDomField(String fieldName,
                              String ancestorField)
                       throws com.google.gwt.core.ext.UnableToCompleteException
Declare a field that will hold an Element instance. Returns a token that the caller must set as the id attribute of that element in whatever innerHTML expression will reproduce it at runtime.

In the generated code, this token will be replaced by an expression to generate a unique dom id at runtime. Further code will be generated to be run after widgets are instantiated, to use that dom id in a getElementById call and assign the Element instance to its field.

Parameters:
fieldName - The name of the field being declared
ancestorField - The name of fieldName parent
Throws:
com.google.gwt.core.ext.UnableToCompleteException

declareDomIdHolder

public String declareDomIdHolder()
                          throws com.google.gwt.core.ext.UnableToCompleteException
Declare a variable that will be filled at runtime with a unique id, safe for use as a dom element's id attribute.

Returns:
that variable's name.
Throws:
com.google.gwt.core.ext.UnableToCompleteException

declareField

public String declareField(String typeName,
                           XMLElement elem)
                    throws com.google.gwt.core.ext.UnableToCompleteException
Declares a field of the given type name, returning the name of the declared field. If the element has a field or id attribute, use its value. Otherwise, create and return a new, private field name for it.

Throws:
com.google.gwt.core.ext.UnableToCompleteException

declareFieldIfNeeded

public String declareFieldIfNeeded(XMLElement elem)
                            throws com.google.gwt.core.ext.UnableToCompleteException
If this element has a gwt:field attribute, create a field for it of the appropriate type, and return the field name. If no gwt:field attribute is found, do nothing and return null

Returns:
The new field name, or null if no field is created
Throws:
com.google.gwt.core.ext.UnableToCompleteException

declareTemplateCall

public String declareTemplateCall(String html)
                           throws IllegalArgumentException
Writes a new SafeHtml template to the generated BinderImpl.

Returns:
The invocation of the SafeHtml template function with the arguments filled in
Throws:
IllegalArgumentException

detokenate

public String detokenate(String betokened)
Given a string containing tokens returned by tokenForStringExpression(java.lang.String), tokenForSafeHtmlExpression(java.lang.String) or declareDomField(java.lang.String, java.lang.String), return a string with those tokens replaced by the appropriate expressions. (It is not normally necessary for an XMLElement.Interpreter or ElementParser to make this call, as the tokens are typically replaced by the TemplateWriter itself.)


die

public void die(String message)
         throws com.google.gwt.core.ext.UnableToCompleteException
Post an error message and halt processing. This method always throws an UnableToCompleteException

Throws:
com.google.gwt.core.ext.UnableToCompleteException

die

public void die(String message,
                Object... params)
         throws com.google.gwt.core.ext.UnableToCompleteException
Post an error message and halt processing. This method always throws an UnableToCompleteException

Throws:
com.google.gwt.core.ext.UnableToCompleteException

die

public void die(XMLElement context,
                String message,
                Object... params)
         throws com.google.gwt.core.ext.UnableToCompleteException
Post an error message about a specific XMLElement and halt processing. This method always throws an UnableToCompleteException

Throws:
com.google.gwt.core.ext.UnableToCompleteException

endAttachedSection

public void endAttachedSection()
End the current attachable section. This will detach the element if it was ever attached and execute any detach statements.

See Also:
beginAttachedSection(String)

ensureAttached

public void ensureAttached()
Ensure that the specified element is attached to the DOM.

See Also:
beginAttachedSection(String)

ensureCurrentFieldAttached

public void ensureCurrentFieldAttached()
Ensure that the specified field is attached to the DOM. The field must hold an object that responds to Element getElement(). Convenience wrapper for ensureAttached()(field + ".getElement()").

See Also:
beginAttachedSection(String)

findFieldType

public com.google.gwt.core.ext.typeinfo.JClassType findFieldType(XMLElement elem)
                                                          throws com.google.gwt.core.ext.UnableToCompleteException
Finds the JClassType that corresponds to this XMLElement, which must be a Widget or an Element.

Throws:
com.google.gwt.core.ext.UnableToCompleteException - If no such widget class exists
RuntimeException - if asked to handle a non-widget, non-DOM element

genPropertySet

public void genPropertySet(String fieldName,
                           String propName,
                           String value)
Generates the code to set a property value (assumes that 'value' is a valid Java expression).


genStringPropertySet

public void genStringPropertySet(String fieldName,
                                 String propName,
                                 String value)
Generates the code to set a string property.


getBundleAttributeParser

@Deprecated
public com.google.gwt.uibinder.attributeparsers.AttributeParser getBundleAttributeParser(com.google.gwt.uibinder.rebind.XMLAttribute attribute)
                                                                                  throws com.google.gwt.core.ext.UnableToCompleteException
Deprecated. exists only to support BundleAttributeParser, which will be leaving us soon.

Finds an attribute BundleAttributeParser for the given xml attribute, if any, based on its namespace uri.

Returns:
the parser or null
Throws:
com.google.gwt.core.ext.UnableToCompleteException

getBundleClass

public com.google.gwt.uibinder.rebind.model.ImplicitClientBundle getBundleClass()

getDesignTime

public com.google.gwt.uibinder.rebind.DesignTimeUtils getDesignTime()
Returns the DesignTimeUtils, not null.


getFieldManager

public com.google.gwt.uibinder.rebind.FieldManager getFieldManager()

getLogger

public com.google.gwt.uibinder.rebind.MortalLogger getLogger()
Returns the logger, at least until we get get it handed off to parsers via constructor args.


getMessages

public com.google.gwt.uibinder.rebind.messages.MessagesWriter getMessages()
Get the MessagesWriter for this UI, generating it if necessary.


getOracle

public com.google.gwt.core.ext.typeinfo.TypeOracle getOracle()
Gets the type oracle.


getOwnerClass

public com.google.gwt.uibinder.rebind.model.OwnerClass getOwnerClass()

getUiFieldAttributeName

public String getUiFieldAttributeName()

isBinderElement

public boolean isBinderElement(XMLElement elem)

isElementAssignableTo

public boolean isElementAssignableTo(XMLElement elem,
                                     Class<?> possibleSuperclass)
                              throws com.google.gwt.core.ext.UnableToCompleteException
Throws:
com.google.gwt.core.ext.UnableToCompleteException

isElementAssignableTo

public boolean isElementAssignableTo(XMLElement elem,
                                     com.google.gwt.core.ext.typeinfo.JClassType possibleSupertype)
                              throws com.google.gwt.core.ext.UnableToCompleteException
Throws:
com.google.gwt.core.ext.UnableToCompleteException

isImportedElement

public boolean isImportedElement(XMLElement elem)

isOwnerFieldLazyDomElement

public boolean isOwnerFieldLazyDomElement(String fieldName)
Checks whether the given owner field name is a LazyDomElement or not.


isRenderableElement

public boolean isRenderableElement(XMLElement elem)
                            throws com.google.gwt.core.ext.UnableToCompleteException
Throws:
com.google.gwt.core.ext.UnableToCompleteException

isWidgetElement

public boolean isWidgetElement(XMLElement elem)
                        throws com.google.gwt.core.ext.UnableToCompleteException
Throws:
com.google.gwt.core.ext.UnableToCompleteException

parseElementToField

public String parseElementToField(XMLElement elem)
                           throws com.google.gwt.core.ext.UnableToCompleteException
Parses the object associated with the specified element, and returns the name of the field (possibly private) that will hold it. The element is likely to make recursive calls back to this method to have its children parsed.

Parameters:
elem - the xml element to be parsed
Returns:
the name of the field containing the parsed widget
Throws:
com.google.gwt.core.ext.UnableToCompleteException

parseElementToFieldWriter

public com.google.gwt.uibinder.rebind.FieldWriter parseElementToFieldWriter(XMLElement elem)
                                                                     throws com.google.gwt.core.ext.UnableToCompleteException
Parses the object associated with the specified element, and returns the field writer that will hold it. The element is likely to make recursive calls back to this method to have its children parsed.

Parameters:
elem - the xml element to be parsed
Returns:
the field holder just created
Throws:
com.google.gwt.core.ext.UnableToCompleteException

setFieldInitializer

public void setFieldInitializer(String fieldName,
                                String factoryMethod)
Gives the writer the initializer to use for this field instead of the default GWT.create call.

Throws:
IllegalStateException - if an initializer has already been set

setFieldInitializerAsConstructor

public void setFieldInitializerAsConstructor(String fieldName,
                                             com.google.gwt.core.ext.typeinfo.JClassType type,
                                             String... args)
Instructs the writer to initialize the field with a specific constructor invocation, instead of the default GWT.create call.

Parameters:
fieldName - the field to initialize
type - the type of the field
args - arguments to the constructor call

tokenForSafeConstant

public String tokenForSafeConstant(String expression)
Like tokenForStringExpression(java.lang.String), but used for runtime expressions that we trust to be safe to interpret at runtime as HTML without escaping, like translated messages with simple formatting. Wrapped in a call to SafeHtmlUtils.fromSafeConstant(java.lang.String) to keep the expression from being escaped by the SafeHtml template.

Parameters:
expression - must resolve to trusted HTML string

tokenForSafeHtmlExpression

public String tokenForSafeHtmlExpression(String expression)
Like tokenForStringExpression(java.lang.String), but used for runtime SafeHtml instances.

Parameters:
expression - must resolve to SafeHtml object

tokenForStringExpression

public String tokenForStringExpression(String expression)
Returns a string token that can be used in place the given expression inside any string literals. Before the generated code is written, the expression will be stitched back into the generated code in place of the token, surrounded by plus signs. This is useful in strings to be handed to setInnerHTML() and setText() calls, to allow a unique dom id attribute or other runtime expression in the string.

Parameters:
expression - must resolve to String

useLazyWidgetBuilders

public boolean useLazyWidgetBuilders()

useSafeHtmlTemplates

public boolean useSafeHtmlTemplates()
Returns:
true of SafeHtml integration is in effect

warn

public void warn(String message)
Post a warning message.


warn

public void warn(String message,
                 Object... params)
Post a warning message.


warn

public void warn(XMLElement context,
                 String message,
                 Object... params)
Post a warning message.



Copyright © 2012. All Rights Reserved.