Example usage for org.w3c.dom Element getAttributes

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

Introduction

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

Prototype

public NamedNodeMap getAttributes();

Source Link

Document

A NamedNodeMap containing the attributes of this node (if it is an Element) or null otherwise.

Usage

From source file:com.amalto.core.storage.hibernate.DefaultStorageClassLoader.java

private static void addProperty(Document document, Node sessionFactoryElement, String propertyName,
        String propertyValue) {//from   ww w  . j  av a2 s  .  co m
    Element property = document.createElement("property"); //$NON-NLS-1$
    Attr name = document.createAttribute("name"); //$NON-NLS-1$
    name.setValue(propertyName);
    property.getAttributes().setNamedItem(name);
    property.appendChild(document.createTextNode(propertyValue));
    sessionFactoryElement.appendChild(property);
}

From source file:Main.java

public static String logElement(Element El, int level) {

    String Es = "";
    String indentText = "  ";
    String addIndT = "";

    // add indent
    int ind = 0;//  ww w .  j  a v a 2s  .  c o m
    while (ind < level) {
        addIndT = addIndT + indentText;
        ind++;

    }
    String name = El.getNodeName();
    Es = "\n" + addIndT + "<" + name + " ";
    // Attribs
    NamedNodeMap namedNodeMap = El.getAttributes();
    StringBuilder sb = new StringBuilder(Es);
    for (int i = 0; i < namedNodeMap.getLength(); i++) {
        Attr att = (Attr) namedNodeMap.item(i);
        sb.append(" " + Es + att.getName() + "=\"" + att.getNodeValue() + "\" ");
        // Es = " " + Es + att.getName() + "=\"" + att.getNodeValue() +
        // "\" ";
    }
    sb.append(">");
    // Es = Es + ">";
    Es = sb.toString();
    NodeList pl = El.getChildNodes();
    int index = pl.getLength();
    // text nodes
    if (index > 0) {
        int i = 0;
        while (i < index) {
            Node DomNode = pl.item(i);
            if ((DomNode.getNodeType()) == org.w3c.dom.Node.TEXT_NODE) {
                String Etext = DomNode.getNodeValue();
                Es = Es + "\n " + addIndT + addIndT + Etext;
            }
            i++;
        }
    }
    // Child Elements
    if (index > 0) {
        level++;
        int i = 0;
        while (i < index) {
            Node DomNode = pl.item(i);
            if ((DomNode.getNodeType()) == org.w3c.dom.Node.ELEMENT_NODE) {
                Element el = (Element) DomNode;
                Es = Es + logElement(el, level);
            }
            i++;
        }
    }
    Es = Es + "\n" + addIndT + "</" + name + ">";

    return Es;
}

From source file:org.jasypt.spring31.xml.encryption.DigesterConfigBeanDefinitionParser.java

private static Class<?> computeConfigClass(final Element element) {

    boolean isSimpleConfig = false;
    boolean isStringConfig = false;
    boolean isEnvironmentConfig = false;
    boolean isStringEnvironmentConfig = false;

    final NamedNodeMap attributesMap = element.getAttributes();
    final int attributesLen = attributesMap.getLength();
    for (int i = 0; i < attributesLen; i++) {
        final Node attribute = attributesMap.item(i);
        final String attributeName = attribute.getNodeName();
        if (!isSimpleConfig && PARAMS_SIMPLE.contains(attributeName)) {
            isSimpleConfig = true;//  w w  w . j a v a2s.  c o m
        }
        if (!isStringConfig && PARAMS_STRING.contains(attributeName)) {
            isStringConfig = true;
        }
        if (!isEnvironmentConfig && PARAMS_ENVIRONMENT.contains(attributeName)) {
            isEnvironmentConfig = true;
        }
        if (!isStringEnvironmentConfig && PARAMS_STRING_ENVIRONMENT.contains(attributeName)) {
            isStringEnvironmentConfig = true;
        }
    }

    if (isStringEnvironmentConfig || (isEnvironmentConfig && isStringConfig)) {
        return EnvironmentStringDigesterConfig.class;
    }
    if (isEnvironmentConfig) {
        return EnvironmentDigesterConfig.class;
    }
    if (isStringConfig) {
        return SimpleStringDigesterConfig.class;
    }
    return SimpleDigesterConfig.class;

}

From source file:Main.java

