1 package com.genimen.djeneric.web.util; 2 3 import java.io.BufferedWriter ; 4 import java.io.ByteArrayOutputStream ; 5 import java.io.FileOutputStream ; 6 import java.io.IOException ; 7 import java.io.OutputStreamWriter ; 8 import java.util.HashMap ; 9 10 import javax.servlet.ServletException ; 11 import javax.xml.transform.OutputKeys ; 12 import javax.xml.transform.Result ; 13 import javax.xml.transform.Source ; 14 import javax.xml.transform.Templates ; 15 import javax.xml.transform.Transformer ; 16 import javax.xml.transform.TransformerConfigurationException ; 17 import javax.xml.transform.TransformerException ; 18 import javax.xml.transform.TransformerFactory ; 19 import javax.xml.transform.TransformerFactoryConfigurationError ; 20 import javax.xml.transform.dom.DOMSource ; 21 import javax.xml.transform.stream.StreamResult ; 22 23 import org.w3c.dom.Document ; 24 25 public class DjenericXSLTransform 26 { 27 private static HashMap _cachedTemplates = new HashMap (); 28 private static boolean _caching = false; 29 private static String _viewDumpLocation = "/temp/dump"; 30 private String _templateRoot; 31 32 public DjenericXSLTransform(String templateRoot) 33 { 34 if (!templateRoot.endsWith("/") && !templateRoot.endsWith("\\")) templateRoot += "/"; 35 _templateRoot = templateRoot; 36 } 37 38 public String transform(Document doc, String path) throws IOException , ServletException 39 { 40 try 41 { 42 path = _templateRoot + path; 43 44 DOMSource input = new DOMSource (doc); 45 46 Transformer trans = this.createTransformer(path); 47 ByteArrayOutputStream bos = new ByteArrayOutputStream (50000); 48 Result res = new StreamResult (bos); 49 50 trans.transform(input, res); 51 52 String result = bos.toString("UTF-8"); 53 54 if (_viewDumpLocation != null) 55 { 56 String fn = _viewDumpLocation; 57 if (!fn.endsWith("/") && !fn.endsWith("\\")) fn += "/"; 58 59 String name = path; 60 int idx = path.lastIndexOf("/"); 61 if (idx != -1) name = name.substring(idx + 1); 62 63 BufferedWriter w = new BufferedWriter ( 64 new OutputStreamWriter (new FileOutputStream (fn + name + ".html"), "UTF-8")); 65 w.write(result); 66 w.close(); 67 68 w = new BufferedWriter (new OutputStreamWriter (new FileOutputStream (fn + name + ".xml"), "UTF-8")); 69 w.write(doc2String(doc)); 70 w.close(); 71 } 72 73 return result; 74 } 75 catch (TransformerException ex) 76 { 77 throw new ServletException (ex); 78 } 79 } 80 81 public static void clearCache() 82 { 83 _cachedTemplates = new HashMap (); 84 } 85 86 private String doc2String(Document doc) throws TransformerConfigurationException , 87 TransformerFactoryConfigurationError , TransformerException 88 { 89 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 90 91 Transformer serializer = TransformerFactory.newInstance().newTransformer(); 92 serializer.setOutputProperty(OutputKeys.INDENT, "yes"); 94 serializer.transform(new DOMSource (doc), new StreamResult (baos)); 95 return baos.toString(); 96 } 97 98 private Transformer createTransformer(String path) throws TransformerException 99 { 100 Templates result = (Templates ) _cachedTemplates.get(path); 101 if (result == null || !_caching) 102 { 103 TransformerFactory tFactory = DjenericTransformerFactory.newInstance(); 104 105 Source src = tFactory.getURIResolver().resolve(path, ""); 106 107 result = tFactory.newTemplates(src); 108 _cachedTemplates.put(path, result); 109 } 110 return result.newTransformer(); 111 } 112 113 public static void setViewDumpLocation(String viewDumpLocation) 114 { 115 _viewDumpLocation = viewDumpLocation; 116 } 117 118 public static void setXSLCaching(boolean caching) 119 { 120 _caching = caching; 121 } 122 } | Popular Tags |