1
2
3
4 package net.sf.tomp.xtcl.resolver;
5
6 import javax.xml.transform.Source;
7 import javax.xml.transform.TransformerException;
8
9 import net.sf.tomp.xml.resolver.JavaResolver;
10 import net.sf.tomp.xtcl.Context;
11
12 import org.apache.xml.resolver.CatalogManager;
13
14 /***
15 * @author tomp
16 */
17 public class XTResolver extends JavaResolver {
18
19 protected static final String XTCL_PREFIX = "xtcl:";
20
21 private Context context;
22
23 /***
24 * default constructor
25 */
26 public XTResolver() {
27 super();
28 }
29
30 /***
31 * @param privateCatalog
32 */
33 public XTResolver(boolean privateCatalog) {
34 super(privateCatalog);
35 }
36
37 /***
38 * @param manager
39 */
40 public XTResolver(CatalogManager manager) {
41 super(manager);
42 }
43
44 /***
45 * @return Returns the context.
46 */
47 public Context getContext() {
48 return context;
49 }
50
51 /***
52 * @param context The context to set.
53 */
54 public void setContext(Context context) {
55 this.context = context;
56 }
57
58 /***
59 *
60 * forwards all calls to super.getResolvedEntity
61 *
62 * @see org.apache.xml.resolver.tools.CatalogResolver#getResolvedEntity(java.lang.String,
63 * java.lang.String)
64 */
65 public String getResolvedEntity(String publicId, String systemId) {
66
67
68 String s = super.getResolvedEntity(publicId, systemId);
69
70
71 return s;
72 }
73
74 /***
75 * hrefs starting with XTCL Context's prefix
76 * are resolved by asking context.refToSource
77 *
78 * @see javax.xml.transform.URIResolver#resolve(java.lang.String,
79 * java.lang.String)
80 */
81 public Source resolve(String href, String base) throws TransformerException {
82 if (isXT(href, base)) {
83 String ref = href;
84 String fragment = null;
85 int hashPos = href.indexOf("#");
86
87 if (hashPos >= 0) {
88 ref = href.substring(0, hashPos);
89 fragment = href.substring(hashPos + 1);
90 }
91
92 ref = ref.substring(XTCL_PREFIX.length());
93
94
95 return context.refToSource(ref);
96 } else {
97 return super.resolve(href, base);
98 }
99 }
100
101 private boolean isXT(String href, String base) {
102 return href.startsWith(XTCL_PREFIX);
103 }
104 }