View Javadoc

1   /*
2    * Copyright 2000-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  
17  package org.apache.jetspeed.webservices.finance.stockmarket;
18  
19  import org.xml.sax.helpers.XMLFilterImpl;
20  import org.xml.sax.ContentHandler;
21  import org.xml.sax.*;
22  
23  
24  class SOAPResponseHandler extends XMLFilterImpl 
25  {
26      String outputNameSpace = null;
27      String outputElementName = null;
28      String outputPartElementName = null;
29  
30      public SOAPResponseHandler(String ns, String en, String pen) {
31          outputNameSpace = ns;
32          outputElementName = en;
33          outputPartElementName = pen;
34      }
35  
36      boolean inSoapEnv = false;
37      boolean inSoapBody = false;
38      boolean inOperation = false;
39      boolean inResult = false;
40      boolean debugParse = false;
41  
42      StringBuffer resultBuffer = null;
43      XMLFilterImpl resultHandler = null;
44      int resultElementStack = 0;
45  
46      boolean wasFault = false;
47      boolean inFault = false;
48      boolean inFaultString = false;
49      StringBuffer faultContent = null;
50  
51      public void setContentHandler(ContentHandler handler) {
52          ((XMLReader)getParent()).setContentHandler(handler);
53      }
54  
55      public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
56          String uriName = uri+":"+localName;
57          if ("http://schemas.xmlsoap.org/soap/envelope/:Fault".equals(uriName)) {
58              if (debugParse) System.out.println("Found Fault");
59              inFault = true;
60          } else if (inFault && "faultstring".equals(localName)) {
61              if (debugParse) System.out.println("Found Fault String");
62              inFaultString = true;
63              faultContent = new StringBuffer();
64          } else if ("http://schemas.xmlsoap.org/soap/envelope/:Envelope".equals(uriName)) {
65              if (debugParse) System.out.println("Found Envelope");
66              inSoapEnv = true;
67          } else if (inSoapEnv && "http://schemas.xmlsoap.org/soap/envelope/:Body".equals(uriName)) {
68              if (debugParse) System.out.println("Found Body");
69              inSoapBody = true;
70          } else if (inSoapBody && (outputNameSpace+":"+outputElementName).equals(uriName)) {
71              if (debugParse) System.out.println("Found operation: "+localName);
72              inOperation = true;
73          } else if (inOperation && outputPartElementName.equals(localName)) {
74              if (attributes.getValue("href") != null) throw new SAXNotSupportedException("href attributes not supported");
75              if (debugParse) System.out.println("Found Part: "+localName);
76              if (resultHandler != null) {
77                  resultHandler.setParent(this);
78                  getParent().setContentHandler(resultHandler);
79              } else {
80                  resultBuffer = new StringBuffer();
81              }
82              inResult = true;
83          } else if (!inFault) {
84              if (debugParse) System.out.println("Found Unknown Element: <"+qName+">    ns:"+uri);
85              inResult = false;
86              inSoapEnv = false;
87              inSoapBody = false;
88          }
89      }
90  
91      public void endElement(String uri, String localName, String qName) throws SAXException {
92          if (inResult && outputPartElementName.equals(localName)) {
93              inResult = false;
94          } else if ("http://schemas.xmlsoap.org/soap/envelope/:Fault".equals(uri+":"+localName)) {
95              wasFault = true;
96              inFault = false;
97          } else if (inFault && "faultstring".equals(localName)) {
98              inFaultString = false;
99          }
100     }
101 
102     public void characters(char[] ch, int start, int length) throws SAXException {
103         if (inResult) {
104             resultBuffer.append(new String(ch,start,length));
105         }
106         if (inFault) faultContent.append(new String(ch,start,length));
107     }
108     public String getResult() {
109         if (resultBuffer == null) return null;
110         return resultBuffer.toString();
111     }
112     public void setResultHandler(XMLFilterImpl handler) {
113         resultHandler = handler;
114     }
115     public boolean isFault() {
116         return wasFault;
117     }
118     public String getFaultContent() {
119         if (faultContent == null) return "";
120         return faultContent.toString();
121     }
122 }