1 17 package org.apache.excalibur.xml; 18 19 import java.io.IOException ; 20 21 import org.apache.avalon.framework.activity.Disposable; 22 import org.apache.avalon.framework.component.Component; 23 import org.apache.avalon.framework.logger.AbstractLogEnabled; 24 import org.apache.avalon.framework.parameters.ParameterException; 25 import org.apache.avalon.framework.parameters.Parameterizable; 26 import org.apache.avalon.framework.parameters.Parameters; 27 import org.apache.avalon.framework.service.ServiceException; 28 import org.apache.avalon.framework.service.ServiceManager; 29 import org.apache.avalon.framework.service.Serviceable; 30 import org.apache.avalon.framework.thread.ThreadSafe; 31 import org.apache.excalibur.source.Source; 32 import org.apache.excalibur.source.SourceResolver; 33 import org.apache.xml.resolver.CatalogManager; 34 import org.apache.xml.resolver.tools.CatalogResolver; 35 import org.xml.sax.InputSource ; 36 import org.xml.sax.SAXException ; 37 38 39 50 public class DefaultEntityResolver extends AbstractLogEnabled 51 implements EntityResolver, 52 Serviceable, 53 Parameterizable, 54 ThreadSafe, 55 Disposable, 56 Component { 57 58 59 protected CatalogManager catalogManager = new CatalogManager(); 60 61 62 protected CatalogResolver catalogResolver = new CatalogResolver(catalogManager); 63 64 65 protected ServiceManager manager; 66 67 68 protected SourceResolver resolver; 69 70 76 public void parameterize(Parameters params) 77 throws ParameterException 78 { 79 80 String verbosity = params.getParameter("verbosity", null); 82 if ( null != verbosity ) 83 { 84 if (this.getLogger().isDebugEnabled()) 85 { 86 this.getLogger().debug("Setting Catalog resolver " 87 + "verbosity level to " + verbosity); 88 } 89 int verbosityLevel = 0; 90 try 91 { 92 verbosityLevel = Integer.parseInt(verbosity); 93 catalogManager.setVerbosity(verbosityLevel); 94 } 95 catch (NumberFormatException ce1) 96 { 97 this.getLogger().warn("Trouble setting Catalog verbosity", 98 ce1); 99 } 100 } 101 102 String catalogFile = params.getParameter("catalog", this.defaultCatalog()); 104 if ( null == catalogFile) 105 { 106 this.getLogger().warn("No default catalog defined."); 107 } 108 else 109 { 110 this.parseCatalog(catalogFile); 111 } 112 113 String localCatalogFile = params.getParameter("local-catalog", null); 115 if ( null != localCatalogFile ) 116 { 117 this.parseCatalog( localCatalogFile ); 118 } 119 } 120 121 124 protected void parseCatalog(String file) 125 { 126 if (this.getLogger().isDebugEnabled()) 127 { 128 this.getLogger().debug("Additional Catalog is " + file); 129 } 130 131 Source source = null; 132 try 133 { 134 source = this.resolver.resolveURI(file); 135 this.catalogResolver.getCatalog().parseCatalog(source.getURI()); 136 } 137 catch (Exception e) 138 { 139 this.getLogger().warn("Could not get Catalog file. Trying again: " + file, e); 140 141 if ( null != source ) 143 { 144 try 145 { 146 String mimeType = source.getMimeType(); 147 if ( null == mimeType ) mimeType =" text/plain"; 148 this.catalogResolver.getCatalog().parseCatalog(mimeType, source.getInputStream()); 149 } 150 catch (Exception ex) 151 { 152 this.getLogger().warn("Could not get Catalog file: " + file, ex); 153 } 154 } 155 } 156 finally 157 { 158 this.resolver.release( source ); 159 } 160 } 161 162 165 protected String defaultCatalog() 166 { 167 return null; 168 } 169 170 175 public void service(ServiceManager manager) 176 throws ServiceException 177 { 178 this.manager = manager; 179 this.resolver = (SourceResolver) this.manager.lookup( SourceResolver.ROLE ); 180 } 181 182 216 public InputSource resolveEntity(String publicId, String systemId) 217 throws SAXException , IOException 218 { 219 InputSource altInputSource = catalogResolver.resolveEntity(publicId, 220 systemId); 221 if (altInputSource != null) 222 { 223 if (this.getLogger().isDebugEnabled()) 224 { 225 this.getLogger().debug("Resolved catalog entity: " 226 + publicId + " " + altInputSource.getSystemId()); 227 } 228 } 229 230 return altInputSource; 231 } 232 233 236 public void dispose() 237 { 238 if ( null != this.resolver ) 239 { 240 this.manager.release( this.resolver ); 241 } 242 } 243 } 244 | Popular Tags |