KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > izforge > izpack > installer > ScriptParser


1 /*
2  * IzPack - Copyright 2001-2007 Julien Ponge, All Rights Reserved.
3  *
4  * http://www.izforge.com/izpack/
5  * http://developer.berlios.de/projects/izpack/
6  *
7  * Copyright 2001 Johannes Lehtinen
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */

21
22 package com.izforge.izpack.installer;
23
24 import java.io.BufferedInputStream JavaDoc;
25 import java.io.BufferedOutputStream JavaDoc;
26 import java.io.File JavaDoc;
27 import java.io.FileInputStream JavaDoc;
28 import java.io.FileOutputStream JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.util.Collection JavaDoc;
31 import java.util.Iterator JavaDoc;
32
33 import com.izforge.izpack.ParsableFile;
34 import com.izforge.izpack.util.OsConstraint;
35 import com.izforge.izpack.util.VariableSubstitutor;
36
37 /**
38  * The script parser classe.
39  *
40  * @author Julien Ponge
41  * @author Johannes Lehtinen
42  */

43 public class ScriptParser
44 {
45
46     /** The install path. */
47     public final static String JavaDoc INSTALL_PATH = "INSTALL_PATH";
48
49     /** The Java home path. */
50     public final static String JavaDoc JAVA_HOME = "JAVA_HOME";
51     
52     /** The ClassPath. */
53     public final static String JavaDoc CLASS_PATH = "CLASS_PATH";
54
55     /** The user home path. */
56     public final static String JavaDoc USER_HOME = "USER_HOME";
57
58     /** The user name. */
59     public final static String JavaDoc USER_NAME = "USER_NAME";
60
61         /** The hostname. */
62     public final static String JavaDoc HOST_NAME = "HOST_NAME";
63
64         /** The ip address. */
65     public final static String JavaDoc IP_ADDRESS = "IP_ADDRESS";
66
67     /** The file separator character. */
68     public final static String JavaDoc FILE_SEPARATOR = "FILE_SEPARATOR";
69
70     /** The application name. */
71     public final static String JavaDoc APP_NAME = "APP_NAME";
72
73     /** The application URL. */
74     public final static String JavaDoc APP_URL = "APP_URL";
75
76     /** The application version. */
77     public final static String JavaDoc APP_VER = "APP_VER";
78
79     /** The language IS03 code. */
80     public final static String JavaDoc ISO3_LANG = "ISO3_LANG";
81     
82
83     /** The files to parse. */
84     private Collection JavaDoc files;
85
86     /** The variables substituror. */
87     private VariableSubstitutor vs;
88
89     /**
90      * Constructs a new parser. The parsable files specified must have pretranslated paths
91      * (variables expanded and file separator characters converted if necessary).
92      *
93      * @param files the parsable files to process
94      * @param vs the variable substitutor to use
95      */

96     public ScriptParser(Collection JavaDoc files, VariableSubstitutor vs)
97     {
98         this.files = files;
99         this.vs = vs;
100     }
101
102     /**
103      * Parses the files.
104      *
105      * @exception Exception Description of the Exception
106      */

107     public void parseFiles() throws Exception JavaDoc
108     {
109         // Parses the files
110
Iterator JavaDoc iter = files.iterator();
111         while (iter.hasNext())
112         {
113             // If interrupt is desired, return immediately.
114
if (Unpacker.isInterruptDesired()) return;
115             // Create a temporary file for the parsed data
116
// (Use the same directory so that renaming works later)
117
ParsableFile pfile = (ParsableFile) iter.next();
118
119             // check whether the OS matches
120
if (!OsConstraint.oneMatchesCurrentSystem(pfile.osConstraints))
121             {
122                 continue;
123             }
124
125             File JavaDoc file = new File JavaDoc(pfile.path);
126             File JavaDoc parsedFile = File.createTempFile("izpp", null, file.getParentFile());
127
128             // Parses the file
129
// (Use buffering because substitutor processes byte at a time)
130
FileInputStream JavaDoc inFile = new FileInputStream JavaDoc(file);
131             BufferedInputStream JavaDoc in = new BufferedInputStream JavaDoc(inFile, 5120);
132             FileOutputStream JavaDoc outFile = new FileOutputStream JavaDoc(parsedFile);
133             BufferedOutputStream JavaDoc out = new BufferedOutputStream JavaDoc(outFile, 5120);
134             vs.substitute(in, out, pfile.type, pfile.encoding);
135             in.close();
136             out.close();
137
138             // Replace the original file with the parsed one
139
file.delete();
140             if (!parsedFile.renameTo(file))
141                 throw new IOException JavaDoc("Could not rename file " + parsedFile + " to " + file);
142         }
143     }
144 }
145
Popular Tags