Java Code Examples for org.w3c.dom.Element#getPreviousSibling()

The following examples show how to use org.w3c.dom.Element#getPreviousSibling() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AppEngineXsltTransformTest.java    From google-cloud-eclipse with Apache License 2.0 5 votes vote down vote up
@Test
public void testAddRuntimeAtEnd() {
  Document transformed = transform("/xslt/addJava8Runtime.xsl",
      "<appengine-web-app xmlns=\"http://appengine.google.com/ns/1.0\"><threadsafe>true</threadsafe></appengine-web-app>");
  Element runtime = getRuntimeElement(transformed);
  assertEquals("java8", runtime.getTextContent());
  Text previous = (Text) runtime.getPreviousSibling();
  assertTrue(previous.getNodeValue().trim().isEmpty());
  Text next = (Text) runtime.getNextSibling();
  String s = next.getNodeValue();
  assertEquals("\n", s);
}
 
Example 2
Source File: XMLUtil.java    From org.hl7.fhir.core with Apache License 2.0 4 votes vote down vote up
public static Element getPrevSibling(Element e) {
  Node n = e.getPreviousSibling();
  while (n != null && n.getNodeType() != Node.ELEMENT_NODE)
    n = n.getPreviousSibling();
  return (Element) n;
}
 
Example 3
Source File: XmlPrettyPrinter.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
private boolean newlineBeforeElementOpen(Element element, int depth) {
    if (hasBlankLineAbove()) {
        return false;
    }

    if (mPrefs.removeEmptyLines || depth <= 0) {
        return false;
    }

    if (isMarkupElement(element)) {
        return false;
    }

    // See if this element should be separated from the previous element.
    // This is the case if we are not compressing whitespace (checked above),
    // or if we are not immediately following a comment (in which case the
    // newline would have been added above it), or if we are not in a formatting
    // style where
    if (mStyle == XmlFormatStyle.LAYOUT) {
        // In layouts we always separate elements
        return true;
    }

    if (mStyle == XmlFormatStyle.MANIFEST || mStyle == XmlFormatStyle.RESOURCE
            || mStyle == XmlFormatStyle.FILE) {
        Node curr = element.getPreviousSibling();

        // <style> elements are traditionally separated unless it follows a comment
        if (TAG_STYLE.equals(element.getTagName())) {
            if (curr == null
                    || curr.getNodeType() == Node.ELEMENT_NODE
                    || (curr.getNodeType() == Node.TEXT_NODE
                    && curr.getNodeValue().trim().isEmpty()
                    && (curr.getPreviousSibling() == null
                    || curr.getPreviousSibling().getNodeType()
                    == Node.ELEMENT_NODE))) {
                return true;
            }
        }

        // In all other styles, we separate elements if they have a different tag than
        // the previous one (but we don't insert a newline inside tags)
        while (curr != null) {
            short nodeType = curr.getNodeType();
            if (nodeType == Node.ELEMENT_NODE) {
                Element sibling = (Element) curr;
                if (!element.getTagName().equals(sibling.getTagName())) {
                    return true;
                }
                break;
            } else if (nodeType == Node.TEXT_NODE) {
                String text = curr.getNodeValue();
                if (!text.trim().isEmpty()) {
                    break;
                }
                // If there is just whitespace, continue looking for a previous sibling
            } else {
                // Any other previous node type, such as a comment, means we don't
                // continue looking: this element should not be separated
                break;
            }
            curr = curr.getPreviousSibling();
        }
        if (curr == null && depth <= 1) {
            // Insert new line inside tag if it's the first element inside the root tag
            return true;
        }

        return false;
    }

    return false;
}
 
