Statistics
| Revision:

root / import / ext3D / trunk / install-extension3d / IzPack / src / lib / com / izforge / izpack / installer / ScriptParser.java @ 15280

History | View | Annotate | Download (4.37 KB)

1
/*
2
 *  $Id: ScriptParser.java,v 1.1 2006/06/14 07:29:07 cesar Exp $
3
 *  IzPack
4
 *  Copyright (C) 2001-2004 Julien Ponge
5
 *
6
 *  File :               ScriptParser.java
7
 *  Description :        Parses the scripts files for special variables.
8
 *  Author's email :     julien@izforge.com
9
 *  Author's Website :   http://www.izforge.com
10
 *
11
 *  Portions are Copyright (c) 2001 Johannes Lehtinen
12
 *  johannes.lehtinen@iki.fi
13
 *  http://www.iki.fi/jle/
14
 *
15
 *  This program is free software; you can redistribute it and/or
16
 *  modify it under the terms of the GNU General Public License
17
 *  as published by the Free Software Foundation; either version 2
18
 *  of the License, or any later version.
19
 *
20
 *  This program is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU General Public License for more details.
24
 *
25
 *  You should have received a copy of the GNU General Public License
26
 *  along with this program; if not, write to the Free Software
27
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
28
 */
29
package com.izforge.izpack.installer;
30

    
31
import java.io.BufferedInputStream;
32
import java.io.BufferedOutputStream;
33
import java.io.File;
34
import java.io.FileInputStream;
35
import java.io.FileOutputStream;
36
import java.io.IOException;
37
import java.util.Collection;
38
import java.util.Iterator;
39

    
40
import com.izforge.izpack.ParsableFile;
41
import com.izforge.izpack.util.OsConstraint;
42

    
43
/**
44
 * The script parser classe.
45
 * @author Julien Ponge
46
 * @author Johannes Lehtinen
47
 */
48
public class ScriptParser
49
{
50
  /**  The install path. */
51
  public final static String INSTALL_PATH = "INSTALL_PATH";
52

    
53
  /**  The Java home path. */
54
  public final static String JAVA_HOME = "JAVA_HOME";
55

    
56
  /**  The user home path. */
57
  public final static String USER_HOME = "USER_HOME";
58

    
59
  /**  The user name. */
60
  public final static String USER_NAME = "USER_NAME";
61

    
62
  /**  The file separator character. */
63
  public final static String FILE_SEPARATOR = "FILE_SEPARATOR";
64

    
65
  /** The application name. */
66
  public final static String APP_NAME = "APP_NAME";
67

    
68
  /** The application URL. */
69
  public final static String APP_URL = "APP_URL";
70

    
71
  /** The application version. */
72
  public final static String APP_VER = "APP_VER";
73

    
74
  /** The language IS03 code. */
75
  public final static String ISO3_LANG = "ISO3_LANG";
76

    
77
  /**  The files to parse. */
78
  private Collection files;
79

    
80
  /**  The variables substituror. */
81
  private VariableSubstitutor vs;
82

    
83
  /**
84
   *  Constructs a new parser. The parsable files specified must have
85
   *  pretranslated paths (variables expanded and file separator characters
86
   *  converted if necessary).
87
   *
88
   * @param  files  the parsable files to process
89
   * @param  vs     the variable substitutor to use
90
   */
91
  public ScriptParser(Collection files, VariableSubstitutor vs)
92
  {
93
    this.files = files;
94
    this.vs = vs;
95
  }
96

    
97
  /**
98
   *  Parses the files.
99
   *
100
   * @exception  Exception  Description of the Exception
101
   */
102
  public void parseFiles() throws Exception
103
  {
104
    // Parses the files
105
    Iterator iter = files.iterator();
106
    while (iter.hasNext())
107
    {
108
      // Create a temporary file for the parsed data
109
      // (Use the same directory so that renaming works later)
110
      ParsableFile pfile = (ParsableFile) iter.next();
111

    
112
      // check whether the OS matches
113
      if (!OsConstraint.oneMatchesCurrentSystem(pfile.osConstraints))
114
      {
115
        continue;
116
      }
117

    
118
      File file = new File(pfile.path);
119
      File parsedFile = File.createTempFile("izpp", null, file.getParentFile());
120

    
121
      // Parses the file
122
      // (Use buffering because substitutor processes byte at a time)
123
      FileInputStream inFile = new FileInputStream(file);
124
      BufferedInputStream in = new BufferedInputStream(inFile, 5120);
125
      FileOutputStream outFile = new FileOutputStream(parsedFile);
126
      BufferedOutputStream out = new BufferedOutputStream(outFile, 5120);
127
      vs.substitute(in, out, pfile.type, pfile.encoding);
128
      in.close();
129
      out.close();
130

    
131
      // Replace the original file with the parsed one
132
      file.delete();
133
      if (!parsedFile.renameTo(file))
134
        throw new IOException(
135
          "Could not rename file " + parsedFile + " to " + file);
136
    }
137
  }
138
}