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 Body 76 { 77 private Vector bodyEntries = null; 78 private AttributeHandler attrHandler = new AttributeHandler(); 79 80 public void setAttribute(QName attrQName, String value) 81 { 82 attrHandler.setAttribute(attrQName, value); 83 } 84 85 public String getAttribute(QName attrQName) 86 { 87 return attrHandler.getAttribute(attrQName); 88 } 89 90 public void removeAttribute(QName attrQName) 91 { 92 attrHandler.removeAttribute(attrQName); 93 } 94 95 public void declareNamespace(String nsPrefix, String namespaceURI) 96 { 97 attrHandler.declareNamespace(nsPrefix, namespaceURI); 98 } 99 100 public void setBodyEntries(Vector bodyEntries) 101 { 102 this.bodyEntries = bodyEntries; 103 } 104 105 public Vector getBodyEntries() 106 { 107 return bodyEntries; 108 } 109 110 public void marshall(String inScopeEncStyle, Writer sink, NSStack nsStack, 111 XMLJavaMappingRegistry xjmr, SOAPContext ctx) 112 throws IllegalArgumentException , IOException 113 { 114 attrHandler.populateNSStack(nsStack); 115 116 String declEncStyle = getAttribute(new QName( 117 Constants.NS_URI_SOAP_ENV, Constants.ATTR_ENCODING_STYLE)); 118 String actualEncStyle = declEncStyle != null 119 ? declEncStyle 120 : inScopeEncStyle; 121 122 String soapEnvNSPrefix = attrHandler.getUniquePrefixFromURI( 124 Constants.NS_URI_SOAP_ENV, Constants.NS_PRE_SOAP_ENV, nsStack); 125 126 sink.write('<' + soapEnvNSPrefix + ':' + Constants.ELEM_BODY); 127 128 attrHandler.marshall(sink, ctx); 130 131 sink.write('>' + StringUtils.lineSeparator); 132 133 if (bodyEntries != null) 135 { 136 for (Enumeration e = bodyEntries.elements(); e.hasMoreElements();) 137 { 138 Object obj = e.nextElement(); 139 if (obj instanceof Bean) 140 { 141 Bean bodyEntry = (Bean) obj; 142 143 if (Serializer.class.isAssignableFrom(bodyEntry.type)) 144 { 145 ((Serializer)bodyEntry.value).marshall(actualEncStyle, 146 bodyEntry.type, 147 bodyEntry.value, 148 null, 149 sink, 150 nsStack, 151 xjmr, 152 ctx); 153 } 154 else 155 { 156 throw new IllegalArgumentException ("Body entries must implement " + 157 "the Serializer interface."); 158 } 159 } 160 else if (obj instanceof Element) 161 { 162 Utils.marshallNode((Element)obj, sink); 163 } 164 else 165 { 166 throw new IllegalArgumentException ("Unknown type of body entry: '" + 167 obj.getClass () + "'"); 168 } 169 sink.write(StringUtils.lineSeparator); 170 } 171 } 172 173 sink.write("</" + soapEnvNSPrefix + ':' + Constants.ELEM_BODY + '>' + 174 StringUtils.lineSeparator); 175 176 nsStack.popScope(); 177 } 178 179 public static Body unmarshall(Node src, SOAPContext ctx) throws IllegalArgumentException 180 { 181 Element root = (Element)src; 182 Body body = new Body(); 183 Vector bodyEntries = new Vector(); 184 185 body.attrHandler = AttributeHandler.unmarshall(root, ctx); 187 188 for (Element el = DOMUtils.getFirstChildElement(root); 189 el != null; 190 el = DOMUtils.getNextSiblingElement(el)) 191 { 192 bodyEntries.addElement(el); 193 } 194 195 body.setBodyEntries(bodyEntries); 196 197 return body; 198 } 199 200 public String toString() 201 { 202 StringWriter sw = new StringWriter(); 203 PrintWriter pw = new PrintWriter(sw); 204 205 pw.print("[Attributes=" + attrHandler + "] " + 206 "[BodyEntries="); 207 208 if (bodyEntries != null) 209 { 210 pw.println(); 211 212 for (int i = 0; i < bodyEntries.size(); i++) 213 { 214 pw.println("[(" + i + ")=" + bodyEntries.elementAt(i) + "]"); 215 } 216 } 217 218 pw.print("]"); 219 220 return sw.toString(); 221 } 222 } 223 | Popular Tags |