1 10 package org.mmbase.cache.xslt; 11 12 import org.mmbase.cache.Cache; 13 14 import javax.xml.transform.*; 15 import java.util.*; 16 import org.w3c.dom.*; 17 18 import org.mmbase.util.logging.Logger; 19 import org.mmbase.util.logging.Logging; 20 21 31 public class ResultCache extends Cache { 32 33 private static Logger log = Logging.getLoggerInstance(ResultCache.class); 34 35 private static int cacheSize = 50; 36 private static ResultCache cache; 37 38 39 protected int getDefaultMaxEntrySize() { 40 return 1500; 41 } 42 43 46 public static ResultCache getCache() { 47 return cache; 48 } 49 50 static { 51 cache = new ResultCache(cacheSize); 52 putCache(cache); 53 } 54 55 public String getName() { 56 return "XSLTResults"; 57 } 58 public String getDescription() { 59 return "XSL Transformation Results"; 60 } 61 62 65 private ResultCache(int size) { 66 super(size); 67 } 68 69 74 75 public Object put(Object key, Object value) { 76 throw new RuntimeException ("wrong types in cache"); 77 } 78 79 80 85 private StringBuffer append(StringBuffer buf, Node node) { 86 switch(node.getNodeType()) { 87 case Node.ATTRIBUTE_NODE: 88 buf.append(node.getNodeName()).append(node.getNodeValue()); 89 break; 90 case Node.ELEMENT_NODE: { 91 NodeList nl = node.getChildNodes(); 92 for (int i = 0; i < nl.getLength(); i++) { 93 append(buf, nl.item(i)); 94 } 95 } 96 case Node.ENTITY_NODE: 97 case Node.ENTITY_REFERENCE_NODE: 98 buf.append(node.getNodeName()); 99 break; 100 case Node.CDATA_SECTION_NODE: 101 case Node.TEXT_NODE: 102 buf.append(node.getNodeValue().hashCode()); 103 break; 104 default: 105 log.debug("Unknown nodetype " + node.getNodeType()); 106 break; 107 } 108 109 return buf; 110 } 111 116 private String getKey(Source xsl, Map params, Properties props, Document src) { 117 StringBuffer key = new StringBuffer (""+(xsl.getSystemId() + "/" + (params != null ? params.toString() : "") + "/" + (props != null ? props.toString() : "")+ "/")); 118 119 return append(key, src.getDocumentElement()).toString(); 120 } 121 122 133 public String get(Templates temp, Source xsl, Map params, Properties props, Document src) { 134 String key = null; 135 String result = null; 136 if (isActive()) { 137 key = getKey(xsl, params, props, src); 138 if (log.isDebugEnabled()) { 139 log.debug("Getting result of XSL transformation: " + key); 140 } 141 result = (String ) get(key); 142 } 143 if (result == null) { 144 try { 145 Transformer transformer = temp.newTransformer(); 147 if (params != null) { 149 Iterator i = params.entrySet().iterator(); 150 while (i.hasNext()) { 151 Map.Entry entry = (Map.Entry) i.next(); 152 transformer.setParameter((String ) entry.getKey(), entry.getValue()); 153 } 154 } 155 if (props != null) { 156 transformer.setOutputProperties(props); 157 } 158 159 java.io.StringWriter res = new java.io.StringWriter (); 160 transformer.transform(new javax.xml.transform.dom.DOMSource (src), 161 new javax.xml.transform.stream.StreamResult (res)); 162 result = res.toString(); 163 } catch (TransformerException e) { 164 result = e.toString(); 165 } 166 if (isActive()) { 168 if (result.length() < getMaxEntrySize()) { 169 if (log.isDebugEnabled()) { 170 log.debug("Put xslt Result in cache with key " + key); 171 } 172 super.put(key, result); 173 } else { 174 if (log.isDebugEnabled()) { 175 log.debug("xslt Result of key " + key.substring(100) + " is too big to put in cache. " + result.length() + " >= " + getMaxEntrySize()); 176 } 177 } 178 } 179 180 } 181 182 return result; 183 184 } 185 186 } 187 | Popular Tags |