001/*
002 /*
003 * IzPack - Copyright 2001-2005 Julien Ponge, All Rights Reserved.
004 * 
005 * http://www.izforge.com/izpack/
006 * http://developer.berlios.de/projects/izpack/
007 * 
008 * Copyright 2003 Jonathan Halliday
009 * 
010 * Licensed under the Apache License, Version 2.0 (the "License");
011 * you may not use this file except in compliance with the License.
012 * You may obtain a copy of the License at
013 * 
014 *     http://www.apache.org/licenses/LICENSE-2.0
015 *     
016 * Unless required by applicable law or agreed to in writing, software
017 * distributed under the License is distributed on an "AS IS" BASIS,
018 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019 * See the License for the specific language governing permissions and
020 * limitations under the License.
021 */
022
023package com.izforge.izpack.panels;
024
025import java.io.IOException;
026
027import net.n3.nanoxml.XMLElement;
028
029import com.izforge.izpack.installer.AutomatedInstallData;
030import com.izforge.izpack.installer.CompileHandler;
031import com.izforge.izpack.installer.CompileResult;
032import com.izforge.izpack.installer.CompileWorker;
033import com.izforge.izpack.installer.PanelAutomation;
034import com.izforge.izpack.installer.PanelAutomationHelper;
035
036/**
037 * Functions to support automated usage of the CompilePanel
038 * 
039 * @author Jonathan Halliday
040 * @author Tino Schwarze
041 */
042public class CompilePanelAutomationHelper extends PanelAutomationHelper implements PanelAutomation,
043        CompileHandler
044{
045
046    private CompileWorker worker = null;
047
048    private int job_max = 0;
049
050    private String job_name = null;
051
052    private int last_line_len = 0;
053
054    /**
055     * Save data for running automated.
056     * 
057     * @param installData installation parameters
058     * @param panelRoot unused.
059     */
060    public void makeXMLData(AutomatedInstallData installData, XMLElement panelRoot)
061    {
062        // not used here - during automatic installation, no automatic
063        // installation information is generated
064    }
065
066    /**
067     * Perform the installation actions.
068     * 
069     * @param panelRoot The panel XML tree root.
070     */
071    public void runAutomated(AutomatedInstallData idata, XMLElement panelRoot)
072    {
073        XMLElement compiler_xml = panelRoot.getFirstChildNamed("compiler");
074
075        String compiler = null;
076
077        if (compiler_xml != null) compiler = compiler_xml.getContent();
078
079        if (compiler == null)
080        {
081            System.out.println("invalid automation data: could not find compiler");
082            return;
083        }
084
085        XMLElement args_xml = panelRoot.getFirstChildNamed("arguments");
086
087        String args = null;
088
089        if (args_xml != null) args = args_xml.getContent();
090
091        if (args_xml == null)
092        {
093            System.out.println("invalid automation data: could not find compiler arguments");
094            return;
095        }
096
097        try
098        {
099            this.worker = new CompileWorker(idata, this);
100            this.worker.setCompiler(compiler);
101            this.worker.setCompilerArguments(args);
102
103            this.worker.run();
104        }
105        catch (IOException e)
106        {
107            e.printStackTrace();
108        }
109    }
110
111    /**
112     * Reports progress on System.out
113     * 
114     * @see com.izforge.izpack.util.AbstractUIProgressHandler#startAction(String, int)
115     */
116    public void startAction(String name, int noOfJobs)
117    {
118        System.out.println("[ Starting compilation ]");
119        this.job_name = "";
120    }
121
122    /**
123     * Reports the error to System.err
124     * 
125     * @param error the error
126     * @see CompileHandler#handleCompileError(CompileResult)
127     */
128    public void handleCompileError(CompileResult error)
129    {
130        System.out.println();
131        System.out.println("[ Compilation failed ]");
132        System.err.println("Command line: " + error.getCmdline());
133        System.err.println();
134        System.err.println("stdout of compiler:");
135        System.err.println(error.getStdout());
136        System.err.println("stderr of compiler:");
137        System.err.println(error.getStderr());
138        // do not abort compilation, just continue
139        error.setAction(CompileResult.ACTION_CONTINUE);
140    }
141
142    /**
143     * Sets state variable for thread sync.
144     * 
145     * @see com.izforge.izpack.util.AbstractUIProgressHandler#stopAction()
146     */
147    public void stopAction()
148    {
149        if ((this.job_name != null) && (this.last_line_len > 0))
150        {
151            String line = this.job_name + ": done.";
152            System.out.print("\r" + line);
153            for (int i = line.length(); i < this.last_line_len; i++)
154                System.out.print(' ');
155            System.out.println();
156        }
157
158        if (this.worker.getResult().isSuccess()) System.out.println("[ Compilation successful ]");
159    }
160
161    /**
162     * Tell about progress.
163     * 
164     * @param val
165     * @param msg
166     * @see com.izforge.izpack.util.AbstractUIProgressHandler#progress(int, String)
167     */
168    public void progress(int val, String msg)
169    {
170        double percentage = ((double) val) * 100.0d / (double) this.job_max;
171
172        String percent = (new Integer((int) percentage)).toString() + '%';
173        String line = this.job_name + ": " + percent;
174
175        int line_len = line.length();
176
177        System.out.print("\r" + line);
178        for (int i = line_len; i < this.last_line_len; i++)
179            System.out.print(' ');
180
181        this.last_line_len = line_len;
182    }
183
184    /**
185     * Reports progress to System.out
186     * 
187     * @param jobName The next job's name.
188     * @param max unused
189     * @param jobNo The next job's number.
190     * @see com.izforge.izpack.util.AbstractUIProgressHandler#nextStep(String, int, int)
191     */
192    public void nextStep(String jobName, int max, int jobNo)
193    {
194        if ((this.job_name != null) && (this.last_line_len > 0))
195        {
196            String line = this.job_name + ": done.";
197            System.out.print("\r" + line);
198            for (int i = line.length(); i < this.last_line_len; i++)
199                System.out.print(' ');
200            System.out.println();
201        }
202
203        this.job_max = max;
204        this.job_name = jobName;
205        this.last_line_len = 0;
206    }
207}