KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thaiopensource > xml > sax > XmlBaseHandler


1 package com.thaiopensource.xml.sax;
2
3 import com.thaiopensource.util.Uri;
4 import org.xml.sax.Locator JavaDoc;
5
6 public class XmlBaseHandler {
7   private int depth = 0;
8   private Locator JavaDoc loc;
9   private Entry stack = null;
10
11   private static class Entry {
12     private Entry parent;
13     private String JavaDoc attValue;
14     private String JavaDoc systemId;
15     private int depth;
16   }
17
18   public void setLocator(Locator JavaDoc 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 JavaDoc 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 JavaDoc getSystemId() {
42     return loc == null ? null : loc.getSystemId();
43   }
44
45   public String JavaDoc getBaseUri() {
46     return getBaseUri1(getSystemId(), stack);
47   }
48
49   private static String JavaDoc getBaseUri1(String JavaDoc 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), baseUri);
57   }
58 }
59
Popular Tags