1 16 package org.apache.axis.message; 17 18 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 ; 31 import org.xml.sax.SAXException ; 32 import org.xml.sax.helpers.DefaultHandler ; 33 34 import javax.xml.soap.SOAPException ; 35 import java.io.CharArrayWriter ; 36 37 public class SOAPHandler extends DefaultHandler 38 { 39 public MessageElement myElement = null; 40 private MessageElement[] myElements; 41 private int myIndex = 0; 42 43 private CharArrayWriter val; 44 45 public SOAPHandler() { 46 } 47 48 53 public SOAPHandler(MessageElement[] elements, int index) { 54 myElements = elements; 55 myIndex = index; 56 } 57 58 public void startElement(String namespace, String localName, 59 String prefix, Attributes attributes, 60 DeserializationContext context) 61 throws SAXException 62 { 63 SOAPConstants soapConstants = context.getSOAPConstants(); 64 65 if (soapConstants == SOAPConstants.SOAP12_CONSTANTS) { 66 String 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 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 (fault); 79 } 80 } 81 } 82 83 84 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 (axisFault); 92 } 93 } 94 context.pushNewElement(myElement); 95 } 96 } 97 98 public MessageElement makeNewElement(String namespace, String localName, 99 String prefix, Attributes 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 namespace, String localName, 108 DeserializationContext context) 109 throws SAXException 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 namespace, 122 String localName, 123 String prefix, 124 Attributes attributes, 125 DeserializationContext context) 126 throws SAXException 127 { 128 addTextNode(); 129 SOAPHandler handler = new SOAPHandler(); 130 return handler; 131 } 132 133 private void addTextNode() throws SAXException { 134 if (myElement != null) { 135 if (val != null && val.size() > 0) { 136 String s = StringUtils.strip(val.toString()); 137 val.reset(); 138 139 if(s.length()>0){ 146 try { 147 myElement.addTextNode(s); 149 } catch (SOAPException e) { 150 throw new SAXException (e); 151 } 152 } 153 } 154 } 155 } 156 157 public void onEndChild(String namespace, String localName, 158 DeserializationContext context) 159 throws SAXException 160 { 161 } 162 163 public void characters(char[] chars, int start, int end) 164 throws SAXException 165 { 166 if (val == null) { 167 val = new CharArrayWriter (); 168 } 169 val.write(chars, start, end); 170 } 171 } 172 | Popular Tags |