import java.net.*;              // Needed for URL 
import java.io.*;               // Declares the InputSource object below
import org.xml.sax.*;           // Declares the interfaces for the parsers
import javax.xml.parsers.*;     // Declares the JAXP parser
import org.w3c.dom.*;           // Declares the Document & Node objects for the XML doc
import com.finwin.util.*;       // Needed for the finwin utilities

public class option_chain {
    
    public static void main(String args[]) {
        // Show some help in case of a problem.
        if (args.length < 2) {
                System.out.println("Usage:\n"+
                    "\t\tjava option_chain <symbol1> <month-code>");
            return;
        }
        
        URL url = null;
        try {
            url = new URL("http://www.finwin.com/processCT/processOptiontag.cfm");
        } catch (MalformedURLException mue) {}

        HTTPRequest req = new HTTPRequest(HTTPRequest.POST, url);

        req.addPostValue("username", "apande"/* TODO: Place your FinWin user name here */ );
        req.addPostValue("password", "AN1lMALA"/* TODO: Place your FinWin password here */ );
        req.addPostValue("id", "186"/* TODO: Place your FinWin ID here */ );
        req.addPostValue("request", "optionchain");
        req.addPostValue("type", "query");
        req.addPostValue("symbol", args[0].toUpperCase());
        req.addPostValue("month", args[1].toUpperCase());
        req.addPostValue("puts", "1");

        int rc = req.request();

        if (rc/100 == 2) { // a 2xx class response (ie. good)
            String  wddxResponse = req.getResponse();
            if (wddxResponse!=null) {
                try {
                    // Obtain an instance of the DocumentBuilderFactor which will
                    // generate the DocumentBuilder XML parser
                    DocumentBuilderFactory  f = DocumentBuilderFactory.newInstance();
                    // Obtain an instance of the DocumentBuilder XML parser
                    DocumentBuilder         b = f.newDocumentBuilder();
                    // Construct an InputSource for the Document Builder to read from
                    InputSource             is = new InputSource(new StringReader(wddxResponse));
                    // Parse the document
                    Document                doc = b.parse(is);  
                    // Construct a WDDXDocument to work with the data
                    WDDXDocument            wDoc = new WDDXDocument(doc);
                    
                    WDDXObject              obj = wDoc.getData();

                    if (obj instanceof WDDXStruct) {
                        WDDXStruct  st = (WDDXStruct)obj;
                        WDDXVar     var;
                        // Obtain the number of fields in the structure
                        int         fields = st.getFieldCount();
                        int         i;
                    
                        // Make a list of all the fields in the WDDXStruct  
                        System.out.print("Fields: ");
                        for(i=0; i<fields;i++) {
                            var = (WDDXVar)st.getField(i);
                            if (var!=null)
                                System.out.print(var.getName()+" ");
                        }
                        System.out.println("");

                        // Get the CALLS and display them
                        var = (WDDXVar)st.getField("call_recs");
                        if (var != null) {
                            WDDXRecordset   rs = (WDDXRecordset)var.get();
                            System.err.println("rs="+rs);
                            int             rows = rs.getRowCount();
                            String          cols[] = rs.getColumnNames();
                            int             j;

                            for(i=0;i<cols.length;i++) 
                                if (i<1) System.out.print(cols[i]); 
                                else System.out.print(", "+cols[i]);
                            System.out.println("");

                            for(i=0;i<rows;i++) {
                                for(j=0;j<cols.length;j++) 
                                    if (i<1) System.out.print(rs.getValue(i,j)); 
                                    else System.out.print(", "+rs.getValue(i,j));
                                System.out.println(""); 
                            }
                        }
                    } else {
                        System.out.println("obj="+obj);
                        System.out.println(wddxResponse);
                    }
                } catch (ParserConfigurationException e) {
                    e.printStackTrace();
                } catch (SAXException e) {
                    e.printStackTrace();
                    System.out.println("wddxResponse:"+wddxResponse);
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                    System.out.println("wddxResponse:"+wddxResponse);
                }
            }
        }
    }
}