Example usage for com.google.gwt.uibinder.rebind XMLElement consumeRawAttribute

List of usage examples for com.google.gwt.uibinder.rebind XMLElement consumeRawAttribute

Introduction

In this page you can find the example usage for com.google.gwt.uibinder.rebind XMLElement consumeRawAttribute.

Prototype

public String consumeRawAttribute(String name) 

Source Link

Document

Consumes the given attribute and returns its trimmed value, or null if it was unset.

Usage

From source file:com.jhickman.web.gwt.gxtuibinder.elementparsers.util.ElementParserUtil.java

License:Apache License

/**
 * Consumes and applies attributes to given element
 * //from  ww  w .j  a  va  2 s.  c  o  m
 * @param elem
 * @param fieldName
 * @param type
 * @param writer
 * @throws UnableToCompleteException
 */
public static void applyAttributes(XMLElement elem, String fieldName, JClassType type, UiBinderWriter writer)
        throws UnableToCompleteException {

    // first get the special Margin attribute
    ElementParserUtil.consumeRegionAttributes(elem, fieldName, writer);

    Map<String, JType> setterMethods = fetchSetterMethods(type);
    int attributeCount = elem.getAttributeCount();

    // count backwards since we're consuming as we go
    for (int i = attributeCount - 1; i >= 0; i--) {
        // always get 0 because we're consuming them
        XMLAttribute attribute = elem.getAttribute(i);

        String setterMethod = "set" + initialCap(attribute.getName());

        if (setterMethods.containsKey(setterMethod)) {
            JType setterType = setterMethods.get(setterMethod);
            String value;

            if ("float".equals(setterType.getQualifiedSourceName())) {
                value = attribute.consumeRawValue();
                float floatValue = 0;
                try {
                    floatValue = Float.parseFloat(value);
                } catch (NumberFormatException e) {
                    writer.die(elem, "Cannot parse float value for attribute '%s'.  Found %s.",
                            attribute.getName(), value);
                }
                // assuming that we didn't die due to a NumberFormatException, we can use the value
                value = floatValue + "f";
            } else if ("java.lang.Number".equals(setterType.getQualifiedSourceName())) {
                value = elem.consumeRawAttribute(attribute.getName());
            } else {
                value = elem.consumeAttribute(attribute.getName(), setterType);
            }
            writer.addStatement("%s.%s(%s);", fieldName, setterMethod, value);

        } else {
            try {
                writer.findFieldType(elem);
            } catch (UnableToCompleteException e) {
                writer.warn(elem, "Found attribute without associated setter method: %s.  IGNORING",
                        attribute.getName());
            }
        }
    }
}

From source file:com.jhickman.web.gwt.gxtuibinder.elementparsers.util.ElementParserUtil.java

License:Apache License

/**
 * Consumes the styleAttribute attribute from the provided elements.
 * //from w w  w  .j a v a  2  s. co  m
 * Allowed value format is:
 * styleName:value;styleName:value
 * 
 * 
 * @param elem
 * @param fieldName
 * @param writer
 * @return
 * @throws UnableToCompleteException
 */
public static void consumeStyleAttribute(XMLElement elem, String fieldName, UiBinderWriter writer)
        throws UnableToCompleteException {
    String styleAttributes = elem.consumeRawAttribute("styleAttribute");
    if (styleAttributes == null)
        return;

    String[] styles = styleAttributes.split("\\s*;\\s*");
    for (String style : styles) {
        String[] keyValue = style.split("\\s*:\\s*");
        if (keyValue.length != 2) {
            writer.die(elem,
                    "styleAttribute should be in format: styleName:styleValue;styleName:styleValue  but found %s",
                    styleAttributes);
        }

        writer.addStatement("%s.setStyleAttribute(\"%s\", \"%s\");", fieldName, keyValue[0], keyValue[1]);
    }

}