View Javadoc

1   /*
2    * Copyright 2006-2008 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.osgi.context.support;
18  
19  import java.io.IOException;
20  import java.util.Iterator;
21  import java.util.LinkedHashMap;
22  import java.util.Map;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.springframework.util.Assert;
27  import org.xml.sax.EntityResolver;
28  import org.xml.sax.InputSource;
29  import org.xml.sax.SAXException;
30  
31  /**
32   * Delegated XML entity resolver.
33   * 
34   * @author Costin Leau
35   * 
36   */
37  class DelegatedEntityResolver implements EntityResolver {
38  
39  	/** logger */
40  	private static final Log log = LogFactory.getLog(DelegatedEntityResolver.class);
41  
42  	private final Map resolvers = new LinkedHashMap(2);
43  
44  
45  	public void addEntityResolver(EntityResolver resolver, String resolverToString) {
46  		Assert.notNull(resolver);
47  		resolvers.put(resolver, resolverToString);
48  	}
49  
50  	public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
51  		boolean trace = log.isTraceEnabled();
52  
53  		for (Iterator iterator = resolvers.entrySet().iterator(); iterator.hasNext();) {
54  			Map.Entry entry = (Map.Entry) iterator.next();
55  			EntityResolver entityResolver = (EntityResolver) entry.getKey();
56  			if (trace)
57  				log.trace("Trying to resolve entity [" + publicId + "|" + systemId + "] through resolver "
58  						+ entry.getValue());
59  			InputSource entity = entityResolver.resolveEntity(publicId, systemId);
60  
61  			String resolvedMsg = (entity != null ? "" : "not ");
62  			if (trace)
63  				log.trace("Entity [" + publicId + "|" + systemId + "] was " + resolvedMsg
64  						+ "resolved through entity resolver " + entry.getValue());
65  
66  			if (entity != null) {
67  				return entity;
68  			}
69  		}
70  		return null;
71  	}
72  }