| 1 package com.sslexplorer.agent.client.util; 2 3 import java.io.BufferedReader ; 4 import java.io.ByteArrayOutputStream ; 5 import java.io.File ; 6 import java.io.FileInputStream ; 7 import java.io.FileOutputStream ; 8 import java.io.IOException ; 9 import java.io.InputStreamReader ; 10 import java.io.PrintWriter ; 11 import java.io.StringReader ; 12 import java.text.MessageFormat ; 13 import java.util.Enumeration ; 14 import java.util.Hashtable ; 15 import java.util.Random ; 16 17 24 public class FileReplacement { 25 26 public final static String DOS_EOL = "dos"; 27 public final static String UNIX_EOL = "unix"; 28 public final static String MAC_EOL = "mac"; 29 public final static String PLATFORM_EOL = "platform"; 30 31 33 private Hashtable tokens = new Hashtable (); 34 private File templateFile; 35 private File outputFile; 36 private File cwd; 37 private String parameter; 38 private String encoding = "UTF8"; private String convertLineEndings; 40 41 46 public FileReplacement(File cwd) { 47 this.cwd = cwd; 48 } 49 50 56 public String getId() { 57 return parameter; 58 } 59 60 void processReplacementXML(XMLElement el, AbstractApplicationLauncher launcher) throws IOException { 61 if (!el.getName().equalsIgnoreCase("replacements")) { throw new IOException (Messages.getString("FileReplacement.elementIsNotReplacements")); } 64 65 if (el.getAttribute("templateFile") == null) { throw new IOException (Messages.getString("FileReplacement.elementRequiresTemplateFileAttribute")); } 68 69 convertLineEndings = el.getStringAttribute("convertLineEndings"); 70 71 templateFile = new File (cwd, el.getAttribute("templateFile").toString()); 73 if (launcher.events != null) 74 launcher.events.debug(MessageFormat.format(Messages.getString("FileReplacement.templateFile"), new Object [] { templateFile.getAbsolutePath() } ) ); 76 if (el.getAttribute("outputFile") != null) { outputFile = new File (cwd, el.getAttribute("outputFile").toString()); } else { 79 outputFile = getTempFile(cwd); 80 } 81 82 if (launcher.events != null) 83 launcher.events.debug(MessageFormat.format(Messages.getString("FileReplacement.outputFile"), new Object [] { outputFile.getAbsolutePath() } ) ); 85 if (el.getAttribute("parameter") == null) { throw new IOException (Messages.getString("FileReplacement.replacementsRequiresParameterAttribute")); } 88 89 parameter = el.getAttribute("parameter").toString(); launcher.addParameter(parameter, outputFile.getAbsolutePath()); 91 92 if (el.getAttribute("encoding") != null) encoding = el.getAttribute("encoding").toString(); 95 if (launcher.events != null) 96 launcher.events.debug(MessageFormat.format(Messages.getString("FileReplacement.outputFileEncoding"), new Object [] { encoding } ) ); 98 for (Enumeration e = el.getChildren().elements(); e.hasMoreElements();) { 99 XMLElement child = (XMLElement) e.nextElement(); 100 101 if (!child.getName().equalsIgnoreCase("replace")) { throw new IOException (MessageFormat.format(Messages.getString("FileReplacement.notSupported"), new Object [] { child.getName() } ) ) ; } 104 105 if (child.getAttribute("token") == null || child.getAttribute("value") == null) { throw new IOException (Messages.getString("FileReplacement.replaceRequiresTokenAndValue")); } 108 109 tokens.put(child.getAttribute("token").toString(), child.getAttribute("value").toString()); } 111 112 } 113 114 void createReplacementsFile(AbstractApplicationLauncher launcher) throws IOException { 115 116 FileInputStream in = new FileInputStream (templateFile); 117 ByteArrayOutputStream tmp = new ByteArrayOutputStream (); 118 119 byte[] buf = new byte[4096]; 120 int read; 121 122 while ((read = in.read(buf)) > -1) { 123 tmp.write(buf, 0, read); 124 } 125 126 String outputContent = launcher.replaceTokens(new String (tmp.toByteArray(), encoding)); 128 129 for (Enumeration e = tokens.keys(); e.hasMoreElements();) { 130 131 String token = (String ) e.nextElement(); 132 String value = (String ) tokens.get(token); 133 134 if (launcher.events != null) 135 launcher.events.debug(MessageFormat.format(Messages.getString("FileReplacement.processingReplacement"), new Object [] { token, value } ) ); 137 outputContent = AbstractApplicationLauncher.replaceAllTokens(outputContent, token, value); 139 140 } 141 142 if(convertLineEndings != null && !convertLineEndings.equals(PLATFORM_EOL)) { 143 BufferedReader br = new BufferedReader (new StringReader (outputContent)); 144 String line = null; 145 PrintWriter pw = new PrintWriter (new FileOutputStream (outputFile)); 146 try { 147 while( ( line = br.readLine() ) != null) { 148 if(DOS_EOL.equals(convertLineEndings)) { 149 pw.print(line + "\r\n"); 150 } 151 else if(UNIX_EOL.equals(convertLineEndings)) { 152 pw.print(line + "\n"); 153 } 154 else if(MAC_EOL.equals(convertLineEndings)) { 155 pw.print(line + "\r"); 156 } 157 else { 158 pw.println(line); 159 } 160 } 161 } finally { 162 pw.close(); 163 } 164 } 165 else { 166 FileOutputStream out = new FileOutputStream (outputFile); 167 try { 168 out.write(outputContent.getBytes(encoding)); 169 } finally { 170 out.close(); 171 } 172 } 173 174 175 } 176 177 static File getTempFile(File near) throws IOException { 178 String path = null; 179 if (near != null) 180 if (near.isFile()) 181 path = near.getParent(); 182 else if (near.isDirectory()) 183 path = near.getPath(); 184 185 Random wheel = new Random (); File tempFile = null; 187 do { 188 int unique = (wheel.nextInt() & Integer.MAX_VALUE) % 90000000 + 10000000; 190 tempFile = new File (path, Integer.toString(unique) + ".tmp"); } while (tempFile.exists()); 192 new FileOutputStream (tempFile).close(); 198 199 if (false) { 201 System.out.println(tempFile.getCanonicalPath()); 202 } 203 return tempFile; 204 } } 206 | Popular Tags |