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 abstract class NamespaceContextImpl implements NamespaceContext 47 { 48 protected final ArrayList <ElementBinding> _stack 50 = new ArrayList <ElementBinding>(); 51 52 protected int _version = 0; 53 54 NamespaceContextImpl() 55 { 56 _stack.add(null); 57 } 58 59 62 public void push() 63 { 64 _stack.add(null); 65 } 66 67 70 public void pop() 71 { 72 ElementBinding eltBinding = _stack.remove(_stack.size() - 1); 73 74 if (eltBinding != null) { 75 ArrayList <Decl> oldBinding = eltBinding.getOldBindingList(); 76 77 for (int i = 0; oldBinding != null && i < oldBinding.size(); i++) { 78 Decl decl = oldBinding.get(i); 79 NamespaceBinding binding = decl.getBinding(); 80 81 _version++; 82 83 binding.setUri(decl.getOldUri()); 84 binding.setVersion(_version); 85 } 86 87 eltBinding.clear(); 88 } 89 } 90 91 public void setElementName(QName name) 92 { 93 ElementBinding eltBinding = _stack.get(_stack.size() - 1); 94 95 if (eltBinding == null) { 96 eltBinding = new ElementBinding(); 97 98 _stack.set(_stack.size() - 1, eltBinding); 99 } 100 101 eltBinding.setName(name); 102 } 103 104 public QName getElementName() 105 { 106 ElementBinding eltBinding = _stack.get(_stack.size() - 1); 107 108 if (eltBinding != null) 109 return eltBinding.getName(); 110 else 111 return null; 112 } 113 114 117 public abstract void declare(String prefix, String uri); 118 119 public String getNamespaceURI(String prefix) 120 { 121 throw new UnsupportedOperationException (); 122 } 123 124 public String getPrefix(String uri) 125 { 126 throw new UnsupportedOperationException (); 127 } 128 129 public Iterator getPrefixes(String uri) 130 { 131 throw new UnsupportedOperationException (); 132 } 133 134 static class ElementBinding 135 { 136 private QName _name; 137 private ArrayList <Decl> _declList; 138 139 public void setName(QName name) 140 { 141 _name = name; 142 } 143 144 public void clear() 145 { 146 _declList = null; 147 } 148 149 public QName getName() 150 { 151 return _name; 152 } 153 154 public void addOldBinding(NamespaceBinding binding, String prefix, 155 String oldUri, String newUri) 156 { 157 if (_declList == null) 158 _declList = new ArrayList <Decl>(); 159 160 _declList.add(new Decl(binding, prefix, oldUri, newUri)); 161 } 162 163 public ArrayList <Decl> getOldBindingList() 164 { 165 return _declList; 166 } 167 } 168 169 static class Decl { 170 private final NamespaceBinding _binding; 171 private final String _prefix; 172 private final String _oldUri; 173 private final String _newUri; 174 175 Decl(NamespaceBinding binding, String prefix, 176 String oldUri, String newUri) 177 { 178 _binding = binding; 179 _prefix = prefix; 180 _oldUri = oldUri; 181 _newUri = newUri; 182 } 183 184 NamespaceBinding getBinding() 185 { 186 return _binding; 187 } 188 189 String getPrefix() 190 { 191 return _prefix; 192 } 193 194 String getOldUri() 195 { 196 return _oldUri; 197 } 198 199 String getNewUri() 200 { 201 return _newUri; 202 } 203 } 204 205 static class PrefixIterator 206 { 207 private ElementBinding _eltBinding; 208 private int _index = 0; 209 210 PrefixIterator(NamespaceContextImpl context, ElementBinding eltBinding) 211 { 212 _eltBinding = eltBinding; 213 } 214 215 public void remove() 216 { 217 throw new RuntimeException ("not supported"); 218 } 219 220 public boolean hasNext() 221 { 222 if (_eltBinding == null) 223 return false; 224 225 return _index < _eltBinding.getOldBindingList().size(); 226 } 227 228 public Object next() 229 { 230 if (_eltBinding == null) 231 return null; 232 233 ArrayList <Decl> oldBindingList = _eltBinding.getOldBindingList(); 234 235 if (_index < oldBindingList.size()) 236 return oldBindingList.get(_index++).getPrefix(); 237 238 return null; 239 } 240 } 241 } 242 | Popular Tags |