1 22 package org.jboss.varia.deployment.convertor; 23 24 import java.net.URL ; 25 import java.util.Properties ; 26 import java.util.Enumeration ; 27 import java.io.InputStream ; 28 import java.io.OutputStream ; 29 import java.io.IOException ; 30 31 import javax.xml.transform.TransformerFactory ; 32 import javax.xml.transform.Transformer ; 33 import javax.xml.transform.Templates ; 34 import javax.xml.transform.TransformerException ; 35 36 import javax.xml.transform.stream.StreamSource ; 37 import javax.xml.transform.stream.StreamResult ; 38 39 44 public class XslTransformer 45 { 46 54 private static TransformerFactory transformerFactory = 55 TransformerFactory.newInstance(); 56 57 63 public static synchronized void applyTransformation(InputStream srcIs, 64 OutputStream destOs, 65 InputStream templateIs, 66 Properties outputProps) 67 throws TransformerException , IOException 68 { 69 StreamSource source = new StreamSource (srcIs); 70 StreamResult result = new StreamResult (destOs); 71 StreamSource template = new StreamSource (templateIs); 72 73 80 Templates templates = transformerFactory.newTemplates( template ); 81 82 Transformer transformer = templates.newTransformer(); 83 if( outputProps != null ) 84 { 85 transformer.setOutputProperties( outputProps ); 86 } 87 88 transformer.transform( source, result ); 89 } 90 91 92 98 public static synchronized void applyTransformation(InputStream srcIs, 99 OutputStream destOs, 100 InputStream templateIs, 101 Properties outputProps, 102 Properties xslParams) 103 throws TransformerException , IOException 104 { 105 StreamSource source = new StreamSource ( srcIs ); 106 StreamResult result = new StreamResult ( destOs ); 107 StreamSource template = new StreamSource ( templateIs ); 108 109 Templates templates = transformerFactory.newTemplates( template ); 110 111 Transformer transformer = templates.newTransformer(); 113 if(outputProps != null) 114 { 115 transformer.setOutputProperties(outputProps); 116 } 117 118 if(xslParams != null) 120 { 121 Enumeration keys = xslParams.propertyNames(); 124 while( keys.hasMoreElements() ) 125 { 126 String key = (String )keys.nextElement(); 127 transformer.setParameter(key, xslParams.getProperty(key)); 128 } 129 } 130 131 transformer.transform( source, result ); 132 } 133 } 134 | Popular Tags |