com.google.gwt.uibinder.elementparsers.HtmlInterpreter Maven / Gradle / Ivy
/*
* Copyright 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.gwt.uibinder.elementparsers;
import com.google.gwt.core.ext.UnableToCompleteException;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.uibinder.rebind.UiBinderWriter;
import com.google.gwt.uibinder.rebind.XMLElement;
import com.google.gwt.uibinder.rebind.XMLElement.Interpreter;
/**
* This is the most generally useful interpreter, and the most likely to be used
* by a custom parser when calling {@link XMLElement#consumeInnerHtml}.
*
* - Assigns computed values to element attributes (e.g.
* class="{style.pretty}")
*
- Generates fields to hold named dom elements (e.g. <div
* gwt:field="importantDiv">)
*
- Turns <ui:msg> and <ui:attr> elements into methods on a
* generated Messages interface
*
- Fails if any element encountered is a widget
*
*/
public class HtmlInterpreter implements XMLElement.Interpreter {
/**
* A convenience factory method for the most common use of this class, to work
* with HTML that will eventually be rendered under a
* {@link com.google.gwt.user.client.ui.UIObject} (or really, any object that
* responds to getElement()
). Uses an instance of
* {@link HtmlMessageInterpreter} to process message elements.
*
* @param uiExpression An expression that can be evaluated at runtime to find
* an object whose getElement() method can be called to get an
* ancestor of all Elements generated from the interpreted HTML.
*/
public static HtmlInterpreter newInterpreterForUiObject(
UiBinderWriter writer, String uiExpression) {
String ancestorExpression = writer.useLazyWidgetBuilders()
? uiExpression : uiExpression + ".getElement()";
return new HtmlInterpreter(writer, ancestorExpression,
new HtmlMessageInterpreter(writer, ancestorExpression));
}
private final UiBinderWriter writer;
private final InterpreterPipe pipe;
/**
* Rather than using this constructor, you probably want to use the
* {@link #newInterpreterForUiObject} factory method.
*
* @param ancestorExpression An expression that can be evaluated at runtime to
* find an Element that will be an ancestor of all Elements generated
* from the interpreted HTML.
* @param messageInterpreter an interpreter to handle msg and ph elements,
* typically an instance of {@link HtmlMessageInterpreter}. This
* interpreter gets last crack
*/
public HtmlInterpreter(UiBinderWriter writer, String ancestorExpression,
Interpreter messageInterpreter) {
this.writer = writer;
this.pipe = new InterpreterPipe();
pipe.add(new FieldInterpreter(writer, ancestorExpression));
/* UiTextInterpreter and UiSafeHtmlInterpreter must be invoked before
* ComputedAttributeInterpreter to function properly
*/
pipe.add(new UiTextInterpreter(writer));
pipe.add(new UiSafeHtmlInterpreter(writer));
pipe.add(new ComputedAttributeInterpreter(writer));
pipe.add(new AttributeMessageInterpreter(writer));
pipe.add(messageInterpreter);
}
public String interpretElement(XMLElement elem)
throws UnableToCompleteException {
if (writer.useLazyWidgetBuilders() &&
writer.isElementAssignableTo(elem, SafeHtml.class)) {
String childFieldName = writer.parseElementToField(elem);
return writer.tokenForSafeHtmlExpression(childFieldName);
}
if (writer.isImportedElement(elem)) {
writer.die(elem, "Not allowed in an HTML context");
}
return pipe.interpretElement(elem);
}
}