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 NamespaceWriterContext extends NamespaceContextImpl 47 { 48 private final HashMap <String ,NamespaceBinding> _bindings 50 = new HashMap <String ,NamespaceBinding>(); 51 52 private int _uniqueId = 0; 53 private NamespaceBinding _nullBinding = new NamespaceBinding(null, null, 0); 54 55 private boolean _repair = false; 56 57 public NamespaceWriterContext() 58 { 59 this(false); 60 } 61 62 public NamespaceWriterContext(boolean repair) 63 { 64 super(); 65 66 _repair = repair; 67 } 68 69 public void declare(String prefix, String uri) 70 { 71 declare(prefix, uri, false); 72 } 73 74 77 public void declare(String prefix, String uri, boolean emit) 78 { 79 NamespaceBinding binding; 80 81 if (uri == null) 82 binding = _nullBinding; 83 else { 84 binding = _bindings.get(uri); 85 86 if (binding != null && 87 binding.getPrefix() != null && 88 binding.getPrefix().equals(prefix)) { 89 binding.setEmit(emit); 91 return; 92 } 93 else if (binding == null) { 94 binding = new NamespaceBinding(prefix, null, _version); 97 98 _bindings.put(uri, binding); 99 } 100 } 101 102 ElementBinding eltBinding = _stack.get(_stack.size() - 1); 103 104 if (eltBinding == null) { 105 eltBinding = new ElementBinding(); 106 107 _stack.set(_stack.size() - 1, eltBinding); 108 } 109 110 eltBinding.addOldBinding(binding, prefix, binding.getUri(), uri); 111 112 _version++; 113 binding.setPrefix(prefix); 114 binding.setUri(uri); 115 binding.setVersion(_version); 116 binding.setEmit(emit); 117 } 118 119 123 public String declare(String uri) 124 { 125 NamespaceBinding binding = _bindings.get(uri); 126 127 if (binding != null) 129 return binding.getPrefix(); 130 131 String prefix = "ns" + _uniqueId++; 132 133 declare(prefix, uri, _repair); 134 135 return prefix; 136 } 137 138 141 public String getPrefix(String uri) 142 { 143 NamespaceBinding binding = _bindings.get(uri); 144 145 if (binding == null) 146 return null; 147 148 return binding.getPrefix(); 149 } 150 151 public void emitDeclarations(WriteStream ws) 152 throws IOException 153 { 154 for (NamespaceBinding binding : _bindings.values()) 155 binding.emit(ws); 156 } 157 } 158 | Popular Tags |