1 24 package org.ofbiz.base.util.template; 25 26 import java.util.Map ; 27 import java.util.Set ; 28 import java.util.Iterator ; 29 import java.io.StringReader ; 30 import java.net.URL ; 31 import java.net.URLConnection ; 32 import java.io.InputStream ; 33 34 import org.ofbiz.base.util.UtilValidate; 35 import org.ofbiz.base.util.UtilXml; 36 import org.ofbiz.base.util.URLConnector; 37 import org.ofbiz.base.util.cache.UtilCache; 38 import org.ofbiz.base.location.FlexibleLocation; 39 import javax.xml.transform.Transformer ; 40 import javax.xml.transform.TransformerFactory ; 41 import javax.xml.transform.Templates ; 42 import javax.xml.transform.Source ; 43 import javax.xml.transform.dom.DOMSource ; 44 import javax.xml.transform.dom.DOMResult ; 45 import javax.xml.transform.TransformerConfigurationException ; 46 import javax.xml.transform.TransformerException ; 47 import org.w3c.dom.Document ; 48 import org.w3c.dom.Node ; 49 import org.ofbiz.base.util.GeneralException; 50 import java.io.IOException ; 51 52 import javax.xml.transform.stream.StreamSource ; 53 54 67 public final class XslTransform { 68 69 public static final String module = XslTransform.class.getName(); 70 public static UtilCache xslTemplatesCache = new UtilCache("XsltTemplates", 0, 0); 71 72 public static Document transform(Map context, Map params) 73 throws GeneralException, IOException , TransformerConfigurationException , TransformerException { 74 Document outputDocument = null; 75 TransformerFactory tFactory = TransformerFactory.newInstance(); 76 Templates translet = null; 77 String templateName = (String )context.get("templateName"); 78 if (UtilValidate.isNotEmpty(templateName)) { 79 translet = (Templates ) xslTemplatesCache.get(templateName); 80 } 81 82 if (translet == null ) { 83 String templateUrl = (String )context.get("templateUrl"); 84 String templateString = (String )context.get("templateString"); 85 Document templateDocument = (Document )context.get("templateDocument"); 86 Source templateSource = getSource(templateDocument, templateUrl, templateString); 87 translet = tFactory.newTemplates(templateSource); 88 if (UtilValidate.isNotEmpty(templateName)) { 89 xslTemplatesCache.put(templateName, translet); 90 } 91 } 92 if (translet != null ) { 93 Transformer transformer = translet.newTransformer(); 94 if (params != null) { 95 Set entrySet = params.entrySet(); 96 Iterator iter = entrySet.iterator(); 97 while (iter.hasNext()) { 98 Map.Entry entry = (Map.Entry )iter.next(); 99 String key = (String )entry.getKey(); 100 Object val = entry.getValue(); 101 transformer.setParameter(key, val); 102 } 103 } 104 105 DOMResult outputResult = new DOMResult (UtilXml.makeEmptyXmlDocument()); 106 107 String inputUrl = (String )context.get("inputUrl"); 108 String inputString = (String )context.get("inputString"); 109 Document inputDocument = (Document )context.get("inputDocument"); 110 Source inputSource = getSource(inputDocument, inputUrl, inputString); 111 112 transformer.transform(inputSource, outputResult); 113 Node nd = outputResult.getNode(); 114 outputDocument = (Document )nd; 115 } 116 117 return outputDocument; 118 } 119 120 private static Source getSource(Document inputDocument, String inputUrl, String inputString) throws GeneralException, IOException { 121 Source source = null; 122 if (inputDocument != null) { 123 source = new DOMSource (inputDocument); 124 } else if (UtilValidate.isNotEmpty(inputString)) { 125 source = new StreamSource (new StringReader (inputString)); 126 } else if (UtilValidate.isNotEmpty(inputUrl)) { 127 URL url = FlexibleLocation.resolveLocation(inputUrl); 128 URLConnection conn = URLConnector.openConnection(url); 129 InputStream in = conn.getInputStream(); 130 source = new StreamSource (in); 131 } 132 return source; 133 } 134 } 135 | Popular Tags |