1 32 33 package com.knowgate.dataxslt; 34 35 import java.io.File ; 36 import java.io.IOException ; 37 import java.io.FileNotFoundException ; 38 import java.io.InputStream ; 39 import java.io.OutputStream ; 40 import java.io.StringBufferInputStream ; 41 import java.io.ByteArrayInputStream ; 42 import java.io.ByteArrayOutputStream ; 43 import java.io.UnsupportedEncodingException ; 44 45 import java.util.Date ; 46 import java.util.WeakHashMap ; 47 import java.util.Iterator ; 48 import java.util.Properties ; 49 import java.util.Enumeration ; 50 51 import javax.xml.transform.TransformerFactory ; 52 import javax.xml.transform.Transformer ; 53 import javax.xml.transform.Templates ; 54 import javax.xml.transform.TransformerException ; 55 import javax.xml.transform.TransformerConfigurationException ; 56 import javax.xml.transform.stream.StreamSource ; 57 import javax.xml.transform.stream.StreamResult ; 58 59 import com.knowgate.debug.DebugFile; 60 import com.knowgate.misc.Gadgets; 61 62 72 public class StylesheetCache { 73 74 private StylesheetCache() { } 75 76 78 88 public static synchronized Transformer newTransformer(String sFilePath) 89 throws FileNotFoundException , IOException , TransformerException , TransformerConfigurationException { 90 91 if (DebugFile.trace) { 92 DebugFile.writeln("Begin StylesheetCache.newTransformer(" + sFilePath + ")"); 93 DebugFile.incIdent(); 94 } 95 96 File oFile = new File (sFilePath); 97 98 if (!oFile.exists()) { 99 if (DebugFile.trace) { 100 DebugFile.writeln("File not found " + sFilePath); 101 DebugFile.decIdent(); 102 } 103 throw new FileNotFoundException (sFilePath); 104 } 105 long lastMod = oFile.lastModified(); 106 107 TransformerFactory oFactory; 108 Templates oTemplates; 109 StreamSource oStreamSrc; 110 SheetEntry oSheet = (SheetEntry) oCache.get(sFilePath); 111 112 if (null!=oSheet) { 113 if (DebugFile.trace) { 114 DebugFile.writeln("Cache hit: Cached stylesheet date "+new Date (oSheet.lastModified).toString() + " Disk file date "+new Date (lastMod).toString()); 115 } 116 if (lastMod>oSheet.lastModified) { 117 oSheet = null; 118 oCache.remove(sFilePath); 119 } 120 } 122 if (null==oSheet) { 123 if (DebugFile.trace) DebugFile.writeln("TransformerFactory.newInstance()"); 124 oFactory = TransformerFactory.newInstance(); 125 if (DebugFile.trace) DebugFile.writeln("new StreamSource("+sFilePath+")"); 126 oStreamSrc = new StreamSource (sFilePath); 127 oTemplates = oFactory.newTemplates(oStreamSrc); 128 oSheet = new SheetEntry(lastMod, oTemplates); 129 oCache.put(sFilePath, oSheet); 130 } 131 132 if (DebugFile.trace) DebugFile.writeln("javax.xml.transform.Templates.newTransformer()"); 133 Transformer oTransformer = oSheet.templates.newTransformer(); 134 135 if (DebugFile.trace) { 136 DebugFile.decIdent(); 137 DebugFile.writeln("End StylesheetCache.newTransformer()"); 138 } 139 140 return oTransformer; 141 } 143 145 155 public static void setParameters(Transformer oXSL, Properties oProps) 156 throws NullPointerException { 157 158 if (DebugFile.trace) { 159 DebugFile.writeln("Begin StylesheetCache.setParameters(Transformer, Properties)"); 160 if (null==oXSL) throw new NullPointerException ("StylesheetCache.setParameters() Transformer may not be null"); 161 if (null==oXSL) throw new NullPointerException ("StylesheetCache.setParameters() Properties may not be null"); 162 DebugFile.incIdent(); 163 } 164 165 String sKey, sVal; 166 Iterator myIterator = oProps.keySet().iterator(); 167 168 while (myIterator.hasNext()) 169 { 170 sKey = (String ) myIterator.next(); 171 sVal = oProps.getProperty(sKey); 172 173 if (DebugFile.trace) { 174 DebugFile.writeln("set param_" + sKey + " = " + sVal); 175 } 176 oXSL.setParameter("param_" + sKey, sVal); 177 } 179 if (DebugFile.trace) { 180 DebugFile.decIdent(); 181 DebugFile.writeln("End StylesheetCache.setParameters()"); 182 } 183 } 185 187 202 public static void transform (String sStyleSheetPath, 203 InputStream oXMLInputStream, 204 OutputStream oOutputStream, Properties oProps) 205 throws IOException , FileNotFoundException , 206 NullPointerException , TransformerException , TransformerConfigurationException { 207 208 long lElapsed = 0; 209 210 if (DebugFile.trace) { 211 lElapsed = System.currentTimeMillis(); 212 213 DebugFile.writeln("Begin StylesheetCache.transform(" + sStyleSheetPath + ", InputStream, Properties)"); 214 DebugFile.incIdent(); 215 } 216 217 Transformer oTransformer = StylesheetCache.newTransformer(sStyleSheetPath); 218 219 if (null!=oProps) setParameters(oTransformer, oProps); 220 221 StreamSource oStreamSrcXML = new StreamSource (oXMLInputStream); 222 223 StreamResult oStreamResult = new StreamResult (oOutputStream); 224 225 if (DebugFile.trace) DebugFile.writeln("Transformer.transform(StreamSource,StreamResult)"); 226 227 oTransformer.transform(oStreamSrcXML, oStreamResult); 228 229 if (DebugFile.trace) { 230 DebugFile.writeln("done in " + String.valueOf(System.currentTimeMillis()-lElapsed) + " miliseconds"); 231 DebugFile.decIdent(); 232 DebugFile.writeln("End StylesheetCache.transform()"); 233 } 234 } 236 238 255 public static String transform (String sStyleSheetPath, String sXMLInput, Properties oProps) 256 throws IOException , FileNotFoundException , UnsupportedEncodingException , 257 NullPointerException , TransformerException , TransformerConfigurationException { 258 259 if (DebugFile.trace) { 260 DebugFile.writeln("Begin StylesheetCache.transform(" + sStyleSheetPath + ", String, Properties)"); 261 DebugFile.incIdent(); 262 } 263 264 if (null==sXMLInput) { 265 if (DebugFile.trace) DebugFile.decIdent(); 266 throw new NullPointerException ("StylesheetCache.transform() XML input String may not be null"); 267 } 268 269 String sEncoding; 272 int iEnc = Gadgets.indexOfIgnoreCase(sXMLInput, "encoding"); 273 if (iEnc<0) { 274 sEncoding = "ISO8859_1"; 275 } else { 276 int iBeg = iEnc+8; 277 int iEnd; 278 while (sXMLInput.charAt(iBeg)==' ' || sXMLInput.charAt(iBeg)=='=') iBeg++; 279 while (sXMLInput.charAt(iBeg)==' ') iBeg++; 280 if (sXMLInput.charAt(iBeg)=='"') { 281 iEnd = ++iBeg; 282 while (sXMLInput.charAt(iEnd)!='"') iEnd++; 283 } else { 284 iEnd = iBeg; 285 while (sXMLInput.charAt(iEnd)!=' ' && sXMLInput.charAt(iEnd)!='?') iEnd++; 286 } sEncoding = sXMLInput.substring(iBeg, iEnd); 288 } 291 if (DebugFile.trace) { 292 DebugFile.writeln("XML input file encoding is "+sEncoding); 293 } 294 295 ByteArrayOutputStream oOutputStream = new ByteArrayOutputStream (); 296 ByteArrayInputStream oXMLInputStream = new ByteArrayInputStream (sXMLInput.getBytes(sEncoding)); 297 Transformer oTransformer = StylesheetCache.newTransformer(sStyleSheetPath); 298 if (null!=oProps) setParameters(oTransformer, oProps); 299 StreamSource oStreamSrcXML = new StreamSource (oXMLInputStream); 300 StreamResult oStreamResult = new StreamResult (oOutputStream); 301 if (DebugFile.trace) DebugFile.writeln("Transformer.transform(StreamSource,StreamResult)"); 302 oTransformer.transform(oStreamSrcXML, oStreamResult); 303 oStreamSrcXML = null; 304 oXMLInputStream.close(); 305 String sRetVal = oOutputStream.toString(sEncoding); 306 if (DebugFile.trace) { 307 if (null==sRetVal) 308 DebugFile.writeln("Transformer.transform() returned null"); 309 else 310 DebugFile.writeln("Transformer.transform() returned "+String.valueOf(sRetVal.length())+" characters"); 311 } 312 oStreamResult = null; 313 oOutputStream.close(); 314 315 if (DebugFile.trace) { 316 DebugFile.decIdent(); 317 DebugFile.writeln("End StylesheetCache.transform()"); 318 } 319 return sRetVal; 320 } 322 324 static class SheetEntry { 325 long lastModified; 326 Templates templates; 327 328 SheetEntry (long lLastModified, Templates oTemplats) { 329 lastModified = lLastModified; 330 templates = oTemplats; 331 } 332 } 334 private static WeakHashMap oCache = new WeakHashMap (); 335 }
| Popular Tags
|