| 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 |
|
| 17 |
import java.io.*; |
| 18 |
import java.util.* ; |
| 19 |
import java.net.URL; |
| 20 |
|
| 21 |
import org.w3c.dom.* ; |
| 22 |
import org.apache.axis.utils.XMLUtils ; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
/** |
| 27 |
* See \samples\stock\readme for info. |
| 28 |
* |
| 29 |
* @author Sanjiva Weerawarana ([email protected]) |
| 30 |
* @author Doug Davis ([email protected]) |
| 31 |
*/ |
| 32 |
public class StockQuoteService { |
| 33 |
public float getQuote (String symbol) throws Exception { |
| 34 |
// get a real (delayed by 20min) stockquote from |
| 35 |
// http://services.xmethods.net/axis/. The IP addr |
| 36 |
// below came from the host that the above form posts to .. |
| 37 |
|
| 38 |
// NOTE THAT THIS RETURNS 66.25 WHERE THE ORDINARY EXAMPLE RETURNS 55.25! |
| 39 |
if ( symbol.equals("XXX") ) return( (float) 66.25 ); |
| 40 |
|
| 41 |
Document doc = null ; |
| 42 |
|
| 43 |
doc = XMLUtils.newDocument( "http://services.xmethods.net/axis/getQuote?s=" |
| 44 |
+symbol ); |
| 45 |
|
| 46 |
Element elem = doc.getDocumentElement(); |
| 47 |
NodeList list = elem.getElementsByTagName("stock_quote"); |
| 48 |
|
| 49 |
elem = (Element) list.item(0); |
| 50 |
list = elem.getElementsByTagName( "price" ); |
| 51 |
elem = (Element) list.item( 0 ); |
| 52 |
String quoteStr = elem.getAttribute("value"); |
| 53 |
try { |
| 54 |
return Float.valueOf(quoteStr).floatValue(); |
| 55 |
} catch (NumberFormatException e1) { |
| 56 |
// maybe its an int? |
| 57 |
try { |
| 58 |
return Integer.valueOf(quoteStr).intValue() * 1.0F; |
| 59 |
} catch (NumberFormatException e2) { |
| 60 |
return -1.0F; |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
} |