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;
}
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;
}
};
}
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();
}
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;
}
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;
}
};
}
Aggregations