001: /*
002: * To change this template, choose Tools | Templates
003: * and open the template in the editor.
004: */
005: package org.netbeans.identity.samples;
006:
007: import java.io.BufferedReader;
008: import java.io.IOException;
009: import java.io.InputStreamReader;
010: import java.net.MalformedURLException;
011: import java.net.URL;
012: import java.net.URLConnection;
013: import java.util.Calendar;
014: import java.util.GregorianCalendar;
015: import java.util.HashMap;
016: import java.util.Map;
017:
018: /**
019: *
020: * @author Peter Liu
021: */
022: public class GetQuote {
023:
024: public GetQuote() {
025: init();
026: }
027:
028: /**
029: * Returns the stock quote for the stock symbol.
030: * If unable to obtain the real time quote, returns a cached value
031: */
032: public Map getQuote(String symbol) {
033: // Obtain the quote from Yahoo! Service
034: Map data = getYahooQuote(symbol);
035: if (data == null) {
036: // Unable to obtain from Yahoo! Get from local cache
037: data = getCachedQuote(symbol);
038: }
039:
040: // Return the results
041: return (data);
042: }
043:
044: private Map getYahooQuote(String ticker) {
045: URL url = null;
046: try {
047: // URL for the stock quote from Yahoo! service
048: url = new URL(
049: "http://download.finance.yahoo.com/d/quotes.csv?s="
050: + ticker + "&d=t&f=sl1d1t1c1ohgvj1pp2wern");
051:
052: // Set the timeouts for connection and read
053: URLConnection connection = url.openConnection();
054: connection.setConnectTimeout(1000);
055: connection.setReadTimeout(1000);
056:
057: // Request for the stock quote
058: BufferedReader in = new BufferedReader(
059: new InputStreamReader(url.openStream()));
060: String[] values = null;
061: String str;
062: if ((str = in.readLine()) != null) {
063: values = str.split(",");
064: }
065: in.close();
066: if ((values == null) || values.length < 16) {
067: return (null);
068: }
069: // Populate stock values
070: Map map = new HashMap();
071: map.put("symbol", removeQuotes(values[0]));
072: map.put("company", removeQuotes(values[15]));
073: map.put("realValue", values[1]);
074: map.put("time", removeQuotes(values[2]) + " "
075: + removeQuotes(values[3]));
076: map.put("volume", values[8]);
077: map.put("open", values[5]);
078: map.put("change", values[4]);
079: map.put("dayHigh", values[6]);
080: map.put("dayLow", values[7]);
081: map.put("yearRange", removeQuotes(values[12]));
082: map.put("marketCap", values[9]);
083: map.put("message", "");
084: return map;
085: } catch (MalformedURLException ex) {
086: ex.printStackTrace();
087: } catch (IOException ex) {
088: ex.printStackTrace();
089: }
090: return null;
091: }
092:
093: private Map getCachedQuote(String symbol) {
094: Map data = (Map) stockData.get(symbol);
095: if (data == null) {
096: data = (Map) stockData.get("ORCL");
097: }
098: data.put("symbol", symbol);
099: data.put("company", symbol.toUpperCase());
100: return (data);
101: }
102:
103: /**
104: * Assign static values for stock quotes.
105: * Used as fall-back if quote cannot be
106: * obtained from Yahoo!
107: */
108: private void init() {
109: Map stockValues = new HashMap();
110: stockValues.put("company", "");
111: stockValues.put("realValue", "7.36");
112: stockValues.put("volume", "31,793,369");
113: stockValues.put("open", "7.37");
114: stockValues.put("change", "-0.01");
115: stockValues.put("dayHigh", "7.38");
116: stockValues.put("dayLow", "7.12");
117: stockValues.put("yearRange", "N/A");
118: stockValues.put("marketCap", "N/A");
119: stockValues.put("message", "Quote AUTO Generated");
120: stockValues.put("time", getTime());
121: stockData.put("SUNW", stockValues);
122:
123: stockValues = new HashMap();
124: stockValues.put("realValue", "16.35");
125: stockValues.put("company", "");
126: stockValues.put("volume", "38,544,715");
127: stockValues.put("open", "16.35");
128: stockValues.put("change", "-0.27");
129: stockValues.put("dayHigh", "16.64");
130: stockValues.put("dayLow", "16.31");
131: stockValues.put("yearRange", "N/A");
132: stockValues.put("marketCap", "N/A");
133: stockValues.put("message", "Quote AUTO Generated");
134: stockValues.put("time", getTime());
135: stockData.put("ORCL", stockValues);
136: }
137:
138: private String getTime() {
139: GregorianCalendar time = new GregorianCalendar();
140: return (time.get(Calendar.MONTH) + "/"
141: + time.get(Calendar.DAY_OF_MONTH) + "/"
142: + time.get(Calendar.YEAR) + " "
143: + time.get(Calendar.HOUR) + ":"
144: + time.get(Calendar.MINUTE) + time.get(Calendar.AM_PM));
145: }
146:
147: private String removeQuotes(String key) {
148: return (key.replaceAll("\"", ""));
149: }
150:
151: private static Map stockData = new HashMap();
152: }
|