Search in sources :

Example 1 with QName

use of groovy.xml.QName in project groovy by apache.

the class XmlSlurper method startElement.

/* (non-Javadoc)
    * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
    */
public void startElement(final String namespaceURI, final String localName, final String qName, final Attributes atts) throws SAXException {
    addCdata();
    final Map<String, String> attributes = new NamespaceAwareHashMap();
    final Map<String, String> attributeNamespaces = new HashMap<String, String>();
    for (int i = atts.getLength() - 1; i != -1; i--) {
        if (atts.getURI(i).length() == 0) {
            attributes.put(atts.getQName(i), atts.getValue(i));
        } else {
            String key = new QName(atts.getURI(i), atts.getLocalName(i)).toString();
            attributes.put(key, atts.getValue(i));
            attributeNamespaces.put(key, atts.getURI(i));
        }
    }
    final Node newElement;
    if (namespaceURI.length() == 0) {
        newElement = new Node(currentNode, qName, attributes, attributeNamespaces, namespaceURI);
    } else {
        newElement = new Node(currentNode, localName, attributes, attributeNamespaces, namespaceURI);
    }
    if (currentNode != null) {
        currentNode.addChild(newElement);
    }
    stack.push(currentNode);
    currentNode = newElement;
}
Also used : NamespaceAwareHashMap(groovy.util.slurpersupport.NamespaceAwareHashMap) NamespaceAwareHashMap(groovy.util.slurpersupport.NamespaceAwareHashMap) HashMap(java.util.HashMap) QName(groovy.xml.QName) Node(groovy.util.slurpersupport.Node)

Example 2 with QName

use of groovy.xml.QName in project groovy by apache.

the class AntBuilderLocator method createNode.

protected Object createNode(final Object name, final Map attributes) {
    final Attributes attrs = buildAttributes(attributes);
    String tagName = name.toString();
    String ns = "";
    if (name instanceof QName) {
        QName q = (QName) name;
        tagName = q.getLocalPart();
        ns = q.getNamespaceURI();
    }
    // import can be used only as top level element
    if ("import".equals(name)) {
        antXmlContext.setCurrentTarget(implicitTarget);
    } else if ("target".equals(name) && !insideTask) {
        return onStartTarget(attrs, tagName, ns);
    } else if ("defineTarget".equals(name) && !insideTask) {
        return onDefineTarget(attrs, "target", ns);
    }
    try {
        antElementHandler.onStartElement(ns, tagName, tagName, attrs, antXmlContext);
    } catch (final SAXParseException e) {
        log.log(Level.SEVERE, "Caught: " + e, e);
    }
    insideTask = true;
    final RuntimeConfigurable wrapper = antXmlContext.getWrapperStack().lastElement();
    return wrapper.getProxy();
}
Also used : QName(groovy.xml.QName) SAXParseException(org.xml.sax.SAXParseException) Attributes(org.xml.sax.Attributes)

Example 3 with QName

use of groovy.xml.QName in project groovy by apache.

the class Node method getByName.

/**
     * Provides lookup of elements by name.
     *
     * @param name the name of interest
     * @return the nodes matching name
     */
private NodeList getByName(String name) {
    NodeList answer = new NodeList();
    for (Object child : children()) {
        if (child instanceof Node) {
            Node childNode = (Node) child;
            Object childNodeName = childNode.name();
            if (childNodeName instanceof QName) {
                QName qn = (QName) childNodeName;
                if (qn.matches(name)) {
                    answer.add(childNode);
                }
            } else if (name.equals(childNodeName)) {
                answer.add(childNode);
            }
        }
    }
    return answer;
}
Also used : QName(groovy.xml.QName)

Example 4 with QName

use of groovy.xml.QName in project groovy-core by groovy.

the class AntBuilderLocator method createNode.

protected Object createNode(final Object name, final Map attributes) {
    final Attributes attrs = buildAttributes(attributes);
    String tagName = name.toString();
    String ns = "";
    if (name instanceof QName) {
        QName q = (QName) name;
        tagName = q.getLocalPart();
        ns = q.getNamespaceURI();
    }
    // import can be used only as top level element
    if ("import".equals(name)) {
        antXmlContext.setCurrentTarget(implicitTarget);
    } else if ("target".equals(name) && !insideTask) {
        return onStartTarget(attrs, tagName, ns);
    } else if ("defineTarget".equals(name) && !insideTask) {
        return onDefineTarget(attrs, "target", ns);
    }
    try {
        antElementHandler.onStartElement(ns, tagName, tagName, attrs, antXmlContext);
    } catch (final SAXParseException e) {
        log.log(Level.SEVERE, "Caught: " + e, e);
    }
    insideTask = true;
    final RuntimeConfigurable wrapper = antXmlContext.getWrapperStack().lastElement();
    return wrapper.getProxy();
}
Also used : QName(groovy.xml.QName) SAXParseException(org.xml.sax.SAXParseException) Attributes(org.xml.sax.Attributes)

Example 5 with QName

use of groovy.xml.QName in project groovy-core by groovy.

the class DOMCategory method appendNode.

public static Element appendNode(Element self, Object name, String value) {
    Document doc = self.getOwnerDocument();
    Element newChild;
    if (name instanceof QName) {
        QName qn = (QName) name;
        newChild = doc.createElementNS(qn.getNamespaceURI(), qn.getQualifiedName());
    } else {
        newChild = doc.createElement(name.toString());
    }
    if (value != null) {
        Text text = doc.createTextNode(value);
        newChild.appendChild(text);
    }
    self.appendChild(newChild);
    return newChild;
}
Also used : QName(groovy.xml.QName) Element(org.w3c.dom.Element) Text(org.w3c.dom.Text) Document(org.w3c.dom.Document)

Aggregations

QName (groovy.xml.QName)12 GroovyObject (groovy.lang.GroovyObject)2 NamespaceAwareHashMap (groovy.util.slurpersupport.NamespaceAwareHashMap)2 Node (groovy.util.slurpersupport.Node)2 HashMap (java.util.HashMap)2 Iterator (java.util.Iterator)2 Document (org.w3c.dom.Document)2 Element (org.w3c.dom.Element)2 Text (org.w3c.dom.Text)2 Attributes (org.xml.sax.Attributes)2 SAXParseException (org.xml.sax.SAXParseException)2