1 51 package org.apache.fop.tools.xslt; 52 53 import javax.xml.transform.*; 54 55 import java.io.InputStream ; 56 import java.io.Writer ; 57 58 import java.util.HashMap ; 59 import org.w3c.dom.Document ; 60 61 64 65 public class TraxTransform { 66 67 70 private static HashMap _stylesheetCache = new HashMap (); 71 72 public static Transformer getTransformer(String xsltFilename, 73 boolean cache) { 74 try { 75 if (cache && _stylesheetCache.containsKey(xsltFilename)) { 76 Templates cachedStylesheet = 77 (Templates)_stylesheetCache.get(xsltFilename); 78 return cachedStylesheet.newTransformer(); 79 } 80 81 Source xslSheet = 82 new javax.xml.transform.stream.StreamSource (xsltFilename); 83 84 85 90 TransformerFactory factory = TransformerFactory.newInstance(); 91 92 Templates compiledSheet = factory.newTemplates(xslSheet); 93 if (cache) { 94 _stylesheetCache.put(xsltFilename, compiledSheet); 95 } 96 return compiledSheet.newTransformer(); 97 } catch (TransformerConfigurationException ex) { 98 ex.printStackTrace(); 99 } 100 return null; 101 102 } 103 104 public static void transform(String xmlSource, String xslURL, 105 String outputFile) { 106 transform(new javax.xml.transform.stream.StreamSource (xmlSource), 107 new javax.xml.transform.stream.StreamSource (xslURL), 108 new javax.xml.transform.stream.StreamResult (outputFile)); 109 } 110 111 public static void transform(Document xmlSource, String xslURL, 112 String outputFile) { 113 114 transform(new javax.xml.transform.dom.DOMSource (xmlSource), 115 new javax.xml.transform.stream.StreamSource (xslURL), 116 new javax.xml.transform.stream.StreamResult (outputFile)); 117 118 } 119 120 public static void transform(String xmlSource, String xslURL, 121 Writer output) { 122 transform(new javax.xml.transform.stream.StreamSource (xmlSource), 123 new javax.xml.transform.stream.StreamSource (xslURL), 124 new javax.xml.transform.stream.StreamResult (output)); 125 } 126 127 public static void transform(Document xmlSource, InputStream xsl, 128 Document outputDoc) { 129 transform(new javax.xml.transform.dom.DOMSource (xmlSource), 130 new javax.xml.transform.stream.StreamSource (xsl), 131 new javax.xml.transform.dom.DOMResult (outputDoc)); 132 } 133 134 public static void transform(Source xmlSource, Source xslSource, 135 Result result) { 136 try { 137 Transformer transformer; 138 if (xslSource.getSystemId() == null) { 139 TransformerFactory factory = TransformerFactory.newInstance(); 140 transformer = factory.newTransformer(xslSource); 141 } else { 142 transformer = getTransformer(xslSource.getSystemId(), true); 143 } 144 transformer.transform(xmlSource, result); 145 } catch (TransformerConfigurationException ex) { 146 ex.printStackTrace(); 147 } catch (TransformerException ex) { 148 ex.printStackTrace(); 149 } 150 151 } 152 153 } 154 | Popular Tags |