import com.finwin.util.*;   // Needed for the WDDXDocument, WDDXObject, & WDDXRecordset
import java.net.*;          // Needed for URL
import java.io.*;           // Needed for InputSource
import org.xml.sax.*;       // Needed for XML Parser Interfaces
import org.apache.xerces.parsers.*; // Needed for XML4J DocumentBuilder, etc.
import org.w3c.dom.*;       // Needed for the DOM Interfaces

/**
An Example using the XML4J XML Parser.

This example uses the XML4J XML Parser from IBM(http://www.alphaworks.ibm.com/tech/xml4j).
The example is intended to demonstrate how to utilize the <code>HTTPRequest</code>
in conjunction with XML4J, and the <code>WDDXDocument</code> utilities.


@see com.finwin.util.HTTPRequest
@see com.finwin.util.WDDXDocument
@see com.finwin.util.WDDXRecordset
*/
public class xml4jtest {

    /**
    Main Method for the XML4J Example.
    @param args The list of symbols to quote
    */
    public static void main(String args[]) {
        // Tell the user if he didn't specify enough parameters
        if (args.length < 1) {
            System.out.println("Usage:\n"+
                    "\t\t\tjava xml4jtest <symbol1> [symbol2] [...]");
            return;
        }

        // Construct a url to request the quote data from in this case it indicates
        // the FinWin server's processquotetag.cfm resource.
        URL url=null;
        try { url = new URL("http://www.finwin.com/processct/processquotetag.cfm");
        } catch(MalformedURLException mue) {}

        // Construct the HTTPRequest object which will do the work of requesting
        // the data from FinWin via a HTTP 1.0 Request
        HTTPRequest req = new HTTPRequest(HTTPRequest.POST, url);

        // Specify the POST Request Parameters
        req.addPostValue("USERNAME", "" /* TODO: Specify your FinWin User name here */);
        req.addPostValue("PASSWORD", "" /* TODO: Specify your FinWin Password here */);
        req.addPostValue("ID", "" /* TODO: Specify your FinWin ID here */);
        req.addPostValue("request", "quote");
        req.addPostValue("TYPE", "query");
        for(int i = 0;i<args.length;i++)
            req.addPostValue("symbol", args[i]);
    
        int rc = req.request();             // make the request
        if (rc/100 == 2) { // a 2xx class response (ie. good)

            // Obtain the WDDX response text
            String wddx = req.getResponse();
        
            if (wddx!=null) { 
                try {
                    // Obtain a reference to the DocumentBuilder, the XML Parser
                    DOMParser       parser = new DOMParser();
                    // Create an input source for the builder to read from
                    InputSource             is = new InputSource(new StringReader(wddx));
                    // Parse the Document
                    parser.parse(is);

                    Document                doc = parser.getDocument(); 
                    // Construct a WDDXDocument to process the DOM Document
                    WDDXDocument            wdoc = new WDDXDocument(doc);
    
                    // Obtain the WDDXRecordset from the document   
                    WDDXObject  obj = wdoc.getData();
                    if (obj instanceof WDDXRecordset) {
                        WDDXRecordset   rs = (WDDXRecordset)obj;
                        String  cols[] = rs.getColumnNames();
                        int     rowCount = rs.getRowCount();
                        int     i,j;
    
                        // Display all the columns and their values
                        for(i=0;i<cols.length;i++)
                            if (i<0) System.out.print(cols[i]);
                            else  System.out.print(", "+cols[i]);
                        System.out.println("");
                        for(i=0;i<rowCount;i++) {
                            for(j=0;j<cols.length;j++)
                                if (j<0) System.out.print(rs.getValue(i,j));
                                else  System.out.print(", "+rs.getValue(i,j));
                            System.out.println("");
                        }
            
                        System.out.println("\n\n\n");
    
                        // Now display only a slected set of columns and their values   
                        System.out.println("Symbol, High, Low, Open, Close, Last, Volume");
                        for(i=0;i<rowCount;i++) {
                            System.out.print(rs.getValue(i,"SYM"));
                            System.out.print(", "+rs.getValue(i,"HIGH"));
                            System.out.print(", "+rs.getValue(i,"LOW"));
                            System.out.print(", "+rs.getValue(i,"OPEN"));
                            System.out.print(", "+rs.getValue(i,"PREV"));
                            System.out.print(", "+rs.getValue(i,"LAST"));
                            System.out.println(", "+rs.getValue(i,"VOLUME"));
                        }
                    } else {
                        // Maybe a Security Error was generated ???
                        System.err.println("Unexpected data received: "+obj);
                    }
                } catch (SAXException e) {
                    e.printStackTrace();
                    System.out.println("wddx:"+wddx);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } else {
            System.err.println("The server responded with:"+rc+"\n"+req.getResponse());
        }
    }
}