krysalis-version/src/java/org/krysalis/version/ant/environment EnvironmentTask.java,NONE,1.1

[email protected]
Newsgroups gmane.comp.krysalis.sandbox
Message-ID <[email protected]>
Update of /cvsroot/metamorphosis/krysalis-version/src/java/org/krysalis/version/ant/environment
In directory sc8-pr-cvs1:/tmp/cvs-serv13187/src/java/org/krysalis/version/ant/environment

Added Files:
	EnvironmentTask.java 
Log Message:
Back into one project...

--- NEW FILE: EnvironmentTask.java ---
/*
The Krysalis Patchy Software License, Version 1.1_01

Copyright (c) 2002-2003 Nicola Ken Barozzi.  All rights reserved.

This Licence is compatible with the BSD licence as described and 
approved by http://www.opensource.org/, and is based on the
Apache Software Licence Version 1.1.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in
   the documentation and/or other materials provided with the
   distribution.

3. The end-user documentation included with the redistribution,
   if any, must include the following acknowledgment:
      "This product includes software developed for project
       Krysalis (http://www.krysalis.org/)."
   Alternately, this acknowledgment may appear in the software itself,
   if and wherever such third-party acknowledgments normally appear.

4. The names "Krysalis" and "Nicola Ken Barozzi" and
   "Krysalis Centipede" must not be used to endorse or promote products
   derived from this software without prior written permission. For
   written permission, please contact [email protected].

5. Products derived from this software may not be called "Krysalis",
   "Krysalis Centipede", nor may "Krysalis" appear in their name, without
    prior written permission of Nicola Ken Barozzi.

6. This software may contain voluntary contributions made by many 
   individuals, who decided to donate the code to this project in respect 
   of this licence.

THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
====================================================================
*/
package org.krysalis.version.ant.environment;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Iterator;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.util.DOMElementWriter;
import org.krysalis.version.Version;
import org.krysalis.version.ant.AntTask;
import org.krysalis.version.collecting.Collector;
import org.krysalis.version.collecting.EntryCollection;
import org.krysalis.version.collecting.VersionCollector;
import org.krysalis.version.collecting.collectors.VersionBrandCollector;
import org.krysalis.version.exception.VersionException;
import org.krysalis.version.loading.VersionLoadContext;
import org.krysalis.version.util.dom.DOMUtils;
import org.krysalis.version.util.note.AnnotationScratchpad;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;

/**
 * @author [email protected]
 */
public class EnvironmentTask extends AntTask {
    private boolean m_xml = false; //:TODO: Add to tests, add dest/file, add overWrite
    private boolean m_full = false;
    private boolean m_annotated = false;

    /**
     * Constructor for ConstraintEvaluatorTask.
     */
    public EnvironmentTask() {
    }

    public void execute() throws org.apache.tools.ant.BuildException {
        try {

            log(
                (m_annotated ? "Annotated " : "")
                    + (m_full ? "Full" : "Branded")
                    + " Version Information",
                Project.MSG_INFO);

            Collector vs =
                (m_full
                    ? ((Collector) new VersionCollector())
                    : ((Collector) new VersionBrandCollector()));

            EntryCollection collection = vs.collect();

            if (m_xml)
                generateXML(collection);
            else
                generateLog(collection);

        }
        catch (BuildException be) {
            throw be;
        }
        catch (IOException ioe) {
            throw new BuildException("Environment processing failed: ", ioe, getLocation());
        }
        catch (VersionException ve) {
            throw new BuildException("Environment processing failed: ", ve, getLocation());
        }
    }

    private void generateLog(EntryCollection collection) {

        AnnotationScratchpad pad = collection.getAnnotations();
        if (null != pad)
            m_ant.logScratchpad(pad);

        if (collection.hasEntries()) {
            collection.sort();

            for (Iterator i = collection.iterator(); i.hasNext();) {
                Object entry = (Object) i.next();
                if (entry instanceof VersionLoadContext) {
                    VersionLoadContext context = (VersionLoadContext) entry;
                    Version version = context.getVersion();

                    log(
                        "Version: "
                            + version.getId()
                            + ":"
                            + version.getVersion(),
                        Project.MSG_INFO);

                    if (m_annotated && context.hasAnnotations()) {
                        for (Iterator ii = context.getAnnotations().iterator();
                            ii.hasNext();
                            ) {
                            Object note = ii.next();
                            log(" - " + note, Project.MSG_INFO);
                        }
                    }
                }
                else
                    log(
                        entry.getClass().getName() + ":" + entry.toString(),
                        Project.MSG_INFO);
            }
        }
        else
            log("None found in environment", Project.MSG_INFO);
    }

    private void generateXML(EntryCollection collection) throws IOException {

        //:TODO: Move pad?

        AnnotationScratchpad pad = collection.getAnnotations();
        if (null != pad)
            m_ant.logScratchpad(pad);

        if (collection.hasEntries()) {
            collection.sort();

            Document doc = DOMUtils.createDocument();
            
            Element env = doc.createElement("Environment");
            
            doc.appendChild(env);

            for (Iterator i = collection.iterator(); i.hasNext();) {
                Object entry = (Object) i.next();
                if (entry instanceof VersionLoadContext) {
                    VersionLoadContext context = (VersionLoadContext) entry;
                    Version version = context.getVersion();
//:TODO: Create XML Exporter -- but JDK ???? only, keep out of core
// XMLHelper.exportVersion(Version v) -- checks to see if it is a
// CompoundV -- and hence .. -- also Kry -- and hence
// 
//  Import XML as 'smart'
//
                    Element v = doc.createElement("Version");
                    Attr a = doc.createAttribute("versionId");
                    a.setNodeValue(version.getId());
                    v.setAttributeNode(a);
                    Text t = doc.createTextNode(version.getVersion());
                    v.appendChild(t);
                    env.appendChild(v);

                    if (m_annotated && context.hasAnnotations()) {
                        for (Iterator ii = context.getAnnotations().iterator();
                            ii.hasNext();
                            ) {
                            Object note = ii.next();
                            log(" - " + note, Project.MSG_INFO);
                        }
                    }
                }
                else
                    log(
                        entry.getClass().getName() + ":" + entry.toString(),
                        Project.MSG_INFO);
            }

            DOMElementWriter writer = new DOMElementWriter();
            OutputStream out = new FileOutputStream("env.xml");
            writer.write(env, out);
            out.flush();
            out.close();
        }
        else
            log("None found in environment", Project.MSG_INFO);
    }
    /**
     * Returns the full.
     * @return boolean
     */
    public boolean isFull() {
        return m_full;
    }

    /**
     * Sets the full.
     * @param full The full to set
     */
    public void setFull(boolean full) {
        m_full = full;
    }
    /**
     * Returns the annotated.
     * @return boolean
     */
    public boolean isAnnotated() {
        return m_annotated;
    }

    /**
     * Sets the annotated.
     * @param annotated The annotated to set
     */
    public void setAnnotated(boolean annotated) {
        m_annotated = annotated;
    }
}




-------------------------------------------------------
This SF.NET email is sponsored by:
SourceForge Enterprise Edition + IBM + LinuxWorld = Something 2 See!
http://www.vasoftware.com
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.