Attachment 'AbstractValidatorTransformer.java'

Download

   1 /** Creado en 27-feb-2004 */
   2 package fcc.ima.cocoon;
   3 
   4 import java.io.IOException;
   5 import java.io.InputStream;
   6 import java.util.Map;
   7 
   8 import org.apache.avalon.framework.parameters.Parameters;
   9 import org.apache.cocoon.ProcessingException;
  10 import org.apache.cocoon.components.sax.XMLTeePipe;
  11 import org.apache.cocoon.environment.SourceResolver;
  12 import org.apache.cocoon.transformation.AbstractTransformer;
  13 import org.apache.cocoon.xml.AttributesImpl;
  14 import org.apache.cocoon.xml.XMLConsumer;
  15 import org.iso_relax.verifier.Schema;
  16 import org.iso_relax.verifier.Verifier;
  17 import org.iso_relax.verifier.VerifierConfigurationException;
  18 import org.iso_relax.verifier.VerifierFactory;
  19 import org.iso_relax.verifier.VerifierHandler;
  20 import org.xml.sax.EntityResolver;
  21 import org.xml.sax.ErrorHandler;
  22 import org.xml.sax.InputSource;
  23 import org.xml.sax.SAXException;
  24 import org.xml.sax.SAXParseException;
  25 
  26 /**For debugging or quality control purposes, it checks the conformance of the XML data it receives with any schema, DTD supplied to the transformer
  27  * @author dperezcar */
  28 public abstract class AbstractValidatorTransformer extends AbstractTransformer implements ErrorHandler {
  29 	/**Compiled schema used for validate*/
  30 	protected Schema schema;
  31 	/**For accessing entity files and other auxiliary files*/
  32 	protected EntityResolver entityResolver;
  33 	protected AttributesImpl attr = new AttributesImpl();
  34 	protected String defaultFactory = "com.sun.msv.verifier.jarv.TheFactoryImpl";
  35 	/**For locating errors and warnings, even in the absence of a real {@link org.xml.sax.Locator}*/
  36 	protected LocationTracker locator;
  37 
  38 	/**Adapter from {@link SourceResolver} to {@link EntityResolver}.*/	
  39 	static class MyEntityResolver implements EntityResolver {
  40 		MyEntityResolver(SourceResolver resolver) {
  41 			this.resolver = resolver;
  42 		}
  43 		/** @see org.xml.sax.EntityResolver#resolveEntity(java.lang.String, java.lang.String) */
  44 		public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
  45 			return new InputSource(resolver.resolveURI(systemId).getInputStream());
  46 		}
  47 		protected SourceResolver resolver;
  48 	}
  49 
  50 	/** @see org.apache.cocoon.sitemap.SitemapModelComponent#setup(org.apache.cocoon.environment.SourceResolver, java.util.Map, java.lang.String, org.apache.avalon.framework.parameters.Parameters) */
  51 	public void setup(SourceResolver resolver, Map objectModel, String src, Parameters par) 
  52 		throws ProcessingException, SAXException, IOException {
  53 
  54 		this.entityResolver = new MyEntityResolver(resolver);
  55 		try {
  56 			VerifierFactory factory;
  57 			String factoryName = par.getParameter("language", null);
  58 			if (factoryName != null) {
  59 				factory = VerifierFactory.newInstance(factoryName);
  60 			} else {
  61 				try {
  62 					factory = (VerifierFactory)Class.forName(par.getParameter("factory", defaultFactory)).newInstance();
  63 				} catch (Exception e) {
  64 					throw new ProcessingException(e);
  65 				}
  66 			}
  67 			factory.setEntityResolver(entityResolver);
  68 			InputStream stream = resolver.resolveURI(src).getInputStream();
  69 			try {
  70 				InputSource source = new InputSource(stream);
  71 				source.setSystemId(src);
  72 				schema = factory.compileSchema(source);
  73 			} finally {
  74 				stream.close();
  75 			}
  76 		} catch (VerifierConfigurationException e) {
  77 			throw new ProcessingException(e);
  78 		}
  79 	}
  80 	/** @see org.apache.cocoon.xml.XMLProducer#setConsumer(org.apache.cocoon.xml.XMLConsumer) */
  81 	protected XMLConsumer getVerifierConsumer() throws VerifierConfigurationException, SAXException {
  82 		Verifier verifier = schema.newVerifier();
  83 		verifier.setErrorHandler(this);
  84 		verifier.setEntityResolver(entityResolver);
  85 		locator = new LocationTracker();
  86 		VerifierHandler verifierHandler = verifier.getVerifierHandler();
  87 		verifierHandler.setDocumentLocator(locator);
  88 		return new XMLTeePipe(locator, new MiXMLPipe(verifierHandler));
  89 	}
  90 	/**Get all info for determining the error source*/
  91 	protected String getErrorMsg(SAXParseException exc) {
  92 		StringBuffer sb = new StringBuffer(20);
  93 		sb.append(exc.getMessage());
  94 		if (exc.getLineNumber() >= 0) {
  95 			sb.append(", line=");
  96 			sb.append(exc.getLineNumber());
  97 		}
  98 		if (exc.getColumnNumber() >= 0) {
  99 			sb.append(", column=");
 100 			sb.append(exc.getColumnNumber());
 101 		}
 102 		if (exc.getSystemId() != null) {
 103 			sb.append(", system id=");
 104 			sb.append(exc.getSystemId());
 105 		}
 106 		if (exc.getPublicId() != null) {
 107 			sb.append(", public id=");
 108 			sb.append(exc.getPublicId());
 109 		}
 110 		return sb.toString();
 111 	}
 112 }

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.