////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //PIMS: Personal Invstment Management System //Class : InstallNext. //This is the second part of the Installation program, which basically downloads //the share prices from the net and initializes the prices and the Companies files. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// import java.lang.*; import java.io.*; import java.util.*; import java.net.*; import java.awt.*; import javax.swing.*; public class InstallNext extends JFrame{ private String HOME; private int numShares = 0; private int fill=0; private String host = "202.87.40.188";//www.indiainfoline.com private String fileName = "stok/nse/pric.xls"; private String shares[][]; private TextField progress; private boolean downloadOption; public InstallNext(String HOME, boolean downloadOption){ this.HOME = HOME;//Set the home directory. this.downloadOption = downloadOption; download();//Download the current share prices. new InstallFinal();//Start the final installation stage. } public void download(){ try{ setSize(500,100); setLocation(320,280); setTitle("PIMS Installation Wizard"); setBackground(Color.white); Container CP = getContentPane(); CP.setLayout(new FlowLayout()); progress = new TextField(15); Label p = new Label("PIMS require the current prices from the net. This may take a few minutes..."); CP.add(progress); CP.add(p); setVisible(true); //load the current prices from the net if(downloadOption)downloadPrices(host, fileName); Process P = java.lang.Runtime.getRuntime().exec("./toHTML"); P.waitFor(); shares = parseFile(); writeSharePrices(); writeCompanies(); progress.setText("Prices Downloaded"); setVisible(false); }catch(Exception e){ e.printStackTrace(); new Error("Problem in Downloading prices from the net, installation aborted."); } } public String downloadPrices(String host,String file_name)throws Exception{ String result=null; String request=""; String req="http://"+host+"/"+file_name; try{ Socket soc=new Socket(host,80); OutputStream out=soc.getOutputStream(); InputStream in=soc.getInputStream(); request="GET "+req+" HTTP/1.0\r\n"+""+"\r\n"; //writing the request to the given host byte buffer[]=request.getBytes(); out.write(buffer); //reading the reply FileOutputStream FW = new FileOutputStream("./price.xls"); byte header[]=new byte[500]; int head[] = new int[500]; int c; int i=0; while(true){//reading the downloded file. if(i>=500)break;//reading only the header. c=in.read(); if(c == -1)break; header[i]=(byte) c; head[i++] = c; } String temp=new String(header,0,500); //read the length of the file. int CL = temp.indexOf("Content-Length"); int index=temp.indexOf("\r\n\r\n"); int fileLength = Integer.parseInt(temp.substring(CL+16, index)); int n=Integer.parseInt(temp.substring(9,12)); if(n==200){//download OK. //read the rest of the file and write to the output file. for(int k=index+4;k<500;++k)FW.write(head[k]); i = 500 - index - 4; while(true){//reading the downloded file. c=in.read(); i++; fill = (i*15)/fileLength; if(i%(fileLength/15) == 0)showProgress(); FW.write(c); if(i == fileLength)break; } } in.close(); out.close(); }catch(Exception e){System.out.println(e);} return(result); } /* //displays the progress bar public void paint(Graphics g){ g.setColor(Color.black); g.drawRect(100,40,300,30); g.fillRect(100,40,fill,30); if(fill != 300) g.drawString("PIMS require the current prices from the net. This may take a few minutes...", 150, 80); else g.drawString("Download Complete...", 150, 80); } */ private void showProgress(){ String stars=""; for(int i=0;i"); end = Line.indexOf("<",st); Line = Line.substring(st+1, end); //} companyName = Line; for(i=0;i<3;++i)BR.readLine();//eat the intermediate lines. Line = BR.readLine();//read the price //for(i=0;i<1;++i){ st = Line.indexOf(">"); end = Line.indexOf("<",st); Line = Line.substring(st+1, end); //} price = Line; shares[numShares][0] = companyName; shares[numShares][1] = price; numShares++; } } return(shares); } private void writeLine(FileWriter fw, String Line)throws Exception{ int i=0; int length = Line.length(); char buffer[] = new char[length]; Line.getChars(0, length, buffer, 0); fw.write(buffer); fw.write('\n'); } private void writeSharePrices()throws Exception{ FileWriter FW = new FileWriter(HOME+"/.pims/prices"); for(int i=0;i