001    package com.thaiopensource.validate.jarv;
002    
003    import com.thaiopensource.util.PropertyMap;
004    import com.thaiopensource.validate.ValidateProperty;
005    import com.thaiopensource.validate.Validator;
006    import org.iso_relax.verifier.Verifier;
007    import org.xml.sax.ContentHandler;
008    import org.xml.sax.DTDHandler;
009    import org.xml.sax.EntityResolver;
010    import org.xml.sax.SAXException;
011    import org.xml.sax.helpers.DefaultHandler;
012    
013    public class VerifierValidator implements Validator {
014      private final Verifier verifier;
015      private ContentHandler handler;
016    
017      private static class ExceptionReportHandler extends DefaultHandler {
018        private final SAXException storedException;
019    
020        ExceptionReportHandler(SAXException storedException) {
021          this.storedException = storedException;
022        }
023    
024        public void startDocument()
025                throws SAXException {
026          throw storedException;
027        }
028      }
029    
030      public VerifierValidator(Verifier verifier, PropertyMap properties) {
031        this.verifier = verifier;
032        verifier.setErrorHandler(ValidateProperty.ERROR_HANDLER.get(properties));
033        EntityResolver er = ValidateProperty.ENTITY_RESOLVER.get(properties);
034        if (er != null)
035          verifier.setEntityResolver(er);
036        try {
037          handler = verifier.getVerifierHandler();
038        }
039        catch (SAXException e) {
040          handler = new ExceptionReportHandler(e);
041        }
042      }
043    
044      public void reset() {
045        try {
046          handler = verifier.getVerifierHandler();
047        }
048        catch (SAXException e) {
049          handler = new ExceptionReportHandler(e);
050        }
051      }
052    
053      public ContentHandler getContentHandler() {
054        return handler;
055      }
056    
057      public DTDHandler getDTDHandler() {
058        return null;
059      }
060    }