1 57 58 package org.apache.soap; 59 60 import java.io.*; 61 import java.util.*; 62 import org.w3c.dom.*; 63 import org.apache.soap.util.xml.*; 64 import org.apache.soap.rpc.SOAPContext; 65 66 72 class AttributeHandler 73 { 74 private Hashtable attributes = new Hashtable(); 75 private Hashtable namespaceURIs2Prefixes = new Hashtable(); 76 private int nsPrefixIndex = 0; 77 78 public AttributeHandler() 79 { 80 namespaceURIs2Prefixes.put(Constants.NS_URI_XMLNS, 82 Constants.NS_PRE_XMLNS); 83 } 84 85 public void setAttribute(QName attrQName, String value) 86 { 87 attributes.put(attrQName, value); 88 89 if (attrQName.getNamespaceURI().equals(Constants.NS_URI_XMLNS)) 91 { 92 namespaceURIs2Prefixes.put(value, attrQName.getLocalPart()); 93 } 94 } 95 96 public String getAttribute(QName attrQName) 97 { 98 return (String )attributes.get(attrQName); 99 } 100 101 public void removeAttribute(QName attrQName) 102 { 103 attributes.remove(attrQName); 104 } 105 106 private Enumeration getAttributeQNames() 107 { 108 generateNSDeclarations(); 109 110 return attributes.keys(); 111 } 112 113 public void declareNamespace(String nsPrefix, String namespaceURI) 114 { 115 setAttribute(new QName(Constants.NS_URI_XMLNS, nsPrefix), namespaceURI); 116 } 117 118 private void generateNSDeclarations() 119 { 120 Enumeration keys = attributes.keys(); 121 122 while (keys.hasMoreElements()) 123 { 124 QName qname = (QName)keys.nextElement(); 125 126 getPrefixFromURI(qname.getNamespaceURI()); 128 } 129 } 130 131 private String getPrefixFromURI(String namespaceURI) 132 { 133 if ("".equals(namespaceURI)) return null ; 134 135 String nsPrefix = (String )namespaceURIs2Prefixes.get(namespaceURI); 136 137 if (nsPrefix == null) 138 { 139 nsPrefix = "ns" + nsPrefixIndex++; 140 141 setAttribute(new QName(Constants.NS_URI_XMLNS, nsPrefix), namespaceURI); 142 } 143 144 return nsPrefix; 145 } 146 147 public void populateNSStack(NSStack nsStack) 148 { 149 generateNSDeclarations(); 150 151 nsStack.pushScope(); 152 153 Enumeration e = namespaceURIs2Prefixes.keys(); 154 155 while (e.hasMoreElements()) 156 { 157 String namespaceURI = (String )e.nextElement(); 158 String namespacePrefix = getPrefixFromURI(namespaceURI); 159 160 if (namespacePrefix != null) 161 nsStack.addNSDeclaration(namespacePrefix, namespaceURI); 162 } 163 } 164 165 public String getUniquePrefixFromURI(String namespaceURI, 166 String preferredPrefix, 167 NSStack nsStack) 168 { 169 String retPrefix = nsStack.getPrefixFromURI(namespaceURI); 170 171 if (retPrefix == null) 172 { 173 int prefixCount = 0; 174 175 if (preferredPrefix == null) 176 { 177 preferredPrefix = "ns"; 178 prefixCount++; 179 } 180 181 while (retPrefix == null) 182 { 183 String newPrefix = preferredPrefix + (prefixCount > 0 184 ? prefixCount + "" 185 : ""); 186 187 if (nsStack.getURIFromPrefix(newPrefix) == null) 189 { 190 nsStack.popScope(); 192 declareNamespace(newPrefix, namespaceURI); 193 populateNSStack(nsStack); 194 retPrefix = nsStack.getPrefixFromURI(namespaceURI); 195 } 196 else 197 { 198 prefixCount++; 199 } 200 } 201 } 202 203 return retPrefix; 204 } 205 206 public void marshall(Writer sink, SOAPContext ctx) 207 throws IllegalArgumentException , IOException 208 { 209 Enumeration attrQNames = getAttributeQNames(); 210 211 while (attrQNames.hasMoreElements()) 212 { 213 QName attrQName = (QName)attrQNames.nextElement(); 214 215 sink.write(' ') ; 216 String nsPrefix ; 217 if ((nsPrefix = getPrefixFromURI(attrQName.getNamespaceURI())) != null) 218 sink.write(nsPrefix + ':') ; 219 sink.write(attrQName.getLocalPart() + "=\"" + 220 getAttribute(attrQName) + '\"'); 221 } 222 } 223 224 public static AttributeHandler unmarshall(Node src, SOAPContext ctx) 225 throws IllegalArgumentException 226 { 227 NamedNodeMap attrs = src.getAttributes(); 228 AttributeHandler attrHandler = new AttributeHandler(); 229 int size = attrs.getLength(); 230 231 for (int i = 0; i < size; i++) 232 { 233 Attr attr = (Attr)attrs.item(i); 234 String namespaceURI = attr.getNamespaceURI(); 235 String localName = attr.getLocalName(); 236 String value = attr.getValue(); 237 238 attrHandler.setAttribute(new QName(namespaceURI, localName), value); 239 } 240 241 return attrHandler; 242 } 243 244 public String toString() 245 { 246 StringWriter sw = new StringWriter(); 247 248 try 249 { 250 sw.write("{"); 251 marshall(sw, new SOAPContext()); 252 sw.write("}"); 253 } 254 catch (Exception e) 255 { 256 } 257 258 return sw.toString(); 259 } 260 } 261 | Popular Tags |