Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * DataHarvesterApp.java
- */
- package dataharvester;
- import com.mysql.jdbc.Statement;
- import java.io.IOException;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.List;
- import java.util.ArrayList;
- import java.util.StringTokenizer;
- import org.jdesktop.application.Action;
- import org.jsoup.Jsoup;
- import org.jsoup.nodes.Document;
- import org.jsoup.nodes.Element;
- import org.jsoup.select.Elements;
- import org.jdesktop.application.Application;
- import org.jdesktop.application.SingleFrameApplication;
- /**
- * The main class of the application.
- */
- public class DataHarvesterApp extends SingleFrameApplication {
- public static List<String> extractLinks (String url) throws IOException {
- final ArrayList<String> result = new ArrayList<String>();
- Document doc = Jsoup.connect(url).get();
- Elements links = doc.select("a[href]");
- Elements media = doc.select("[src]");
- Elements imports = doc.select("link[href]");
- // href ...
- for (Element link : links) {
- result.add(link.attr("abs:href"));
- }
- // img ...
- for (Element src : media) {
- result.add(src.attr("abs:src"));
- }
- // js, css, ...
- for (Element link : imports) {
- result.add(link.attr("abs:href"));
- }
- return result;
- }
- public static void linkCounterDB() throws IOException{
- }
- public static void linkCounterLocal() throws IOException {
- //link counter function
- String firstField = DataHarvesterView.fieldFirstLink.getText();
- String lastField = DataHarvesterView.fieldFourthLink.getText();
- int startPoint = Integer.valueOf(DataHarvesterView.fieldSecondLink.getText());
- int endPoint = Integer.valueOf(DataHarvesterView.fieldThirdLink.getText());
- StringBuffer sb = new StringBuffer();
- for(int i = startPoint; i<=endPoint; i++) {
- List<String> links = DataHarvesterApp.extractLinks(firstField + i + lastField);
- for (String link : links) {
- // System.out.println(link);
- sb.append(link+"\n");
- }
- //
- }
- DataHarvesterView.textLinks.setText(sb.toString());
- }
- public static void separateLinksDB() throws IOException{
- databaseCode();
- }
- public static void separateLinksLocal() throws IOException{
- String linksList = DataHarvesterView.fieldLinks.getText().toString();
- StringTokenizer st = new StringTokenizer(linksList);
- StringBuffer sb= new StringBuffer();
- while (st.hasMoreTokens()) {
- //System.out.println(st.nextToken());
- List<String> links = DataHarvesterApp.extractLinks(st.nextToken());
- for (String link : links) {
- // System.out.println(link);
- sb.append(link+"\n");
- }
- }
- DataHarvesterView.textLinks.setText(sb.toString());
- }
- public static void databaseCode() {
- Connection con = null;
- Statement st = null;
- ResultSet rs = null;
- try {
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- con = DriverManager.getConnection("jdbc:mysql:///cyberia",
- "root", "churchill");
- st = (Statement) con.createStatement();
- rs = st.executeQuery("SELECT * FROM items");
- while(rs.next()) {
- int userId = rs.getInt(1);
- String name = rs.getString(2);
- String description = rs.getString(3);
- String licence = rs.getString(4);
- System.out.println(userId + ". " + name + " - " +
- description + " (" + licence + ")");
- }
- } catch (Exception e) {
- System.err.println("Exception: " + e.getMessage());
- } finally {
- try {
- if(rs != null)
- rs.close();
- if(st != null)
- st.close();
- if(con != null)
- con.close();
- } catch (SQLException e) {
- }
- }
- }
- /**
- * At startup create and show the main frame of the application.
- */
- @Override protected void startup() {
- show(new DataHarvesterView(this));
- }
- /**
- * This method is to initialize the specified window by injecting resources.
- * Windows shown in our application come fully initialized from the GUI
- * builder, so this additional configuration is not needed.
- */
- @Override protected void configureWindow(java.awt.Window root) {
- }
- /**
- * A convenient static getter for the application instance.
- * @return the instance of DataHarvesterApp
- */
- public static DataHarvesterApp getApplication() {
- return Application.getInstance(DataHarvesterApp.class);
- }
- /**
- * Main method launching the application.
- */
- public static void main(String[] args) throws Exception{
- launch(DataHarvesterApp.class, args);
- }
- @Action
- public void startListing() throws IOException {
- //case link counter radio buttooon
- if(DataHarvesterView.radioButtonCounter.isSelected()) {
- if(DataHarvesterView.buttonDB.isSelected()) {
- linkCounterDB();
- }
- else if(DataHarvesterView.buttonLocal.isSelected()) {
- linkCounterLocal();
- }
- }
- //case separate pages radio button
- else if(DataHarvesterView.radioButtonList.isSelected()){
- if(DataHarvesterView.buttonDB.isSelected()) {
- separateLinksDB();
- }
- else if(DataHarvesterView.buttonLocal.isSelected()) {
- separateLinksLocal();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement