snichol 2002/11/18 08:42:41 Modified: java/src/org/apache/soap Envelope.java Body.java Log: Submitted by: Pavel Ausianik <[EMAIL PROTECTED]> Reduce number of AttributeHandlers. Additional edits by Scott Nichol to add javadoc and other comments, plus try to throw exceptions before doing unnecessary work. Revision Changes Path 1.12 +84 -33 xml-soap/java/src/org/apache/soap/Envelope.java Index: Envelope.java =================================================================== RCS file: /home/cvs/xml-soap/java/src/org/apache/soap/Envelope.java,v retrieving revision 1.11 retrieving revision 1.12 diff -u -r1.11 -r1.12 --- Envelope.java 6 Nov 2002 15:11:08 -0000 1.11 +++ Envelope.java 18 Nov 2002 16:42:40 -0000 1.12 @@ -71,21 +71,32 @@ * * @author Matthew J. Duftler ([EMAIL PROTECTED]) * @author Sanjiva Weerawarana ([EMAIL PROTECTED]) + * @author Pavel Ausianik ([EMAIL PROTECTED]) + * @author Scott Nichol ([EMAIL PROTECTED]) */ public class Envelope { + // The SOAP envelope header private Header header = null; + // The SOAP envelope body private Body body = null; + // The SOAP envelope (non-Body) entries; type is Element private Vector envelopeEntries = null; - private AttributeHandler attrHandler = new AttributeHandler(); + // Attributes from XML element + private AttributeHandler attrHandler = null; private static final QName N_SOAP_ENV = new QName(Constants.NS_URI_XMLNS, Constants.NS_PRE_SOAP_ENV); private static final QName N_SCHEMA_XSI = new QName(Constants.NS_URI_XMLNS, Constants.NS_PRE_SCHEMA_XSI); private static final QName N_SCHEMA_XSD = new QName(Constants.NS_URI_XMLNS, Constants.NS_PRE_SCHEMA_XSD); private static final QName N_ENCOD_STYLE = new QName(Constants.NS_URI_SOAP_ENV, Constants.ATTR_ENCODING_STYLE); + /** + * Default constructor. Typically used for outgoing messages. + */ public Envelope() { + attrHandler = new AttributeHandler(); + // Declare the "SOAP-ENV" namespace. setAttribute(N_SOAP_ENV, Constants.NS_URI_SOAP_ENV); @@ -96,51 +107,91 @@ setAttribute(N_SCHEMA_XSD, Constants.NS_URI_CURRENT_SCHEMA_XSD); } + /** + * Constructor that specifies an attribute handler. Typically used for + * incoming messages. + * + * @param attrHandler The attribute handler. + */ + public Envelope(AttributeHandler attrHandler) { + this.attrHandler = attrHandler; + } + + /** + * Sets an XML element attribute. + */ public void setAttribute(QName attrQName, String value) { attrHandler.setAttribute(attrQName, value); } + /** + * Gets an XML element attribute. + */ public String getAttribute(QName attrQName) { return attrHandler.getAttribute(attrQName); } + /** + * Removes an XML element attribute. + */ public void removeAttribute(QName attrQName) { attrHandler.removeAttribute(attrQName); } + /** + * Sets a namespace prefix/URI for an XML element attribute. + */ public void declareNamespace(String nsPrefix, String namespaceURI) { attrHandler.declareNamespace(nsPrefix, namespaceURI); } + /** + * Sets the SOAP envelope header. + */ public void setHeader(Header header) { this.header = header; } + /** + * Gets the SOAP envelope header. + */ public Header getHeader() { return header; } + /** + * Sets the SOAP envelope body. + */ public void setBody(Body body) { this.body = body; } + /** + * Gets the SOAP envelope body. + */ public Body getBody() { return body; } + /** + * Sets the SOAP envelope (non-Body) entries (type Element). + */ public void setEnvelopeEntries(Vector envelopeEntries) { this.envelopeEntries = envelopeEntries; } + /** + * Gets the SOAP envelope (non-Body) entries (type Element). + */ public Vector getEnvelopeEntries() { return envelopeEntries; @@ -156,17 +207,27 @@ marshall(sink, xjmr, new SOAPContext()); } + /** + * Marshalls the Envelope as XML. + */ public void marshall(Writer sink, XMLJavaMappingRegistry xjmr, SOAPContext ctx) throws IllegalArgumentException, IOException { + // There must always be a <SOAP-ENV:Body>. + Body body = getBody(); + if (body == null) { + throw new IllegalArgumentException("An '" + Constants.Q_ELEM_ENVELOPE + + "' must contain a: '" + + Constants.Q_ELEM_BODY + "'."); + } + // Initialize the namespace stack. NSStack nsStack = new NSStack(); attrHandler.populateNSStack(nsStack); Header header = getHeader(); - Body body = getBody(); Vector envelopeEntries = getEnvelopeEntries(); String declEncStyle = getAttribute(N_ENCOD_STYLE); @@ -192,17 +253,8 @@ header.marshall(sink, nsStack, xjmr, ctx); } - // There must always be a <SOAP-ENV:Body>. - if (body != null) - { - body.marshall(declEncStyle, sink, nsStack, xjmr, ctx); - } - else - { - throw new IllegalArgumentException("An '" + Constants.Q_ELEM_ENVELOPE + - "' must contain a: '" + - Constants.Q_ELEM_BODY + "'."); - } + // Serialize the body + body.marshall(declEncStyle, sink, nsStack, xjmr, ctx); // Serialize any envelope entries (in addition to <SOAP-ENV:Body>). if (envelopeEntries != null) @@ -233,17 +285,17 @@ return unmarshall(src, new SOAPContext()); } + /** + * Unmarshalls the envelope from XML. + */ public static Envelope unmarshall(Node src, SOAPContext ctx) throws IllegalArgumentException { Element root = (Element)src; - Envelope env = new Envelope(); + Envelope env = null; if (Constants.Q_ELEM_ENVELOPE.matches(root)) { - // Deserialize any envelope attributes. - env.attrHandler = AttributeHandler.unmarshall(root, ctx); - // Examine the subelements of the envelope. Element headerEl = null; Element bodyEl = null; @@ -260,22 +312,6 @@ bodyEl = tempEl; tempEl = DOMUtils.getNextSiblingElement(tempEl); } - - // Deserialize any header entries. - if (headerEl != null) - { - Header header = Header.unmarshall(headerEl, ctx); - - env.setHeader(header); - } - - // Deserialize any body entries. - if (bodyEl != null) - { - Body body = Body.unmarshall(bodyEl, ctx); - - env.setBody(body); - } else { throw new IllegalArgumentException("An '" + Constants.Q_ELEM_ENVELOPE + @@ -284,6 +320,18 @@ "' element."); } + // Deserialize any envelope attributes. + env = new Envelope(AttributeHandler.unmarshall(root, ctx)); + + // Deserialize the header if present. + if (headerEl != null) + { + env.setHeader(Header.unmarshall(headerEl, ctx)); + } + + // Deserialize the body entry. + env.setBody(Body.unmarshall(bodyEl, ctx)); + // Deserialize any envelope entries (in addition to <SOAP-ENV:Body>). if (tempEl != null) { @@ -321,6 +369,9 @@ return env; } + /** + * Gets this envelope as a string. + */ public String toString() { StringWriter sw = new StringWriter(); 1.10 +49 -3 xml-soap/java/src/org/apache/soap/Body.java Index: Body.java =================================================================== RCS file: /home/cvs/xml-soap/java/src/org/apache/soap/Body.java,v retrieving revision 1.9 retrieving revision 1.10 diff -u -r1.9 -r1.10 --- Body.java 6 Nov 2002 15:11:08 -0000 1.9 +++ Body.java 18 Nov 2002 16:42:40 -0000 1.10 @@ -74,42 +74,83 @@ * @author Matthew J. Duftler ([EMAIL PROTECTED]) * @author Sanjiva Weerawarana ([EMAIL PROTECTED]) * @author Scott Nichol ([EMAIL PROTECTED]) + * @author Pavel Ausianik ([EMAIL PROTECTED]) */ public class Body { + // Body entries are Element or Bean private Vector bodyEntries = null; - private AttributeHandler attrHandler = new AttributeHandler(); + // Attributes from XML element + private AttributeHandler attrHandler = null; + /** + * Default constructor. Typically used for outgoing messages. + */ + public Body() { + attrHandler = new AttributeHandler(); + } + + /** + * Constructor that specifies the attribute handler. Typically used + * for incoming messages. + * + * @param attrHandler The attribute handler. + */ + public Body(AttributeHandler attrHandler) { + this.attrHandler = attrHandler; + } + + /** + * Sets an XML element attribute. + */ public void setAttribute(QName attrQName, String value) { attrHandler.setAttribute(attrQName, value); } + /** + * Gets an XML element attribute. + */ public String getAttribute(QName attrQName) { return attrHandler.getAttribute(attrQName); } + /** + * Removes an XML element attribute. + */ public void removeAttribute(QName attrQName) { attrHandler.removeAttribute(attrQName); } + /** + * Sets a namespace prefix/URI for an XML element attribute. + */ public void declareNamespace(String nsPrefix, String namespaceURI) { attrHandler.declareNamespace(nsPrefix, namespaceURI); } + /** + * Sets the body entries, which are of type Element or Bean. + */ public void setBodyEntries(Vector bodyEntries) { this.bodyEntries = bodyEntries; } + /** + * Gets the body entries, which are of type Element or Bean. + */ public Vector getBodyEntries() { return bodyEntries; } + /** + * Marshalls the Body as XML. + */ public void marshall(String inScopeEncStyle, Writer sink, NSStack nsStack, XMLJavaMappingRegistry xjmr, SOAPContext ctx) throws IllegalArgumentException, IOException @@ -198,14 +239,16 @@ nsStack.popScope(); } + /** + * Unmarshalls the body from XML. + */ public static Body unmarshall(Node src, SOAPContext ctx) throws IllegalArgumentException { Element root = (Element)src; - Body body = new Body(); + Body body = new Body(AttributeHandler.unmarshall(root, ctx)); Vector bodyEntries = new Vector(); // Deserialize any body attributes. - body.attrHandler = AttributeHandler.unmarshall(root, ctx); for (Element el = DOMUtils.getFirstChildElement(root); el != null; @@ -219,6 +262,9 @@ return body; } + /** + * Gets this body as a string. + */ public String toString() { StringWriter sw = new StringWriter();
-- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>