001    package com.thaiopensource.validate.jarv;
002    
003    import com.thaiopensource.util.PropertyMap;
004    import com.thaiopensource.validate.IncorrectSchemaException;
005    import com.thaiopensource.validate.Schema;
006    import com.thaiopensource.validate.SchemaReader;
007    import com.thaiopensource.validate.Validator;
008    import com.thaiopensource.validate.Option;
009    import com.thaiopensource.validate.AbstractSchema;
010    import org.iso_relax.verifier.VerifierConfigurationException;
011    import org.iso_relax.verifier.VerifierFactory;
012    import org.xml.sax.InputSource;
013    import org.xml.sax.SAXException;
014    
015    import java.io.IOException;
016    
017    public class VerifierFactorySchemaReader implements SchemaReader {
018      private final VerifierFactory vf;
019    
020      static private class SchemaImpl extends AbstractSchema {
021        final org.iso_relax.verifier.Schema schema;
022    
023        private SchemaImpl(org.iso_relax.verifier.Schema schema) {
024          this.schema = schema;
025        }
026    
027        public Validator createValidator(PropertyMap properties) {
028          try {
029            return new VerifierValidator(schema.newVerifier(), properties);
030          }
031          catch (VerifierConfigurationException e) {
032            Exception cause = e.getCauseException();
033            if (cause instanceof RuntimeException
034                && (e.getMessage() == null || e.getMessage().equals(cause.getMessage())))
035              throw (RuntimeException)cause;
036            throw new JarvConfigurationException(e);
037          }
038        }
039      }
040    
041      public VerifierFactorySchemaReader(VerifierFactory vf) {
042        this.vf = vf;
043      }
044    
045      public Schema createSchema(InputSource in, PropertyMap properties)
046              throws IOException, SAXException, IncorrectSchemaException {
047        try {
048          return new SchemaImpl(vf.compileSchema(in));
049        }
050        catch (SAXException e) {
051          System.err.println("compileSchema threw a SAXException class " + e.getClass().toString());
052          if (e.getException() != null)
053            System.err.println("cause has class " + e.getException().getClass().toString());
054          throw e;
055        }
056        catch (VerifierConfigurationException e) {
057          for (;;) {
058            Exception cause = e.getCauseException();
059            String message = e.getMessage();
060            if (cause != null && message != null && message.equals(cause.getMessage()))
061              message = null; // don't really have a message
062            if (message == null) {
063              if (cause instanceof RuntimeException)
064                throw (RuntimeException)cause;
065              if (cause instanceof SAXException)
066                throw (SAXException)cause;
067              if (cause instanceof IOException)
068                throw (IOException)cause;
069              if (cause instanceof VerifierConfigurationException) {
070                e = (VerifierConfigurationException)cause;
071                continue;
072              }
073            }
074            throw new SAXException(message, cause);
075          }
076        }
077    
078      }
079    
080      public Option getOption(String uri) {
081        return null;
082      }
083    }