001/*
002 * IzPack - Copyright 2001-2005 Julien Ponge, All Rights Reserved.
003 * 
004 * http://www.izforge.com/izpack/
005 * http://developer.berlios.de/projects/izpack/
006 * 
007 * Copyright 2001 Johannes Lehtinen
008 * 
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 * 
013 *     http://www.apache.org/licenses/LICENSE-2.0
014 *     
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 */
021
022package com.izforge.izpack.installer;
023
024import java.io.BufferedInputStream;
025import java.io.BufferedOutputStream;
026import java.io.File;
027import java.io.FileInputStream;
028import java.io.FileOutputStream;
029import java.io.IOException;
030import java.util.Collection;
031import java.util.Iterator;
032
033import com.izforge.izpack.ParsableFile;
034import com.izforge.izpack.util.OsConstraint;
035import com.izforge.izpack.util.VariableSubstitutor;
036
037/**
038 * The script parser classe.
039 * 
040 * @author Julien Ponge
041 * @author Johannes Lehtinen
042 */
043public class ScriptParser
044{
045
046    /** The install path. */
047    public final static String INSTALL_PATH = "INSTALL_PATH";
048
049    /** The Java home path. */
050    public final static String JAVA_HOME = "JAVA_HOME";
051
052    /** The user home path. */
053    public final static String USER_HOME = "USER_HOME";
054
055    /** The user name. */
056    public final static String USER_NAME = "USER_NAME";
057
058    /** The file separator character. */
059    public final static String FILE_SEPARATOR = "FILE_SEPARATOR";
060
061    /** The application name. */
062    public final static String APP_NAME = "APP_NAME";
063
064    /** The application URL. */
065    public final static String APP_URL = "APP_URL";
066
067    /** The application version. */
068    public final static String APP_VER = "APP_VER";
069
070    /** The language IS03 code. */
071    public final static String ISO3_LANG = "ISO3_LANG";
072
073    /** The files to parse. */
074    private Collection files;
075
076    /** The variables substituror. */
077    private VariableSubstitutor vs;
078
079    /**
080     * Constructs a new parser. The parsable files specified must have pretranslated paths
081     * (variables expanded and file separator characters converted if necessary).
082     * 
083     * @param files the parsable files to process
084     * @param vs the variable substitutor to use
085     */
086    public ScriptParser(Collection files, VariableSubstitutor vs)
087    {
088        this.files = files;
089        this.vs = vs;
090    }
091
092    /**
093     * Parses the files.
094     * 
095     * @exception Exception Description of the Exception
096     */
097    public void parseFiles() throws Exception
098    {
099        // Parses the files
100        Iterator iter = files.iterator();
101        while (iter.hasNext())
102        {
103            // If interrupt is desired, return immediately.
104            if (Unpacker.isInterruptDesired()) return;
105            // Create a temporary file for the parsed data
106            // (Use the same directory so that renaming works later)
107            ParsableFile pfile = (ParsableFile) iter.next();
108
109            // check whether the OS matches
110            if (!OsConstraint.oneMatchesCurrentSystem(pfile.osConstraints))
111            {
112                continue;
113            }
114
115            File file = new File(pfile.path);
116            File parsedFile = File.createTempFile("izpp", null, file.getParentFile());
117
118            // Parses the file
119            // (Use buffering because substitutor processes byte at a time)
120            FileInputStream inFile = new FileInputStream(file);
121            BufferedInputStream in = new BufferedInputStream(inFile, 5120);
122            FileOutputStream outFile = new FileOutputStream(parsedFile);
123            BufferedOutputStream out = new BufferedOutputStream(outFile, 5120);
124            vs.substitute(in, out, pfile.type, pfile.encoding);
125            in.close();
126            out.close();
127
128            // Replace the original file with the parsed one
129            file.delete();
130            if (!parsedFile.renameTo(file))
131                throw new IOException("Could not rename file " + parsedFile + " to " + file);
132        }
133    }
134}