Example usage for org.w3c.dom Element getTagName

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

Introduction

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

Prototype

public String getTagName();

Source Link

Document

The name of the element.

Usage

From source file:Main.java

/**
 * Checks to see if parent node has more of these nodes looks at tagName
 * //from  w  w w .  j  a v a 2 s  .  com
 * @param node
 * @return
 */
private static final boolean parentNodeHasMoreOfThese(Element node) {
    int count = 0;
    NodeList children = node.getParentNode().getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            if (node.getTagName().equals(((Element) child).getTagName())) {
                count++;
                if (count > 1)
                    return true;
            }
        }
    }

    return false;
}

From source file:Main.java

/**
 * Checks whether the expected XML element is contained in the actual
 * element. I.e., every attribute of the expected element must occur in the
 * actual element, with the same value, and all child nodes in the expected
 * element must occur in the actual element; if multiple child nodes with
 * the same name are found in the expected element, the first found
 * identifying attribute in the expected child element is used to identify
 * the child in the actual element./*from www  . j  a va 2s.  c  o m*/
 * 
 * @param expected
 * @param actual
 * @return
 */
public static void assertContains(Element expected, Element actual, String messagePrefix,
        String... identifyingAttributes) {
    if (!expected.getTagName().equals(actual.getTagName())) {
        throw new AssertionError(messagePrefix + "\nExpected tagname differs from actual tagname (expected '"
                + printNodeOnly(expected) + "', found " + printNodeOnly(actual) + ")");
    }

    // Compare attributes
    NamedNodeMap expectedAttributes = expected.getAttributes();
    for (int i = expectedAttributes.getLength() - 1; i >= 0; i--) {
        String expectedAttributeName = expectedAttributes.item(i).getNodeName();
        String expectedAttributeValue = expectedAttributes.item(i).getNodeValue();

        String actualValue = actual.getAttribute(expectedAttributeName);
        if (actualValue == null || actualValue.isEmpty()) {
            throw new AssertionError(messagePrefix + "\nThe attribute '" + expectedAttributeName
                    + "' with value '" + expectedAttributeValue + "' was not found in the result "
                    + printNodeOnly(actual));
        }
        if (!expectedAttributeValue.equals(actualValue)) {
            throw new AssertionError(messagePrefix + "\nThe attribute '" + expectedAttributeName
                    + "' has value '" + actualValue + "' instead of '" + expectedAttributeValue
                    + "' in the result " + printNodeOnly(actual));
        }
    }

    // Compare child elements
    Node child = expected.getFirstChild();
    while (child != null) {
        if (child instanceof Element) {
            assertContainsChildElement((Element) child, actual, messagePrefix, identifyingAttributes);
        }
        child = child.getNextSibling();
    }
}

From source file:com.cburch.draw.shapes.SvgReader.java

public static AbstractCanvasObject createShape(Element elt) {
    String name = elt.getTagName();
    AbstractCanvasObject ret;// w  w w  .j  av  a  2 s  .c o  m
    if (name.equals("ellipse")) {
        ret = createOval(elt);
    } else if (name.equals("line")) {
        ret = createLine(elt);
    } else if (name.equals("path")) {
        ret = createPath(elt);
    } else if (name.equals("polyline")) {
        ret = createPolyline(elt);
    } else if (name.equals("polygon")) {
        ret = createPolygon(elt);
    } else if (name.equals("rect")) {
        ret = createRectangle(elt);
    } else if (name.equals("text")) {
        ret = createText(elt);
    } else {
        return null;
    }
    List<Attribute<?>> attrs = ret.getAttributes();
    if (attrs.contains(DrawAttr.PAINT_TYPE)) {
        String stroke = elt.getAttribute("stroke");
        String fill = elt.getAttribute("fill");
        if (stroke.equals("") || stroke.equals("none")) {
            ret.setValue(DrawAttr.PAINT_TYPE, DrawAttr.PAINT_FILL);
        } else if (fill.equals("none")) {
            ret.setValue(DrawAttr.PAINT_TYPE, DrawAttr.PAINT_STROKE);
        } else {
            ret.setValue(DrawAttr.PAINT_TYPE, DrawAttr.PAINT_STROKE_FILL);
        }
    }
    attrs = ret.getAttributes(); // since changing paintType could change it
    if (attrs.contains(DrawAttr.STROKE_WIDTH) && elt.hasAttribute("stroke-width")) {
        Integer width = Integer.valueOf(elt.getAttribute("stroke-width"));
        ret.setValue(DrawAttr.STROKE_WIDTH, width);
    }
    if (attrs.contains(DrawAttr.STROKE_COLOR)) {
        String color = elt.getAttribute("stroke");
        String opacity = elt.getAttribute("stroke-opacity");
        if (!color.equals("none")) {
            ret.setValue(DrawAttr.STROKE_COLOR, getColor(color, opacity));
        }
    }
    if (attrs.contains(DrawAttr.FILL_COLOR)) {
        String color = elt.getAttribute("fill");
        if (color.equals(""))
            color = "#000000";
        String opacity = elt.getAttribute("fill-opacity");
        if (!color.equals("none")) {
            ret.setValue(DrawAttr.FILL_COLOR, getColor(color, opacity));
        }
    }
    return ret;
}

