1 29 30 package com.caucho.xml.stream; 31 32 import com.caucho.vfs.WriteStream; 33 34 import javax.xml.XMLConstants ; 35 import javax.xml.namespace.NamespaceContext ; 36 import javax.xml.namespace.QName ; 37 import java.io.IOException ; 38 import java.util.ArrayList ; 39 import java.util.HashMap ; 40 import java.util.Iterator ; 41 import java.util.Map ; 42 43 46 public class NamespaceReaderContext extends NamespaceContextImpl 47 { 48 private final HashMap <String ,NamespaceBinding> _bindings 50 = new HashMap <String ,NamespaceBinding>(); 51 52 private NamespaceBinding _nullEltBinding 53 = new NamespaceBinding(null, null, 0); 54 private NamespaceBinding _nullAttrBinding 55 = new NamespaceBinding(null, null, 0); 56 57 60 public void declare(String prefix, String uri) 61 { 62 NamespaceBinding binding = getElementNamespace(prefix); 63 64 ElementBinding eltBinding = _stack.get(_stack.size() - 1); 65 66 if (eltBinding == null) { 67 eltBinding = new ElementBinding(); 68 69 _stack.set(_stack.size() - 1, eltBinding); 70 } 71 72 eltBinding.addOldBinding(binding, prefix, binding.getUri(), uri); 73 74 _version++; 75 binding.setUri(uri); 76 binding.setVersion(_version); 77 } 78 79 82 public String getUri(String prefix) 83 { 84 NamespaceBinding binding = _bindings.get(prefix); 85 86 if (binding != null) 87 return binding.getUri(); 88 else 89 return null; 90 } 91 92 94 public String getNamespaceURI(String prefix) 95 { 96 NamespaceBinding binding = _bindings.get(prefix); 97 98 if (binding != null) 99 return binding.getUri(); 100 101 String uri = null; 102 103 if (XMLConstants.XML_NS_PREFIX.equals(prefix)) 104 uri = XMLConstants.XML_NS_URI; 105 else if (XMLConstants.XMLNS_ATTRIBUTE.equals(prefix)) 106 uri = XMLConstants.XMLNS_ATTRIBUTE_NS_URI; 107 108 return uri; 109 } 110 111 113 public int getDepth() 115 { 116 return _stack.size() - 1; 117 } 118 119 public int getNumDecls() 120 { 121 ElementBinding eltBinding = _stack.get(_stack.size() - 1); 122 123 if (eltBinding != null) { 124 ArrayList <Decl> oldBindingList = eltBinding.getOldBindingList(); 125 126 if (oldBindingList != null) 127 return oldBindingList.size(); 128 else 129 return 0; 130 } 131 else 132 return 0; 133 } 134 135 public String getUri(int i) 136 { 137 ElementBinding eltBinding = _stack.get(_stack.size() - 1); 138 139 if (eltBinding != null) { 140 return eltBinding.getOldBindingList().get(i).getNewUri(); 141 } 142 else 143 return null; 144 } 145 146 public String getPrefix(int i) 147 { 148 ElementBinding eltBinding = _stack.get(_stack.size() - 1); 149 150 if (eltBinding != null) { 151 return eltBinding.getOldBindingList().get(i).getPrefix(); 152 } 153 154 return null; 155 } 156 158 NamespaceBinding getElementNamespace(String prefix) 159 { 160 NamespaceBinding binding; 161 162 if (prefix == null) 164 binding = _nullEltBinding; 165 else 166 binding = _bindings.get(prefix); 167 168 if (binding != null) 169 return binding; 170 else { 171 binding = new NamespaceBinding(prefix, null, _version); 172 173 _bindings.put(prefix, binding); 174 175 return binding; 176 } 177 } 178 179 NamespaceBinding getAttributeNamespace(String prefix) 180 { 181 NamespaceBinding binding; 182 183 if (prefix == null) 185 binding = _nullAttrBinding; 186 else 187 binding = _bindings.get(prefix); 188 189 if (binding != null) 190 return binding; 191 else { 192 binding = new NamespaceBinding(prefix, null, _version); 193 194 _bindings.put(prefix, binding); 195 196 return binding; 197 } 198 } 199 } 200 | Popular Tags |