1 16 package org.apache.cocoon.components.validation.impl; 17 18 import java.io.IOException ; 19 20 import org.apache.cocoon.components.validation.Schema; 21 import org.apache.cocoon.components.validation.SchemaParser; 22 import org.apache.cocoon.components.validation.ValidatorException; 23 import org.apache.excalibur.source.Source; 24 import org.apache.excalibur.source.SourceValidity; 25 import org.apache.excalibur.store.Store; 26 import org.xml.sax.SAXException ; 27 28 37 public class CachingValidator extends DefaultValidator { 38 39 40 private Store store = null; 41 42 45 public CachingValidator() { 46 super(); 47 } 48 49 52 public void initialize() 53 throws Exception { 54 this.store = (Store) this.manager.lookup(Store.TRANSIENT_STORE); 55 super.initialize(); 56 } 57 58 61 public void dispose() { 62 try { 63 super.dispose(); 64 } finally { 65 if (this.store != null) this.manager.release(this.store); 66 } 67 } 68 69 90 public Schema getSchema(SchemaParser parser, Source source, String grammar) 91 throws IOException , SAXException { 92 93 94 String uri = source.getURI(); 95 String key = this.getClass().getName() + "[" + parser.getClass().getName() 96 + ":" + grammar + "]@" + source.getURI(); 97 Schema schema = null; 98 SourceValidity validity = null; 99 schema = (Schema) this.store.get(key); 100 101 102 if (schema != null) { 103 validity = schema.getValidity(); 104 if (validity == null) { 105 106 this.logger.warn("Cached schema " + uri + " has null validity"); 107 this.store.remove(key); 108 schema = null; 109 } else if (validity.isValid() != SourceValidity.VALID) { 110 if (this.logger.isDebugEnabled()) { 111 this.logger.debug("Cached schema " + uri + " no longer valid"); 112 } 113 this.store.remove(key); 114 schema = null; 115 } else if (this.logger.isDebugEnabled()) { 116 this.logger.debug("Valid cached schema found for " + uri); 117 } 118 } else if (this.logger.isDebugEnabled()) { 119 this.logger.debug("Schema " + uri + " not found in cache"); 120 } 121 122 123 if (schema == null) { 124 schema = super.getSchema(parser, source, grammar); 125 validity = schema.getValidity(); 126 if (validity != null) { 127 if (validity.isValid() == SourceValidity.VALID) { 128 this.store.store(key, schema); 129 } 130 } 131 } 132 133 134 return schema; 135 } 136 137 149 protected String detectGrammar(Source source) 150 throws IOException , SAXException , ValidatorException { 151 152 String uri = source.getURI(); 153 String key = this.getClass().getName() + "@" + source.getURI(); 154 155 CachedGrammar grammar = null; 156 grammar = (CachedGrammar) this.store.get(key); 157 158 159 if (grammar != null) { 160 if (grammar.validity == null) { 161 162 this.logger.warn("Grammar for " + uri + " has null validity"); 163 this.store.remove(key); 164 grammar = null; 165 } else if (grammar.validity.isValid() != SourceValidity.VALID) { 166 if (this.logger.isDebugEnabled()) { 167 this.logger.debug("Grammar for " + uri + " no longer valid"); 168 } 169 this.store.remove(key); 170 grammar = null; 171 } else if (this.logger.isDebugEnabled()) { 172 this.logger.debug("Valid cached grammar " + grammar + " for " + uri); 173 } 174 } 175 176 177 if (grammar != null) { 178 return grammar.grammar; 179 } else { 180 String language = super.detectGrammar(source); 181 SourceValidity validity = source.getValidity(); 182 if (validity != null) { 183 if (validity.isValid() == SourceValidity.VALID) { 184 this.store.store(key, new CachedGrammar(validity, language)); 185 } 186 } 187 return language; 188 } 189 } 190 191 195 private static final class CachedGrammar { 196 private final SourceValidity validity; 197 private final String grammar; 198 private CachedGrammar(SourceValidity validity, String grammar) { 199 this.validity = validity; 200 this.grammar = grammar; 201 } 202 } 203 } 204 | Popular Tags |