public static String logElement(final Element elem, final int level) {

    String estr = "";
    String indentText = "  ";
    String addIndT = "";

    // add indent
    int ind = 0;//from   w w w.  ja  v  a2s  .  com
    while (ind < level) {
        addIndT = addIndT + indentText;
        ind++;

    }
    String name = elem.getNodeName();
    estr = "\n" + addIndT + "<" + name + " ";
    // Attribs
    NamedNodeMap namedNodeMap = elem.getAttributes();
    StringBuilder sb = new StringBuilder(estr);
    for (int i = 0; i < namedNodeMap.getLength(); i++) {
        Attr att = (Attr) namedNodeMap.item(i);
        sb.append(" " + estr + att.getName() + "=\"" + att.getNodeValue() + "\" ");
    }
    sb.append(">");
    estr = sb.toString();
    NodeList pl = elem.getChildNodes();
    int index = pl.getLength();
    // text nodes
    if (index > 0) {
        int i = 0;
        while (i < index) {
            Node domNode = pl.item(i);
            if ((domNode.getNodeType()) == org.w3c.dom.Node.TEXT_NODE) {
                String Etext = domNode.getNodeValue();
                estr = estr + "\n " + addIndT + addIndT + Etext;
            }
            i++;
        }
    }
    // Child Elements
    if (index > 0) {
        int i = 0;
        while (i < index) {
            Node domNode = pl.item(i);
            if ((domNode.getNodeType()) == org.w3c.dom.Node.ELEMENT_NODE) {
                Element el = (Element) domNode;
                estr = estr + logElement(el, level + 1);
            }
            i++;
        }
    }
    estr = estr + "\n" + addIndT + "</" + name + ">";

    return estr;
}

From source file:Main.java

public static boolean validate(Element ele, String[] requiredAttributes, String[] optionalAttributes,
        String[] requiredElements, String[] optionalElements) {

    boolean[] foundAttributes = new boolean[requiredAttributes.length];

    for (int i = 0; i < foundAttributes.length; i++) {
        foundAttributes[i] = false;/* w  ww  .j  a v a2  s . c  o m*/
    }

    for (int i = 0; i < ele.getAttributes().getLength(); i++) {
        Node n = ele.getAttributes().item(i);

        boolean found = false;

        for (int j = 0; j < requiredAttributes.length; j++) {
            if (requiredAttributes[j].equalsIgnoreCase(n.getNodeName())) {
                foundAttributes[j] = true;
                found = true;
                break;
            }
        }

        if (!found) {
            for (String c : optionalAttributes) {
                if (n.getNodeName().equalsIgnoreCase(c)) {
                    found = true;
                    break;
                }
            }
        }

        // If we've got this far and nothings been found then there must have been an unexpected attribute
        if (!found) {
            throw new IllegalArgumentException("Error validating XML Element: " + ele.getNodeName()
                    + "; Found unexpected attribute: " + n.getNodeName());
        }
    }

    // Check all required attributes were found
    for (int i = 0; i < foundAttributes.length; i++) {
        if (foundAttributes[i] == false) {
            throw new IllegalArgumentException("Error validating XML Element: " + ele.getNodeName()
                    + "; Failed to find expected attribute or child element named: " + requiredAttributes[i]);
        }
    }

    boolean[] foundElements = new boolean[requiredElements.length];

    for (int i = 0; i < foundElements.length; i++) {
        foundElements[i] = false;
    }

    for (int i = 0; i < ele.getChildNodes().getLength(); i++) {
        Node n = ele.getChildNodes().item(i);

        boolean found = false;

        for (int j = 0; j < requiredElements.length; j++) {
            if (requiredElements[j].equalsIgnoreCase(n.getNodeName())) {
                foundElements[j] = true;
                found = true;
                break;
            }
        }

        if (!found) {
            for (String c : optionalElements) {
                if (n.getNodeName().equalsIgnoreCase(c)) {
                    found = true;
                    break;
                }
            }
        }

        // If we've got this far and nothings been found then there must have been an unexpected element
        if (!found) {

            if (n.getNodeName().startsWith("#") || n.getNodeName().startsWith("<!--")) {
                // Then we just ignore these elements
            } else {
                throw new IllegalArgumentException("Error validating XML Element: " + ele.getNodeName()
                        + "; Found unexpected child node: " + n.getNodeName());
            }
        }
    }

    // Check all required child elements were found
    for (int i = 0; i < foundElements.length; i++) {
        if (foundElements[i] == false) {
            throw new IllegalArgumentException("Error validating XML Element: " + ele.getNodeName()
                    + "; Failed to find expected attribute or child element named: " + requiredElements[i]);
        }
    }

    return true;
}

