package to.specular.elementposition;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import net.htmlparser.jericho.*;
import java.net.*;
import java.io.*;
import java.util.*;
public class init extends JFrame {
private JLabel lblKeyword;
private JTextField keyword;
private JButton exec;
private JScrollPane sp;
private JTextArea output;
public init(){
this.setTitle("HTML Element Position");
this.setResizable(true);
this.setLayout(new GridBagLayout());
GridBagConstraints gbc;
this.lblKeyword = new JLabel("Search for:");
gbc=makeGBC(0, 0, 1, 1);
gbc.anchor = GridBagConstraints.WEST;
this.add(this.lblKeyword,gbc);
this.keyword = new JTextField();
this.keyword.setText("test");
gbc=makeGBC(0, 1, 1, 1);
gbc.fill=GridBagConstraints.HORIZONTAL;
this.add(this.keyword, gbc);
this.exec = new JButton("Execute");
gbc=makeGBC(0, 2, 0, 1);
gbc.anchor = GridBagConstraints.EAST;
gbc.fill=GridBagConstraints.BOTH;
gbc.weightx=1.0;
gbc.weighty=0.1;
this.add(this.exec, gbc);
this.output = new JTextArea();
gbc=makeGBC(0, 3, 1, 2);
gbc.fill=GridBagConstraints.BOTH;
gbc.weightx=1.0;
gbc.weighty=1.0;
this.add(this.output, gbc);
this.sp = new JScrollPane(this.output);
this.sp.setPreferredSize(new Dimension(500,100));
gbc = makeGBC(0, 3, 1, 1);
gbc.fill=GridBagConstraints.BOTH;
gbc.weightx=1.0;
gbc.weighty=1.0;
this.add(this.sp, gbc);
this.exec.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
String pos = getPosition();
System.out.println("Position: "+pos);
output.setText( pos );
}
});
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.setPreferredSize(new Dimension(700,600));
this.pack();
this.setLocation
(
(Toolkit.getDefaultToolkit().getScreenSize().width-this.getWidth())/2,
(Toolkit.getDefaultToolkit().getScreenSize().height-this.getHeight())/2
);
this.setVisible(true);
}
private String getPosition()
{
String urlString = "";
String result="";
if( !this.keyword.getText().isEmpty() )
{
URL url;
URLConnection uc;
StringBuilder parsedContentFromUrl = new StringBuilder();
//File is UTF-8 and HTML charset UTF-8
//urlString = "http://devcon.pcriot.com/cube/javahtmlparsing/file-utf8_html-utf8.html";
//File is ANSI and HTML charset windows-1250
//urlString = "http://devcon.pcriot.com/cube/javahtmlparsing/file-ansi_html-windows1250.html";
//File is ISO 8859-2 and HTML charset ISO 8859-1
urlString = "http://devcon.pcriot.com/cube/javahtmlparsing/file-iso88592_html-iso88591.html";
//
MicrosoftConditionalCommentTagTypes.register();
PHPTagTypes.register();
PHPTagTypes.PHP_SHORT.deregister();
MasonTagTypes.register();
try{
url = new URL( urlString );
uc = url.openConnection();
uc.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20100101 Firefox/5.0");
uc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
uc.connect();
uc.getInputStream();
BufferedInputStream in = new BufferedInputStream(uc.getInputStream());
int ch;
while ((ch = in.read()) != -1) {
parsedContentFromUrl.append((char) ch);
}
Source source = new Source( parsedContentFromUrl );
//result = source.getRenderer().toString();
result = source.toString();
}
catch (Exception e) {
result = e.toString();
}
//
return result;
}else{
return "No keyword given";
}
}
private GridBagConstraints makeGBC(int gx, int gy, int gw, int gh)
{
GridBagConstraints gbc=new GridBagConstraints();
gbc.gridx=gx;
gbc.gridy=gy;
gbc.gridwidth=gw;
gbc.gridheight=gh;
gbc.fill=GridBagConstraints.NONE;
gbc.weightx=0;
gbc.weighty=0;
gbc.anchor=GridBagConstraints.CENTER;
gbc.insets=new Insets(2,2,2,2);
return gbc;
}
public static void main(String[] args) {
try {
for (LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(laf.getName())) {
UIManager.setLookAndFeel(laf.getClassName());
break;
}
}
} catch (Exception e) {
// If Nimbus is not available, you can set the GUI to another look and feel.
}
new init();
}
}