Example usage for org.w3c.dom Node getBaseURI

List of usage examples for org.w3c.dom Node getBaseURI

Introduction

In this page you can find the example usage for org.w3c.dom Node getBaseURI.

Prototype

public String getBaseURI();

Source Link

Document

The absolute base URI of this node or null if the implementation wasn't able to obtain an absolute URI.

Usage

From source file:org.kisoonlineapp.XthmlCheckTest.java

/**
 * Huebsches String-Format fuer einen Node
 *
 * @param n/* ww w . j  a v a2s . c  o m*/
 * @return human readable repr. of a Node
 */
private String formatNode(Node n) {
    final String SEP = "\n";
    StringBuilder sb = new StringBuilder();
    sb.append(n.getNodeName()).append(SEP).append(n.getNodeValue()).append(SEP).append(n.getNodeType())
            .append(SEP).append(n.getBaseURI()).append(SEP).append(n.getLocalName()).append(SEP)
            .append(n.getNamespaceURI()).append(SEP).append(n.getPrefix()).append(SEP);
    String formatNode = sb.toString();
    return formatNode;
}

From source file:org.miloss.fgsms.common.Utility.java

/**
 * uses a variety of classes to determine the name of the first XML child
 * node of a SOAP:envelope node/* w  w  w  . ja va 2s  .  c  om*/
 *
 * @param xml
 * @return
 */
public static String getActionNameFromXML(String xml) {
    if (stringIsNullOrEmpty(xml)) {
        return "";
    }
    try {
        InputStream is = null;
        Document xmlDocument = null;
        try {
            is = new ByteArrayInputStream(xml.trim().getBytes(Constants.CHARSET));
            xmlDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(is);
        } catch (Exception ex) {
            log.log(Level.DEBUG,
                    "fgsms Utility, error identifying request method by building request message as xml document: "
                            + ex.getLocalizedMessage());
        }
        if (xmlDocument == null) {
            String workingdoc = "";
            try {
                workingdoc = xml.substring(xml.indexOf("<"), xml.lastIndexOf(">"));
            } catch (Exception ex) {
            }
            if (Utility.stringIsNullOrEmpty(workingdoc)) {
                log.log(Level.WARN,
                        "fgsms Utility, error identifying request method by building request message as xml document: this most likely isn't xml");
                return "";
            }

            if (Utility.stringIsNullOrEmpty(xml)) // return "";
            {
                is = new ByteArrayInputStream(workingdoc.getBytes(Constants.CHARSET));
            }
            xmlDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(is);

        }

        XPath xpath = XPathFactory.newInstance().newXPath();
        javax.xml.xpath.XPathExpression xp = xpath.compile("/Envelope/Body");
        try {
            DeferredElementImpl j = (DeferredElementImpl) xp.evaluate(xmlDocument, XPathConstants.NODE);

            //org.apache.xerces.dom.DeferredElementImpl j2 = (org.apache.xerces.dom.DeferredElementImpl)
            for (int i = 0; i < j.getChildNodes().getLength(); i++) {
                if (!j.getChildNodes().item(i).getNodeName().equalsIgnoreCase("#text")) {
                    log.log(Level.DEBUG,
                            "Found action via com.sun.org.apache.xerces.internal.dom.DeferredElementImpl");
                    return j.getChildNodes().item(i).getNodeName();

                }
            }
        } catch (NoClassDefFoundError c) {
            log.log(Level.DEBUG,
                    "couldn't find com.sun.org.apache.xerces.internal.dom.DeferredElementImpl, trying alternate method...");
        } catch (ClassCastException e) {
            log.log(Level.DEBUG,
                    "couldn't find com.sun.org.apache.xerces.internal.dom.DeferredElementImpl, trying alternate method...");
        } catch (Exception ex) {
            log.log(Level.DEBUG,
                    "error caught using com.sun.org.apache.xerces.internal.dom.DeferredElementImpl, trying alternate method...",
                    ex);
        }

        try {
            org.apache.xerces.dom.DeferredElementImpl j = (org.apache.xerces.dom.DeferredElementImpl) xp
                    .evaluate(xmlDocument, XPathConstants.NODE);
            for (int i = 0; i < j.getChildNodes().getLength(); i++) {
                if (!j.getChildNodes().item(i).getNodeName().equalsIgnoreCase("#text")) {
                    Node n = j.getChildNodes().item(i);
                    String s = n.getNodeName();
                    s = n.getNodeValue();
                    s = n.getNodeValue();
                    s = n.getBaseURI();
                    log.log(Level.DEBUG, "Found action via org.apache.xerces.dom.DeferredElementImpl");
                    return j.getChildNodes().item(i).getNodeName();

                }
            }

        } catch (NoClassDefFoundError c) {
            log.log(Level.DEBUG,
                    "couldn't find org.apache.xerces.dom.DeferredElementImpl, giving up determining action");
        } catch (Exception ex) {
            log.log(Level.DEBUG, "error caught using org.apache.xerces.dom.DeferredElementImpl", ex);
        }

    } catch (Exception ex) {
        log.log(Level.DEBUG,
                "fgsms Utility, error identifying request method from xml, defaulted to urn:undeterminable",
                ex);
    }
    return "";
}

From source file:ua.utility.kfsdbupgrade.MaintainableXMLConversionServiceImpl.java

/**
 * If a mapping for the <code>node</code> class exists in
 * {@link #classNameRuleMap}, the given <code>node</code> is renamed. Then,
 * if the <code>node</code> is a valid class, it is passed to the
 * {@link #transformNode(Document, Node, Class, Map)} method first with the
 * {@link #classPropertyRuleMap} value for the classname, then with the
 * {@link #classPropertyRuleMap} value for "<code>*</code>".
 * /*from  w  w  w  .j  a va 2  s  .co  m*/
 * @param document
 *            Root level {@link Document}
 * @param node
 *            {@link Node} to transform
 * @throws ClassNotFoundException
 * @throws XPathExpressionException
 * @throws IllegalAccessException
 * @throws InvocationTargetException
 * @throws NoSuchMethodException
 * @throws InstantiationException
 */
private void transformClassNode(Document document, Node node)
        throws ClassNotFoundException, XPathExpressionException, IllegalAccessException,
        InvocationTargetException, NoSuchMethodException, InstantiationException {

    String className = node.getNodeName();
    LOGGER.trace("Transforming class node for : " + node.getBaseURI() + "/" + className);
    if (this.classNameRuleMap.containsKey(className)) {
        String newClassName = this.classNameRuleMap.get(className);
        document.renameNode(node, null, newClassName);
        className = newClassName;
    }

    if (isValidClass(className)) {
        Class<?> dataObjectClass = Class.forName(className);

        if (classPropertyRuleMap.containsKey(className)) {
            transformNode(document, node, dataObjectClass, classPropertyRuleMap.get(className));
        }

        transformNode(document, node, dataObjectClass, classPropertyRuleMap.get("*"));
    } else if (xPathTransformationRulesMap.containsKey(className)) {
        //specific transformations for docs whose classes cannot be instantiated like in the above code.
        xPathApplyTransformations(document, className, xPathTransformationRulesMap.get(className));
    }
}

From source file:ua.utility.kfsdbupgrade.MaintainableXMLConversionServiceImpl.java

/**
 * For each child of <code>node</code>
 * /*from w  w  w .j av  a2 s .co m*/
 * @param document
 * @param node
 * @param currentClass
 * @param propertyMappings
 * @throws ClassNotFoundException
 * @throws XPathExpressionException
 * @throws IllegalAccessException
 * @throws InvocationTargetException
 * @throws NoSuchMethodException
 * @throws InstantiationException
 */
private void transformNode(Document document, Node node, Class<?> currentClass,
        Map<String, String> propertyMappings) throws ClassNotFoundException, XPathExpressionException,
        IllegalAccessException, InvocationTargetException, NoSuchMethodException, InstantiationException {
    LOGGER.trace("Transforming node: " + node.getBaseURI() + "/" + node.getNodeName());
    for (Node childNode = node.getFirstChild(); childNode != null;) {
        Node nextChild = childNode.getNextSibling();
        String propertyName = childNode.getNodeName();
        if (childNode.hasAttributes()) {
            XPath xpath = XPathFactory.newInstance().newXPath();
            Node serializationAttribute = childNode.getAttributes().getNamedItem(SERIALIZATION_ATTRIBUTE);
            if (serializationAttribute != null
                    && StringUtils.equals(serializationAttribute.getNodeValue(), "custom")) {
                Node classAttribute = childNode.getAttributes().getNamedItem(CLASS_ATTRIBUTE);
                if (classAttribute != null && StringUtils.equals(classAttribute.getNodeValue(),
                        "org.kuali.rice.kns.util.TypedArrayList")) {
                    handleTypedArrayList(document, xpath, (Element) childNode);
                } else if (isTargetEffortCertificationReportPositionsNode(childNode)) {
                    // Need to skip over ECRD positions list due to needing serialization attr
                    // that otherwise was getting stripped on line 924. This also avoids a child
                    // list node from getting errantly pruned off ECRD doc types
                    deleteAllNoneListProxyChildren(childNode);
                } else {
                    ((Element) childNode).removeAttribute(SERIALIZATION_ATTRIBUTE);

                    XPathExpression mapContentsExpression = xpath.compile("//" + propertyName + "/map/string");
                    NodeList mapContents = (NodeList) mapContentsExpression.evaluate(childNode,
                            XPathConstants.NODESET);
                    List<Node> nodesToAdd = new ArrayList<Node>();
                    if (mapContents.getLength() > 0 && mapContents.getLength() % 2 == 0) {
                        for (int i = 0; i < mapContents.getLength(); i++) {
                            Node keyNode = mapContents.item(i);
                            Node valueNode = mapContents.item(++i);
                            Node entryNode = document.createElement("entry");
                            entryNode.appendChild(keyNode);
                            entryNode.appendChild(valueNode);
                            nodesToAdd.add(entryNode);
                        }
                    }
                    for (Node removeNode = childNode.getFirstChild(); removeNode != null;) {
                        Node nextRemoveNode = removeNode.getNextSibling();
                        childNode.removeChild(removeNode);
                        removeNode = nextRemoveNode;
                    }
                    for (Node nodeToAdd : nodesToAdd) {
                        childNode.appendChild(nodeToAdd);
                    }
                }
            }
        }
        if (propertyMappings != null && propertyMappings.containsKey(propertyName)) {
            String newPropertyName = propertyMappings.get(propertyName);
            if (StringUtils.isNotBlank(newPropertyName)) {
                document.renameNode(childNode, null, newPropertyName);
                propertyName = newPropertyName;
            } else {
                // If there is no replacement name then the element needs
                // to be removed and skip all other processing
                node.removeChild(childNode);
                childNode = nextChild;
                continue;
            }
        }

        if (dateRuleMap != null && dateRuleMap.containsKey(propertyName)) {
            String newDateValue = dateRuleMap.get(propertyName);
            if (StringUtils.isNotBlank(newDateValue)) {
                if (childNode.getTextContent().length() == 10) {
                    childNode.setTextContent(childNode.getTextContent() + " " + newDateValue);

                }
            }
        }

        if ((currentClass != null) && isValidClass(currentClass)) {
            if (childNode.hasChildNodes() && !(Collection.class.isAssignableFrom(currentClass)
                    || Map.class.isAssignableFrom(currentClass))) {
                PropertyClassKey key = new PropertyClassKey(currentClass, propertyName);
                Optional<Class<?>> propertyClass = propertyClassCache.getUnchecked(key);
                if (propertyClass.isPresent()
                        && classPropertyRuleMap.containsKey(propertyClass.get().getName())) {
                    transformNode(document, childNode, propertyClass.get(),
                            this.classPropertyRuleMap.get(propertyClass.get().getName()));
                }
                transformNode(document, childNode, propertyClass.orNull(), classPropertyRuleMap.get("*"));
            }
        }
        childNode = nextChild;
    }
}