Attachment 'ValidatorTransformer.java'

Download

   1 /** Creado en 27-feb-2004 */
   2 package fcc.ima.cocoon;
   3 
   4 import java.io.IOException;
   5 import java.util.Map;
   6 
   7 import org.apache.avalon.framework.configuration.Configurable;
   8 import org.apache.avalon.framework.configuration.Configuration;
   9 import org.apache.avalon.framework.configuration.ConfigurationException;
  10 import org.apache.avalon.framework.parameters.Parameters;
  11 import org.apache.cocoon.ProcessingException;
  12 import org.apache.cocoon.components.sax.XMLTeePipe;
  13 import org.apache.cocoon.environment.SourceResolver;
  14 import org.apache.cocoon.xml.XMLConsumer;
  15 import org.xml.sax.SAXException;
  16 import org.xml.sax.SAXParseException;
  17 
  18 /**For debugging or quality control purposes, it checks the conformance of the XML data it receives with any schema, DTD supplied to the transformer
  19  * @author <a href="mailto:dperezcar@fcc.es">Perez Carmona, David</a> 
  20  * @author <a href="mailto:d.madama@pro-netics.com>Daniele Madama</a> */
  21 public class ValidatorTransformer extends AbstractValidatorTransformer implements Configurable {
  22 		/**Default value for {@link #active} parameter.*/
  23 	private boolean defaultActive;
  24 		/**It's validation active?*/
  25 	private boolean active;
  26 		/**Throw exceptions when an error is detected?*/
  27 	protected boolean stopForErrors, stopForWarnings, stopForFatal;
  28 
  29 	/** @see org.apache.cocoon.sitemap.SitemapModelComponent#setup(org.apache.cocoon.environment.SourceResolver, java.util.Map, java.lang.String, org.apache.avalon.framework.parameters.Parameters) */
  30 	public void setup(SourceResolver resolver, Map objectModel, String src, Parameters par) 
  31 		throws ProcessingException, SAXException, IOException {
  32 
  33 		active = par.getParameterAsBoolean("active", defaultActive);
  34 		if (!active) {
  35 			return;
  36 		}
  37 		try {
  38 			super.setup(resolver, objectModel, src, par);
  39 		} catch (Exception e) {
  40 			getLogger().error("Loading validator "+src, e);
  41 			active = false;
  42 		}
  43 		if (par.isParameter("stop-for-fatal")) {
  44 			stopForFatal = par.getParameterAsBoolean("stop-for-fatal", true);
  45 		}
  46 		if (par.isParameter("stop-for-errors")) {
  47 			stopForErrors = par.getParameterAsBoolean("stop-for-errors", false);
  48 		}
  49 		if (par.isParameter("stop-for-warnings")) {
  50 			stopForWarnings = par.getParameterAsBoolean("stop-for-warnings", false);
  51 		}
  52 	}
  53 	/** @see org.apache.cocoon.xml.XMLProducer#setConsumer(org.apache.cocoon.xml.XMLConsumer) */
  54 	public void setConsumer(XMLConsumer consumer) {
  55 		if (active) {
  56 			try {
  57 				super.setConsumer(new XMLTeePipe(consumer, getVerifierConsumer()));
  58 			} catch (Exception e) {
  59 				getLogger().error("Error setting consumer", e);
  60 				super.setConsumer(consumer);
  61 			}
  62 		} else {
  63 			super.setConsumer(consumer);
  64 		}
  65 	}
  66 	/** @see org.apache.avalon.excalibur.pool.Recyclable#recycle() */
  67 	public void recycle() {
  68 		super.recycle();
  69 	}
  70 	/** @see org.xml.sax.ErrorHandler#error(org.xml.sax.SAXParseException) */
  71 	public void error(SAXParseException exc) throws SAXException {
  72 		getLogger().error(getErrorMsg(exc));
  73 		if (stopForErrors) {
  74 			throw exc;
  75 		}
  76 	}
  77 	/** @see org.xml.sax.ErrorHandler#fatalError(org.xml.sax.SAXParseException) */
  78 	public void fatalError(SAXParseException exc) throws SAXException {
  79 		getLogger().fatalError(getErrorMsg(exc));
  80 		if (stopForFatal) {
  81 			throw exc;
  82 		}
  83 	}
  84 	/** @see org.xml.sax.ErrorHandler#warning(org.xml.sax.SAXParseException) */
  85 	public void warning(SAXParseException exc) throws SAXException {
  86 		getLogger().warn(getErrorMsg(exc));
  87 		if (stopForWarnings) {
  88 			throw exc;
  89 		}
  90 	}
  91 	/** @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration) */
  92 	public void configure(Configuration conf) throws ConfigurationException {
  93 		defaultFactory = conf.getChild("factory").getValue("com.sun.msv.verifier.jarv.TheFactoryImpl");
  94 		defaultActive = conf.getChild("active").getValueAsBoolean(true);
  95 		stopForWarnings = conf.getChild("stop-for-warnings").getValueAsBoolean(false);
  96 		stopForErrors = conf.getChild("stop-for-errors").getValueAsBoolean(false);
  97 		stopForFatal = conf.getChild("stop-for-fatal").getValueAsBoolean(true);
  98 	}
  99 }

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.

You are not allowed to attach a file to this page.