From source file:com.jaspersoft.jasperserver.ws.xml.Unmarshaller.java

public static ResourceDescriptor readResourceDescriptor(Element rpNode) {

    ResourceDescriptor rd = new ResourceDescriptor();
    NamedNodeMap nodeAttributes;/* ww  w  .  ja v  a2s. c  o  m*/

    if (rpNode instanceof Element) {
        nodeAttributes = rpNode.getAttributes();
    } else {
        // the current implementation currently rely on rpNode being an Element
        throw new IllegalStateException("repNode is not an Element");
    }

    if (nodeAttributes.getNamedItem("name") != null)
        rd.setName(nodeAttributes.getNamedItem("name").getNodeValue());
    if (nodeAttributes.getNamedItem("wsType") != null)
        rd.setWsType(nodeAttributes.getNamedItem("wsType").getNodeValue());
    if (nodeAttributes.getNamedItem(ResourceDescriptor.REFERENCE_TYPE) != null)
        rd.setReferenceType(nodeAttributes.getNamedItem(ResourceDescriptor.REFERENCE_TYPE).getNodeValue());
    if (nodeAttributes.getNamedItem("uriString") != null)
        rd.setUriString(nodeAttributes.getNamedItem("uriString").getNodeValue());
    if (nodeAttributes.getNamedItem("isNew") != null)
        rd.setIsNew(nodeAttributes.getNamedItem("isNew").getNodeValue().equals("true"));

    NodeList childsOfChild = rpNode.getChildNodes();
    for (int c_count = 0; c_count < childsOfChild.getLength(); c_count++) {
        Node child_child = (Node) childsOfChild.item(c_count);

        if (child_child.getNodeType() == Node.ELEMENT_NODE && child_child.getNodeName().equals("label")) {
            rd.setLabel(readPCDATA(child_child));
        } else if (child_child.getNodeType() == Node.ELEMENT_NODE
                && child_child.getNodeName().equals("description")) {
            rd.setDescription(readPCDATA(child_child));
        } else if (child_child.getNodeType() == Node.ELEMENT_NODE
                && child_child.getNodeName().equals("creationDate")) {
            Date creationDate = readCreationDate(child_child);
            if (creationDate != null) {
                rd.setCreationDate(creationDate);
            }
        } else if (child_child.getNodeType() == Node.ELEMENT_NODE
                && child_child.getNodeName().equals("resourceProperty")) {
            rd.setResourceProperty(readResourceProperty(child_child));
        } else if (child_child.getNodeType() == Node.ELEMENT_NODE
                && child_child.getNodeName().equals("resourceDescriptor")) {
            rd.getChildren().add(readResourceDescriptor((Element) child_child));
        } else if (child_child.getNodeType() == Node.ELEMENT_NODE
                && child_child.getNodeName().equals("parameter")) {
            rd.getParameters().add(readResourceParameter(child_child));
        }
    }

    return rd;
}

From source file:Main.java

/**
 * Performs the actual recursive dumping of a DOM tree to a given
 * <CODE>PrintStream</CODE>. Note that dump is intended to be a detailed
 * debugging aid rather than pretty to look at. 
 * /*w w w . ja v a 2 s  .co m*/
 * @param    out            The <CODE>PrintStream</CODE> to write to.
 * @param    node         The <CODE>Node</CODE> under consideration.
 * @param    indent         The level of indentation.
 * @see      #dump(Node)
 * @see      #dump(PrintStream, Node)
 * @since   TFP 1.0
 */
