All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.crabshue.commons.xpath.saxon.NodeInfoUtils Maven / Gradle / Ivy

package com.crabshue.commons.xpath.saxon;

import java.math.BigInteger;

import org.apache.commons.lang3.StringUtils;
import org.w3c.dom.Node;

import net.sf.saxon.dom.NodeOverNodeInfo;
import net.sf.saxon.om.NodeInfo;
import net.sf.saxon.type.Type;

/**
 * Utility class for {@link NodeInfo} (Saxon) manipulations.
 *
 * @author vinh
 */
public class NodeInfoUtils {

    /**
     * Convert a {@link NodeInfo} to {@link Node}
     *
     * @param nodeInfo the NodeInfo object
     * @return the converted Node.
     */
    public static Node convertToNode(final NodeInfo nodeInfo) {
        return NodeOverNodeInfo.wrap(nodeInfo);
    }

    /**
     * Resolve a XML elements to its {@link String} value.
     *
     * @param element the XML element.
     * @return the {@link String} value.
     */
    public static String extractValue(final Object element) {
        String ret = null;
        if (element instanceof NodeInfo) {
            switch (((NodeInfo) element).getNodeKind()) {
                case Type.WHITESPACE_TEXT:
                    break;
                case Type.ELEMENT:
                case Type.ITEM:
                case Type.ATTRIBUTE:
                case Type.NODE:
                case Type.TEXT:
                    String text = ((NodeInfo) element).getStringValue();
                    if (StringUtils.isNotBlank(text)) {
                        ret = StringUtils.normalizeSpace(text);
                    }
                    break;
                default:
                    throw new IllegalArgumentException("Unexpected element type from xpath");
            }
        } else if (element instanceof String) {
            ret = StringUtils.normalizeSpace((String) element);
        } else if (element instanceof BigInteger) {
            ret = String.valueOf(element);
        } else {
            throw new IllegalArgumentException("Unforeseen xpath element " + element.getClass());
        }
        return ret;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy