Example usage for org.w3c.dom Element getLocalName

List of usage examples for org.w3c.dom Element getLocalName

Introduction

In this page you can find the example usage for org.w3c.dom Element getLocalName.

Prototype

public String getLocalName();

Source Link

Document

Returns the local part of the qualified name of this node.

Usage

From source file:Main.java

/**
 * recursively transform the prefix and the namespace of a node where getNamespaceURI is NULL
 * @param node the node to transform//from   w  w  w  . ja v  a  2  s . c o  m
 * @param prefix the new prefix
 * @param namespaceuri the new namespace uri
 * @return the new node with NS and prefix changed
 */
static public Node setPrefixNamespace(Node node, String prefix, String namespaceuri) {
    Node dest = null;
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        Element e = (Element) node;
        if (e.getNamespaceURI() == null) {

            Element e2 = node.getOwnerDocument().createElementNS(namespaceuri,
                    (prefix != null ? prefix + ':' + e.getLocalName() : e.getLocalName()));
            NamedNodeMap nodes = e.getAttributes();
            for (int i = 0; i < nodes.getLength(); ++i) {
                Attr att = (Attr) (node.getOwnerDocument().importNode(nodes.item(i), true));
                e2.setAttributeNode(att);
            }
            dest = e2;
        } else {
            dest = node.getOwnerDocument().importNode(node, false);
        }
    } else {
        dest = node.getOwnerDocument().importNode(node, false);
    }

    for (Node c = node.getFirstChild(); c != null; c = c.getNextSibling()) {
        dest.appendChild(setPrefixNamespace(c, prefix, namespaceuri));
    }
    return dest;
}

From source file:Main.java

/**
 * Get the QName of the passed element. If the passed element has no namespace
 * URI, only the tag name is used. Otherwise namespace URI, local name and
 * prefix are used.//ww w . j  av a 2s  .c o m
 *
 * @param aElement
 *        The element to be used. May not be <code>null</code>.
 * @return The created {@link QName}.
 * @since 8.4.1
 */
@Nonnull
public static QName getQName(@Nonnull final Element aElement) {
    final String sNamespaceURI = aElement.getNamespaceURI();
    if (sNamespaceURI == null)
        return new QName(aElement.getTagName());
    return new QName(sNamespaceURI, aElement.getLocalName(), getPrefix(aElement));
}

From source file:org.apache.ftpserver.config.spring.SpringUtil.java

/**
 * Parse specific Spring elements, bean and ref
 * //from  www  . j ava  2  s .  co m
 * @param parent
 *            The element in which we will look for Spring elements
 * @param parserContext
 *            The Spring parser context
 * @param builder
 *            The Spring bean definition builder
 * @return The Spring bean definition
 */
public static Object parseSpringChildElement(final Element parent, final ParserContext parserContext,
        final BeanDefinitionBuilder builder) {
    Element springElm = getChildElement(parent, null, null);

    String ln = springElm.getLocalName();
    if ("bean".equals(ln)) {
        return parserContext.getDelegate().parseBeanDefinitionElement(springElm, builder.getBeanDefinition());
    } else if ("ref".equals(ln)) {
        return parserContext.getDelegate().parsePropertySubElement(springElm, builder.getBeanDefinition());
    } else {
        throw new FtpServerConfigurationException("Unknown spring element " + ln);
    }
}

From source file:org.apache.smscserver.config.spring.SpringUtil.java

/**
 * Parse specific Spring elements, bean and ref
 * /*  ww w .  ja va  2s .  c  o  m*/
 * @param parent
 *            The element in which we will look for Spring elements
 * @param parserContext
 *            The Spring parser context
 * @param builder
 *            The Spring bean definition builder
 * @return The Spring bean definition
 */
public static Object parseSpringChildElement(final Element parent, final ParserContext parserContext,
        final BeanDefinitionBuilder builder) {
    Element springElm = SpringUtil.getChildElement(parent, null, null);

    String ln = springElm.getLocalName();
    if ("bean".equals(ln)) {
        return parserContext.getDelegate().parseBeanDefinitionElement(springElm, builder.getBeanDefinition());
    } else if ("ref".equals(ln)) {
        return parserContext.getDelegate().parsePropertySubElement(springElm, builder.getBeanDefinition());
    } else {
        throw new SmscServerConfigurationException("Unknown spring element " + ln);
    }
}

From source file:Main.java

/**
 * Print SAML Attribute Element and replace its prefix with the input
 * prefix./*from   ww w .ja  v a  2s  .  co  m*/
 * 
 * @param node
 *            A DOM tree Node
 * @param prefix
 *            A String representing the new prefix
 * @return An xml String representation of the DOM tree.
 */
public static String printAttributeValue(Element node, String prefix) {
    if (node == null) {
        return null;
    }

    StringBuffer xml = new StringBuffer(100);
    xml.append('<');
    xml.append(prefix).append(node.getLocalName());
    NamedNodeMap attrs = node.getAttributes();
    int length = attrs.getLength();
    for (int i = 0; i < length; i++) {
        Attr attr = (Attr) attrs.item(i);
        xml.append(' ');
        xml.append(attr.getNodeName());
        xml.append("=\"");
        // xml.append(normalize(attr.getNodeValue()));
        xml.append(attr.getNodeValue());
        xml.append('"');
    }
    xml.append('>');
    NodeList children = node.getChildNodes();
    if (children != null) {
        int len = children.getLength();
        for (int i = 0; i < len; i++) {
            xml.append(print(children.item(i)));
        }
    }
    xml.append("</");
    xml.append(prefix).append(node.getLocalName());
    xml.append('>');

    return xml.toString();

}

From source file:DOMUtils.java

public static void compareNodes(Node expected, Node actual) throws Exception {
    if (expected.getNodeType() != actual.getNodeType()) {
        throw new Exception("Different types of nodes: " + expected + " " + actual);
    }// w  ww.j a  v a 2s  .  co m
    if (expected instanceof Document) {
        Document expectedDoc = (Document) expected;
        Document actualDoc = (Document) actual;
        compareNodes(expectedDoc.getDocumentElement(), actualDoc.getDocumentElement());
    } else if (expected instanceof Element) {
        Element expectedElement = (Element) expected;
        Element actualElement = (Element) actual;

        // compare element names
        if (!expectedElement.getLocalName().equals(actualElement.getLocalName())) {
            throw new Exception("Element names do not match: " + expectedElement.getLocalName() + " "
                    + actualElement.getLocalName());
        }
        // compare element ns
        String expectedNS = expectedElement.getNamespaceURI();
        String actualNS = actualElement.getNamespaceURI();
        if ((expectedNS == null && actualNS != null) || (expectedNS != null && !expectedNS.equals(actualNS))) {
            throw new Exception("Element namespaces names do not match: " + expectedNS + " " + actualNS);
        }

        String elementName = "{" + expectedElement.getNamespaceURI() + "}" + actualElement.getLocalName();

        // compare attributes
        NamedNodeMap expectedAttrs = expectedElement.getAttributes();
        NamedNodeMap actualAttrs = actualElement.getAttributes();
        if (countNonNamespaceAttribures(expectedAttrs) != countNonNamespaceAttribures(actualAttrs)) {
            throw new Exception(elementName + ": Number of attributes do not match up: "
                    + countNonNamespaceAttribures(expectedAttrs) + " "
                    + countNonNamespaceAttribures(actualAttrs));
        }
        for (int i = 0; i < expectedAttrs.getLength(); i++) {
            Attr expectedAttr = (Attr) expectedAttrs.item(i);
            if (expectedAttr.getName().startsWith("xmlns")) {
                continue;
            }
            Attr actualAttr = null;
            if (expectedAttr.getNamespaceURI() == null) {
                actualAttr = (Attr) actualAttrs.getNamedItem(expectedAttr.getName());
            } else {
                actualAttr = (Attr) actualAttrs.getNamedItemNS(expectedAttr.getNamespaceURI(),
                        expectedAttr.getLocalName());
            }
            if (actualAttr == null) {
                throw new Exception(elementName + ": No attribute found:" + expectedAttr);
            }
            if (!expectedAttr.getValue().equals(actualAttr.getValue())) {
                throw new Exception(elementName + ": Attribute values do not match: " + expectedAttr.getValue()
                        + " " + actualAttr.getValue());
            }
        }

        // compare children
        NodeList expectedChildren = expectedElement.getChildNodes();
        NodeList actualChildren = actualElement.getChildNodes();
        if (expectedChildren.getLength() != actualChildren.getLength()) {
            throw new Exception(elementName + ": Number of children do not match up: "
                    + expectedChildren.getLength() + " " + actualChildren.getLength());
        }
        for (int i = 0; i < expectedChildren.getLength(); i++) {
            Node expectedChild = expectedChildren.item(i);
            Node actualChild = actualChildren.item(i);
            compareNodes(expectedChild, actualChild);
        }
    } else if (expected instanceof Text) {
        String expectedData = ((Text) expected).getData().trim();
        String actualData = ((Text) actual).getData().trim();

        if (!expectedData.equals(actualData)) {
            throw new Exception("Text does not match: " + expectedData + " " + actualData);
        }
    }
}

From source file:cz.incad.cdk.RepairVCProcess.java

public static void checkRelsExt(String pid, Document relsExt, FedoraAccess fa) {
    Element descElement = XMLUtils.findElement(relsExt.getDocumentElement(), "Description",
            FedoraNamespaces.RDF_NAMESPACE_URI);
    List<Element> delems = XMLUtils.getElements(descElement);
    for (Element rel : delems) {
        if (rel.getNamespaceURI() != null) {
            if (rel.getNamespaceURI().equals(FedoraNamespaces.RDF_NAMESPACE_URI)
                    && rel.getLocalName().equals("isMemberOfCollection")) {
                Attr resource = rel.getAttributeNodeNS(FedoraNamespaces.RDF_NAMESPACE_URI, "resource");
                if (resource != null) {
                    String value = resource.getValue();
                    if (value.startsWith(PIDParser.INFO_FEDORA_PREFIX)) {
                        try {
                            PIDParser pars = new PIDParser(value);
                            pars.disseminationURI();
                        } catch (LexerException e) {
                            LOGGER.log(Level.SEVERE, e.getMessage(), e);
                            //repair(pid, relsExt, fa, resource, value);
                        }/* ww  w  .java 2 s . c om*/
                    } else {
                        repair(pid, relsExt, fa, resource, value);
                    }
                }
            }
        }
    }
}

From source file:Main.java

/**
 * Returns the element name of the given element.
 * @param element/* w w  w .ja v  a  2  s  .com*/
 * @return String
 */
public static String getElementName(Element element) {
    // When loading from disk the local name will be setup correctly, but if the local name
    // is fetched directly after calling Document.createElement() then the value will be null.
    // See the JavaDoc for more info.  To workaround this, use the complete node name.
    String elemName = element.getLocalName();
    if (elemName == null) {
        elemName = element.getNodeName();
    }
    return elemName;
}

From source file:org.apache.ftpserver.config.spring.SpringUtil.java

/**
 * Get the first child element matching the local name and namespace
 * //from   w  w  w  . j  av a 2s .c  om
 * @param parent
 *            The element for which to locate the child
 * @param ns
 *            The namespace to match, or null for any namespace
 * @param localName
 *            The local name to match, or null for any local name
 * @return The first child matching the criteria
 */
public static Element getChildElement(final Element parent, final String ns, final String localName) {
    List<Element> elements = getChildElements(parent);

    for (Element element : elements) {
        if ((ns == null || ns.equals(element.getNamespaceURI())
                && (localName == null || localName.equals(element.getLocalName())))) {
            return element;
        }
    }

    return null;
}

From source file:org.apache.ftpserver.config.spring.SpringUtil.java

/**
 * Get the text context of first child element matching the local name and
 * namespace//from  w ww .  j  a v a 2s  .co  m
 * 
 * @param parent
 *            The element for which to locate the child
 * @param ns
 *            The namespace to match, or null for any namespace
 * @param localName
 *            The local name to match, or null for any local name
 * @return The text content of the first child matching the criteria or null
 *         if element not found
 */
public static String getChildElementText(final Element parent, final String ns, final String localName) {
    List<Element> elements = getChildElements(parent);

    for (Element element : elements) {
        if ((ns == null || ns.equals(element.getNamespaceURI())
                && (localName == null || localName.equals(element.getLocalName())))) {
            return DomUtils.getTextValue(element);
        }
    }

    return null;
}