From source file:com.photon.phresco.impl.WindowsApplicationProcessor.java

private static boolean referenceCheck(Document doc) {
    NodeList project = doc.getElementsByTagName(PROJECT);
    Boolean flag = false;/*from   w  ww .  ja  v  a2  s.c o m*/
    for (int i = 0; i < project.getLength(); i++) {
        Element PROEJCT = (Element) project.item(i);
        NodeList ITEMGROUPs = PROEJCT.getElementsByTagName(ITEMGROUP);
        for (int j = 0; j < ITEMGROUPs.getLength(); j++) {
            Element ITEMGROUP = (Element) ITEMGROUPs.item(j);
            NodeList references = ITEMGROUP.getElementsByTagName(REFERENCE);
            for (int k = 0; k < references.getLength(); k++) {
                Element reference = (Element) references.item(k);
                if (reference.getTagName().equalsIgnoreCase(REFERENCE)) {
                    flag = true;
                } else {
                    flag = false;
                }
            }
        }
    }
    return flag;
}

From source file:Main.java

public static String getTagLocalName(final Element element) {
    if (element == null) {
        return null;
    }//from   w w  w. ja va 2 s . c om
    final String localName = element.getLocalName();
    if (localName != null) {
        return localName;
    }
    return element.getTagName();
}

From source file:com.googlecode.sardine.util.SardineUtil.java

/** */
public static Map<String, String> extractCustomProps(List<Element> elements) {
    Map<String, String> customPropsMap = new HashMap<String, String>(elements.size());

    for (Element element : elements) {
        String[] keys = element.getTagName().split(":", 2);
        String key = (keys.length > 1) ? keys[1] : keys[0];

        customPropsMap.put(key, element.getTextContent());
    }/*from ww w  . j  av  a2 s  .  co m*/

    return customPropsMap;
}

From source file:Main.java

protected static void writeXMLwalkTree(Node node, int indent, PrintWriter out) {
    if (node == null)
        throw new NullPointerException("Null node passed to writeXMLwalkTree()");
    if (node.hasChildNodes()) {
        if (node instanceof Element) {
            Element elem = (Element) node;
            //elem.normalize();
            out.print("\n");
            for (int j = 0; j < indent; j++) {
                out.print(" ");
            }/*from  ww  w . jav  a2s .c  om*/
            out.print("<" + elem.getTagName());
            NamedNodeMap attrs = elem.getAttributes();
            for (int i = 0; i < attrs.getLength(); i++) {
                Attr a = (Attr) attrs.item(i);
                out.print(" " + a.getName() + "=\"" + a.getValue() + "\"");
            }
            out.print(">");
            NodeList nl = node.getChildNodes();
            for (int i = 0; i < nl.getLength(); i++) {
                writeXMLwalkTree(nl.item(i), indent + 2, out);
            }
            //              for(int j=0;j<indent;j++) {
            //                  out.print(" ");
            //              }
            out.println("</" + elem.getTagName() + ">");
        }
    } else {
        if (node instanceof Element) {
            Element elem = (Element) node;
            out.print("\n");
            for (int j = 0; j < indent; j++) {
                out.print(" ");
            }
            out.print("<" + elem.getTagName());
            NamedNodeMap attrs = elem.getAttributes();
            for (int i = 0; i < attrs.getLength(); i++) {
                Attr a = (Attr) attrs.item(i);
                out.print(" " + a.getName() + "=\"" + a.getValue() + "\"");
            }
            out.println("/>");
        } else if (node instanceof CDATASection) {
            CDATASection cdata = (CDATASection) node;
            //              for(int j=0;j<indent;j++) {
            //                  out.print(" ");
            //              }
            out.print("<![CDATA[" + cdata.getData() + "]]>");
        } else if (node instanceof Text) {
            Text text = (Text) node;
            StringBuilder buf = new StringBuilder(text.getData().length());
            for (int i = 0; i < text.getData().length(); i++) {
                if (text.getData().charAt(i) == '\n' || text.getData().charAt(i) == '\r'
                        || text.getData().charAt(i) == ' ' || text.getData().charAt(i) == '\t') {
                    if (buf.length() > 0 && buf.charAt(buf.length() - 1) != ' ') {
                        buf.append(' ');
                    }
                } else {
                    buf.append(text.getData().charAt(i));
                }
            }
            if (buf.length() > 0 && !buf.toString().equals(" ")) {
                StringBuilder buf2 = new StringBuilder(buf.length() + indent);
                //                  for(int j=0;j<indent;j++) {
                //                      buf2.append(' ');
                //                  }
                buf2.append(buf.toString());
                out.print(buf2);
            }
        }
    }
}

