/*
 * -------------------------------------------------------------------------
 *      $Id: Binding.java,v 1.2 2004/05/26 19:08:48 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.SeriesAnalysis;
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 functions for analysis
 *
 * @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("functions.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) {
        String text = null;
        char mnemonic = ' ';
        String fullname = 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("text")) {
                text = value;
            } else if (name.equals("mnemonic")) {
                mnemonic = value.charAt(0);
            } else if (name.equals("full")) {
                fullname = value;
            }
        }

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

        if (element.getNodeName().equals("function")) {
            list.add(new Function(text, mnemonic, fullname));
        }
    }


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