/*
 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 compile a GAUSS gcg file from the source
 * files.
 * 
 * @author Markus Kraetzig 
 */
public final class GCGSet {
    private static final Logger log = Logger.getLogger(GCGSet.class);

    public final String GCG_NAME;

    public final String LIBS;

    private final List fileList = new ArrayList();

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

            for (Iterator it = data.fileList.iterator(); it.hasNext();)
                xml.add(new SRCFile(it.next() + ""));
        }

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

            GCGSet newSet = new GCGSet(gcgNam, libraries);

            while (xml.hasNext()) {
                SRCFile file = (SRCFile) xml.getNext();
                newSet.addSRCFile(file.srcFile);
            }
            return newSet;
        }
    };

    /**
     * Innter class that stores an src file.
     */
    public static class SRCFile {
        final String srcFile;

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

            }

            public SRCFile parse(XmlElement xml) {
                String srcFile = xml.getAttribute("filename") + "";
                return new SRCFile(srcFile);
            }
        };

        public SRCFile(String srcFile) {
            this.srcFile = srcFile;
        }

    }

    /**
     * Creates a GCGSet with then name of the compiled file.
     * 
     * @param gcgName
     *            the name of the output file (without path) *
     * @param libraries
     *            comma separated libraries to load before calling compile
     * 
     * @throws RuntimeException
     *             if (gcgName == null)
     */
    public GCGSet(String gcgName, String libraries) {

        if (gcgName == null)
            throw new RuntimeException("\nGCG name was null.");

        this.GCG_NAME = gcgName;
        this.LIBS = libraries;
    }

    /**
     * Adds an src file to the list.
     * 
     * @param fileName
     *            the src file to add
     * @throws RuntimeException
     *             if the sourcefile does not exist
     * @throws IllegalArgumentException
     *             if (fileName == null)
     */
    public void addSRCFile(String fileName) {

        if (fileName == null)
            throw new IllegalArgumentException("\nFilename was null.");

        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 (!fileList.contains(fName))
            fileList.add(fName);
    }

    /**
     * Gets a list iterator for all source file names. The order from the XML
     * representation is preserved.
     * 
     * @return an iterator
     */
    public Iterator iterator() {
        return fileList.iterator();
    }

    /**
     * Compiles a GAUSS gcg file 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.compileGCG()) != null) {
                StdMessages.error(error, "GAUSS Compile Error");
                System.exit(0);
            } else {
                StdMessages
                        .infoGeneral("GAUSS gcg file compilation finished ok.");
                System.exit(0);
            }
        } else
            log.error("GAUSS could not be initialized, compilation failed.");
    }

}
 

Ads help maintain this website.