1 16 package org.apache.cocoon.components.validation.impl; 17 18 import java.io.IOException ; 19 import java.util.ArrayList ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 23 import org.apache.excalibur.source.Source; 24 import org.apache.excalibur.source.SourceResolver; 25 import org.apache.excalibur.source.SourceValidity; 26 import org.apache.excalibur.source.impl.validity.AggregatedValidity; 27 import org.xml.sax.EntityResolver ; 28 import org.xml.sax.InputSource ; 29 import org.xml.sax.SAXException ; 30 31 41 public class ValidationResolver implements EntityResolver { 42 43 44 private final SourceResolver sourceResolver; 45 46 private final EntityResolver entityResolver; 47 48 private final AggregatedValidity sourceValidity; 49 50 51 private final List sources = new ArrayList (); 52 53 54 private boolean closed = false; 55 56 62 public ValidationResolver(SourceResolver sourceResolver, 63 EntityResolver entityResolver) { 64 if (sourceResolver == null) throw new NullPointerException ("Null source"); 65 if (entityResolver == null) throw new NullPointerException ("Null entity"); 66 this.sourceValidity = new AggregatedValidity(); 67 this.sourceResolver = sourceResolver; 68 this.entityResolver = entityResolver; 69 } 70 71 74 public InputSource resolveSource(Source source) 75 throws IOException , SAXException { 76 return this.resolveSource(source, null, null); 77 } 78 79 83 public InputSource resolveSource(Source source, String systemId) 84 throws IOException , SAXException { 85 return this.resolveSource(source, systemId, null); 86 } 87 88 96 public InputSource resolveSource(Source source, String systemId, String publicId) 97 throws IOException , SAXException { 98 if (this.closed) throw new IllegalStateException ("Resolver closed"); 99 100 101 if (source == null) throw new NullPointerException ("Null source specified"); 102 103 104 this.sourceValidity.add(source.getValidity()); 105 106 107 if (systemId == null) systemId = source.getURI(); 108 109 110 InputSource input = new InputSource (systemId); 111 input.setByteStream(source.getInputStream()); 112 if (publicId != null) input.setPublicId(publicId); 113 return input; 114 } 115 116 120 public InputSource resolveEntity(String systemId) 121 throws IOException , SAXException { 122 return this.resolveEntity(null, null, systemId); 123 } 124 125 129 public InputSource resolveEntity(String publicId, String systemId) 130 throws IOException , SAXException { 131 return this.resolveEntity(null, publicId, systemId); 132 } 133 134 138 public InputSource resolveEntity(String base, String publicId, String systemId) 139 throws IOException , SAXException { 140 if (this.closed) throw new IllegalStateException ("Resolver closed"); 141 142 143 if (systemId == null) { 144 InputSource source = this.entityResolver.resolveEntity(publicId, null); 145 if ((source == null) || (source.getSystemId() == null)) { 146 throw new IOException ("Can't resolve \"" + publicId + "\""); 147 } 148 systemId = source.getSystemId(); 149 } 150 151 152 final Source source; 153 if (base == null) { 154 source = this.sourceResolver.resolveURI(systemId); 155 } else { 156 source = this.sourceResolver.resolveURI(systemId, base, null); 157 } 158 159 160 this.sources.add(source); 161 162 163 return this.resolveSource(source, systemId, publicId); 164 } 165 166 171 public SourceValidity close() { 172 173 174 Iterator iterator = this.sources.iterator(); 175 while (iterator.hasNext()) { 176 this.sourceResolver.release((Source) iterator.next()); 177 } 178 179 180 this.closed = true; 181 182 183 return this.sourceValidity; 184 } 185 186 190 protected void finalize() 191 throws Throwable { 192 try { 193 super.finalize(); 194 } finally { 195 if (this.closed) return; 196 this.close(); 197 } 198 } 199 } 200 | Popular Tags |