1 16 package org.apache.cocoon.transformation; 17 18 import java.io.IOException ; 19 import java.io.Serializable ; 20 import java.util.ArrayList ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 import java.util.Map ; 24 25 import org.apache.avalon.framework.activity.Disposable; 26 import org.apache.avalon.framework.configuration.Configurable; 27 import org.apache.avalon.framework.configuration.Configuration; 28 import org.apache.avalon.framework.configuration.ConfigurationException; 29 import org.apache.avalon.framework.parameters.Parameters; 30 import org.apache.avalon.framework.service.ServiceException; 31 import org.apache.avalon.framework.service.ServiceManager; 32 import org.apache.avalon.framework.service.Serviceable; 33 import org.apache.cocoon.ProcessingException; 34 import org.apache.cocoon.caching.CacheableProcessingComponent; 35 import org.apache.cocoon.components.validation.ValidationHandler; 36 import org.apache.cocoon.components.validation.Validator; 37 import org.apache.cocoon.environment.SourceResolver; 38 import org.apache.cocoon.xml.XMLConsumer; 39 import org.apache.excalibur.source.Source; 40 import org.apache.excalibur.source.SourceValidity; 41 import org.xml.sax.ContentHandler ; 42 import org.xml.sax.ErrorHandler ; 43 import org.xml.sax.SAXException ; 44 import org.xml.sax.SAXParseException ; 45 import org.xml.sax.helpers.AttributesImpl ; 46 47 86 public class ValidationReportTransformer extends AbstractTransformer 87 implements Configurable, Serviceable, Disposable, CacheableProcessingComponent { 88 89 90 private ServiceManager serviceManager = null; 91 92 private Validator validator = null; 93 94 private String grammar = null; 95 96 97 private Report report = null; 98 99 private ValidationHandler handler = null; 100 101 private XMLConsumer consumer = null; 102 103 private String key = null; 104 105 108 public ValidationReportTransformer() { 109 super(); 110 } 111 112 119 public void service(ServiceManager manager) 120 throws ServiceException { 121 this.serviceManager = manager; 122 this.validator = (Validator) manager.lookup(Validator.ROLE); 123 } 124 125 136 public void configure(Configuration configuration) 137 throws ConfigurationException { 138 this.grammar = configuration.getChild("grammar").getValue(null); 139 } 140 141 145 public void dispose() { 146 this.serviceManager.release(this.validator); 147 } 148 149 158 public void setup(SourceResolver resolver, Map objectModel, String source, 159 Parameters parameters) 160 throws ProcessingException, SAXException , IOException { 161 Source s = null; 162 try { 163 Report r = new Report(); 164 String g = parameters.getParameter("grammar", this.grammar); 165 s = resolver.resolveURI(source); 166 if (g == null) { 167 this.handler = this.validator.getValidationHandler(s, r); 168 } else{ 169 this.handler = this.validator.getValidationHandler(s, g, r); 170 } 171 this.setContentHandler(this.handler); 172 this.setLexicalHandler(this.handler); 173 this.report = r; 174 } finally { 175 if (source != null) resolver.release(s); 176 } 177 } 178 179 185 public void setConsumer(XMLConsumer consumer) { 186 this.consumer = consumer; 187 } 188 189 196 public void endDocument() 197 throws SAXException { 198 super.endDocument(); 199 this.report.generateReport(this.consumer); 200 } 201 202 208 public Serializable getKey() { 209 return this.key; 210 } 211 212 218 public SourceValidity getValidity() { 219 return this.handler.getValidity(); 220 } 221 222 225 public void recycle() { 226 this.consumer = null; 227 this.handler = null; 228 this.report = null; 229 this.key = null; 230 super.recycle(); 231 } 232 233 236 private static final class Report implements ErrorHandler { 237 238 private static final String NS = "http://apache.org/cocoon/validation/1.0"; 239 private final List entries = new ArrayList (); 240 241 public void warning(SAXParseException exception) 242 throws SAXException { 243 this.entries.add(new ReportEntry("warning", exception)); 244 } 245 246 public void error(SAXParseException exception) 247 throws SAXException { 248 this.entries.add(new ReportEntry("error", exception)); 249 } 250 251 public void fatalError(SAXParseException exception) 252 throws SAXException { 253 this.entries.add(new ReportEntry("fatal", exception)); 254 } 255 256 private void generateReport(ContentHandler handler) 257 throws SAXException { 258 259 handler.startDocument(); 260 handler.startPrefixMapping("", NS); 261 AttributesImpl attributes = new AttributesImpl (); 262 handler.startElement(NS, "report", "report", attributes); 263 264 265 Iterator iterator = this.entries.iterator(); 266 while(iterator.hasNext()) { 267 ReportEntry entry = (ReportEntry) iterator.next(); 268 attributes.clear(); 269 270 if (entry.exception.getPublicId() != null) { 271 if (! "".equals(entry.exception.getPublicId())) { 272 attributes.addAttribute("", "public", "public", "CDATA", 273 entry.exception.getPublicId()); 274 } 275 } 276 277 if (entry.exception.getSystemId() != null) { 278 if (! "".equals(entry.exception.getSystemId())) { 279 attributes.addAttribute("", "system", "system", "CDATA", 280 entry.exception.getSystemId()); 281 } 282 } 283 284 if (entry.exception.getLineNumber() >= 0) { 285 String l = Integer.toString(entry.exception.getLineNumber()); 286 attributes.addAttribute("", "line", "line", "CDATA", l); 287 } 288 289 if (entry.exception.getColumnNumber() >= 0) { 290 String c = Integer.toString(entry.exception.getColumnNumber()); 291 attributes.addAttribute("", "column", "column", "CDATA", c); 292 } 293 294 String level = entry.level; 295 handler.startElement(NS, level, level, attributes); 296 char message[] = entry.exception.getMessage().toCharArray(); 297 handler.characters(message, 0, message.length); 298 handler.endElement(NS, level, level); 299 } 300 301 302 handler.endElement(NS, "report", "report"); 303 handler.endPrefixMapping(""); 304 handler.endDocument(); 305 } 306 } 307 308 private static final class ReportEntry { 309 310 private final String level; 311 private final SAXParseException exception; 312 313 private ReportEntry(String level, SAXParseException exception) { 314 this.level = level; 315 this.exception = exception; 316 } 317 } 318 } 319 | Popular Tags |