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

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

Introduction

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

Prototype

public boolean hasChildNodes() 

Source Link

Usage

From source file:org.vectomatic.dev.svg.impl.gen.SVGImageParser.java

License:Open Source License

@Override
public void parse(XMLElement elem, String fieldName, JClassType type, UiBinderWriter writer)
        throws UnableToCompleteException {
    if (elem.hasAttribute(ATTR_RESOURCE) && elem.hasChildNodes()) {
        writer.die("In %s, attribute \"%s\" and inline svg are mutually exclusive", elem, ATTR_RESOURCE);
    }// w ww  .ja v  a2s  .  c  o m
    if (!(elem.hasAttribute(ATTR_RESOURCE) || elem.hasChildNodes())) {
        writer.die("In %s, attribute \"%s\" or inline svg must be present", elem, ATTR_RESOURCE);
    }
    if (elem.hasAttribute(ATTR_RESOURCE)) {
        JClassType svgResourceType = writer.getOracle().findType(SVGResource.class.getCanonicalName());
        String resource = elem.consumeAttribute(ATTR_RESOURCE, svgResourceType);
        writer.addStatement("%s.setResource(%s);", fieldName, resource);
    } else {
        boolean validated = true;
        if (elem.hasAttribute(ATTR_VALIDATED)) {
            String value = elem.consumeBooleanAttribute(ATTR_VALIDATED);
            validated = Boolean.valueOf(value);
        }
        Element container = elem.getElement();
        NodeList childNodes = container.getChildNodes();
        Element root = null;
        for (int i = 0, length = childNodes.getLength(); i < length; i++) {
            Node node = childNodes.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                if (root == null && SVGConstants.SVG_NAMESPACE_URI.equals(node.getNamespaceURI())
                        && SVGConstants.SVG_SVG_TAG.equals(node.getLocalName())) {
                    root = (Element) node;
                } else {
                    writer.die("In %s, attribute \"%s\" or inline svg must be present", elem, ATTR_RESOURCE);
                }
            }
        }
        if (root == null) {
            writer.die("In %s, attribute \"%s\" or inline svg must be present", elem, ATTR_RESOURCE);
        }
        writer.beginAttachedSection(fieldName + ".getElement()");
        SvgInterpreter interpreter = SvgInterpreter.newInterpreterForUiObject(writer, fieldName, root);
        String rawSvg = elem.consumeInnerHtml(interpreter);
        if (validated) {
            SVGValidator.validate(rawSvg, elem.getLocation().getSystemId(), null, writer);
        }
        String omSvgParser = OMSVGParser.class.getName();
        writer.addStatement("%s.setSvgElement(%s.parse(\"%s\"));", fieldName, omSvgParser, rawSvg);
        writer.endAttachedSection();
    }
}