1 16 package org.apache.cocoon.transformation; 17 18 import org.apache.avalon.framework.component.ComponentException; 19 import org.apache.avalon.framework.component.ComponentManager; 20 import org.apache.avalon.framework.component.Composable; 21 import org.apache.avalon.framework.parameters.Parameters; 22 23 import org.apache.cocoon.ProcessingException; 24 import org.apache.cocoon.caching.CacheValidity; 25 import org.apache.cocoon.caching.Cacheable; 26 import org.apache.cocoon.caching.IncludeCacheValidity; 27 import org.apache.cocoon.components.source.SourceUtil; 28 import org.apache.cocoon.environment.SourceResolver; 29 import org.apache.cocoon.xml.IncludeXMLConsumer; 30 import org.apache.cocoon.xml.XMLUtils; 31 32 import org.apache.excalibur.source.Source; 33 import org.xml.sax.Attributes ; 34 import org.xml.sax.SAXException ; 35 36 import java.io.IOException ; 37 import java.util.Map ; 38 39 62 public class CachingCIncludeTransformer extends AbstractTransformer 63 implements Composable, Cacheable { 64 65 public static final String CINCLUDE_NAMESPACE_URI = "http://apache.org/cocoon/include/1.0"; 66 public static final String CINCLUDE_INCLUDE_ELEMENT = "include"; 67 public static final String CINCLUDE_INCLUDE_ELEMENT_SRC_ATTRIBUTE = "src"; 68 public static final String CINCLUDE_INCLUDE_ELEMENT_ELEMENT_ATTRIBUTE = "element"; 69 public static final String CINCLUDE_INCLUDE_ELEMENT_NS_ATTRIBUTE = "ns"; 70 public static final String CINCLUDE_INCLUDE_ELEMENT_PREFIX_ATTRIBUTE = "prefix"; 71 72 73 protected SourceResolver sourceResolver; 74 75 76 protected ComponentManager manager = null; 77 78 79 protected IncludeCacheValidity currentCacheValidity; 80 81 82 protected IncludeXMLConsumer consumer; 83 84 87 public void setup(SourceResolver resolver, Map objectModel, 88 String source, Parameters parameters) 89 throws ProcessingException, SAXException , IOException { 90 this.sourceResolver = resolver; 91 } 92 93 96 public final void compose(final ComponentManager manager) throws ComponentException { 97 this.manager = manager; 98 } 99 100 103 public void recycle() { 104 super.recycle(); 105 this.sourceResolver = null; 106 this.currentCacheValidity = null; 107 } 108 109 public void startElement(String uri, String name, String raw, Attributes attr) 110 throws SAXException { 111 if (uri != null && name != null 112 && uri.equals(CINCLUDE_NAMESPACE_URI) 113 && name.equals(CINCLUDE_INCLUDE_ELEMENT)) { 114 115 this.processCIncludeElement(attr.getValue("",CINCLUDE_INCLUDE_ELEMENT_SRC_ATTRIBUTE), 116 attr.getValue("",CINCLUDE_INCLUDE_ELEMENT_ELEMENT_ATTRIBUTE), 117 attr.getValue("",CINCLUDE_INCLUDE_ELEMENT_NS_ATTRIBUTE), 118 attr.getValue("",CINCLUDE_INCLUDE_ELEMENT_PREFIX_ATTRIBUTE)); 119 120 } else { 121 super.startElement(uri, name, raw, attr); 122 } 123 } 124 125 public void endElement(String uri, String name, String raw) throws SAXException { 126 if (uri != null && name != null 127 && uri.equals(CINCLUDE_NAMESPACE_URI) 128 && name.equals(CINCLUDE_INCLUDE_ELEMENT)) { 129 return; 130 } 131 super.endElement(uri, name, raw); 132 } 133 134 public void endDocument() 135 throws SAXException { 136 super.endDocument(); 137 if(currentCacheValidity != null) { 138 currentCacheValidity.setIsNew2False(); 139 } 140 } 141 142 protected void processCIncludeElement(String src, String element, String ns, String prefix) 143 throws SAXException { 144 145 if (element == null) element=""; 146 if (ns == null) ns=""; 147 if (prefix == null) prefix=""; 148 149 if (this.getLogger().isDebugEnabled()) { 150 getLogger().debug("Processing CInclude element: SRC=" + src 151 + ", element=" + element 152 + ", ns=" + ns 153 + ", prefix=" + prefix); 154 } 155 156 if(currentCacheValidity != null ) { 158 Source temp = null; 159 try { 160 temp = sourceResolver.resolveURI(src); 161 currentCacheValidity.add(src, temp.getLastModified()); 162 if (this.getLogger().isDebugEnabled()) { 163 getLogger().debug("currentCacheValidity: " + currentCacheValidity); 164 } 165 } catch (Exception e) { 166 throw new SAXException ("CachingCIncludeTransformer could not resolve resource", e); 167 } finally { 168 sourceResolver.release(temp); 169 } 170 } 171 172 if (!"".equals(element)) { 173 if (!ns.equals("")) { 174 super.startPrefixMapping(prefix, ns); 175 } 176 super.startElement(ns, 177 element, 178 (!ns.equals("") && !prefix.equals("") ? prefix+":"+element : element), 179 XMLUtils.EMPTY_ATTRIBUTES); 180 } 181 182 Source source = null; 183 try { 184 source = this.sourceResolver.resolveURI(src); 185 SourceUtil.parse(this.manager, source, getConsumer()); 186 } catch (Exception e) { 187 throw new SAXException ("CachingCIncludeTransformer could not read resource", e); 188 } finally { 189 sourceResolver.release(source); 190 } 191 192 if (!"".equals(element)) { 193 super.endElement(ns, element, (!ns.equals("") && !prefix.equals("") ? prefix+":"+element : element)); 194 if (!ns.equals("")) { 195 super.endPrefixMapping(prefix); 196 } 197 } 198 } 199 200 208 209 public long generateKey() { 210 return 1; 211 } 212 213 222 223 public CacheValidity generateValidity() { 224 225 try { 226 currentCacheValidity = new IncludeCacheValidity(sourceResolver); 227 return currentCacheValidity; 228 } catch (RuntimeException e) { 229 getLogger().warn("CachingCIncludeTransformer: could not generateKey", e); 230 return null; 231 } 232 } 233 234 protected IncludeXMLConsumer getConsumer() { 235 if(consumer == null) { 236 consumer = new IncludeXMLConsumer(this); 237 } 238 return consumer; 239 } 240 } 241 | Popular Tags |