protected static void setMetaClass(Class nodelistClass,
MetaClass metaClass) {
final MetaClass newMetaClass = new DelegatingMetaClass(metaClass) {
/* (non-Javadoc)
* @see groovy.lang.DelegatingMetaClass#getAttribute(java.lang.Object, java.lang.String)
*/
public Object getAttribute(final Object object, final String attribute) {
NodeList nl = (NodeList) object;
Iterator it = nl.iterator();
List result = new ArrayList();
while (it.hasNext()) {
Node node = (Node) it.next();
result.add(node.attributes().get(attribute));
}
return result;
}
public void setAttribute(final Object object, final String attribute, final Object newValue) {
NodeList nl = (NodeList) object;
Iterator it = nl.iterator();
while (it.hasNext()) {
Node node = (Node) it.next();
node.attributes().put(attribute, newValue);
}
}
/* (non-Javadoc)
* @see groovy.lang.MetaClass#getProperty(java.lang.Object, java.lang.String)
*/
public Object getProperty(Object object, String property) {
if (object instanceof NodeList) {
NodeList nl = (NodeList) object;
return nl.getAt(property);
}
return super.getProperty(object, property);
}
};
GroovySystem.getMetaClassRegistry().setMetaClass(nodelistClass, newMetaClass);
}
|