Example usage for org.apache.commons.jelly XMLOutput endElement

List of usage examples for org.apache.commons.jelly XMLOutput endElement

Introduction

In this page you can find the example usage for org.apache.commons.jelly XMLOutput endElement.

Prototype

public void endElement(String uri, String localName, String qName) throws SAXException 

Source Link

Document

Receive notification of the end of an element.

Usage

From source file:org.kohsuke.stapler.jelly.groovy.JellyBuilder.java

@SuppressWarnings({ "ChainOfInstanceofChecks" })
protected void doInvokeMethod(QName name, Object args) {
    List list = InvokerHelper.asList(args);

    Map attributes = Collections.EMPTY_MAP;
    Closure closure = null;//from w ww  .ja  v  a  2s .c  o m
    String innerText = null;

    // figure out what parameters are what
    switch (list.size()) {
    case 0:
        break;
    case 1: {
        Object object = list.get(0);
        if (object instanceof Map) {
            attributes = (Map) object;
        } else if (object instanceof Closure) {
            closure = (Closure) object;
            break;
        } else {
            if (object != null)
                innerText = object.toString();
        }
        break;
    }
    case 2: {
        Object object1 = list.get(0);
        Object object2 = list.get(1);
        if (object1 instanceof Map) {
            attributes = (Map) object1;
            if (object2 instanceof Closure) {
                closure = (Closure) object2;
            } else if (object2 != null) {
                innerText = object2.toString();
            }
        } else {
            innerText = object1.toString();
            if (object2 instanceof Closure) {
                closure = (Closure) object2;
            } else if (object2 instanceof Map) {
                attributes = (Map) object2;
            } else {
                throw new MissingMethodException(name.toString(), getClass(), list.toArray());
            }
        }
        break;
    }
    case 3: {
        Object arg0 = list.get(0);
        Object arg1 = list.get(1);
        Object arg2 = list.get(2);
        if (arg0 instanceof Map && arg2 instanceof Closure) {
            closure = (Closure) arg2;
            attributes = (Map) arg0;
            innerText = arg1.toString();
        } else if (arg1 instanceof Map && arg2 instanceof Closure) {
            closure = (Closure) arg2;
            attributes = (Map) arg1;
            innerText = arg0.toString();
        } else {
            throw new MissingMethodException(name.toString(), getClass(), list.toArray());
        }
        break;
    }
    default:
        throw new MissingMethodException(name.toString(), getClass(), list.toArray());
    }

    if (isTag(name)) {// bridge to other Jelly tags
        try {
            TagScript tagScript = createTagScript(name, attributes);
            if (tagScript != null) {
                Script body = NULL_SCRIPT;

                if (closure != null) {
                    final Closure theClosure = closure;
                    body = new Script() {
                        public Script compile() throws JellyException {
                            return this;
                        }

                        public void run(JellyContext context, XMLOutput output) throws JellyTagException {
                            JellyContext oldc = setContext(context);
                            XMLOutput oldo = setOutput(output);
                            try {
                                theClosure.setDelegate(JellyBuilder.this);
                                theClosure.call();
                            } finally {
                                setContext(oldc);
                                setOutput(oldo);
                            }
                        }
                    };
                } else if (innerText != null)
                    body = new TextScript(innerText);

                tagScript.setTagBody(body);
                tagScript.run(context, output);
                return;
            }
        } catch (JellyException e) {
            throw new RuntimeException(e);
        }
    }

    // static tag
    this.attributes.clear();
    for (Entry e : ((Map<?, ?>) attributes).entrySet()) {
        Object v = e.getValue();
        if (v == null)
            continue;
        String attName = e.getKey().toString();
        this.attributes.addAttribute("", attName, attName, "CDATA", v.toString());
    }
    try {
        output.startElement(name.getNamespaceURI(), name.getLocalPart(), name.getQualifiedName(),
                this.attributes);
        if (closure != null) {
            closure.setDelegate(this);
            closure.call();
        }
        if (innerText != null)
            text(innerText);
        output.endElement(name.getNamespaceURI(), name.getLocalPart(), name.getQualifiedName());
    } catch (SAXException e) {
        throw new RuntimeException(e); // what's the proper way to handle exceptions in Groovy?
    }
}