KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > message > SOAPHandler


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.axis.message;
17
18 /** A <code>SOAPHandler</code>
19  *
20  * @author Glen Daniels (gdaniels@allaire.com)
21  */

22
23 import org.apache.axis.AxisFault;
24 import org.apache.axis.Constants;
25 import org.apache.axis.encoding.DeserializationContext;
26 import org.apache.axis.encoding.TypeMappingRegistry;
27 import org.apache.axis.soap.SOAPConstants;
28 import org.apache.axis.utils.Messages;
29 import org.apache.axis.utils.StringUtils;
30 import org.xml.sax.Attributes JavaDoc;
31 import org.xml.sax.SAXException JavaDoc;
32 import org.xml.sax.helpers.DefaultHandler JavaDoc;
33
34 import javax.xml.soap.SOAPException JavaDoc;
35 import java.io.CharArrayWriter JavaDoc;
36
37 public class SOAPHandler extends DefaultHandler JavaDoc
38 {
39     public MessageElement myElement = null;
40     private MessageElement[] myElements;
41     private int myIndex = 0;
42
43     private CharArrayWriter JavaDoc val;
44     
45     public SOAPHandler() {
46     }
47
48    /**
49     * This constructor allows deferred setting of any elements
50     * @param elements array of message elements to be populated
51     * @param index position in array where the message element is to be created
52     */

53     public SOAPHandler(MessageElement[] elements, int index) {
54         myElements = elements;
55         myIndex = index;
56     }
57     
58     public void startElement(String JavaDoc namespace, String JavaDoc localName,
59                              String JavaDoc prefix, Attributes JavaDoc attributes,
60                              DeserializationContext context)
61         throws SAXException JavaDoc
62     {
63         SOAPConstants soapConstants = context.getSOAPConstants();
64
65         if (soapConstants == SOAPConstants.SOAP12_CONSTANTS) {
66             String JavaDoc encodingStyle = attributes.getValue(Constants.URI_SOAP12_ENV,
67                                 Constants.ATTR_ENCODING_STYLE);
68
69             if (encodingStyle != null && !encodingStyle.equals("")
70                 && !encodingStyle.equals(Constants.URI_SOAP12_NOENC)
71                 && !Constants.isSOAP_ENC(encodingStyle)) {
72                 TypeMappingRegistry tmr = context.getTypeMappingRegistry();
73                 // TODO: both soap encoding style is registered ?
74
if (tmr.getTypeMapping(encodingStyle) == tmr.getDefaultTypeMapping()) {
75                     AxisFault fault = new AxisFault(Constants.FAULT_SOAP12_DATAENCODINGUNKNOWN,
76                         null, Messages.getMessage("invalidEncodingStyle"), null, null, null);
77
78                     throw new SAXException JavaDoc(fault);
79                 }
80             }
81         }
82
83
84         // By default, make a new element
85
if (!context.isDoneParsing() && !context.isProcessingRef()) {
86             if (myElement == null) {
87                 try {
88                     myElement = makeNewElement(namespace, localName, prefix,
89                                                attributes, context);
90                 } catch (AxisFault axisFault) {
91                     throw new SAXException JavaDoc(axisFault);
92                 }
93             }
94             context.pushNewElement(myElement);
95         }
96     }
97
98     public MessageElement makeNewElement(String JavaDoc namespace, String JavaDoc localName,
99                              String JavaDoc prefix, Attributes JavaDoc attributes,
100                              DeserializationContext context)
101         throws AxisFault
102     {
103         return new MessageElement(namespace, localName,
104                                                prefix, attributes, context);
105     }
106
107     public void endElement(String JavaDoc namespace, String JavaDoc localName,
108                            DeserializationContext context)
109         throws SAXException JavaDoc
110     {
111         if (myElement != null) {
112             addTextNode();
113
114             if (myElements != null) {
115                 myElements[myIndex] = myElement;
116             }
117             myElement.setEndIndex(context.getCurrentRecordPos());
118         }
119     }
120     
121     public SOAPHandler onStartChild(String JavaDoc namespace,
122                                     String JavaDoc localName,
123                                     String JavaDoc prefix,
124                                     Attributes JavaDoc attributes,
125                                     DeserializationContext context)
126         throws SAXException JavaDoc
127     {
128         addTextNode();
129         SOAPHandler handler = new SOAPHandler();
130         return handler;
131     }
132
133     private void addTextNode() throws SAXException JavaDoc {
134         if (myElement != null) {
135             if (val != null && val.size() > 0) {
136                 String JavaDoc s = StringUtils.strip(val.toString());
137                 val.reset();
138                 
139                 // we need to check the length of STRIPPED string
140
// to avoid appending ignorable white spaces as
141
// message elmenet's child.
142
// (SOAPHeader and others does not accept text children...
143
// but in SAAJ 1.2's DOM view, this could be incorrect.
144
// we need to keep the ignorable white spaces later)
145
if(s.length()>0){
146                     try {
147                         // add unstripped string as text child.
148
myElement.addTextNode(s);
149                     } catch (SOAPException JavaDoc e) {
150                         throw new SAXException JavaDoc(e);
151                     }
152                 }
153             }
154         }
155     }
156     
157     public void onEndChild(String JavaDoc namespace, String JavaDoc localName,
158                            DeserializationContext context)
159         throws SAXException JavaDoc
160     {
161     }
162
163     public void characters(char[] chars, int start, int end)
164         throws SAXException JavaDoc
165     {
166         if (val == null) {
167             val = new CharArrayWriter JavaDoc();
168         }
169         val.write(chars, start, end);
170     }
171 }
172
Popular Tags