View Javadoc

1   /*
2    * Copyright 1999-2006 Faculty of Mathematics, Physics and Informatics, Comenius
3    * University, Bratislava. This file is protected by the Mozilla Public License
4    * version 1.1 (the License); you may not use this file except in compliance
5    * with the License. You may obtain a copy of the License at
6    * http://euromath2.sourceforge.net/license.html Unless required by applicable
7    * law or agreed to in writing, software distributed under the License is
8    * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
9    * KIND, either express or implied. See the License for the specific language
10   * governing permissions and limitations under the License.
11   */
12  package sk.uniba.euromath.document;
13  import java.io.IOException;
14  import java.net.URI;
15  import java.net.URISyntaxException;
16  import java.util.List;
17  import org.xml.sax.EntityResolver;
18  import org.xml.sax.InputSource;
19  import org.xml.sax.SAXException;
20  import sk.baka.ikslibs.sax.SAXExceptionFixed;
21  import sk.uniba.euromath.config.EuromathConfig;
22  import sk.uniba.euromath.config.bind.ResolveType;
23  import sk.uniba.euromath.tools.URLDir;
24  /***
25   * Implements the entity resolver, that resolves entity references to locally
26   * stored entities, when possible. This mapping is defined in config file. If no
27   * mapping is possible then resolver roots local URI in given Eclipse project
28   * path.
29   * @author Martin Vysny
30   */
31  public final class EmEntityResolver implements EntityResolver {
32  	/***
33  	 * Directory where the document is located.
34  	 */
35  	public final URI root;
36  	/***
37  	 * Constructor.
38  	 * @param root directory where the document is located.
39  	 */
40  	public EmEntityResolver(URLDir root) {
41  		super();
42  		this.root = root.getURI();
43  	}
44  	/*
45  	 * (non-Javadoc)
46  	 * @see org.xml.sax.EntityResolver#resolveEntity(java.lang.String,
47  	 * java.lang.String)
48  	 */
49  	public InputSource resolveEntity(String publicId, String systemId)
50  			throws SAXException, IOException {
51  		final InputSource result = findLocalEntities(systemId);
52  		if (result != null)
53  			return result;
54  		// not stored locally, process it normally
55  		final URI resolvedURI = root.resolve(systemId);
56  		return new InputSource(resolvedURI.toString());
57  	}
58  	/***
59  	 * Checks if an entity set with given systemId is stored in local
60  	 * repository. If yes then returns source redirected to the locally stored
61  	 * set.
62  	 * @param systemId URI address of the entity set to check
63  	 * @return input source pointing onto same locally stored entity set, or
64  	 * <code>null</code> if this set is not known.
65  	 * @throws SAXException
66  	 * @throws IOException
67  	 */
68  	private InputSource findLocalEntities(String systemId) throws SAXException,
69  			IOException {
70  		// check if we have entity definition, denoted by systemId, stored
71  		// locally.
72  		if (EuromathConfig.getConfig().getLocalCache() == null)
73  			return null;
74  		@SuppressWarnings("unchecked")
75  		final List<ResolveType> entityRefs = EuromathConfig
76  				.getConfig().getLocalCache().getResolve();
77  		if (entityRefs == null)
78  			return null;
79  		for (final ResolveType item : entityRefs) {
80  			if (systemId.equals(item.getUrl())) {
81  				try {
82  					return new InputSource(EuromathConfig.getSchemaLoc()
83  							.resolve(item.getValue()).toString());
84  				} catch (URISyntaxException ex) {
85  					throw new SAXExceptionFixed(ex);
86  				}
87  			}
88  		}
89  		// cannot find locally stored entities, return null
90  		return null;
91  	}
92  }