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