1 17 package org.apache.ws.jaxme.util; 18 19 import java.util.ArrayList ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 23 import javax.xml.XMLConstants ; 24 import javax.xml.namespace.NamespaceContext ; 25 26 27 33 public class NamespaceSupport implements NamespaceContext { 34 List prefixList; 35 String cachedURI, cachedPrefix; 36 37 38 public NamespaceSupport() { 39 } 40 41 43 public void reset() { 44 cachedURI = cachedPrefix = null; 45 if (prefixList != null) { 46 prefixList.clear(); 47 } 48 } 49 50 52 public void declarePrefix(String pPrefix, String pURI) { 53 if (pURI == null) { pURI = ""; } 54 if (cachedURI == null) { 55 cachedURI = pURI; 56 cachedPrefix = pPrefix; 57 } else { 58 if (prefixList == null) { prefixList = new ArrayList (); } 59 prefixList.add(cachedPrefix); 60 prefixList.add(cachedURI); 61 cachedPrefix = pPrefix; 62 cachedURI = pURI; 63 } 64 } 65 66 69 public void undeclarePrefix(String pPrefix) { 70 if (pPrefix.equals(cachedPrefix)) { 71 if (prefixList != null && prefixList.size() > 0) { 72 cachedURI = prefixList.remove(prefixList.size()-1).toString(); 73 cachedPrefix = prefixList.remove(prefixList.size()-1).toString(); 74 } else { 75 cachedPrefix = cachedURI = null; 76 } 77 } else { 78 for (int i = prefixList.size()-2; i >= 0; i -= 2) { 79 if (pPrefix.equals(prefixList.get(i))) { 80 prefixList.remove(i); 81 prefixList.remove(i); 82 return; 83 } 84 } 85 throw new IllegalStateException ("Undeclared prefix: " + pPrefix); 86 } 87 } 88 89 96 public String getNamespaceURI(String pPrefix) { 97 if (pPrefix == null) { 98 throw new IllegalArgumentException ("Namespace prefix must not be null"); 99 } 100 if (cachedURI != null) { 101 if (cachedPrefix.equals(pPrefix)) { return cachedURI; } 102 if (prefixList != null) { 103 for (int i = prefixList.size(); i > 0; i -= 2) { 104 if (pPrefix.equals(prefixList.get(i-2))) { 105 return (String ) prefixList.get(i-1); 106 } 107 } 108 } 109 } 110 if (XMLConstants.XML_NS_PREFIX.equals(pPrefix)) { 111 return XMLConstants.XML_NS_URI; 112 } else if (XMLConstants.XMLNS_ATTRIBUTE.equals(pPrefix)) { 113 return XMLConstants.XMLNS_ATTRIBUTE_NS_URI; 114 } 115 return null; 116 } 117 118 128 public String getPrefix(String pURI) { 129 if (pURI == null) { 130 throw new IllegalArgumentException ("Namespace URI must not be null"); 131 } 132 if (cachedURI != null) { 133 if (cachedURI.equals(pURI)) { return cachedPrefix; } 134 if (prefixList != null) { 135 for (int i = prefixList.size(); i > 0; i -= 2) { 136 if (pURI.equals(prefixList.get(i-1))) { 137 return (String ) prefixList.get(i-2); 138 } 139 } 140 } 141 } 142 if (XMLConstants.XML_NS_URI.equals(pURI)) { 143 return XMLConstants.XML_NS_PREFIX; 144 } else if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(pURI)) { 145 return XMLConstants.XMLNS_ATTRIBUTE; 146 } 147 return null; 148 } 149 150 158 public String getAttributePrefix(String pURI) { 159 if (pURI == null) { 160 throw new IllegalArgumentException ("Namespace URI must not be null"); 161 } 162 if (pURI.length() == 0) { 163 return ""; 164 } 165 if (cachedURI != null) { 166 if (cachedURI.equals(pURI) && cachedPrefix.length() > 0) { 167 return cachedPrefix; 168 } 169 if (prefixList != null) { 170 for (int i = prefixList.size(); i > 0; i -= 2) { 171 if (pURI.equals(prefixList.get(i-1))) { 172 String prefix = (String ) prefixList.get(i-2); 173 if (prefix.length() > 0) { 174 return prefix; 175 } 176 } 177 } 178 } 179 } 180 if (XMLConstants.XML_NS_URI.equals(pURI)) { 181 return XMLConstants.XML_NS_PREFIX; 182 } else if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(pURI)) { 183 return XMLConstants.XMLNS_ATTRIBUTE; 184 } 185 return null; 186 } 187 188 189 190 197 public Iterator getPrefixes(String pURI) { 198 if (pURI == null) { 199 throw new IllegalArgumentException ("Namespace URI must not be null"); 200 } 201 List list = new ArrayList (); 202 if (cachedURI != null) { 203 if (cachedURI.equals(pURI)) { list.add(cachedPrefix); } 204 if (prefixList != null) { 205 for (int i = prefixList.size(); i > 0; i -= 2) { 206 if (pURI.equals(prefixList.get(i-1))) { 207 list.add(prefixList.get(i-2)); 208 } 209 } 210 } 211 } 212 if (pURI.equals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI)) { 213 list.add(XMLConstants.XMLNS_ATTRIBUTE); 214 } else if (pURI.equals(XMLConstants.XML_NS_URI)) { 215 list.add(XMLConstants.XML_NS_PREFIX); 216 } 217 return list.iterator(); 218 } 219 220 222 public boolean isPrefixDeclared(String pPrefix) { 223 if (cachedURI != null) { 224 if (cachedPrefix != null && cachedPrefix.equals(pPrefix)) { return true; } 225 if (prefixList != null) { 226 for (int i = prefixList.size(); i > 0; i -= 2) { 227 if (prefixList.get(i-2).equals(pPrefix)) { 228 return true; 229 } 230 } 231 } 232 } 233 return "xml".equals(pPrefix); 234 } 235 236 247 public int getContext() { 248 return (prefixList == null ? 0 : prefixList.size()) + 249 (cachedURI == null ? 0 : 2); 250 } 251 252 274 public String checkContext(int i) { 275 if (getContext() == i) { 276 return null; 277 } 278 String result = cachedPrefix; 279 if (prefixList != null && prefixList.size() > 0) { 280 cachedURI = prefixList.remove(prefixList.size()-1).toString(); 281 cachedPrefix = prefixList.remove(prefixList.size()-1).toString(); 282 } else { 283 cachedURI = null; 284 cachedPrefix = null; 285 } 286 return result; 287 } 288 } 289 | Popular Tags |