1 50 51 package org.apache.excalibur.configuration.validation; 52 53 import org.apache.avalon.framework.configuration.Configuration; 54 import org.apache.avalon.framework.configuration.ConfigurationException; 55 import org.apache.avalon.framework.configuration.DefaultConfiguration; 56 import org.apache.avalon.framework.configuration.DefaultConfigurationSerializer; 57 import org.iso_relax.verifier.Schema; 58 import org.iso_relax.verifier.Verifier; 59 import org.iso_relax.verifier.VerifierConfigurationException; 60 import org.iso_relax.verifier.VerifierHandler; 61 import org.xml.sax.ErrorHandler ; 62 import org.xml.sax.SAXException ; 63 import org.xml.sax.SAXParseException ; 64 65 69 public class JarvConfigurationValidator implements ConfigurationValidator 70 { 71 private final DefaultConfigurationSerializer m_serializer = 72 new DefaultConfigurationSerializer(); 73 74 private final Schema m_schema; 75 76 public JarvConfigurationValidator( Schema schema ) 77 { 78 m_schema = schema; 79 } 80 81 public ValidationResult isFeasiblyValid( Configuration configuration ) 82 throws ConfigurationException 83 { 84 final ValidationResult result = new ValidationResult(); 85 86 result.setResult( true ); 87 88 return result; 89 } 90 91 public ValidationResult isValid( Configuration configuration ) 92 throws ConfigurationException 93 { 94 final ValidationResult result = new ValidationResult(); 95 final DefaultConfiguration branched = 96 new DefaultConfiguration( "root", configuration.getLocation() ); 97 branched.addAll( configuration ); 98 branched.makeReadOnly(); 99 100 try 101 { 102 final Verifier verifier = m_schema.newVerifier(); 103 final VerifierHandler handler = verifier.getVerifierHandler(); 104 105 verifier.setErrorHandler( new ErrorHandler () 106 { 107 public void warning( SAXParseException exception ) 108 throws SAXException 109 { 110 result.addWarning( exception.getMessage() ); 111 } 112 113 public void error( SAXParseException exception ) 114 throws SAXException 115 { 116 result.addError( exception.getMessage() ); 117 } 118 119 public void fatalError( final SAXParseException exception ) 120 throws SAXException 121 { 122 result.addError( exception.getMessage() ); 123 } 124 } ); 125 126 m_serializer.serialize( handler, branched ); 127 128 result.setResult( handler.isValid() ); 129 130 return result; 131 } 132 catch( final VerifierConfigurationException e ) 133 { 134 final String message = "Unable to verify configuration"; 135 throw new ConfigurationException( message, e ); 136 } 137 catch( final SAXException e ) 138 { 139 final String message = "Unable to parse configuration"; 140 throw new ConfigurationException( message, e ); 141 } 142 catch( final IllegalStateException e ) 143 { 144 final String message = "Unable to parse configuration"; 145 throw new ConfigurationException( message, e ); 146 } 147 } 148 } 149 | Popular Tags |