01: package org.kohsuke.rngom.xml.sax;
02:
03: import org.kohsuke.rngom.util.Uri;
04: import org.xml.sax.Locator;
05:
06: public class XmlBaseHandler {
07: private int depth = 0;
08: private Locator loc;
09: private Entry stack = null;
10:
11: private static class Entry {
12: private Entry parent;
13: private String attValue;
14: private String systemId;
15: private int depth;
16: }
17:
18: public void setLocator(Locator loc) {
19: this .loc = loc;
20: }
21:
22: public void startElement() {
23: ++depth;
24: }
25:
26: public void endElement() {
27: if (stack != null && stack.depth == depth)
28: stack = stack.parent;
29: --depth;
30: }
31:
32: public void xmlBaseAttribute(String value) {
33: Entry entry = new Entry();
34: entry.parent = stack;
35: stack = entry;
36: entry.attValue = Uri.escapeDisallowedChars(value);
37: entry.systemId = getSystemId();
38: entry.depth = depth;
39: }
40:
41: private String getSystemId() {
42: return loc == null ? null : loc.getSystemId();
43: }
44:
45: public String getBaseUri() {
46: return getBaseUri1(getSystemId(), stack);
47: }
48:
49: private static String getBaseUri1(String baseUri, Entry stack) {
50: if (stack == null
51: || (baseUri != null && !baseUri.equals(stack.systemId)))
52: return baseUri;
53: baseUri = stack.attValue;
54: if (Uri.isAbsolute(baseUri))
55: return baseUri;
56: return Uri.resolve(getBaseUri1(stack.systemId, stack.parent),
57: baseUri);
58: }
59: }
|