Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/* Copyright 2004 - 2007 Kasper Nielsen <kasper@codehaus.org> Licensed under
 * the Apache 2.0 License, see http://coconut.codehaus.org/license.
 */

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import org.w3c.dom.Element;

public class Main {
    public static <T> T loadChildObject(Element parent, String tagName, Class<T> type) throws Exception {
        Element e = getChild(tagName, parent);
        return loadObject(e, type);
    }

    public static Element getChild(String name, Element e) {
        for (int i = 0; i < e.getChildNodes().getLength(); i++) {
            if (e.getChildNodes().item(i).getNodeName().equals(name)) {
                return (Element) e.getChildNodes().item(i);
            }
        }
        return null;
    }

    public static <T> T loadObject(Element e, Class<T> type) throws InstantiationException, IllegalAccessException,
            InvocationTargetException, NoSuchMethodException, ClassNotFoundException {
        if (e != null) {
            String c = e.getAttribute("type");
            Class<T> clazz = (Class) Class.forName(c);
            Constructor<T> con = clazz.getConstructor((Class[]) null);
            return con.newInstance();
        }
        return null;
    }
}