Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2007 Exadel, Inc. and Red Hat, Inc.
 * Distributed under license by Red Hat, Inc. All rights reserved.
 * This program is made available under the terms of the
 * Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Exadel, Inc. and Red Hat, Inc. - initial API and implementation
 ******************************************************************************/

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

public class Main {
    public static Element createElement(Element parent, String path) {
        int i = path.indexOf('.');
        Element element = null;
        if (i < 0) {
            element = parent.getOwnerDocument().createElement(path);
            parent.appendChild(element);
        } else {
            String p = path.substring(0, i), c = path.substring(i + 1);
            Element pe = getUniqueChild(parent, p);
            if (pe == null) {
                pe = parent.getOwnerDocument().createElement(p);
                parent.appendChild(pe);
            }
            element = createElement(pe, c);
        }
        return element;
    }

    public static Element getUniqueChild(Element parent, String name) {
        return getFirstChild(parent, name);
    }

    public static Element getFirstChild(Element parent, String name) {
        NodeList nl = parent.getChildNodes();
        for (int i = 0; i < nl.getLength(); i++) {
            Node n = nl.item(i);
            if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(name)) {
                return (Element) n;
            }
        }
        return null;
    }
}