/*
 * -------------------------------------------------------------------------
 *      $Id: Binding.java,v 1.6 2004/05/26 18:13:18 estewart Exp $
 * -------------------------------------------------------------------------
 *      Copyright (c) 1999 Visual Numerics Inc. All Rights Reserved.
 *
 *      This software is confidential information which is proprietary to
 *      and a trade secret of Visual Numerics, Inc.  Use, duplication or
 *      disclosure is subject to the terms of an appropriate license
 *      agreement.
 *
 *      VISUAL NUMERICS MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE
 *      SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING
 *      BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY,
 *      FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. VISUAL
 *      NUMERICS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 *      AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR
 *      ITS DERIVATIVES.
 *--------------------------------------------------------------------------
 */

package com.imsl.demo.gallery;
import java.io.IOException;
import java.util.*;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.*;
import org.xml.sax.InputSource;

/**
 *  Parse an XML description file of the demos
 *
 * @author  brophy
 * @created February 8, 2001
 */
public class Binding {
    static private final String pathSeparator = System.getProperty("path.separator");

    private List    list;

    /** Creates new Binding */
    public Binding() throws org.xml.sax.SAXException, IOException, ParserConfigurationException {
        InputSource source = new InputSource(getClass().getResourceAsStream("gallery.xml"));
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setValidating(false);
        DocumentBuilder builder = factory.newDocumentBuilder();
        //builder.setErrorHandler(this);
        list = new ArrayList();
        Document document = builder.parse(source);
        parseElement((Element)document.getDocumentElement());
    }

    /**
     *    Parses an element and its children.
     */
    void parseElement(Element element) {
        int group = 0;
        String main = null;
        String title = null;
        String page = null;
        List classpath = null;

        NamedNodeMap map = element.getAttributes();
        int nMap = map.getLength();
        for (int k = 0;  k < nMap;  k++) {
            Node node = map.item(k);
            String name = node.getNodeName();
            String value = node.getNodeValue();
            if (name == null) {
            } else if (name.equals("group")) {
                StringTokenizer st = new StringTokenizer(value,"|");
                while (st.hasMoreElements()) {
                    String token = st.nextToken();
                    if (token.equals("MATH"))  group |= Demo.GROUP_MATH;
                    if (token.equals("STAT"))  group |= Demo.GROUP_STAT;
                    if (token.equals("FINANCE"))  group |= Demo.GROUP_FINANCE;
                    if (token.equals("CHART"))    group |= Demo.GROUP_CHART;
                    if (token.equals("QS"))    group |= Demo.GROUP_QS;
                }
            } else if (name.equals("main")) {
                main = value;
            } else if (name.equals("title")) {
                title = value;
            } else if (name.equals("page")) {
                page = value;
            }
        }

        NodeList child = element.getChildNodes();
        int nChild = child.getLength();
        for (int k = 0;  k < nChild;  k++) {
            Node node = child.item(k);
            if (node.getNodeName().equals("classpath")) {
                classpath = parseClasspath((Element)node);
            } else if (node instanceof Element) {
                parseElement((Element)node);
            }
        }

        if (element.getNodeName().equals("demo")) {
            list.add(new Demo(group, main, title, page, classpath));
        }
    }


    private List parseClasspath(Element element) {
        List list = new ArrayList();
        NodeList child = element.getChildNodes();
        int nChild = child.getLength();
        for (int k = 0;  k < nChild;  k++) {
            Node node = child.item(k);
            if (node.getNodeName().equals("pathelement")) {
                if (node instanceof Element) {
                    list.add(((Element)node).getAttribute("location"));
                }
            }
        }
        return list;
    }

    static public Demo[] getDemos() {
        try {
            return (Demo[])new Binding().list.toArray(new Demo[0]);
        } catch (Exception e) {
            e.printStackTrace();
            return new Demo[0];
        }
    }
}