Example 4
Source File: XmlPrettyPrinter.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
private boolean newlineBeforeElementOpen(Element element, int depth) {
    if (hasBlankLineAbove()) {
        return false;
    }

    if (mPrefs.removeEmptyLines || depth <= 0) {
        return false;
    }

    if (isMarkupElement(element)) {
        return false;
    }

    // See if this element should be separated from the previous element.
    // This is the case if we are not compressing whitespace (checked above),
    // or if we are not immediately following a comment (in which case the
    // newline would have been added above it), or if we are not in a formatting
    // style where
    if (mStyle == XmlFormatStyle.LAYOUT) {
        // In layouts we always separate elements
        return true;
    }

    if (mStyle == XmlFormatStyle.MANIFEST || mStyle == XmlFormatStyle.RESOURCE
            || mStyle == XmlFormatStyle.FILE) {
        Node curr = element.getPreviousSibling();

        // <style> elements are traditionally separated unless it follows a comment
        if (TAG_STYLE.equals(element.getTagName())) {
            if (curr == null
                    || curr.getNodeType() == Node.ELEMENT_NODE
                    || (curr.getNodeType() == Node.TEXT_NODE
                    && curr.getNodeValue().trim().isEmpty()
                    && (curr.getPreviousSibling() == null
                    || curr.getPreviousSibling().getNodeType()
                    == Node.ELEMENT_NODE))) {
                return true;
            }
        }

        // In all other styles, we separate elements if they have a different tag than
        // the previous one (but we don't insert a newline inside tags)
        while (curr != null) {
            short nodeType = curr.getNodeType();
            if (nodeType == Node.ELEMENT_NODE) {
                Element sibling = (Element) curr;
                if (!element.getTagName().equals(sibling.getTagName())) {
                    return true;
                }
                break;
            } else if (nodeType == Node.TEXT_NODE) {
                String text = curr.getNodeValue();
                if (!text.trim().isEmpty()) {
                    break;
                }
                // If there is just whitespace, continue looking for a previous sibling
            } else {
                // Any other previous node type, such as a comment, means we don't
                // continue looking: this element should not be separated
                break;
            }
            curr = curr.getPreviousSibling();
        }
        if (curr == null && depth <= 1) {
            // Insert new line inside tag if it's the first element inside the root tag
            return true;
        }

        return false;
    }

    return false;
}
 
Example 5
Source File: SuperProperties.java    From tomee with Apache License 2.0 4 votes vote down vote up
public synchronized void loadFromXML(final InputStream in) throws IOException {
    if (in == null) {
        throw new NullPointerException();
    }

    final DocumentBuilder builder = getDocumentBuilder();

    try {
        final Document doc = builder.parse(in);
        final NodeList entries = doc.getElementsByTagName("entry");
        if (entries == null) {
            return;
        }

        final int entriesListLength = entries.getLength();
        for (int i = 0; i < entriesListLength; i++) {
            final Element entry = (Element) entries.item(i);
            final String key = entry.getAttribute("key");
            final String value = entry.getTextContent();
            put(key, value);

            // search backwards for a comment
            for (Node node = entry.getPreviousSibling(); node != null && !(node instanceof Element); node = node.getPreviousSibling()) {
                if (node instanceof Comment) {
                    final InputStream cin = new ByteArrayInputStream(((Comment) node).getData().getBytes());

                    // read comment line by line
                    final StringBuilder comment = new StringBuilder();
                    final LinkedHashMap<String, String> attributes = new LinkedHashMap<>();

                    int nextByte;
                    char nextChar;
                    boolean firstLine = true;
                    int commentIndent = Integer.MAX_VALUE;
                    do {
                        // read one line
                        final StringBuilder commentLine = new StringBuilder();
                        int commentLineIndent = 0;
                        boolean inIndent = true;
                        while (true) {
                            nextByte = cin.read();
                            if (nextByte < 0) {
                                break;
                            }
                            nextChar = (char) nextByte; // & 0xff
                            if (inIndent && nextChar == ' ') {
                                commentLineIndent++;
                                commentLine.append(' ');
                            } else if (inIndent && nextChar == '\t') {
                                commentLineIndent += 4;
                                commentLine.append("    ");
                            } else if (nextChar == '\r' || nextChar == '\n') {
                                break;
                            } else {
                                inIndent = false;
                                commentLine.append(nextChar);
                            }
                        }

                        // Determine indent
                        if (!firstLine && commentIndent == Integer.MAX_VALUE && commentLine.length() > 0) {
                            // if this is a new comment block, the comment indent size for this
                            // block is based the first full line of the comment (ignoring the
                            // line with the <!--
                            commentIndent = commentLineIndent;
                        }
                        commentLineIndent = Math.min(commentIndent, commentLineIndent);

                        if (commentLine.toString().trim().startsWith("@")) {
                            // process property attribute
                            final String attribute = commentLine.toString().trim().substring(1);
                            final String[] parts = attribute.split("=", 2);
                            final String attributeName = parts[0].trim();
                            final String attributeValue = parts.length == 2 ? parts[1].trim() : "";
                            attributes.put(attributeName, attributeValue);
                        } else {
                            // append comment
                            if (comment.length() != 0) {
                                comment.append(lineSeparator);
                            }
                            comment.append(commentLine.toString().substring(commentLineIndent));
                        }

                        firstLine = false;
                    } while (nextByte > 0);

                    if (comment.length() > 0) {
                        setComment(key, comment.toString());
                    }
                    this.attributes.put(normalize(key), attributes);

                    break;
                }
            }

        }
    } catch (final SAXException e) {
        throw new InvalidPropertiesFormatException(e);
    }
}
 
