
//
// Mining module for TETDM 
// UrlAccess.java Version 0.58
// This is one of the sample modules.
// You should read the README file.
//

package module.MiningModules.UrlAccess;		// Replace ALL [MiningStyle] with your [module name]

import source.*;
import source.TextData.*;
//import source.Utility.*;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

import java.io.*;
import java.net.*;

import java.util.regex.*;

public class UrlAccess extends MiningModule implements ActionListener
{
	JPanel subPanel;
	JToggleButton utf8, eucjis;
	ButtonGroup encoding;
	
	JTextField textfield;	
	String path = "";

	
	public void initializePanel()
	{
		subPanel = new JPanel(new GridLayout());
		
		utf8 = new JToggleButton("UTF-8");
		eucjis = new JToggleButton("EUC/SHIFT JIS");		
		
		
		textfield = new JTextField("URL");
		
		operationPanel.setBackground(Color.RED);
		subPanel.setBackground(Color.RED);
		
		utf8.setBackground(Color.RED);
		utf8.addActionListener(this);
		subPanel.add(utf8);
//		operationPanel.add(utf8);
		
		eucjis.setBackground(Color.RED);
		eucjis.addActionListener(this);
		subPanel.add(eucjis);		
//		operationPanel.add(eucjis);	
		
		operationPanel.add(subPanel);
		
		textfield.addActionListener(this);
		operationPanel.add(textfield);
		
		
		encoding = new ButtonGroup();
		encoding.add(utf8);
		encoding.add(eucjis);
		
		utf8.setSelected(true);
	}	
	
	public UrlAccess()
	{
		setModuleID(27);						// Set your module ID after you have got it
		setCategoryID(10);		
		pairingVisualizationID = new int[]{3,1};
		setToolType(2);
	}
	
	public void miningOperations(int optionNumber)
	{
		switch(optionNumber)
		{
			case 0:	
				break;
				
			case 1:
				resetData();	//dataID = 0;
				if(path.indexOf("http") == 0 || path.indexOf("HTTP") == 0 )
					setDataString(getUrlText(path));
				else
					textfield.setText("URL");
				displayOperations(0);				
				break;				
				
			case 25:
				resetData();	//dataID = 0;			
				path = textfield.getText();				
				if(path.indexOf("http") == 0 || path.indexOf("HTTP") == 0 )
					setDataString(getUrlText(path));
				else
					textfield.setText("URL");				
				displayOperations(0);
				break;					
		}
	}

	String getUrlText(String path)
	{
		StringWriter sw = new StringWriter();			//For words
		BufferedWriter bw = new BufferedWriter(sw);
		BufferedReader br = null;
		String line;
		
		HttpURLConnection urlConnection;
		
		try {
            URL url = new URL(path);
			urlConnection = (HttpURLConnection) url.openConnection();
			urlConnection.connect();
			
			if(utf8.isSelected())
				br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(),"UTF-8"));
			
			if(eucjis.isSelected())
				br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(),"JISAutoDetect"));

			if(br == null)
				return "";
				
			while ((line = br.readLine()) != null) 
			{
				bw.write(line);
                   //System.out.println(line);
            }

			bw.flush();
        }
        catch (IOException e) 
		{
            System.err.println(e);
        }
		
		return sw.toString();
	}

	//Action listener
	public void actionPerformed(ActionEvent e)
	{
		writeButtonLog(e);
		//		insideOfActionPerformed(e);
		
		if(e.getSource () == textfield)
			miningOperations(25);		
	}
}
