1 4 5 9 10 package org.openlaszlo.utils; 11 import java.io.*; 12 import java.util.*; 13 import javax.xml.transform.*; 14 15 public abstract class TransformUtils { 16 17 static private Map sTemplatesMap = new HashMap(); 18 19 static private Map sTemplatesLastModified = new HashMap(); 20 21 static public Templates getTemplates(String styleSheetPathname) 22 throws IOException 23 { 24 synchronized (sTemplatesMap) { 25 Templates templates = (Templates) sTemplatesMap.get(styleSheetPathname); 26 Long lastModified = new Long (new File(styleSheetPathname).lastModified()); 27 if (templates != null && 28 !sTemplatesLastModified.get(styleSheetPathname).equals(lastModified)) 29 templates = null; 30 if (templates == null) { 31 CollectingErrorListener errorListener = 32 new CollectingErrorListener(); 33 TransformerFactory factory = 37 new com.icl.saxon.TransformerFactoryImpl(); 38 factory.setErrorListener(errorListener); 39 java.io.InputStream xslInput = 40 new java.net.URL ("file", "", styleSheetPathname).openStream(); 41 try { 42 Source xslSource = 43 new javax.xml.transform.stream.StreamSource (xslInput); 44 templates = factory.newTemplates(xslSource); 45 } catch (TransformerConfigurationException e) { 46 if (errorListener.isEmpty()) 47 throw new ChainedException(e); 48 else 49 throw new ChainedException(errorListener.getMessage(), e); 50 } finally { 51 xslInput.close(); 52 } 53 sTemplatesMap.put(styleSheetPathname, templates); 54 sTemplatesLastModified.put(styleSheetPathname, lastModified); 55 } 56 return templates; 57 } 58 } 59 60 public static void applyTransform(String styleSheetPathname, 61 String xmlString, 62 OutputStream out) 63 throws IOException 64 { 65 applyTransform(styleSheetPathname, new Properties(), xmlString, out); 66 } 67 68 static public void applyTransform(String styleSheetPathname, 71 Properties properties, 72 String xmlString, 73 OutputStream out) 74 throws IOException 75 { 76 PrintWriter writer = new PrintWriter(out); 77 CollectingErrorListener errorListener = 78 new CollectingErrorListener(); 79 try { 80 Templates template = getTemplates(styleSheetPathname); 81 Transformer transformer = template.newTransformer(); 82 transformer.setErrorListener(errorListener); 83 Source xmlSource = 84 new javax.xml.transform.stream.StreamSource ( 85 new StringReader(xmlString)); 86 for (Iterator iter = properties.keySet().iterator(); 87 iter.hasNext(); ) { 88 String key = (String ) iter.next(); 89 String value = properties.getProperty(key); 90 transformer.setParameter(key, value); 91 } 92 transformer.transform(xmlSource, 94 new javax.xml.transform.stream.StreamResult (writer)); 95 writer.close(); 96 } catch (TransformerConfigurationException e) { 97 if (errorListener.isEmpty()) 98 throw new ChainedException(e); 99 else 100 throw new ChainedException(errorListener.getMessage(), e); 101 } catch (TransformerException e) { 102 if (errorListener.isEmpty()) 103 throw new ChainedException(e); 104 else 105 throw new ChainedException(errorListener.getMessage(), e); 106 } 107 } 108 } 109 110 class CollectingErrorListener implements javax.xml.transform.ErrorListener { 111 protected StringBuffer messageBuffer = new StringBuffer (); 112 protected String separator = ""; 113 protected int messageCount = 0; 114 115 private void appendErrorString(TransformerException exception) { 116 messageBuffer.append(separator); 117 separator = "\n"; 118 messageBuffer.append(exception.getMessageAndLocation()); 119 messageCount++; 120 } 121 122 public void error(TransformerException exception) { 123 appendErrorString(exception); 124 } 125 126 public void warning(TransformerException exception) { 127 appendErrorString(exception); 128 } 129 130 public void fatalError(TransformerException exception) { 131 appendErrorString(exception); 132 } 133 134 boolean isEmpty() { 135 return messageCount == 0; 136 } 137 138 String getMessage() { 139 return messageBuffer.toString(); 140 } 141 } 142 | Popular Tags |