/**
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 1999 Bull S.A.
 * Contact: jonas-team@objectweb.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or 1any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * Initial developer: Florent BENOIT
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */

package org.objectweb.jonas_lib.deployment.api;


import java.net.URL;
import java.util.ArrayList;
import java.util.List;


/**
 * This class defines the declarations of the default J2EE 1.4 Schemas
 * @author Florent Benoit
 */
public abstract class CommonsSchemas implements Schemas {


    /**
     * List of schemas used by components
     */
    private static final String[] DEFAULT_SCHEMAS = new String[] {
        "j2ee_1_4.xsd",
        "j2ee_web_services_client_1_1.xsd",
        "xml.xsd",
        "jonas_j2ee_4_0.xsd",
        "jonas_j2ee_4_1.xsd",
        "jonas_j2ee_4_1_2.xsd",
        "jonas_j2ee_4_1_4.xsd",
        "jonas_j2ee_4_2.xsd"
    };


    /**
     * List where the local schemas URLs are stored
     */
    private static ArrayList localSchemas = null;


    /**
     * Build a new object for Schemas handling
     */
    public CommonsSchemas() {
        localSchemas = new ArrayList();
        addSchemas(DEFAULT_SCHEMAS);
    }

    /**
     * Gets the URLs of the local schemas
     * @return the URLs of the local schemas
     */
    public List getlocalSchemas() {
        return localSchemas;
    }


    /**
     * Add to our repository the given local schemas
     * @param schemas schemas to add to the repository
     * @throws IllegalStateException if the dtds is not found as resource
     */
    protected static void addSchemas(String[] schemas) throws IllegalStateException {
        URL url = null;
        for (int i = 0; i <  schemas.length; i++) {
            url = CommonsSchemas.class.getResource("/" +  schemas[i]);
            if (url == null) {
                throw new IllegalStateException("'" +  schemas[i] + "' was not found in the current classloader !");
            }
            localSchemas.add(url.toString());
        }
    }

    /**
     * @param element name of the root element (jonas-ejb-jar, ...)
     * @param schemas array of Schemas (to be always up to date)
     * @return a header for the right element with last element
     */
    public static String getHeaderForElement(String element, String[] schemas) {
        StringBuffer header = new StringBuffer();
        header.append("<");
        header.append(element);
        header.append(" xmlns=\"http://www.objectweb.org/jonas/ns\"\n");
        header.append("               xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n");
        header.append("               xsi:schemaLocation=\"http://www.objectweb.org/jonas/ns\n");
        header.append("                                   http://www.objectweb.org/jonas/ns/");

        // Get the last XSD
        header.append(schemas[schemas.length - 1]);
        header.append("\" >\n");

        return header.toString();
    }

}
