Java XML Element Print printMethodArgs(Element elem, String endient, PrintStream pstrm)

Here you can find the source of printMethodArgs(Element elem, String endient, PrintStream pstrm)

Description

print Method Args

License

Open Source License

Declaration

static private int printMethodArgs(Element elem, String endient,
            PrintStream pstrm) 

Method Source Code

//package com.java2s;
/*  it under the terms of the GNU General Public License as published by    */

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.Attr;
import org.w3c.dom.NodeList;
import org.w3c.dom.NamedNodeMap;

import org.w3c.dom.Comment;
import org.w3c.dom.Text;
import java.io.PrintStream;

public class Main {
    static private final String ELEM_ENDIENT = "\t";

    static private int printMethodArgs(Element elem, String endient,
            PrintStream pstrm) {//  w w w.  jav  a2 s.  co m
        endient += ELEM_ENDIENT;

        int count = 0;

        NodeList nodes = elem.getChildNodes();
        for (int i = 0; i < nodes.getLength(); i++) {
            Node node = nodes.item(i);

            if (node instanceof Element) {
                Element child = (Element) node;
                String tag = child.getTagName();
                if (tag.equals("method-arg")) {
                    if (count == 0) {
                        pstrm.println();
                    }
                    printElem((Element) node, endient, true, pstrm);
                    count++;
                }
            }
        }

        return count;
    }

    static private void printElem(Element elem, String endient,
            boolean toplevel, PrintStream pstrm) {
        if (elem == null) {
            return;
        }

        pstrm.print(endient + "<" + elem.getTagName());
        printAttrs(elem, endient, pstrm);
        int count = printChildElems(elem, endient, toplevel, pstrm);
        if (count == 0) {
            String text = elem.getNodeValue();

            if (is_empty(text) == false) {
                pstrm.print(text);
            }

            pstrm.println("</" + elem.getTagName() + ">");
        } else {
            pstrm.println(endient + "</" + elem.getTagName() + ">");
        }
    }

    static private void printAttrs(Element elem, String endient,
            PrintStream pstrm) {
        NamedNodeMap attrs = elem.getAttributes();

        if (attrs.getLength() == 0) {
            pstrm.print(">");
            return;
        }

        Attr attr = (Attr) attrs.item(0);

        if (attrs.getLength() == 1) {
            pstrm.print(" " + attr.getName() + "=\"" + attr.getValue()
                    + "\">");
            return;
        }

        endient += ELEM_ENDIENT;

        for (int i = 0; i < attrs.getLength(); i++) {
            attr = (Attr) attrs.item(i);
            pstrm.print("\n" + endient + attr.getName() + "=\""
                    + attr.getValue() + "\"");
        }

        pstrm.print(">");
    }

    static private int printChildElems(Element elem, String endient,
            boolean toplevel, PrintStream pstrm) {
        endient += ELEM_ENDIENT;

        int count = 0;
        boolean after_comment = false;

        NodeList nodes = elem.getChildNodes();
        for (int i = 0; i < nodes.getLength(); i++) {
            Node node = nodes.item(i);

            if (node instanceof Element) {
                if (after_comment == false && (toplevel || count == 0)) {
                    pstrm.println("");
                }
                after_comment = false;
                printElem((Element) node, endient, false, pstrm);
                count++;
            } else if (node instanceof Comment) {
                if (toplevel && count != 0) {
                    after_comment = true;
                    pstrm.println("");
                }
                Comment comm = (Comment) node;

                String data = comm.getData();
                String commEnd;

                if (data.indexOf('\n') == -1 && data.indexOf('\r') == -1) {
                    commEnd = "-->";
                } else {
                    commEnd = "\n" + endient + "-->";
                }

                String head = "\n";
                if (toplevel) {
                    head = "";
                }

                pstrm.println(head + endient + "<!--" + data + commEnd);
            } else if (node instanceof Text) { // && elem.getTagName().equals("header") ) {
                pstrm.print(node.getNodeValue());
            }
        }

        return count;
    }

    static boolean is_empty(String str) {
        return (str == null || str.length() == 0);
    }
}

Related

  1. printElement(SOAPElement el)
  2. printElementBody(Element elem, StringWriter sw)
  3. printElementEnd(Element elem, StringWriter sw)
  4. printElementStart(Element elem, StringWriter sw)
  5. printElemShallow(Element elem, String endient, PrintStream pstrm)