1 16 package org.apache.cocoon.transformation; 17 18 import java.io.IOException ; 19 import java.io.Serializable ; 20 import java.util.Map ; 21 22 import org.apache.avalon.framework.activity.Disposable; 23 import org.apache.avalon.framework.configuration.Configurable; 24 import org.apache.avalon.framework.configuration.Configuration; 25 import org.apache.avalon.framework.configuration.ConfigurationException; 26 import org.apache.avalon.framework.parameters.Parameters; 27 import org.apache.avalon.framework.service.ServiceException; 28 import org.apache.avalon.framework.service.ServiceManager; 29 import org.apache.avalon.framework.service.Serviceable; 30 import org.apache.cocoon.ProcessingException; 31 import org.apache.cocoon.caching.CacheableProcessingComponent; 32 import org.apache.cocoon.components.validation.ValidationHandler; 33 import org.apache.cocoon.components.validation.Validator; 34 import org.apache.cocoon.environment.SourceResolver; 35 import org.apache.cocoon.xml.ContentHandlerWrapper; 36 import org.apache.cocoon.xml.XMLConsumer; 37 import org.apache.cocoon.xml.XMLMulticaster; 38 import org.apache.excalibur.source.Source; 39 import org.apache.excalibur.source.SourceValidity; 40 import org.xml.sax.SAXException ; 41 42 60 public class ValidatingTransformer extends AbstractTransformer 61 implements Configurable, Serviceable, Disposable, CacheableProcessingComponent { 62 63 64 private ServiceManager serviceManager = null; 65 66 private Validator validator = null; 67 68 private String grammar = null; 69 70 71 private ValidationHandler handler = null; 72 73 private String key = null; 74 75 78 public ValidatingTransformer() { 79 super(); 80 } 81 82 89 public void service(ServiceManager manager) 90 throws ServiceException { 91 this.serviceManager = manager; 92 this.validator = (Validator) manager.lookup(Validator.ROLE); 93 } 94 95 106 public void configure(Configuration configuration) 107 throws ConfigurationException { 108 this.grammar = configuration.getChild("grammar").getValue(null); 109 } 110 111 115 public void dispose() { 116 this.serviceManager.release(this.validator); 117 } 118 119 128 public void setup(SourceResolver resolver, Map objectModel, String source, 129 Parameters parameters) 130 throws ProcessingException, SAXException , IOException { 131 Source s = null; 132 try { 133 s = resolver.resolveURI(source); 134 String g = parameters.getParameter("grammar", this.grammar); 135 if (g == null) { 136 this.handler = this.validator.getValidationHandler(s); 137 } else{ 138 this.handler = this.validator.getValidationHandler(s, g); 139 } 140 } finally { 141 if (source != null) resolver.release(s); 142 } 143 } 144 145 151 public void setConsumer(XMLConsumer consumer) { 152 XMLConsumer handler = new ContentHandlerWrapper(this.handler, this.handler); 153 super.setConsumer(new XMLMulticaster(handler, consumer)); 154 } 155 156 162 public Serializable getKey() { 163 return this.key; 164 } 165 166 172 public SourceValidity getValidity() { 173 return this.handler.getValidity(); 174 } 175 176 179 public void recycle() { 180 this.handler = null; 181 this.key = null; 182 super.recycle(); 183 } 184 } 185 | Popular Tags |