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      SAX-based XML event handler (filter). Works with org.xml.sax.XMLReader to filter
25      standard SAX events. This class maps the XML represention of a stock into a java class.
26  
27      @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
28      @version $Id: StockQuoteHandler.java,v 1.3 2004/02/23 03:15:29 jford Exp $
29  */
30  
31  public class StockQuoteHandler extends XMLFilterImpl
32  {
33      StringBuffer resultBuffer = null;
34      StockQuote result = null;
35      XMLFilterImpl handler = null;
36  
37      public void startElement(String uri, String localName, String qName, Attributes attributes) 
38          throws SAXException 
39      {
40          if (attributes.getValue("href") != null) 
41              throw new SAXNotSupportedException("href attributes not supported");
42          if (localName.equals("Price")) {
43              if (result == null) result = new BaseStockQuote();
44              resultBuffer = new StringBuffer();
45          } else             if (localName.equals("Name")) {
46              if (result == null) result = new BaseStockQuote();
47              resultBuffer = new StringBuffer();
48          } else             if (localName.equals("Symbol")) {
49              if (result == null) result = new BaseStockQuote();
50              resultBuffer = new StringBuffer();
51          } else             if (localName.equals("Time")) {
52              if (result == null) result = new BaseStockQuote();
53              resultBuffer = new StringBuffer();
54          } else             if (localName.equals("Date")) {
55              if (result == null) result = new BaseStockQuote();
56              resultBuffer = new StringBuffer();
57          } else             if (localName.equals("High")) {
58              if (result == null) result = new BaseStockQuote();
59              resultBuffer = new StringBuffer();
60          } else             if (localName.equals("Volume")) {
61              if (result == null) result = new BaseStockQuote();
62              resultBuffer = new StringBuffer();
63          } else             if (localName.equals("Change")) {
64              if (result == null) result = new BaseStockQuote();
65              resultBuffer = new StringBuffer();
66          } else             if (localName.equals("Opening")) {
67              if (result == null) result = new BaseStockQuote();
68              resultBuffer = new StringBuffer();
69          } else             if (localName.equals("Low")) {
70              if (result == null) result = new BaseStockQuote();
71              resultBuffer = new StringBuffer();
72          } else {
73              throw new SAXException("Unexpected element "+localName);
74          }
75      }
76  
77      public void endElement(String uri, String localName, String qName) throws SAXException {
78          if (localName.equals("Price")) {
79              result.setPrice(resultBuffer.toString());
80          } else if (localName.equals("Name")) {
81              result.setName(resultBuffer.toString());
82          } else if (localName.equals("Symbol")) {
83              result.setSymbol(resultBuffer.toString());
84          } else if (localName.equals("Time")) {
85              result.setTime(resultBuffer.toString());
86          } else if (localName.equals("Date")) {
87              result.setDate(resultBuffer.toString());
88          } else if (localName.equals("High")) {
89              result.setHigh(resultBuffer.toString());
90          } else if (localName.equals("Volume")) {
91              result.setVolume(resultBuffer.toString());
92          } else if (localName.equals("Change")) {
93              result.setChange(resultBuffer.toString());
94          } else if (localName.equals("Opening")) {
95              result.setOpening(resultBuffer.toString());
96          } else if (localName.equals("Low")) {
97              result.setLow(resultBuffer.toString());
98          } else {
99              ContentHandler handler = (ContentHandler)getParent();
100             getParent().setContentHandler(handler);
101             handler.endElement(uri,localName,qName);
102         }
103     }
104 
105     public void characters(char[] ch, int start, int length) throws SAXException {
106         if (resultBuffer != null) resultBuffer.append(new String(ch,start,length));
107     }
108     public void setContentHandler(ContentHandler handler) {
109         ((XMLReader)getParent()).setContentHandler(handler);
110     }
111     public StockQuote getResult() {
112         return result;
113     }
114 }