private static void doDump(PrintStream out, final Node node, int indent) {
    if (node != null) {
        for (int index = 0; index < indent; ++index)
            out.write(' ');

        switch (node.getNodeType()) {
        case Node.DOCUMENT_NODE: {
            Document document = (Document) node;

            out.println("DOCUMENT:");

            doDump(out, document.getDoctype(), indent + 1);
            doDump(out, document.getDocumentElement(), indent + 1);
            break;
        }

        case Node.DOCUMENT_TYPE_NODE: {
            DocumentType type = (DocumentType) node;

            out.println("DOCTYPE: [" + "name=" + format(type.getName()) + "," + "publicId="
                    + format(type.getPublicId()) + "," + "systemId=" + format(type.getSystemId()) + "]");
            break;
        }

        case Node.ELEMENT_NODE: {
            Element element = (Element) node;

            out.println("ELEMENT: [" + "ns=" + format(element.getNamespaceURI()) + "," + "name="
                    + format(element.getLocalName()) + "]");

            NamedNodeMap attrs = element.getAttributes();

            for (int index = 0; index < attrs.getLength(); ++index)
                doDump(out, attrs.item(index), indent + 1);

            for (Node child = element.getFirstChild(); child != null;) {
                doDump(out, child, indent + 1);
                child = child.getNextSibling();
            }
            break;
        }
        case Node.ATTRIBUTE_NODE: {
            Attr attr = (Attr) node;

            out.println("ATTRIBUTE: [" + "ns=" + format(attr.getNamespaceURI()) + "," + "prefix="
                    + format(attr.getPrefix()) + "," + "name=" + format(attr.getLocalName()) + "," + "value="
                    + format(attr.getNodeValue()) + "]");
            break;
        }

        case Node.TEXT_NODE: {
            Text text = (Text) node;

            out.println("TEXT: [" + format(text.getNodeValue()) + "]");

            for (Node child = text.getFirstChild(); child != null;) {
                doDump(out, child, indent + 1);
                child = child.getNextSibling();
            }
            break;
        }

        case Node.CDATA_SECTION_NODE: {
            CDATASection data = (CDATASection) node;

            out.println("CDATA: [" + format(data.getNodeValue()) + "]");
            break;
        }

        case Node.COMMENT_NODE: {
            Comment comm = (Comment) node;

            out.println("COMMENT: [" + format(comm.getNodeValue()) + "]");
            break;
        }

        default:
            out.println("UNKNOWN: [type=" + node.getNodeType() + "]");
            break;
        }
    }
}

From source file:Main.java

/**
 * Performs the actual recursive dumping of a DOM tree to a given
 * <CODE>PrintWriter</CODE>. Note that dump is intended to be a detailed
 * debugging aid rather than pretty to look at. 
 * /*w w w .  jav a2  s .  c om*/
 * @param    out            The <CODE>PrintWriter</CODE> to write to.
 * @param    node         The <CODE>Node</CODE> under consideration.
 * @param    indent         The level of indentation.
 * @see      #dump(PrintWriter, Node)
 * @since   TFP 1.0
 */
private static void doDump(PrintWriter out, final Node node, int indent) {
    if (node != null) {
        for (int index = 0; index < indent; ++index)
            out.write(' ');

        switch (node.getNodeType()) {
        case Node.DOCUMENT_NODE: {
            Document document = (Document) node;

            out.println("DOCUMENT:");

            doDump(out, document.getDoctype(), indent + 1);
            doDump(out, document.getDocumentElement(), indent + 1);
            break;
        }

        case Node.DOCUMENT_TYPE_NODE: {
            DocumentType type = (DocumentType) node;

            out.println("DOCTYPE: [" + "name=" + format(type.getName()) + "," + "publicId="
                    + format(type.getPublicId()) + "," + "systemId=" + format(type.getSystemId()) + "]");
            break;
        }

        case Node.ELEMENT_NODE: {
            Element element = (Element) node;

            out.println("ELEMENT: [" + "ns=" + format(element.getNamespaceURI()) + "," + "name="
                    + format(element.getLocalName()) + "]");

            NamedNodeMap attrs = element.getAttributes();

            for (int index = 0; index < attrs.getLength(); ++index)
                doDump(out, attrs.item(index), indent + 1);

            for (Node child = element.getFirstChild(); child != null;) {
                doDump(out, child, indent + 1);
                child = child.getNextSibling();
            }
            break;
        }
        case Node.ATTRIBUTE_NODE: {
            Attr attr = (Attr) node;

            out.println("ATTRIBUTE: [" + "ns=" + format(attr.getNamespaceURI()) + "," + "prefix="
                    + format(attr.getPrefix()) + "," + "name=" + format(attr.getLocalName()) + "," + "value="
                    + format(attr.getNodeValue()) + "]");
            break;
        }

        case Node.TEXT_NODE: {
            Text text = (Text) node;

            out.println("TEXT: [" + format(text.getNodeValue()) + "]");

            for (Node child = text.getFirstChild(); child != null;) {
                doDump(out, child, indent + 1);
                child = child.getNextSibling();
            }
            break;
        }

        case Node.CDATA_SECTION_NODE: {
            CDATASection data = (CDATASection) node;

            out.println("CDATA: [" + format(data.getNodeValue()) + "]");
            break;
        }

        case Node.COMMENT_NODE: {
            Comment comm = (Comment) node;

            out.println("COMMENT: [" + format(comm.getNodeValue()) + "]");
            break;
        }

        default:
            out.println("UNKNOWN: [type=" + node.getNodeType() + "]");
            break;
        }
    }
}

