import oracle.xml.xsql.*; import oracle.xml.parser.v2.*; import org.w3c.dom.*; import java.net.*; import java.util.StringTokenizer; import java.sql.SQLException; import JTidyConverter; import java.io.IOException; public class LiveQuotes extends XSQLActionHandlerImpl { private static final String YQUOTES = "http://quote.yahoo.com/q?d2=v1&o=d&s="; private static final String YTRANSF = "YahooQuotes-to-QuoteStream.xsl"; public void handleAction(Node root) throws SQLException { XSQLPageRequest req = getPageRequest(); Element actElt = getActionElement(); // Get the list of Symbols from the action element's "symbols" attribute String symbolList = getAttributeAllowingParam("symbols",actElt); if (symbolList == null || symbolList.equals("")) { reportMissingAttribute(root,"symbols"); return; } try { // Prepare the URL to get quotes from Yahoo! Quotes URL yahooUrl = new URL(YQUOTES+symbolList.replace(',','+')); JTidyConverter jtc = new JTidyConverter(); // Convert the dynamically-produced Yahoo Quotes page to XML Document XMLDocument yahooquotes = jtc.XMLifyHTMLFrom(yahooUrl); // Use the XSQL Page Processor's built-in Stylesheet Processor to // have the page processor automatically cache and pool this // transformation as needed. Use processToDocument() to get the // transformed results as an in-memory XML document. Document quoteStream = XSQLStylesheetProcessor.processToDocument(yahooquotes, req.translateURL(YTRANSF), req); // Append the quotes to the root of our XML result Element docElement = quoteStream.getDocumentElement(); quoteStream.removeChild(docElement); root.appendChild(docElement); } catch(MalformedURLException mfx) { /* Ignore */ } catch(IOException io) { this.reportError(root,"Unable to retrieve quotes"); } catch(Exception ex) { this.reportError(root,"Unable to retrieve quotes"); } } } |