1 22 package org.jboss.varia.deployment.convertor; 23 24 import java.io.ByteArrayInputStream ; 25 import java.io.ByteArrayOutputStream ; 26 import java.io.File ; 27 import java.io.FileFilter ; 28 import java.io.FileInputStream ; 29 import java.io.FileOutputStream ; 30 import java.io.InputStream ; 31 import java.io.IOException ; 32 import java.io.OutputStream ; 33 import java.util.Properties ; 34 35 import org.jboss.logging.Logger; 36 37 61 public class JarTransformer 62 { 63 private static Logger log = Logger.getLogger(JarTransformer.class); 65 66 70 public static void transform(File root, Properties globalXslParams) 71 throws Exception 72 { 73 Properties xslParams = new Properties ( globalXslParams ); 75 76 File metaInf = new File (root, "META-INF"); 77 if(!metaInf.exists()) 78 { 79 return; 80 } 82 83 File ejbjar = new File (metaInf, "ejb-jar.xml"); 85 if(ejbjar.exists()) 86 xslParams.setProperty("ejb-jar", ejbjar.getAbsolutePath()); 87 88 File [] files = metaInf.listFiles( 92 new FileFilter () 93 { 94 public boolean accept(File file) 95 { 96 if( file.getName().endsWith( ".xml" ) 97 && !file.isDirectory() ) 98 return true; 99 return false; 100 } 101 } 102 ); 103 104 log.debug("list XML files: " + java.util.Arrays.asList(files)); 105 for(int i = 0; i < files.length; i++) 106 { 107 File file = files[i]; 108 109 String xmlName = file.getName(); 111 String xslName = xslParams.getProperty("resources_path") 112 + xmlName.substring(0, xmlName.length() - 3) 113 + "xsl"; 114 String propsName = xslParams.getProperty("resources_path") 115 + xmlName.substring(0, xmlName.length() - 4) 116 + "-output.properties"; 117 118 InputStream templateIs = null; 120 try 121 { 122 templateIs = JarTransformer.class.getClassLoader(). 123 getResource(xslName).openStream(); 124 } 125 catch( Exception e ) 126 { 127 log.debug("xsl template wasn't found for '" + xmlName + "'"); 128 continue; 129 } 130 131 log.debug("Attempt to transform '" + xmlName + "' with '" + xslName + "'"); 132 133 Properties outputProps = loadProperties( propsName ); 135 136 InputStream input = null; 138 OutputStream output = null; 139 try 140 { 141 input = new FileInputStream (file); 143 byte[] bytes = readBytes(input); 144 input.close(); 145 bytes = transformBytes(bytes, templateIs, outputProps, xslParams); 146 147 String entryname = null; 149 if(outputProps != null) 150 entryname = outputProps.getProperty("newname"); 151 if(entryname == null) 152 entryname = file.getName(); 153 154 output = new FileOutputStream (new File (root, entryname)); 155 writeBytes( output, bytes ); 156 157 log.debug("Entry '" + file.getName() + "' transformed to '" + entryname + "'"); 158 } 159 catch(Exception e) 160 { 161 log.debug("Exception while transforming entry '" + file.getName(), e); 162 } 163 finally 164 { 165 if(templateIs != null) 166 try{ templateIs.close(); } catch(Exception e) {} 167 if(input != null) 168 try{ input.close(); } catch(Exception e) {} 169 if(output != null) 170 try{ output.close(); } catch(Exception e) {} 171 } 172 } 173 } 174 175 180 private static Properties loadProperties( String propsName ) 181 { 182 Properties props = new Properties (); 183 InputStream propsIs = null; 184 try 185 { 186 propsIs = JarTransformer.class.getClassLoader(). 187 getResource(propsName).openStream(); 188 props.load(propsIs); 189 log.debug("Loaded properties '" + propsName + "'"); 190 } 191 catch(Exception e) 192 { 193 log.debug("Couldn't find properties '" + propsName + "'"); 194 } 195 finally 196 { 197 if(propsIs != null) 198 try{ propsIs.close(); } catch(Exception e) {} 199 } 200 return props; 201 } 202 203 207 private static byte[] transformBytes(byte[] bytes, 208 InputStream xslIs, 209 Properties outputprops) 210 throws Exception 211 { 212 ByteArrayInputStream bais = new ByteArrayInputStream (bytes); 213 ByteArrayOutputStream baos = new ByteArrayOutputStream (2048); 214 try 215 { 216 XslTransformer.applyTransformation(bais, baos, xslIs, outputprops); 217 } 218 finally 219 { 220 if(bais != null) 221 try{ bais.close(); } catch(Exception e) {} 222 if(baos != null) 223 try{ baos.close(); } catch(Exception e) {} 224 } 225 return baos.toByteArray(); 226 } 227 228 233 private static byte[] transformBytes( byte[] bytes, 234 InputStream xslIs, 235 Properties outputProps, 236 Properties xslParams ) 237 throws Exception 238 { 239 ByteArrayInputStream bais = new ByteArrayInputStream (bytes); 240 ByteArrayOutputStream baos = new ByteArrayOutputStream (2048); 241 try 242 { 243 XslTransformer.applyTransformation( 244 bais, baos, xslIs, outputProps, xslParams ); 245 } 246 finally 247 { 248 if(bais != null) 249 try{ bais.close(); } catch(Exception e) {} 250 if(baos != null) 251 try{ baos.close(); } catch(Exception e) {} 252 } 253 return baos.toByteArray(); 254 } 255 256 259 private static void writeBytes(OutputStream os, byte[] bytes) 260 throws Exception 261 { 262 os.write(bytes, 0, bytes.length); 263 os.flush(); 264 } 265 266 270 private static int copyBytes(InputStream is, OutputStream os) 271 throws Exception 272 { 273 byte[] buffer = readBytes(is); 274 os.write(buffer, 0, buffer.length); 275 os.flush(); 276 return buffer.length; 277 } 278 279 282 private static byte[] readBytes(InputStream is) 283 throws IOException 284 { 285 byte[] buffer = new byte[ 8192 ]; 286 ByteArrayOutputStream baos = new ByteArrayOutputStream ( 2048 ); 287 int n; 288 baos.reset(); 289 try 290 { 291 while((n = is.read(buffer, 0, buffer.length)) != -1) 292 baos.write(buffer, 0, n); 293 buffer = baos.toByteArray(); 294 } 295 finally 296 { 297 if(baos != null) 298 try{ baos.close(); } catch(Exception e) {} 299 } 300 return buffer; 301 } 302 } 303 | Popular Tags |