Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/**
 * A utility class providing several static methods to handle DOM documents.
 * 
 * @version DESMO-J, Ver. 2.4.1 copyright (c) 2014
 * @author Nicolas Knaak and Gunnar Kiesel
 * 
 * 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.CDATASection;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import org.w3c.dom.Text;

public class Main {
    /**
     * adds an Element with one attribut to a DOM-tree.
     * 
     * @param document
     *            Document: the DOM-tree to add to
     * @param father
     *            Element: the new element will be inserted directly under this
     *            Element in the tree
     * @param name
     *            String: the name of the new element
     * @param attOneName
     *            String: the name of the attribut
     * @param attOneValue
     *            String: the value of the attribut
     */
    public static void addElement(Document document, Element father, String name, String attOneName,
            String attOneValue) {
        Element element = document.createElement(name);
        element.setAttribute(attOneName, attOneValue);
        father.appendChild(element);
    }

    /**
     * adds an Element with two attributs to a DOM-tree.
     * 
     * @param document
     *            Document: the DOM-tree to add to
     * @param father
     *            Element: the new element will be inserted directly under this
     *            Element in the tree
     * @param name
     *            String: the name of the new element
     * @param attOneName
     *            String: the name of the 1st attribut
     * @param attOneValue
     *            String: the value of the 1st attribut
     * @param attTwoName
     *            String: the name of the 2nd attribut
     * @param attTwoValue
     *            String: the value of the 2nd attribut
     */
    public static void addElement(Document document, Element father, String name, String attOneName,
            String attOneValue, String attTwoName, String attTwoValue) {
        Element element = document.createElement(name);
        element.setAttribute(attOneName, attOneValue);
        element.setAttribute(attTwoName, attTwoValue);
        father.appendChild(element);
    }

    /**
     * Creates a DOM element node with the given name. This node must only be
     * used in the specified document.
     * 
     * @param d
     *            the "mother" document of the created node
     * @param name
     *            the name of the node
     * @return a node with the given name
     */
    public static Element createElement(Document d, String name) {
        return d.createElement(name);
    }

    /**
     * Creates a DOM element node with the given name and contents. If indicated
     * the contents is enclosed in CDATA marks.
     * 
     * @param d
     *            the "mother" document of the created Element
     * @param name
     *            the name of the element
     * @param value
     *            the element's value
     * @param isCDATA
     *            a flag indicating if this element contains (unformatted)
     *            CDATA.
     * @return a new DOM Element
     */
    public static Element createElement(Document d, String name, String value, boolean isCDATA) {
        Element e = createElement(d, name);
        if (isCDATA)
            e.appendChild(createCDATA(d, value));
        else
            e.appendChild(createText(d, value));
        return e;
    }

    /**
     * Creates a CDATA section node in an XML document
     * 
     * @param d
     *            the "mother" document of the created text node
     * @param text
     *            the text this CDATA section contains
     * @return a new CDATA section node.
     */
    public static CDATASection createCDATA(Document d, String text) {
        return d.createCDATASection(text);
    }

    /**
     * Creates a text node in an XML document
     * 
     * @param d
     *            the "mother" document of the created text node
     * @param text
     *            contents of the text node
     * @return a new text node
     */
    public static Text createText(Document d, String text) {
        return d.createTextNode(text);
    }
}