1 20 package org.openi.xml; 21 22 import org.apache.log4j.LogManager; 23 import org.apache.log4j.Logger; 24 import java.io.*; 25 import javax.xml.transform.Source ; 26 import javax.xml.transform.Transformer ; 27 import javax.xml.transform.TransformerException ; 28 import javax.xml.transform.TransformerFactory ; 29 import javax.xml.transform.stream.StreamResult ; 30 import javax.xml.transform.stream.StreamSource ; 31 import java.util.Map ; 32 import java.util.Iterator ; 33 34 35 43 public class XMLTransformer { 44 private static Logger logger = LogManager.getLogger(XMLTransformer.class); 46 47 55 public static String transform(String xslString, String xmlString) 56 throws IOException, TransformerException { 57 Source xslt = new StreamSource (new StringReader(xslString)); 58 Source xml = new StreamSource (new StringReader(xmlString)); 59 60 return transform(xslt, xml, null); 61 } 62 63 71 public static String transform(InputStream xslInputStream, 72 InputStream xmlInputStream) throws IOException, TransformerException { 73 Source xslt = new StreamSource (new InputStreamReader(xslInputStream)); 74 Source xml = new StreamSource (new InputStreamReader(xmlInputStream)); 75 76 return transform(xslt, xml, null); 77 } 78 79 88 public static String transform(String xslString, String xmlString, Map xslParam) 89 throws IOException, TransformerException { 90 Source xslt = new StreamSource (new StringReader(xslString)); 91 Source xml = new StreamSource (new StringReader(xmlString)); 92 93 return transform(xslt, xml, xslParam); 94 } 95 96 105 public static String transform(InputStream xslInputStream, 106 InputStream xmlInputStream, Map xslParam) throws IOException, TransformerException { 107 Source xslt = new StreamSource (new InputStreamReader(xslInputStream)); 108 Source xml = new StreamSource (new InputStreamReader(xmlInputStream)); 109 110 return transform(xslt, xml, xslParam); 111 } 112 113 121 public static String transform(Source xslt, Source xml) throws 122 TransformerException , IOException { 123 return transform(xslt, xml, null); 124 } 125 126 127 136 public static String transform(Source xslt, Source xml, Map xslParam) 137 throws IOException, TransformerException { 138 String result = ""; 139 long start = System.currentTimeMillis(); 140 141 TransformerFactory tFactory = TransformerFactory.newInstance(); 142 143 Transformer transformer = tFactory.newTransformer(xslt); 144 145 if(xslParam != null){ 146 Iterator params = xslParam.entrySet().iterator(); 147 while(params.hasNext()){ 148 Map.Entry entry = (Map.Entry ) params.next(); 149 transformer.setParameter((String )entry.getKey(), entry.getValue()); 150 } 151 } 152 153 154 StringWriter resultWriter = new StringWriter(); 155 156 transformer.transform(xml, new StreamResult (resultWriter)); 157 158 resultWriter.flush(); 159 160 result = resultWriter.getBuffer().toString(); 161 162 long elapsed = System.currentTimeMillis() - start; 163 logger.info("Elapsed Time " + elapsed + " ms"); 164 165 return result; 166 } 167 } 168 | Popular Tags |