001: /*
002: * IzPack - Copyright 2001-2008 Julien Ponge, All Rights Reserved.
003: *
004: * http://izpack.org/
005: * http://izpack.codehaus.org/
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:
022: package com.izforge.izpack.installer;
023:
024: import java.io.BufferedInputStream;
025: import java.io.BufferedOutputStream;
026: import java.io.File;
027: import java.io.FileInputStream;
028: import java.io.FileOutputStream;
029: import java.io.IOException;
030: import java.util.Collection;
031: import java.util.Iterator;
032:
033: import com.izforge.izpack.ParsableFile;
034: import com.izforge.izpack.util.OsConstraint;
035: import com.izforge.izpack.util.VariableSubstitutor;
036:
037: /**
038: * The script parser classe.
039: *
040: * @author Julien Ponge
041: * @author Johannes Lehtinen
042: */
043: public class ScriptParser {
044:
045: /** The install path. */
046: public final static String INSTALL_PATH = "INSTALL_PATH";
047:
048: /** The Java home path. */
049: public final static String JAVA_HOME = "JAVA_HOME";
050:
051: /** The ClassPath. */
052: public final static String CLASS_PATH = "CLASS_PATH";
053:
054: /** The user home path. */
055: public final static String USER_HOME = "USER_HOME";
056:
057: /** The user name. */
058: public final static String USER_NAME = "USER_NAME";
059:
060: /** The hostname. */
061: public final static String HOST_NAME = "HOST_NAME";
062:
063: /** The ip address. */
064: public final static String IP_ADDRESS = "IP_ADDRESS";
065:
066: /** The file separator character. */
067: public final static String FILE_SEPARATOR = "FILE_SEPARATOR";
068:
069: /** The application name. */
070: public final static String APP_NAME = "APP_NAME";
071:
072: /** The application URL. */
073: public final static String APP_URL = "APP_URL";
074:
075: /** The application version. */
076: public final static String APP_VER = "APP_VER";
077:
078: /** The language IS03 code. */
079: public final static String ISO3_LANG = "ISO3_LANG";
080:
081: /** The language code as _ll_CC like used with ResourceBoundle. */
082: public final static String LOCALE = "LOCALE_IDENTIFIER";
083:
084: /** The files to parse. */
085: private Collection<ParsableFile> files;
086:
087: /** The variables substituror. */
088: private VariableSubstitutor vs;
089:
090: /**
091: * Constructs a new parser. The parsable files specified must have pretranslated paths
092: * (variables expanded and file separator characters converted if necessary).
093: *
094: * @param files the parsable files to process
095: * @param vs the variable substitutor to use
096: */
097: public ScriptParser(Collection<ParsableFile> files,
098: VariableSubstitutor vs) {
099: this .files = files;
100: this .vs = vs;
101: }
102:
103: /**
104: * Parses the files.
105: *
106: * @exception Exception Description of the Exception
107: */
108: public void parseFiles() throws Exception {
109: // Parses the files
110: Iterator<ParsableFile> iter = files.iterator();
111: while (iter.hasNext()) {
112: // If interrupt is desired, return immediately.
113: if (Unpacker.isInterruptDesired())
114: return;
115: // Create a temporary file for the parsed data
116: // (Use the same directory so that renaming works later)
117: ParsableFile pfile = iter.next();
118:
119: // check whether the OS matches
120: if (!OsConstraint
121: .oneMatchesCurrentSystem(pfile.osConstraints)) {
122: continue;
123: }
124:
125: File file = new File(pfile.path);
126: File parsedFile = File.createTempFile("izpp", null, file
127: .getParentFile());
128:
129: // Parses the file
130: // (Use buffering because substitutor processes byte at a time)
131: FileInputStream inFile = new FileInputStream(file);
132: BufferedInputStream in = new BufferedInputStream(inFile,
133: 5120);
134: FileOutputStream outFile = new FileOutputStream(parsedFile);
135: BufferedOutputStream out = new BufferedOutputStream(
136: outFile, 5120);
137: vs.substitute(in, out, pfile.type, pfile.encoding);
138: in.close();
139: out.close();
140:
141: // Replace the original file with the parsed one
142: file.delete();
143: if (!parsedFile.renameTo(file))
144: throw new IOException("Could not rename file "
145: + parsedFile + " to " + file);
146: }
147: }
148: }
|