1
2
3
4
5
6
7
8
9
10
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
46
47
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
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
71
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
90 return null;
91 }
92 }