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

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

Description

format Element Start

License

Open Source License

Declaration

@SuppressWarnings({ "ChainOfInstanceofChecks" })
    private static void formatElementStart( 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 formatElementStart(/*@Nonnull*/ Document document, /*@Nonnull*/ Element element,
            /*@Nonnull*/ String formatString) {
        Node previousSibling = element.getPreviousSibling();

        if (previousSibling == null || previousSibling instanceof Element) {
            element.getParentNode().insertBefore(document.createTextNode(formatString), element);
        } else if (previousSibling instanceof Text) {
            Text textNode = (Text) previousSibling;
            String text = textNode.getWholeText();

            if (!formatString.equals(text)) {
                textNode.replaceWholeText(trimRight(text) + formatString);
            }//w  w  w  . j a  v  a  2  s  . c  o  m
        }
    }

    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. formatElementEnd( 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)
  9. formatXML(Document xml, Document xsl)