From source file:Main.java

/**
 * Get a single child element with the given name. If more than one child with the given name
 * exists, a warning is logged./* w  w  w.ja v  a2s .  c  o  m*/
 *
 * @param parent the parent node
 * @param name   the name of the child element to get; may be null or "*" to indicate any
 *               element child
 * @param log    where to write the warning if more than one child element was found; may be
 *               null to disable the warning
 * @return the child, or null if none was found
 */
public static Element getOnlyChild(Element parent, String name, Log log) {
    NodeList children = getChildren(parent, name);
    if (children.getLength() == 0)
        return null;
    if (children.getLength() > 1 && log != null)
        log.warn("Expected exactly one child named '" + name + "' of '" + parent.getTagName() + "' but got "
                + children.getLength());
    return (Element) children.item(0);
}

From source file:com.bstek.dorado.config.xml.XmlParseException.java

private static String populateErrorMessage(String message, Node node, Resource resource) {
    StringBuffer sb = new StringBuffer();
    if (message != null)
        sb.append(message);/*from w w  w  .ja  v a2s .c o  m*/

    if (resource != null) {
        sb.append(" - ").append(resource);
    }

    if (node != null) {
        Element element;
        if (node instanceof Element) {
            element = (Element) node;
        } else {
            element = (Element) node.getParentNode();
        }

        if (element != null) {
            sb.append(" - ").append("<" + element.getTagName() + " ");
            NamedNodeMap names = element.getAttributes();
            for (int i = 0; i < 3 && i < names.getLength(); i++) {
                sb.append(populateXmlAttribute(element, names.item(i).getNodeName())).append(" ");
            }
            sb.append("... ");
        }
    }
    return sb.toString();
}

From source file:Main.java

private static void assertContainsChildElement(Element expectedChild, Element actual, String messagePrefix,
        String... identifyingAttributes) {
    List<Element> childrenWithThatName = getDirectChildren(actual, expectedChild.getTagName());
    if (childrenWithThatName.isEmpty()) {
        throw new AssertionError(messagePrefix + "\nThe child element " + printNodeOnly(expectedChild)
                + " was not found in the result " + printNodeOnly(actual));
    }/* w w  w  .j av  a  2 s.c om*/
    if (childrenWithThatName.size() == 1) {
        assertContains(expectedChild, childrenWithThatName.get(0), messagePrefix, identifyingAttributes);
    } else {
        String[] identifyingValues = getAttributeValues(expectedChild, identifyingAttributes);
        int expectedLocationInParent = getLocationInParent(expectedChild, identifyingAttributes,
                identifyingValues);
        Element actualChild = getNthElement(childrenWithThatName, identifyingAttributes, identifyingValues,
                expectedLocationInParent);
        if (actualChild == null) {
            throw new AssertionError(messagePrefix + "\nThe child element " + printNodeOnly(expectedChild)
                    + " was not found in the result " + printNodeOnly(actual));
        } else {
            assertContains(expectedChild, actualChild, messagePrefix, identifyingAttributes);
        }
    }
}