/*
 This file is part of the software framework JStatCom, 
 a library for building applications for data based analysis.

 Copyright (C) 2000-2005  Markus Kraetzig

 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 (at your option) any 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
 */

package com.jstatcom.engine.gauss;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javolution37.javolution.xml.XmlElement;
import javolution37.javolution.xml.XmlFormat;

import org.apache.log4j.Logger;

import com.jstatcom.component.StdMessages;
import com.jstatcom.engine.ConfigHolder;
import com.jstatcom.engine.EngineTypes;

/**
 * This class stores information to create the GAUSS lcg files from the source
 * files.
 * 
 * @author Markus Kraetzig 
 */
public final class LIBSet {
    private static final Logger log = Logger.getLogger(LIBSet.class);

    private final List libList = new ArrayList();

    public final String LIBS;

    /**
     * XML format field for (de)serialization.
     */
    public static final XmlFormat LIBSet_XML = new XmlFormat(
            LIBSet.class) {
        public void format(LIBSet data, XmlElement xml) {
            xml.setAttribute("libs", data.LIBS);

            for (Iterator it = data.libList.iterator(); it.hasNext();)
                xml.add(it.next());
        }

        public LIBSet parse(XmlElement xml) {
            String libraries = xml.getAttribute("libs") + "";

            LIBSet newSet = new LIBSet(libraries);

            while (xml.hasNext()) {
                SRCLibPair pair = (SRCLibPair) xml.getNext();
                newSet.addSRCLibMapping(pair.libraryName, pair.srcFile);
            }
            return newSet;
        }
    };

    /**
     * Innter class that stores a libraryname - sourcefile mapping.
     */
    public static class SRCLibPair {
        public final String srcFile;

        public final String libraryName;

        /**
         * XML format field for (de)serialization.
         */
        public static final XmlFormat SRCLibPair_XML = new XmlFormat(
                SRCLibPair.class) {
            public void format(SRCLibPair data, XmlElement xml) {
                xml.setAttribute("libname", data.libraryName);
                xml.setAttribute("filename", data.srcFile);
            }

            public SRCLibPair parse(XmlElement xml) {
                String srcFile = xml.getAttribute("filename") + "";
                String lib = xml.getAttribute("libname") + "";

                return new SRCLibPair(lib, srcFile);
            }
        };

        /**
         * Creates a SRCLibPair.
         * 
         * @param libraryName
         *            the name of the library
         * @param srcFile
         *            the source file that belongs to the library
         */
        public SRCLibPair(String libraryName, String srcFile) {
            this.srcFile = srcFile;
            this.libraryName = libraryName;
        }

        /**
         * Overrides hashCode, because equals was overwritten.
         * The algorithm was taken from (Joshua Bloch, "Effective Java",
         * Item 8) .
         * 
         * @return hash code
         */
        public final int hashCode() {
            int result = 17;
            result = 37 * result + srcFile.hashCode();
            result = 37 * result + libraryName.toUpperCase().hashCode();
            return result;
        }

        /**
         * Overrides equals to provide logical comparison of two
         * different SRCLibPair objects.
         * 
         * @param o
         *            object to compare with
         * @return true if src file and lib name are equal
         *         without regarding the case for the lib name
         */
        public final boolean equals(Object o) {
            if (o == this)
                return true;

            if (!(o instanceof SRCLibPair))
                return false;
            SRCLibPair other = (SRCLibPair) o;
            boolean ret = (libraryName.equalsIgnoreCase(other.libraryName) && srcFile
                    .equals(other.srcFile));

            return ret;

        }
    }

    /**
     * Generates GAUSS lcg libraries from source files according to the settings
     * for GaussEngine.
     * 
     * @param args
     *            this is ignored
     */
    public static void main(String[] args) {

        GaussEngine gauss = GaussEngine.getInstance();
        gauss.start(false);
        String error = null;
        if (gauss != null)
            if ((error = gauss.createLibs()) != null) {
                StdMessages.error(error, "GAUSS Library Error");
                System.exit(0);
            } else {
                StdMessages
                        .infoGeneral("GAUSS library generation finished ok.");
                System.exit(0);
            }

        else
            log
                    .error("GAUSS could not be initialized, library generation failed.");
    }

    /**
     * Creates an empty LIBSet.
     * 
     * @param libraries
     *            comma separated libraries to load before calling compile
     */
    public LIBSet(String libraries) {
        super();
        this.LIBS = libraries;
    }

    /**
     * Adds a sourcefile - libraryname mapping to the list.
     * 
     * @param libName
     *            the library that should reference fileName
     * @param fileName
     *            the src file to add
     * @throws RuntimeException
     *             if the sourcefile does not exist or if libName
     *             is empty
     * @throws IllegalArgumentException
     *             if (fileName == null || libName == null)
     */
    public void addSRCLibMapping(String libName, String fileName) {

        if (fileName == null || libName == null)
            throw new IllegalArgumentException("Argument was null.");

        libName = libName.trim();
        String fName = fileName.toString().trim();

        File srcFile = new File(EngineTypes.GAUSS.DIR_NAME + "/" + fName);
        String toAdd = srcFile.getAbsolutePath();
        if (!srcFile.isFile())
            throw new RuntimeException("\nFile \"" + toAdd
                    + "\" does not exist.");

        // Check for potential 3.2 problem.
        if (toAdd.indexOf(" ") > 0
                && ConfigHolder.valueOf(EngineTypes.GAUSS).getConfig(
                        GaussConfigKeys.GAUSS_VERSION).equals("3.2"))
            throw new RuntimeException("\nFilename \"" + toAdd
                    + "\" contains whitespace characters.");

        if (libName.length() == 0)
            throw new RuntimeException("\nLibrary name is empty.");

        SRCLibPair pair = new SRCLibPair(libName, fileName);
        if (!libList.contains(pair))
            libList.add(pair);
    }

    /**
     * Gets a list iterator for libname - sourcefile pairs. The order from the
     * XML representation is preserved.
     * 

* The pairs are stored as instances of LIBSet.SRCLibPair. * * @return an iterator */ public Iterator iterator() { return libList.iterator(); } }

Ads help maintain this website.