Example 6
Source File: XmlPrettyPrinter.java    From buck with Apache License 2.0 4 votes vote down vote up
private boolean newlineBeforeElementOpen(Element element, int depth) {
    if (hasBlankLineAbove()) {
        return false;
    }

    if (mPrefs.removeEmptyLines || depth <= 0) {
        return false;
    }

    if (isMarkupElement(element)) {
        return false;
    }

    // See if this element should be separated from the previous element.
    // This is the case if we are not compressing whitespace (checked above),
    // or if we are not immediately following a comment (in which case the
    // newline would have been added above it), or if we are not in a formatting
    // style where
    if (mStyle == XmlFormatStyle.LAYOUT) {
        // In layouts we always separate elements
        return true;
    }

    if (mStyle == XmlFormatStyle.MANIFEST || mStyle == XmlFormatStyle.RESOURCE
            || mStyle == XmlFormatStyle.FILE) {
        Node curr = element.getPreviousSibling();

        // <style> elements are traditionally separated unless it follows a comment
        if (TAG_STYLE.equals(element.getTagName())) {
            if (curr == null
                    || curr.getNodeType() == Node.ELEMENT_NODE
                    || (curr.getNodeType() == Node.TEXT_NODE
                    && curr.getNodeValue().trim().isEmpty()
                    && (curr.getPreviousSibling() == null
                    || curr.getPreviousSibling().getNodeType()
                    == Node.ELEMENT_NODE))) {
                return true;
            }
        }

        // In all other styles, we separate elements if they have a different tag than
        // the previous one (but we don't insert a newline inside tags)
        while (curr != null) {
            short nodeType = curr.getNodeType();
            if (nodeType == Node.ELEMENT_NODE) {
                Element sibling = (Element) curr;
                if (!element.getTagName().equals(sibling.getTagName())) {
                    return true;
                }
                break;
            } else if (nodeType == Node.TEXT_NODE) {
                String text = curr.getNodeValue();
                if (!text.trim().isEmpty()) {
                    break;
                }
                // If there is just whitespace, continue looking for a previous sibling
            } else {
                // Any other previous node type, such as a comment, means we don't
                // continue looking: this element should not be separated
                break;
            }
            curr = curr.getPreviousSibling();
        }
        if (curr == null && depth <= 1) {
            // Insert new line inside tag if it's the first element inside the root tag
            return true;
        }

        return false;
    }

    return false;
}