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.*; 64 import org.apache.soap.util.xml.*; 65 import org.apache.soap.encoding.*; 66 import org.apache.soap.rpc.SOAPContext; 67 68 75 public class Envelope 76 { 77 private Header header = null; 78 private Body body = null; 79 private Vector envelopeEntries = null; 80 private AttributeHandler attrHandler = new AttributeHandler(); 81 82 public Envelope() 83 { 84 declareNamespace(Constants.NS_PRE_SOAP_ENV, 86 Constants.NS_URI_SOAP_ENV); 87 88 declareNamespace(Constants.NS_PRE_SCHEMA_XSI, 90 Constants.NS_URI_CURRENT_SCHEMA_XSI); 91 92 declareNamespace(Constants.NS_PRE_SCHEMA_XSD, 94 Constants.NS_URI_CURRENT_SCHEMA_XSD); 95 } 96 97 public void setAttribute(QName attrQName, String value) 98 { 99 attrHandler.setAttribute(attrQName, value); 100 } 101 102 public String getAttribute(QName attrQName) 103 { 104 return attrHandler.getAttribute(attrQName); 105 } 106 107 public void removeAttribute(QName attrQName) 108 { 109 attrHandler.removeAttribute(attrQName); 110 } 111 112 public void declareNamespace(String nsPrefix, String namespaceURI) 113 { 114 attrHandler.declareNamespace(nsPrefix, namespaceURI); 115 } 116 117 public void setHeader(Header header) 118 { 119 this.header = header; 120 } 121 122 public Header getHeader() 123 { 124 return header; 125 } 126 127 public void setBody(Body body) 128 { 129 this.body = body; 130 } 131 132 public Body getBody() 133 { 134 return body; 135 } 136 137 public void setEnvelopeEntries(Vector envelopeEntries) 138 { 139 this.envelopeEntries = envelopeEntries; 140 } 141 142 public Vector getEnvelopeEntries() 143 { 144 return envelopeEntries; 145 } 146 147 152 public void marshall(Writer sink, XMLJavaMappingRegistry xjmr) 153 throws IllegalArgumentException , IOException { 154 marshall(sink, xjmr, new SOAPContext()); 155 } 156 157 public void marshall(Writer sink, XMLJavaMappingRegistry xjmr, 158 SOAPContext ctx) 159 throws IllegalArgumentException , IOException 160 { 161 NSStack nsStack = new NSStack(); 163 164 attrHandler.populateNSStack(nsStack); 165 166 Header header = getHeader(); 167 Body body = getBody(); 168 Vector envelopeEntries = getEnvelopeEntries(); 169 String declEncStyle = getAttribute(new QName( 170 Constants.NS_URI_SOAP_ENV, Constants.ATTR_ENCODING_STYLE)); 171 172 String soapEnvNSPrefix = attrHandler.getUniquePrefixFromURI( 174 Constants.NS_URI_SOAP_ENV, Constants.NS_PRE_SOAP_ENV, nsStack); 175 176 sink.write(Constants.XML_DECL); 178 179 sink.write('<' + soapEnvNSPrefix + ':' + Constants.ELEM_ENVELOPE); 180 181 attrHandler.marshall(sink, ctx); 183 184 sink.write('>' + StringUtils.lineSeparator); 185 186 if (header != null) 188 { 189 header.marshall(sink, nsStack, xjmr, ctx); 190 } 191 192 if (body != null) 194 { 195 body.marshall(declEncStyle, sink, nsStack, xjmr, ctx); 196 } 197 else 198 { 199 throw new IllegalArgumentException ("An '" + Constants.Q_ELEM_ENVELOPE + 200 "' must contain a: '" + 201 Constants.Q_ELEM_BODY + "'."); 202 } 203 204 if (envelopeEntries != null) 206 { 207 for (Enumeration e = envelopeEntries.elements(); e.hasMoreElements();) 208 { 209 Element envelopeEntryEl = (Element)e.nextElement(); 210 211 Utils.marshallNode(envelopeEntryEl, sink); 212 213 sink.write(StringUtils.lineSeparator); 214 } 215 } 216 217 sink.write("</" + soapEnvNSPrefix + ':' + Constants.ELEM_ENVELOPE + 218 '>' + StringUtils.lineSeparator); 219 } 220 221 226 public static Envelope unmarshall(Node src) 227 throws IllegalArgumentException { 228 return unmarshall(src, new SOAPContext()); 229 } 230 231 public static Envelope unmarshall(Node src, SOAPContext ctx) 232 throws IllegalArgumentException 233 { 234 Element root = (Element)src; 235 Envelope env = new Envelope(); 236 237 if (Constants.Q_ELEM_ENVELOPE.matches(root)) 238 { 239 env.attrHandler = AttributeHandler.unmarshall(root, ctx); 241 242 Element headerEl = null; 244 Element bodyEl = null; 245 Element tempEl = DOMUtils.getFirstChildElement(root); 246 247 if (Constants.Q_ELEM_HEADER.matches(tempEl)) 248 { 249 headerEl = tempEl; 250 tempEl = DOMUtils.getNextSiblingElement(tempEl); 251 } 252 253 if (Constants.Q_ELEM_BODY.matches(tempEl)) 254 { 255 bodyEl = tempEl; 256 tempEl = DOMUtils.getNextSiblingElement(tempEl); 257 } 258 259 if (headerEl != null) 261 { 262 Header header = Header.unmarshall(headerEl, ctx); 263 264 env.setHeader(header); 265 } 266 267 if (bodyEl != null) 269 { 270 Body body = Body.unmarshall(bodyEl, ctx); 271 272 env.setBody(body); 273 } 274 else 275 { 276 throw new IllegalArgumentException ("An '" + Constants.Q_ELEM_ENVELOPE + 277 "' element must contain a: '" + 278 Constants.Q_ELEM_BODY + 279 "' element."); 280 } 281 282 if (tempEl != null) 284 { 285 Vector envelopeEntries = new Vector(); 286 287 while (tempEl != null) 288 { 289 envelopeEntries.addElement(tempEl); 290 tempEl = DOMUtils.getNextSiblingElement(tempEl); 291 } 292 293 env.setEnvelopeEntries(envelopeEntries); 294 } 295 } 296 else 297 { 298 String localName = root.getLocalName(); 299 300 if (localName != null && localName.equals(Constants.ELEM_ENVELOPE)) 301 { 302 throw new IllegalArgumentException (Constants.ERR_MSG_VERSION_MISMATCH); 303 } 304 else 305 { 306 throw new IllegalArgumentException ("Root element of a SOAP message " + 307 "must be: '" + 308 Constants.Q_ELEM_ENVELOPE + "'."); 309 } 310 } 311 312 return env; 313 } 314 315 public String toString() 316 { 317 StringWriter sw = new StringWriter(); 318 PrintWriter pw = new PrintWriter(sw); 319 320 pw.print("[Attributes=" + attrHandler + "] " + 321 "[Header=" + header + "] " + 322 "[Body=" + body + "] " + 323 "[EnvelopeEntries="); 324 325 if (envelopeEntries != null) 326 { 327 pw.println(); 328 329 for (int i = 0; i < envelopeEntries.size(); i++) 330 { 331 pw.println("[(" + i + ")=" + 332 DOM2Writer.nodeToString((Element)envelopeEntries.elementAt(i)) + 333 "]"); 334 } 335 } 336 337 pw.print("]"); 338 339 return sw.toString(); 340 } 341 } 342 | Popular Tags |