Search in sources :

Example 6 with QName

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

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 7 with QName

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

the class Attributes method iterator.

public Iterator iterator() {
    return new NodeIterator(nodeIterator()) {

        protected Object getNextNode(final Iterator iter) {
            while (iter.hasNext()) {
                final Object next = iter.next();
                if (next instanceof Attribute) {
                    return next;
                } else {
                    String attributeKey = Attributes.this.attributeName;
                    if (Attributes.this.namespacePrefix != null && !"*".equals(Attributes.this.namespacePrefix) && Attributes.this.namespacePrefix.length() > 0) {
                        attributeKey = new QName(Attributes.this.lookupNamespace(Attributes.this.namespacePrefix), Attributes.this.attributeName).toString();
                    }
                    final String value = (String) ((Node) next).attributes().get(attributeKey);
                    if (value != null) {
                        return new Attribute(Attributes.this.name, value, new NodeChild((Node) next, Attributes.this.parent.parent, "", Attributes.this.namespaceTagHints), (Attributes.this.namespacePrefix == null || "*".equals(Attributes.this.namespacePrefix)) ? "" : Attributes.this.namespacePrefix, Attributes.this.namespaceTagHints);
                    }
                }
            }
            return null;
        }
    };
}
Also used : QName(groovy.xml.QName) Iterator(java.util.Iterator) GroovyObject(groovy.lang.GroovyObject)

Example 8 with QName

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

the class NamespaceAwareHashMap method adjustForNamespaceIfNeeded.

private Object adjustForNamespaceIfNeeded(Object key) {
    String keyString = key.toString();
    if (keyString.contains("{") || namespaceTagHints == null || namespaceTagHints.isEmpty() || keyString.contains("{") || !keyString.contains(":")) {
        return key;
    }
    final int i = keyString.indexOf(":");
    return new QName(namespaceTagHints.get(keyString.substring(0, i)).toString(), keyString.substring(i + 1)).toString();
}
Also used : QName(groovy.xml.QName)

Example 9 with QName

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

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 10 with QName

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

the class Attributes method iterator.

public Iterator iterator() {
    return new NodeIterator(nodeIterator()) {

        protected Object getNextNode(final Iterator iter) {
            while (iter.hasNext()) {
                final Object next = iter.next();
                if (next instanceof Attribute) {
                    return next;
                } else {
                    String attributeKey = Attributes.this.attributeName;
                    if (Attributes.this.namespacePrefix != null && !"*".equals(Attributes.this.namespacePrefix) && Attributes.this.namespacePrefix.length() > 0) {
                        attributeKey = new QName(Attributes.this.lookupNamespace(Attributes.this.namespacePrefix), Attributes.this.attributeName).toString();
                    }
                    final String value = (String) ((Node) next).attributes().get(attributeKey);
                    if (value != null) {
                        return new Attribute(Attributes.this.name, value, new NodeChild((Node) next, Attributes.this.parent.parent, "", Attributes.this.namespaceTagHints), (Attributes.this.namespacePrefix == null || "*".equals(Attributes.this.namespacePrefix)) ? "" : Attributes.this.namespacePrefix, Attributes.this.namespaceTagHints);
                    }
                }
            }
            return null;
        }
    };
}
Also used : QName(groovy.xml.QName) Iterator(java.util.Iterator) GroovyObject(groovy.lang.GroovyObject)

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