286N/A/*
286N/A * Copyright (c) 2001, 2006, Oracle and/or its affiliates. All rights reserved.
286N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
286N/A *
286N/A * This code is free software; you can redistribute it and/or modify it
286N/A * under the terms of the GNU General Public License version 2 only, as
286N/A * published by the Free Software Foundation. Oracle designates this
286N/A * particular file as subject to the "Classpath" exception as provided
286N/A * by Oracle in the LICENSE file that accompanied this code.
286N/A *
286N/A * This code is distributed in the hope that it will be useful, but WITHOUT
286N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
286N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
286N/A * version 2 for more details (a copy is included in the LICENSE file that
286N/A * accompanied this code).
286N/A *
286N/A * You should have received a copy of the GNU General Public License version
286N/A * 2 along with this work; if not, write to the Free Software Foundation,
286N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
286N/A *
286N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
286N/A * or visit www.oracle.com if you need additional information or have any
286N/A * questions.
286N/A */
286N/A
286N/A// NewInstance.java - create a new instance of a class by name.
286N/A// http://www.saxproject.org
286N/A// Written by Edwin Goei, edwingo@apache.org
286N/A// and by David Brownell, dbrownell@users.sourceforge.net
286N/A// NO WARRANTY! This class is in the Public Domain.
286N/A// $Id: NewInstance.java,v 1.2 2005/06/10 03:50:50 jeffsuttor Exp $
286N/A
286N/Apackage org.xml.sax.helpers;
286N/A
286N/Aimport java.lang.reflect.Method;
286N/Aimport java.lang.reflect.InvocationTargetException;
286N/A
286N/A/**
286N/A * Create a new instance of a class by name.
286N/A *
286N/A * <blockquote>
286N/A * <em>This module, both source code and documentation, is in the
286N/A * Public Domain, and comes with <strong>NO WARRANTY</strong>.</em>
286N/A * See <a href='http://www.saxproject.org'>http://www.saxproject.org</a>
286N/A * for further information.
286N/A * </blockquote>
286N/A *
286N/A * <p>This class contains a static method for creating an instance of a
286N/A * class from an explicit class name. It tries to use the thread's context
286N/A * ClassLoader if possible and falls back to using
286N/A * Class.forName(String).</p>
286N/A *
286N/A * <p>This code is designed to compile and run on JDK version 1.1 and later
286N/A * including versions of Java 2.</p>
286N/A *
286N/A * @author Edwin Goei, David Brownell
524N/A * @version 2.0.1 (sax2r2)
286N/A */
286N/Aclass NewInstance {
524N/A private static final String DEFAULT_PACKAGE = "com.sun.org.apache.xerces.internal";
286N/A /**
286N/A * Creates a new instance of the specified class name
286N/A *
286N/A * Package private so this code is not exposed at the API level.
286N/A */
286N/A static Object newInstance (ClassLoader classLoader, String className)
286N/A throws ClassNotFoundException, IllegalAccessException,
286N/A InstantiationException
286N/A {
524N/A // make sure we have access to restricted packages
524N/A boolean internal = false;
524N/A if (System.getSecurityManager() != null) {
524N/A if (className != null && className.startsWith(DEFAULT_PACKAGE)) {
524N/A internal = true;
524N/A }
524N/A }
524N/A
286N/A Class driverClass;
524N/A if (classLoader == null || internal) {
286N/A driverClass = Class.forName(className);
286N/A } else {
286N/A driverClass = classLoader.loadClass(className);
286N/A }
286N/A return driverClass.newInstance();
286N/A }
286N/A
286N/A}