📄 filereplacement.java
字号:
package com.sslexplorer.vpn.util;
import java.util.Hashtable;
import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.FileOutputStream;
import java.util.Random;
import java.io.ByteArrayOutputStream;
/**
* This utility will take an input file and replace any number of
* tokens before outputing it to an output file, which may or may
* not be temporary.
*
* @author Lee David Painter
* @version $Revision: 1.8 $
*/
public class FileReplacement {
Hashtable tokens = new Hashtable();
File templateFile;
File outputFile;
File cwd;
String parameter;
String encoding = "UTF8";
public FileReplacement(File cwd) {
this.cwd = cwd;
}
public String getId() {
return parameter;
}
void processReplacementXML(XMLElement el, ApplicationLauncher launcher) throws IOException {
if(!el.getName().equalsIgnoreCase("replacements")) {
throw new IOException("Error! Element is not <replacements>");
}
if(el.getAttribute("templateFile")==null) {
throw new IOException("Error! <replacements> element requires 'templateFile' attribute");
}
templateFile = new File(cwd, el.getAttribute("templateFile").toString());
if(launcher.events!=null)
launcher.events.debug("Template file will be " + templateFile.getAbsolutePath());
if(el.getAttribute("outputFile")!=null) {
outputFile = new File(cwd, el.getAttribute("outputFile").toString());
} else {
outputFile = getTempFile(cwd);
}
if(launcher.events!=null)
launcher.events.debug("Output file will be " + outputFile.getAbsolutePath());
if(el.getAttribute("parameter")==null) {
throw new IOException("Error! <replacements> element requires 'parameter' attribute");
}
parameter = el.getAttribute("parameter").toString();
launcher.addParameter(parameter, outputFile.getAbsolutePath());
if(el.getAttribute("encoding")!=null)
encoding = el.getAttribute("encoding").toString();
if(launcher.events!=null)
launcher.events.debug("Output file will be encoded in " + encoding);
for(Enumeration e = el.getChildren().elements(); e.hasMoreElements();) {
XMLElement child = (XMLElement)e.nextElement();
if(!child.getName().equalsIgnoreCase("replace")) {
throw new IOException("Error! <" + child.getName() + "> is not a supported element of <replacements>");
}
if(child.getAttribute("token")==null || child.getAttribute("value")==null) {
throw new IOException("Error! <replace> element requires 'token' and 'value' attributes");
}
tokens.put(child.getAttribute("token").toString(), child.getAttribute("value").toString());
}
}
void createReplacementsFile(ApplicationLauncher launcher) throws IOException {
FileInputStream in = new FileInputStream(templateFile);
ByteArrayOutputStream tmp = new ByteArrayOutputStream();
byte[] buf = new byte[4096];
int read;
while((read = in.read(buf))>-1) {
tmp.write(buf, 0, read);
}
// First replace all standard parameters etc
String outputContent = launcher.replaceTokens(new String(tmp.toByteArray(), encoding));
for(Enumeration e = tokens.keys(); e.hasMoreElements();) {
String token = (String) e.nextElement();
String value = (String) tokens.get(token);
if(launcher.events!=null)
launcher.events.debug("Processing replacement token " + token + "=" + value);
// Perform replacement in String
outputContent = ApplicationLauncher.replaceAllTokens(outputContent, token, value);
}
FileOutputStream out = new FileOutputStream(outputFile);
try {
out.write(outputContent.getBytes(encoding));
}
finally {
out.close();
}
ClientCacheRemover.getInstance().trackFileForRemoval(outputFile);
}
static File getTempFile ( File near ) throws IOException {
String path = null;
if ( near != null )
if ( near.isFile() ) path = near.getParent();
else if ( near.isDirectory() ) path = near.getPath();
Random wheel = new Random(); // seeded from the clock
File tempFile = null;
do
{
// generate random a number 10,000,000 .. 99,999,999
int unique = ( wheel.nextInt() & Integer. MAX_VALUE ) %90000000 + 10000000;
tempFile = new File( path, Integer.toString ( unique) + ".tmp" );
} while ( tempFile.exists() );
// We "finally" found a name not already used. Nearly always the first time.
// Quickly stake our claim to it by opening/closing it to create it.
// In theory somebody could have grabbed it in that tiny window since
// we checked if it exists, but that is highly unlikely.
new FileOutputStream( tempFile ).close();
// debugging peek at the name generated.
if ( false )
{
System.out.println( tempFile.getCanonicalPath());
}
return tempFile;
} // end getTempFile
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -