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

package module.MiningModules.Channel2;		// 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 Channel2 extends MiningModule implements ActionListener
{
	JTextField textfield;
	String path;
	
	JPanel subPanel;
	JButton load,save;
    String buttonNamesInJapanese[];
	String saveText;
	
	public void initializePanel()
	{
		buttonNamesInJapanese = fileReadArray();		
		
		operationPanel.setBackground(Color.RED);		
		
		textfield = new JTextField("URL");
		textfield.addActionListener(this);
		operationPanel.add(textfield);		
		
		subPanel = new JPanel(new GridLayout());
		subPanel.setBackground(Color.RED);		
		
		load = new JButton("Access");
		load.addActionListener(this);
		subPanel.add(load);		
		
		save = new JButton("Save and Execute");
		save.addActionListener(this);
		subPanel.add(save);
		
		operationPanel.add(subPanel);
		
		if(isMenuInJapanese())
		{
			save.setText(buttonNamesInJapanese[0]);
			load.setText(buttonNamesInJapanese[1]);						
		}
	}
	
	public void initializeData()
	{
		path = "";
		saveText = "";
	}
	
	public Channel2()
	{
		setModuleID(28);						// Set your module ID after you have got it
		setCategoryID(6);		
		pairingVisualizationID = new int[]{3,1};
		setToolType(2);
	}
	
	public void miningOperations(int optionNumber)
	{
		switch(optionNumber)
		{
			case 0:
				setDataString("No text");
				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;
				
			case 35:
				text.fileSave(saveText,2);				
				break;
		}
	}
	
	String getUrlText(String path)
	{
		StringWriter sw = new StringWriter();			//For words
		BufferedWriter bw = new BufferedWriter(sw);		
		String line;
		int count;
		
		StringWriter sw2 = new StringWriter();			//For saveText
		BufferedWriter bw2 = new BufferedWriter(sw2);
		
		String title = "<title>(.*)</title>";
        String regex = "(<dt>.*<br><br>)";
        String id = "ID:(.*)<dd>";
		String exid = "<dt>(.*)<dd>(.*)";
		

		
        Pattern p = Pattern.compile(regex);
        Pattern q = Pattern.compile(title);
        Pattern r = Pattern.compile(id);
        Pattern s = Pattern.compile(exid);
		

		
		HttpURLConnection urlConnection;
		
		String comment;
		
		saveText = "";
		count = 0;
		
		try {
            URL url = new URL(path);
			urlConnection = (HttpURLConnection) url.openConnection();
			urlConnection.connect();
			
			BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(),"JISAutoDetect"));//getCharset(urlConnection))
			
			bw.write("<dt>COUNT 0:<br><br>");

			
			
			
			//TITLE
			while ((line = br.readLine()) != null) 
			{
				Matcher m = q.matcher(line);				
				
				if(m.find())
				{
					comment = m.group(1);
					
					bw.write("<H1><span style=color:red;>");
					bw.write(comment);
					bw.write("</span></H1>");
					
					text.focus.setMainFocusString(comment);

					break;
				}
				//System.out.println(line);
            }			
			
			//COMMENT
			while ((line = br.readLine()) != null) 
			{
				Matcher m = p.matcher(line);
				Matcher n = r.matcher(line);
				Matcher o = s.matcher(line);

				if(o.find())
				{
					m.find();					
					
					comment = m.group(1).replaceAll("<dd>","<br>");
					
					bw.write(comment);
					
					comment = o.group(2).replaceAll("<br>",getSentenceTag());
					comment = comment.replaceAll("<a.*?>","");
//					comment = comment.replaceAll("<a.*?>","<pre>");					
					comment = comment.replaceAll("</a>","");					
//					comment = comment.replaceAll("</a>","</pre>");										
					
//					System.out.println((count+1)+":"+comment);
					
					if(count != 0)
						bw2.write(getSegmentTag());					
					bw2.write(comment);
					
					count++;
				}
//				if(n.find())	//ID
//					System.out.println("COUNT "+count+":"+n.group(1));
            }		
			
			bw.flush();
			bw2.flush();
        }
        catch (IOException e) 
		{
            System.err.println(e);
        }
		
		saveText = sw2.toString();
		
		return sw.toString();
	}
	
	//Action listener
	public void actionPerformed(ActionEvent e)
	{
		//		insideOfActionPerformed(e);
		
		if(e.getSource () == textfield)
			miningOperations(25);
		
		if(e.getSource() == save)
			miningOperations(35);
		
		if(e.getSource() == load)
			miningOperations(25);		
	}
}
