1 57 58 package org.apache.soap.encoding.soapenc; 59 60 import java.io.*; 61 import org.w3c.dom.*; 62 import org.apache.soap.util.xml.*; 63 import org.apache.soap.*; 64 65 72 public class SoapEncUtils 73 { 74 public static void generateNullStructure(String inScopeEncStyle, 75 Class javaType, Object context, 76 Writer sink, NSStack nsStack, 77 XMLJavaMappingRegistry xjmr) 78 throws IllegalArgumentException , IOException 79 { 80 generateStructureHeader(inScopeEncStyle, javaType, context, sink, 81 nsStack, xjmr, null, null, true); 82 } 83 84 public static void generateNullArray(String inScopeEncStyle, 85 Class javaType, Object context, 86 Writer sink, NSStack nsStack, 87 XMLJavaMappingRegistry xjmr, 88 QName arrayElementType, 89 String arrayLengthStr) 90 throws IllegalArgumentException , IOException 91 { 92 generateStructureHeader(inScopeEncStyle, javaType, context, sink, 93 nsStack, xjmr, arrayElementType, arrayLengthStr, 94 true); 95 } 96 97 public static void generateArrayHeader(String inScopeEncStyle, 98 Class javaType, Object context, 99 Writer sink, NSStack nsStack, 100 XMLJavaMappingRegistry xjmr, 101 QName arrayElementType, 102 String arrayLengthStr) 103 throws IllegalArgumentException , IOException 104 { 105 generateStructureHeader(inScopeEncStyle, javaType, context, sink, 106 nsStack, xjmr, arrayElementType, arrayLengthStr, 107 false); 108 } 109 110 public static void generateStructureHeader(String inScopeEncStyle, 111 Class javaType, Object context, 112 Writer sink, NSStack nsStack, 113 XMLJavaMappingRegistry xjmr) 114 throws IllegalArgumentException , IOException 115 { 116 generateStructureHeader(inScopeEncStyle, javaType, context, sink, 117 nsStack, xjmr, null, null, false); 118 } 119 120 private static void generateStructureHeader(String inScopeEncStyle, 121 Class javaType, Object context, 122 Writer sink, NSStack nsStack, 123 XMLJavaMappingRegistry xjmr, 124 QName arrayElementType, 125 String arrayLengthStr, 126 boolean isNull) 127 throws IllegalArgumentException , IOException 128 { 129 QName elementType = xjmr.queryElementType(javaType, 130 Constants.NS_URI_SOAP_ENC); 131 String namespaceDecl = ""; 132 133 if (context instanceof PrefixedName) 134 { 135 PrefixedName pname = (PrefixedName)context; 136 QName qname = pname.getQName(); 137 138 if (qname != null) 139 { 140 String namespaceURI = qname.getNamespaceURI(); 141 142 if (namespaceURI != null && !namespaceURI.equals("")) 143 { 144 if (pname.getPrefix() == null) 145 { 146 String prefix = nsStack.getPrefixFromURI(namespaceURI); 147 148 if (prefix == null) 149 { 150 prefix = nsStack.addNSDeclaration(namespaceURI); 151 namespaceDecl = " xmlns:" + prefix + "=\"" + namespaceURI + '\"'; 152 } 153 154 pname.setPrefix(prefix); 155 } 156 } 157 } 158 } 159 160 sink.write('<' + context.toString() + namespaceDecl); 161 162 String elementTypeNS = elementType.getNamespaceURI(); 164 String xsiNamespaceURI = Constants.NS_URI_CURRENT_SCHEMA_XSI; 165 166 if (elementTypeNS.startsWith("http://www.w3.org/") 167 && elementTypeNS.endsWith("/XMLSchema")) 168 { 169 xsiNamespaceURI = elementTypeNS + "-instance"; 170 } 171 172 String xsiNSPrefix = nsStack.getPrefixFromURI(xsiNamespaceURI, sink); 173 String elementTypeNSPrefix = nsStack.getPrefixFromURI(elementTypeNS, sink); 174 175 sink.write(' ' + xsiNSPrefix + ':' + Constants.ATTR_TYPE + "=\"" + 176 elementTypeNSPrefix + ':' + 177 elementType.getLocalPart() + '\"'); 178 179 if (inScopeEncStyle == null 180 || !inScopeEncStyle.equals(Constants.NS_URI_SOAP_ENC)) 181 { 182 String soapEnvNSPrefix = nsStack.getPrefixFromURI( 185 Constants.NS_URI_SOAP_ENV, sink); 186 187 sink.write(' ' + soapEnvNSPrefix + ':' + 188 Constants.ATTR_ENCODING_STYLE + "=\"" + 189 Constants.NS_URI_SOAP_ENC + '\"'); 190 } 191 192 if (arrayElementType != null) 193 { 194 String arrayElementTypeNSPrefix = nsStack.getPrefixFromURI( 195 arrayElementType.getNamespaceURI(), sink); 196 String arrayTypeValue = arrayElementTypeNSPrefix + ':' + 197 arrayElementType.getLocalPart() + 198 '[' + arrayLengthStr + ']'; 199 String soapEncNSPrefix = nsStack.getPrefixFromURI( 200 Constants.NS_URI_SOAP_ENC, sink); 201 202 sink.write(' ' + soapEncNSPrefix + ':' + 203 Constants.ATTR_ARRAY_TYPE + "=\"" + arrayTypeValue + '\"'); 204 } 205 206 if (isNull) 207 { 208 sink.write(' ' + xsiNSPrefix + ':' + nilName(xsiNamespaceURI) + "=\"" + 209 Constants.ATTRVAL_TRUE + "\"/"); 210 } 211 212 sink.write('>'); 213 } 214 215 private static String nilName(String currentSchemaXSI) 216 { 217 return (currentSchemaXSI.equals(Constants.NS_URI_2001_SCHEMA_XSI)) 218 ? Constants.ATTR_NIL 219 : Constants.ATTR_NULL; 220 } 221 222 public static boolean isNull(Element element) 223 { 224 String nullValue = DOMUtils.getAttributeNS(element, 225 Constants.NS_URI_2001_SCHEMA_XSI, 226 Constants.ATTR_NIL); 227 228 if (nullValue == null) 229 { 230 nullValue = DOMUtils.getAttributeNS(element, 231 Constants.NS_URI_2000_SCHEMA_XSI, 232 Constants.ATTR_NULL); 233 } 234 235 if (nullValue == null) 236 { 237 nullValue = DOMUtils.getAttributeNS(element, 238 Constants.NS_URI_1999_SCHEMA_XSI, 239 Constants.ATTR_NULL); 240 } 241 242 return nullValue != null && decodeBooleanValue(nullValue); 243 } 244 245 public static boolean decodeBooleanValue(String value) 246 { 247 switch (value.charAt(0)) 248 { 249 case '0': case 'f': case 'F': 250 return false; 251 252 case '1': case 't': case 'T': 253 return true; 254 255 default: 256 throw new IllegalArgumentException ("Invalid boolean value: " + value); 257 } 258 } 259 260 public static QName getAttributeValue(Element el, 261 String attrNameNamespaceURI, 262 String attrNameLocalPart, 263 String elDesc, 264 boolean isRequired) 265 throws IllegalArgumentException 266 { 267 String attrValue = DOMUtils.getAttributeNS(el, 268 attrNameNamespaceURI, 269 attrNameLocalPart); 270 271 if (attrValue != null) 272 { 273 int index = attrValue.indexOf(':'); 274 275 if (index != -1) 276 { 277 String attrValuePrefix = attrValue.substring(0, index); 278 String attrValueLocalPart = attrValue.substring(index + 1); 279 String attrValueNamespaceURI = 280 DOMUtils.getNamespaceURIFromPrefix(el, attrValuePrefix); 281 282 if (attrValueNamespaceURI != null) 283 { 284 return new QName(attrValueNamespaceURI, attrValueLocalPart); 285 } 286 else 287 { 288 throw new IllegalArgumentException ("Unable to resolve namespace " + 289 "URI for '" + attrValuePrefix + 290 "'."); 291 } 292 } 293 else 294 { 295 throw new IllegalArgumentException ("The value of the '" + 296 attrNameNamespaceURI + ':' + 297 attrNameLocalPart + 298 "' attribute must be " + 299 "namespace-qualified."); 300 } 301 } 302 else if (isRequired) 303 { 304 throw new IllegalArgumentException ("The '" + 305 attrNameNamespaceURI + ':' + 306 attrNameLocalPart + 307 "' attribute must be " + 308 "specified for every " + 309 elDesc + '.'); 310 } 311 else 312 { 313 return null; 314 } 315 } 316 317 321 public static QName getTypeQName(Element el) 322 throws IllegalArgumentException 323 { 324 QName typeQName = getAttributeValue(el, Constants.NS_URI_2001_SCHEMA_XSI, 326 Constants.ATTR_TYPE, null, false); 327 328 if (typeQName != null) 329 return typeQName; 330 331 typeQName = getAttributeValue(el, Constants.NS_URI_2000_SCHEMA_XSI, 333 Constants.ATTR_TYPE, null, false); 334 335 if (typeQName != null) 336 return typeQName; 337 338 typeQName = getAttributeValue(el, Constants.NS_URI_1999_SCHEMA_XSI, 340 Constants.ATTR_TYPE, null, false); 341 342 return typeQName; 343 } 344 } 345 | Popular Tags |