Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 Copyright 2013, 2016 Nationale-Nederlanden
    
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at
    
 http://www.apache.org/licenses/LICENSE-2.0
    
 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */

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

public class Main {
    /**
     * Method getChildTagAsBoolean.
     * Return the boolean-value of the first element with tag
     * <code>tag</code> in the DOM subtree <code>el</code>.
     *
     * <p>
     * To determine true or false, the value of the tag is compared case-
     * insensitive with the values <pre>true</pre>, <pre>yes</pre>, or
     * <pre>on</pre>. If it matches, <code>true</code> is returned. If not,
     * <code>false</code> is returned.
     *
     * <p>
     * If the tag can not be found, <code>false</code> is returned.
     *
     * @param el            DOM subtree
     * @param tag           Name of tag to find
     *
     * @return boolean      The value found.
     */
    static public boolean getChildTagAsBoolean(Element el, String tag) {
        return getChildTagAsBoolean(el, tag, false);
    }

    /**
     * Method getChildTagAsBoolean.
     * Return the boolean-value of the first element with tag
     * <code>tag</code> in the DOM subtree <code>el</code>.
     *
     * <p>
     * To determine true or false, the value of the tag is compared case-
     * insensitive with the values <pre>true</pre>, <pre>yes</pre>, or
     * <pre>on</pre>. If it matches, <code>true</code> is returned. If not,
     * <code>false</code> is returned.
     *
     * <p>
     * If the tag can not be found, the default-value is returned.
     *
     * @param el            DOM subtree
     * @param tag           Name of tag to find
     * @param defaultValue  Default-value in case tag can not
     *                       be found.
     *
     * @return boolean      The value found.
     */
    static public boolean getChildTagAsBoolean(Element el, String tag, boolean defaultValue) {
        String str;
        boolean bool;

        str = getChildTagAsString(el, tag, null);
        if (str == null) {
            return defaultValue;
        }

        bool = false;
        if (str.equalsIgnoreCase("true") || str.equalsIgnoreCase("yes") || str.equalsIgnoreCase("on")) {
            bool = true;
        }
        return bool;
    }

    /**
     * Method getChildTagAsString.
     * Return the value of the first element with tag
     * <code>tag</code> in the DOM subtree <code>el</code>.
     *
     * @param el            DOM subtree
     * @param tag           Name of tag to find
     *
     * @return String       The value found, or null if no matching
     *                       tag is found.
     */
    static public String getChildTagAsString(Element el, String tag) {
        return getChildTagAsString(el, tag, null);
    }

    /**
     * Method getChildTagAsString.
     * Return the value of the first element with tag
     * <code>tag</code> in the DOM subtree <code>el</code>.
     *
     * @param el            DOM subtree
     * @param tag           Name of tag to find
     * @param defaultValue  Default-value in case tag can not
     *                       be found.
     *
     * @return String       The value found.
     */
    static public String getChildTagAsString(Element el, String tag, String defaultValue) {
        Element tmpEl;
        String str = "";

        tmpEl = getFirstChildTag(el, tag);
        if (tmpEl != null) {
            str = getStringValue(tmpEl, true);
        }
        return (str.length() == 0) ? (defaultValue) : (str);
    }

    /**
     * Method getFirstChildTag. Return the first child-node which is an element
     * with tagName equal to given tag.
     * This method only looks at the direct children of the given node, and
     * doesn't descent deeper into the tree.
     *
     * @param el       Element where to get children from
     * @param tag      Tag to match
     * @return Element The element found, or <code>null</code> if no match
     *                  found.
     */
    static public Element getFirstChildTag(Element el, String tag) {
        NodeList nl;
        int len;

        nl = el.getChildNodes();
        len = nl.getLength();
        for (int i = 0; i < len; ++i) {
            Node n = nl.item(i);
            if (n instanceof Element) {
                Element elem = (Element) n;
                if (elem.getTagName().equals(tag)) {
                    return elem;
                }
            }
        }
        return null;
    }

    static public String getStringValue(Element el) {
        return getStringValue(el, true);
    }

    static public String getStringValue(Element el, boolean trimWhitespace) {
        StringBuilder sb = new StringBuilder(1024);
        String str;

        NodeList nl = el.getChildNodes();
        for (int i = 0; i < nl.getLength(); ++i) {
            Node n = nl.item(i);
            if (n instanceof Text) {
                sb.append(n.getNodeValue());
            }
        }
        if (trimWhitespace) {
            str = sb.toString().trim();
        } else {
            str = sb.toString();
        }
        return str;

    }
}