Java XML Document Format formatElementEnd( Document document, Element element, String formatString)

Here you can find the source of formatElementEnd( Document document, Element element, String formatString)

Description

format Element End

License

Open Source License

Declaration

@SuppressWarnings({ "ChainOfInstanceofChecks" })
    private static void formatElementEnd( Document document,  Element element,
             String formatString) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import org.w3c.dom.*;

public class Main {
    @SuppressWarnings({ "ChainOfInstanceofChecks" })
    private static void formatElementEnd(/*@Nonnull*/ Document document, /*@Nonnull*/ Element element,
            /*@Nonnull*/ String formatString) {
        Node lastChild = element.getLastChild();

        if (lastChild != null) {
            if (lastChild instanceof Element) {
                element.appendChild(document.createTextNode(formatString));
            } else if (lastChild instanceof Text) {
                Text textNode = (Text) lastChild;
                String text = textNode.getWholeText();

                if (hasOnlyTextSiblings(textNode)) {
                    String trimmedText = text.trim();
                    if (!trimmedText.equals(text)) {
                        textNode.replaceWholeText(trimmedText);
                    }/*from   w ww  .  j  av a  2s .c om*/
                } else {
                    if (!formatString.equals(text)) {
                        textNode.replaceWholeText(trimRight(text) + formatString);
                    }
                }
            }
        }
    }

    public static boolean hasOnlyTextSiblings(/*@Nonnull*/ Node node) {
        Node leftSibling = node.getPreviousSibling();

        while (leftSibling != null) {
            if (!(leftSibling instanceof Text)) {
                return false;
            }

            leftSibling = leftSibling.getPreviousSibling();
        }

        Node rightSibling = node.getNextSibling();

        while (rightSibling != null) {
            if (!(rightSibling instanceof Text)) {
                return false;
            }

            rightSibling = rightSibling.getNextSibling();
        }

        return true;
    }

    private static String trimRight(/*@Nullable*/ String s) {
        if (s == null) {
            return null;
        }

        int lastIndex = s.length() - 1;
        int index = lastIndex;

        while (index >= 0 && s.charAt(index) <= ' ') {
            --index;
        }

        return index == lastIndex ? s : s.substring(0, index + 1);
    }
}

Related

  1. format(Document doc)
  2. format(Document doc)
  3. format(Document document)
  4. format(Source xslSource, Document xml, URIResolver resolver)
  5. formatElementStart( Document document, Element element, String formatString)
  6. formatNode(Document doc, Node parent, Node node)
  7. formatPython(int start, Document doc, String s)
  8. formatXML(Document document, org.w3c.dom.Element root, String tab)