1 16 package org.apache.cocoon.xml; 17 18 import org.xml.sax.Attributes ; 19 import org.xml.sax.SAXException ; 20 import org.apache.excalibur.source.SourceResolver; 21 import org.apache.excalibur.source.Source; 22 import org.apache.avalon.framework.logger.Logger; 23 24 import java.util.Stack ; 25 import java.util.Collections ; 26 import java.io.IOException ; 27 28 41 public class XMLBaseSupport { 42 public static final String XMLBASE_NAMESPACE_URI = "http://www.w3.org/XML/1998/namespace"; 43 public static final String XMLBASE_ATTRIBUTE = "base"; 44 45 46 private int level = 0; 47 51 private Stack bases = new Stack (); 52 private SourceResolver resolver; 53 private Logger logger; 54 55 public XMLBaseSupport(SourceResolver resolver, Logger logger) { 56 this.resolver = resolver; 57 this.logger = logger; 58 } 59 60 public void setDocumentLocation(String loc) throws SAXException { 61 bases.push(new BaseInfo(loc, -2)); 63 } 64 65 public void startElement(String namespaceURI, String localName, String qName, Attributes attrs) throws SAXException { 66 level++; 67 String base = attrs.getValue(XMLBASE_NAMESPACE_URI, XMLBASE_ATTRIBUTE); 68 if (base != null) { 69 Source baseSource = null; 70 String baseUrl; 71 try { 72 baseSource = resolve(getCurrentBase(), base); 73 baseUrl = baseSource.getURI(); 74 } finally { 75 if (baseSource != null) { 76 resolver.release(baseSource); 77 } 78 } 79 bases.push(new BaseInfo(baseUrl, level)); 80 } 81 } 82 83 public void endElement(String namespaceURI, String localName, String qName) { 84 if (getCurrentBaseLevel() == level) 85 bases.pop(); 86 level--; 87 } 88 89 92 private Source resolve(String baseURI, String location) throws SAXException { 93 try { 94 Source source; 95 if (baseURI != null) { 96 source = resolver.resolveURI(location, baseURI, Collections.EMPTY_MAP); 97 } else { 98 source = resolver.resolveURI(location); 99 } 100 if (logger.isDebugEnabled()) { 101 logger.debug("XMLBaseSupport: resolved location " + location + 102 " against base URI " + baseURI + " to " + source.getURI()); 103 } 104 return source; 105 } catch (IOException e) { 106 throw new SAXException ("XMLBaseSupport: problem resolving uri.", e); 107 } 108 } 109 110 115 public Source makeAbsolute(String spec) throws SAXException { 116 return resolve(getCurrentBase(), spec); 117 } 118 119 private String getCurrentBase() { 120 if (bases.size() > 0) { 121 BaseInfo baseInfo = (BaseInfo)bases.peek(); 122 return baseInfo.getUrl(); 123 } 124 return null; 125 } 126 127 private int getCurrentBaseLevel() { 128 if (bases.size() > 0) { 129 BaseInfo baseInfo = (BaseInfo)bases.peek(); 130 return baseInfo.getLevel(); 131 } 132 return -1; 133 } 134 135 private static final class BaseInfo { 136 private String url; 137 private int level; 138 139 public BaseInfo(String url, int level) { 140 this.url = url; 141 this.level = level; 142 } 143 144 public String getUrl() { 145 return url; 146 } 147 148 public int getLevel() { 149 return level; 150 } 151 } 152 } 153 | Popular Tags |