|
Groovy example source code file (DOMCategory.java)
The Groovy DOMCategory.java source code
/*
* Copyright 2003-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package groovy.xml.dom;
import groovy.lang.Closure;
import groovy.lang.GroovyRuntimeException;
import groovy.lang.IntRange;
import groovy.xml.DOMBuilder;
import groovy.xml.QName;
import org.codehaus.groovy.runtime.InvokerHelper;
import org.codehaus.groovy.runtime.XmlGroovyMethods;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* @author sam
* @author paulk
*/
public class DOMCategory {
private static boolean trimWhitespace = true;
public static Object get(Element element, String elementName) {
return xgetAt(element, elementName);
}
public static Object get(NodeList nodeList, String elementName) {
if (nodeList instanceof Element) {
// things like com.sun.org.apache.xerces.internal.dom.DeferredElementNSImpl
// do implement Element, NodeList and Node. But here we prefer element,
// so we force the usage of Element. Without this DOMCategoryTest may fail
// in strange ways
return xgetAt((Element)nodeList, elementName);
} else {
return xgetAt(nodeList, elementName);
}
}
public static Object get(NamedNodeMap nodeMap, String elementName) {
return xgetAt(nodeMap, elementName);
}
private static Object xgetAt(Element element, String elementName) {
if ("..".equals(elementName)) {
return parent(element);
}
if ("**".equals(elementName)) {
return depthFirst(element);
}
if (elementName.startsWith("@")) {
return element.getAttribute(elementName.substring(1));
}
return getChildElements(element, elementName);
}
private static Object xgetAt(NodeList nodeList, String elementName) {
List results = new ArrayList();
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if (node instanceof Element) {
addResult(results, get((Element)node, elementName));
}
}
if (elementName.startsWith("@")) {
return results;
}
return new NodeListsHolder(results);
}
public static NamedNodeMap attributes(Element element) {
return element.getAttributes();
}
private static String xgetAt(NamedNodeMap namedNodeMap, String elementName) {
Attr a = (Attr) namedNodeMap.getNamedItem(elementName);
return a.getValue();
}
public static int size(NamedNodeMap namedNodeMap) {
return namedNodeMap.getLength();
}
public static Node getAt(Node o, int i) {
return nodeGetAt(o, i);
}
public static Node getAt(NodeListsHolder o, int i) {
return nodeGetAt(o, i);
}
public static Node getAt(NodesHolder o, int i) {
return nodeGetAt(o, i);
}
public static NodeList getAt(Node o, IntRange r) {
return nodesGetAt(o, r);
}
public static NodeList getAt(NodeListsHolder o, IntRange r) {
return nodesGetAt(o, r);
}
public static NodeList getAt(NodesHolder o, IntRange r) {
return nodesGetAt(o, r);
}
private static Node nodeGetAt(Object o, int i) {
if (o instanceof Element) {
Node n = xgetAt((Element)o, i);
if (n != null) return n;
}
if (o instanceof NodeList) {
return xgetAt((NodeList)o, i);
}
return null;
}
private static NodeList nodesGetAt(Object o, IntRange r) {
if (o instanceof Element) {
NodeList n = xgetAt((Element)o, r);
if (n != null) return n;
}
if (o instanceof NodeList) {
return xgetAt((NodeList)o, r);
}
return null;
}
private static Node xgetAt(Element element, int i) {
if (hasChildElements(element, "*")) {
NodeList nodeList = getChildElements(element, "*");
return xgetAt(nodeList, i);
}
return null;
}
private static Node xgetAt(NodeList nodeList, int i) {
if (i < 0) {
i += nodeList.getLength();
}
if (i >= 0 && i < nodeList.getLength()) {
return nodeList.item(i);
}
return null;
}
private static NodeList xgetAt(Element element, IntRange r) {
if (hasChildElements(element, "*")) {
NodeList nodeList = getChildElements(element, "*");
return xgetAt(nodeList, r);
}
return null;
}
private static NodeList xgetAt(NodeList nodeList, IntRange r) {
int from = r.getFromInt();
int to = r.getToInt();
// If the range is of size 1, then we can use the existing
// xgetAt() that takes an integer index.
if (from == to) return new NodesHolder(Arrays.asList(xgetAt(nodeList, from)));
// Normalise negative indices.
if (from < 0) from = from + nodeList.getLength();
if (to < 0) to = to + nodeList.getLength();
// After normalisation, 'from' may be greater than 'to'. In that
// case, we need to reverse them and make sure the range's 'reverse'
// property is correct.
// TODO We should probably use DefaultGroovyMethodsSupport.subListBorders(),
// but that's protected and unavailable to us.
if (from > to) {
r = r.isReverse() ? new IntRange(to, from) : new IntRange(from, to);
from = r.getFromInt();
to = r.getToInt();
}
// Copy the required nodes into a new list.
List<Node> nodes = new ArrayList
Other Groovy examples (source code examples)Here is a short list of links related to this Groovy DOMCategory.java source code file: |
Other websites by Alvin Alexander:
Life/living in Alaska (OneMansAlaska.com)
How I Sold My Business (HowISoldMyBusiness.com)
Copyright 1998-2011 Alvin Alexander, devdaily.com
All Rights Reserved.