01: /*
02: * Copyright 2001-2004 The Apache Software Foundation.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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 samples.stock;
18:
19: import org.w3c.dom.Document;
20: import org.w3c.dom.Element;
21: import org.w3c.dom.NodeList;
22:
23: import javax.xml.parsers.DocumentBuilder;
24: import javax.xml.parsers.DocumentBuilderFactory;
25: import java.net.URL;
26:
27: /**
28: * See \samples\stock\readme for info.
29: *
30: * @author Sanjiva Weerawarana (sanjiva@watson.ibm.com)
31: * @author Doug Davis (dug@us.ibm.com)
32: */
33: public class StockQuoteService {
34: public String test() {
35: return ("Just a test");
36: }
37:
38: public float getQuote(String symbol) throws Exception {
39: // get a real (delayed by 20min) stockquote from
40: // http://services.xmethods.net/axis/. The IP addr
41: // below came from the host that the above form posts to ..
42:
43: if (symbol.equals("XXX"))
44: return ((float) 55.25);
45:
46: URL url = new URL(
47: "http://services.xmethods.net/axis/getQuote?s="
48: + symbol);
49:
50: DocumentBuilderFactory dbf = DocumentBuilderFactory
51: .newInstance();
52: DocumentBuilder db = dbf.newDocumentBuilder();
53:
54: Document doc = db.parse(url.toExternalForm());
55: Element elem = doc.getDocumentElement();
56: NodeList list = elem.getElementsByTagName("stock_quote");
57:
58: if (list != null && list.getLength() != 0) {
59: elem = (Element) list.item(0);
60: list = elem.getElementsByTagName("price");
61: elem = (Element) list.item(0);
62: String quoteStr = elem.getAttribute("value");
63: try {
64: return Float.valueOf(quoteStr).floatValue();
65: } catch (NumberFormatException e1) {
66: // maybe its an int?
67: try {
68: return Integer.valueOf(quoteStr).intValue() * 1.0F;
69: } catch (NumberFormatException e2) {
70: return -1.0F;
71: }
72: }
73: }
74: return (0);
75: }
76: }
|