Java XML Node Save writeDescription(PrintWriter out, Node node, String offSet)

Here you can find the source of writeDescription(PrintWriter out, Node node, String offSet)

Description

Parses and writes the description of a given Node into the output stream

License

Apache License

Parameter

Parameter Description
out The output stream to write in
node The affected Node
offSet The offset that should be used before each line (commonly empty spaces)

Declaration

public static void writeDescription(PrintWriter out, Node node, String offSet) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.PrintWriter;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    /**/*  www.j a  va2  s. c o  m*/
     * Parses and writes the description of a given Node into the output stream
     * @param out The output stream to write in
     * @param node The affected Node
     * @param offSet The offset that should be used before each line (commonly empty spaces)
     */
    public static void writeDescription(PrintWriter out, Node node, String offSet) {
        Node childNodeDescriptive = null;
        Node childNodeBrief = null;
        Node childNodeDescription = null;

        if (node == null)
            return;

        childNodeDescriptive = getNodeByName("descriptive", node.getChildNodes());

        if (childNodeDescriptive == null)
            return;

        childNodeBrief = getNodeByName("brief", childNodeDescriptive.getChildNodes());
        childNodeDescription = getNodeByName("description", childNodeDescriptive.getChildNodes());

        String desc = "";

        String outline = "\n" + offSet + "/**";

        if (childNodeBrief != null) {
            desc = childNodeBrief.getTextContent().trim().replace("\n", "\n" + offSet + " * ");
            outline += "\n" + offSet + " * " + desc;
        }

        if (!outline.endsWith(" * "))
            outline += "\n" + offSet + " *";

        if (childNodeDescription != null) {
            NodeList l = childNodeDescription.getChildNodes();
            for (int i = 0; i < l.getLength(); i++) {
                desc = childNodeDescription.getChildNodes().item(i).getTextContent().trim().replace("\n",
                        "\n" + offSet + " * ");
                outline += "\n" + offSet + " * " + desc;
            }
        }

        //TODO add code examples

        //TODO add param descriptions and return value descriptions 

        //TODO add throws description 

        outline += "\n" + offSet + " */";
        out.println(outline);
    }

    /**
     * Gets the first node with a specific name
     * @param name
     * @param nodeList
     * @return
     */
    static Node getNodeByName(String name, NodeList nodeList) {
        for (int i = 0; i < nodeList.getLength(); i++) {
            if (nodeList.item(i).getNodeName().equals(name)) {
                return nodeList.item(i);
            }
        }
        return null;
    }
}

Related

  1. save(String filename, Node node)
  2. toResultSet(Node node, ResultSet rs)
  3. write(Node node, Writer out)
  4. writeByDom(Node node, Writer writer, int indent)
  5. writeDOM(Node node, PrintWriter writer)
  6. writeNode(Node n, FileOutputStream o)
  7. writeNode(Node n, Writer w)
  8. writeNode(Node node, OutputStream os, boolean formatted)