import java.io.*;
import java.net.*;
import java.util.*;
import java.util.HashMap;
import java.util.regex.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.FileWriter;

class WebSearcher {
    public static void main(String args[]) {
	  Calendar now = Calendar.getInstance(); //インスタンス化
      int h = now.get(now.HOUR_OF_DAY);//時を取得
      int m = now.get(now.MINUTE);     //分を取得
      int f = now.get(now.SECOND);      //秒を取得
      System.out.println(h+"時"+m+"分"+f+"秒");

        Webpage[] page = new Webpage[args.length];
        String[] you = new String[args.length];
	Thread[] threads = new Thread[args.length];
	Queue<String> urlQueue = new LinkedList<String>();
        int w = args.length;
        int y = 0;
	System.out.println("検索したい文字を入力してください。");
        Scanner search = new Scanner(System.in);
	String s = search.next();
        HashMap<Integer, String> map = new HashMap<Integer, String>();
        List<Integer> l = new ArrayList<Integer>(); 
	
	for(int i = 0; i < args.length; i++){
	    System.out.println(args[i]);
	    page[i] = new Webpage();
	    page[i].setKensaku(s);
	    urlQueue.offer(args[i]);
	    String urlString = urlQueue.poll();
            you[i] = urlString;
	    page[i].setURL(urlString);
	    //if(args.length > 1)
		//page[i].setEncoding(args[w - 1]);
	    //page[i].setEncoding();
	    threads[i] = new Thread(page[i]);
	    threads[i].start();
	 }


        for(int i = 0; i < args.length; i++) {
	    try {
		threads[i].join();
	    } 
            catch ( InterruptedException e ) {
	    }
	    map.put(page[i].getGyosuu(), you[i]);
            l.add(page[i].getGyosuu());
	}

	Collections.sort( l );
		try{
		    File file = new File("/home/07fi011/data.txt");
		    PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(file)));
		    System.out.println();
		    System.out.println("順位は: ");
	    for(int i : l){
		writer.println(map.get(i) + "\t" + "| " + i + " 回出現 | " + (w )+ " 位");	
		System.out.println(map.get(i) + "\t" + "| " + i + " 回出現 | " + (w) + " 位");
		w--;
	    }
	    writer.close();
	    	}
		catch(IOException e){
		    System.out.println(e);
		}
    }  
}

class Webpage implements Runnable {
    private URL url;
    private String encoding;
    private String data;
    private int ps3;
    
    public Webpage() {
        url = null;
        ps3 = 0;
	encoding = "JISAutoDetect";
    }
    
    public void setURL(URL url) {
        this.url = url;
    }

    public void setKensaku(String data) {
	this.data = data;
    }
    
    public void setURL(String urlString) {
        try {
	    setURL(new URL(urlString));
	}
        catch (MalformedURLException e) {
            System.err.println("Wrong URL: " + urlString);
        }
    }
    // public void setEncoding() {
    //encoding = "JISAutoDetect";
	
    //  }
	
    public int getGyosuu() {
        return ps3;
    }
    public void run() {
	if(url == null)
	    return;
        Scanner scanner = null;
        try {
            URLConnection urlConnection = url.openConnection();
	    InputStream is = urlConnection.getInputStream();
	    InputStreamReader isr = new InputStreamReader(is,"JISAutoDetect");
	    BufferedReader br = new BufferedReader(isr); 
	    
	    
	    
            urlConnection.connect();
	    System.out.println(" URL: " + url);
	    System.out.println(" Content-Type: " + urlConnection.getContentType());
            scanner = new Scanner(urlConnection.getInputStream(), encoding);
        }
        catch (IOException e) {
            System.err.println("次のアドレスに接続できません。: " + url);
            return;
        }
	Pattern patternBody = Pattern.compile("<\\s*body", Pattern.CASE_INSENSITIVE);
        scanner.useDelimiter(patternBody);
	int n = 1;
	int i = 0;
	int line = 0;
	ps3 = 0;
	Pattern pattern = Pattern.compile(data, Pattern.CASE_INSENSITIVE);
        while(scanner.hasNext()) {
            String content = scanner.nextLine();
	    line++;
	    if(i == 0){
	     i++;
	      continue;
	     }
	    long L00 = System.currentTimeMillis();
		  
		  try {
		  System.in.read();
	      } catch (IOException L01) {
        }
	      
	      long L02 = System.currentTimeMillis();
        System.out.printf("僕は、%.3f秒ほど言葉を失った。", (L02 - L00) / 1000.0);
            Matcher matcher = pattern.matcher(content);
            while(matcher.find()) { 
		
		String str = matcher.replaceAll("[" + data + "]");
                String[] g = content.split(data);
		ps3 = (g.length -1 ) + ps3;
		System.out.println(str);
		System.out.println(line + "行目");
		//System.out.println(s + "が" + n + "回見つかりました" );
		
	    }    
	}
	System.out.println(ps3 + "回みつかった");
    }
}
  b = new JButton("戻る");

 
    JPanel p = new JPanel();
    p.setLayout(new FlowLayout(FlowLayout.CENTER,10,10));
    p.setBackground(Color.white);

    p.add(lab);
    p.add(tf);
    p.add(b);

    c.add(p,BorderLayout.NORTH);
    
    ep = new JEditorPane();
    JScrollPane sp = new JScrollPane(ep);

    c.add(sp,BorderLayout.CENTER);

    tf.addActionListener(this);
    ep.addHyperlinkListener(this);
    b.addActionListener(this);
  }

  public void actionPerformed(ActionEvent ae) {
    if( ae.getSource() == tf ) {
      try {
        String url = tf.getText();
        k++; a[k] = url;
        ep.setPage(url);
        ep.setEditable(false);
      } catch( IOException e ) {
        System.out.println(e);
      }
    }
    if( ae.getSource() == b ) {
      String url;
      if( k > 1 ) { 
        url = a[k-1]; k--;
      } else {
        url = a[1];
      } 
      tf.setText(url);
      try {
        ep.setPage(url);
      } catch( IOException e ) {
        System.out.println(e);
      }
    }
  }

  public void hyperlinkUpdate(HyperlinkEvent he) {
    if( he.getEventType() != HyperlinkEvent.EventType.ACTIVATED ) return;
    String url = he.getURL().toString();
    tf.setText(url);
    k++; a[k] = url;
    try { 
      ep.setPage(url);
    } catch( IOException e ) {
      System.out.println(e);
    }
  }
}