From source file:com.git.original.common.config.XMLFileConfigDocument.java

/**
 * XML??//ww w.  j  a v a  2 s .c o  m
 * 
 * @param elem
 *            XML
 * @return ?
 */
static ConfigNode convertElement(Element elem) {
    if (elem == null) {
        return null;
    }

    ConfigNode cn = new ConfigNode(elem.getTagName(), null);

    NamedNodeMap attrNodeMap = elem.getAttributes();
    if (attrNodeMap != null) {
        for (int i = 0; i < attrNodeMap.getLength(); i++) {
            Node node = attrNodeMap.item(i);
            cn.addAttribute(node.getNodeName(), node.getNodeValue());
        }
    }

    NodeList nodeList = elem.getChildNodes();
    if (nodeList != null && nodeList.getLength() > 0) {
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);

            switch (node.getNodeType()) {
            case Node.ATTRIBUTE_NODE:
                cn.addAttribute(node.getNodeName(), node.getNodeValue());
                break;
            case Node.ELEMENT_NODE:
                ConfigNode child = convertElement((Element) node);
                cn.addChild(child);
                break;
            case Node.TEXT_NODE:
                cn.value = node.getNodeValue();
                break;
            default:
                continue;
            }
        }
    }

    return cn;
}

From source file:Main.java

protected static String nodeToString(Node node, Set<String> parentPrefixes, String namespaceURI)
        throws Exception {
    StringBuilder b = new StringBuilder();

    if (node == null) {
        return "";
    }/*from  w ww  . j  ava 2s .c  o  m*/

    if (node instanceof Element) {
        Element element = (Element) node;
        b.append("<");
        b.append(element.getNodeName());

        Map<String, String> thisLevelPrefixes = new HashMap();
        if (element.getPrefix() != null && !parentPrefixes.contains(element.getPrefix())) {
            thisLevelPrefixes.put(element.getPrefix(), element.getNamespaceURI());
        }

        if (element.hasAttributes()) {
            NamedNodeMap map = element.getAttributes();
            for (int i = 0; i < map.getLength(); i++) {
                Node attr = map.item(i);
                if (attr.getNodeName().startsWith("xmlns"))
                    continue;
                if (attr.getPrefix() != null && !parentPrefixes.contains(attr.getPrefix())) {
                    thisLevelPrefixes.put(attr.getPrefix(), element.getNamespaceURI());
                }
                b.append(" ");
                b.append(attr.getNodeName());
                b.append("=\"");
                b.append(attr.getNodeValue());
                b.append("\"");
            }
        }

        if (namespaceURI != null && !thisLevelPrefixes.containsValue(namespaceURI)
                && !namespaceURI.equals(element.getParentNode().getNamespaceURI())) {
            b.append(" xmlns=\"").append(namespaceURI).append("\"");
        }

        for (Map.Entry<String, String> entry : thisLevelPrefixes.entrySet()) {
            b.append(" xmlns:").append(entry.getKey()).append("=\"").append(entry.getValue()).append("\"");
            parentPrefixes.add(entry.getKey());
        }

        NodeList children = element.getChildNodes();
        boolean hasOnlyAttributes = true;
        for (int i = 0; i < children.getLength(); i++) {
            Node child = children.item(i);
            if (child.getNodeType() != Node.ATTRIBUTE_NODE) {
                hasOnlyAttributes = false;
                break;
            }
        }
        if (!hasOnlyAttributes) {
            b.append(">");
            for (int i = 0; i < children.getLength(); i++) {
                b.append(nodeToString(children.item(i), parentPrefixes, children.item(i).getNamespaceURI()));
            }
            b.append("</");
            b.append(element.getNodeName());
            b.append(">");
        } else {
            b.append("/>");
        }

        for (String thisLevelPrefix : thisLevelPrefixes.keySet()) {
            parentPrefixes.remove(thisLevelPrefix);
        }

    } else if (node.getNodeValue() != null) {
        b.append(encodeText(node.getNodeValue()));
    }

    return b.toString();
}