@Override public Object getAttribute(final Object object, final String attribute) { return GPathResult.this.getProperty("@" + attribute); }
/** * Returns true if the GPathResult is empty, i.e. if, and only if, <code>size()</code> is 0. * * @return true if the GPathResult is empty */ public boolean isEmpty() { return size() == 0; }
/** * Overloads the left shift operator to provide an easy way to * lazily append Objects to this GPathResult. * * @param newValue the Object to append * @return <code>this</code> */ public Object leftShift(final Object newValue) { appendNode(newValue); return this; }
/** * A helper method to allow GPathResults to work with subscript operators * @param index an index * @param newValue the value to put at the given index */ public void putAt(final int index, final Object newValue) { final GPathResult result = (GPathResult)getAt(index); if (newValue instanceof Closure) { result.replaceNode((Closure)newValue); } else { result.replaceBody(newValue); } }
/** * Creates a new GPathResult named <code>name</code> with the parent <code>parent</code>, * the namespacePrefix <code>namespacePrefix</code> and the namespaceTagHints specified in * the <code>namespaceTagHints</code> Map. * * @param parent the GPathResult prior to the application of the expression creating this GPathResult * @param name if the GPathResult corresponds to something with a name, e.g. a node * @param namespacePrefix the namespace prefix if any * @param namespaceTagHints the known tag to namespace mappings */ public GPathResult(final GPathResult parent, final String name, final String namespacePrefix, final Map<String, String> namespaceTagHints) { if (parent == null) { // we are the top of the tree this.parent = this; this.namespaceMap.put("xml", "http://www.w3.org/XML/1998/namespace"); // The XML namespace is always defined } else { this.parent = parent; this.namespaceMap.putAll(parent.namespaceMap); } this.name = name; this.namespacePrefix = namespacePrefix; this.namespaceTagHints = namespaceTagHints; setMetaClass(getMetaClass()); // wrap the standard MetaClass with the delegate }
private GPathResult getNextByDepth() { while (this.iter.hasNext()) { final GPathResult node = (GPathResult) this.iter.next(); this.list.add(node); this.stack.push(this.iter); this.iter = node.children().iterator(); } if (this.list.isEmpty()) { return null; } else { GPathResult result = (GPathResult) this.list.get(0); this.list.remove(0); this.iter = (Iterator) this.stack.pop(); return result; } } };
if (property.startsWith("@")) { if (newValue instanceof String || newValue instanceof GString) { final Iterator iter = iterator(); for (Object o : ((Map) newValue).entrySet()) { final Map.Entry entry = (Map.Entry) o; result.setProperty("@" + entry.getKey(), entry.getValue()); result.replaceNode((Closure) newValue); } else { result.replaceBody(newValue);
return parent(); } else if ("*".equals(property)) { return children(); } else if ("**".equals(property)) { return depthFirst(); } else if (property.startsWith("@")) { if (property.contains(":") && !this.namespaceTagHints.isEmpty()) {
public Iterator nodeIterator() { return new NodeIterator(this.parent.iterator()) { protected Object getNextNode(final Iterator iter) { while (iter.hasNext()) { final Object node = iter.next(); if (DefaultTypeTransformation.castToBoolean(FilteredAttributes.this.closure.call(new Object[]{node}))) { return node; } } return null; } }; }
public Iterator nodeIterator() { if ("*".equals(name)) { return parent.childNodes(); } else { return new NodeIterator(parent.childNodes()) { protected Object getNextNode(Iterator iter) { while (iter.hasNext()) { final Node node = (Node) iter.next(); if (name.equals(node.name())) { if ("*".equals(namespacePrefix) || ("".equals(namespacePrefix) && "".equals(node.namespaceURI())) || node.namespaceURI().equals(namespaceMap.get(namespacePrefix))) { return node; } } } return null; } }; } }
/** * Throws a <code>GroovyRuntimeException</code>, because attributes can have no children. */ public Iterator childNodes() { throw new GroovyRuntimeException("Can't get the child nodes on a GPath expression selecting attributes: ...." + this.parent.name() + "." + name() + ".childNodes()"); }
public void doCall(Object[] args) { final GroovyObject delegate = (GroovyObject)getDelegate(); final GPathResult thisObject = (GPathResult)getThisObject(); Node node = (Node)thisObject.getAt(0); List children = node.children(); for (Object child : children) { delegate.getProperty("mkp"); if (child instanceof Node) { delegate.invokeMethod("yield", new Object[]{new NodeChild((Node) child, thisObject, "*", null)}); } else { delegate.invokeMethod("yield", new Object[]{child}); } } } };
public void setProperty(final String property, final Object newValue) { if (property.startsWith("@")) { if (newValue instanceof String || newValue instanceof GString) { final Iterator iter = iterator(); while (iter.hasNext()) { final NodeChild child = (NodeChild) iter.next(); child.attributes().put(property.substring(1), newValue); } } } else { final GPathResult result = new NodeChildren(this, property, this.namespaceTagHints); if (newValue instanceof Map) { for (Object o : ((Map) newValue).entrySet()) { final Map.Entry entry = (Map.Entry) o; result.setProperty("@" + entry.getKey(), entry.getValue()); } } else { if (newValue instanceof Closure) { result.replaceNode((Closure) newValue); } else { result.replaceBody(newValue); } } } }
public void putAt(final int index, final Object newValue) { final GPathResult result = (GPathResult)getAt(index); if (newValue instanceof Closure) { result.replaceNode((Closure)newValue); } else { result.replaceBody(newValue); } }
public Object getProperty(final String property) { if ("..".equals(property)) { return parent(); } else if ("*".equals(property)) { return children(); } else if ("**".equals(property)) { return depthFirst(); } else if (property.startsWith("@")) { if (property.indexOf(":") != -1) { final int i = property.indexOf(":"); return new Attributes(this, "@" + property.substring(i + 1), property.substring(1, i), this.namespaceTagHints); } else { return new Attributes(this, property, this.namespaceTagHints); } } else { if (property.indexOf(":") != -1) { final int i = property.indexOf(":"); return new NodeChildren(this, property.substring(i + 1), property.substring(0, i), this.namespaceTagHints); } else { return new NodeChildren(this, property, this.namespaceTagHints); } } }
/** * @param parent * @param name * @param namespacePrefix * @param namespaceTagHints */ public GPathResult(final GPathResult parent, final String name, final String namespacePrefix, final Map namespaceTagHints) { if (parent == null) { // we are the top of the tree this.parent = this; this.namespaceMap.put("xml", "http://www.w3.org/XML/1998/namespace"); // The XML namespace is always defined } else { this.parent = parent; this.namespaceMap.putAll(parent.namespaceMap); } this.name = name; this.namespacePrefix = namespacePrefix; this.namespaceTagHints = namespaceTagHints; setMetaClass(getMetaClass()); // wrap the standard MetaClass with the delegate }
private final List list = new LinkedList(); private final Stack stack = new Stack(); private Iterator iter = iterator(); private GPathResult next = getNextByDepth();
public Iterator nodeIterator() { if ("*".equals(name)) { return parent.childNodes(); } else { return new NodeIterator(parent.childNodes()) { protected Object getNextNode(Iterator iter) { while (iter.hasNext()) { final Node node = (Node) iter.next(); if (name.equals(node.name())) { if ("*".equals(namespacePrefix) || ("".equals(namespacePrefix) && "".equals(node.namespaceURI())) || node.namespaceURI().equals(namespaceMap.get(namespacePrefix))) { return node; } } } return null; } }; } }