Advertisement
Guest User

Untitled

a guest
Aug 12th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.75 KB | None | 0 0
  1. /*
  2. * DataHarvesterApp.java
  3. */
  4.  
  5. package dataharvester;
  6.  
  7. import com.mysql.jdbc.Statement;
  8. import java.io.IOException;
  9. import java.sql.Connection;
  10. import java.sql.DriverManager;
  11. import java.sql.ResultSet;
  12. import java.sql.SQLException;
  13. import java.util.List;
  14. import java.util.ArrayList;
  15. import java.util.StringTokenizer;
  16. import org.jdesktop.application.Action;
  17.  
  18. import org.jsoup.Jsoup;
  19. import org.jsoup.nodes.Document;
  20. import org.jsoup.nodes.Element;
  21. import org.jsoup.select.Elements;
  22. import org.jdesktop.application.Application;
  23. import org.jdesktop.application.SingleFrameApplication;
  24.  
  25. /**
  26. * The main class of the application.
  27. */
  28. public class DataHarvesterApp extends SingleFrameApplication {
  29.  
  30. public static List<String> extractLinks (String url) throws IOException {
  31. final ArrayList<String> result = new ArrayList<String>();
  32.  
  33. Document doc = Jsoup.connect(url).get();
  34.  
  35. Elements links = doc.select("a[href]");
  36. Elements media = doc.select("[src]");
  37. Elements imports = doc.select("link[href]");
  38.  
  39. // href ...
  40. for (Element link : links) {
  41. result.add(link.attr("abs:href"));
  42. }
  43.  
  44. // img ...
  45. for (Element src : media) {
  46. result.add(src.attr("abs:src"));
  47. }
  48.  
  49. // js, css, ...
  50. for (Element link : imports) {
  51. result.add(link.attr("abs:href"));
  52. }
  53. return result;
  54. }
  55.  
  56. public static void linkCounterDB() throws IOException{
  57.  
  58. }
  59.  
  60. public static void linkCounterLocal() throws IOException {
  61. //link counter function
  62. String firstField = DataHarvesterView.fieldFirstLink.getText();
  63. String lastField = DataHarvesterView.fieldFourthLink.getText();
  64. int startPoint = Integer.valueOf(DataHarvesterView.fieldSecondLink.getText());
  65. int endPoint = Integer.valueOf(DataHarvesterView.fieldThirdLink.getText());
  66. StringBuffer sb = new StringBuffer();
  67.  
  68. for(int i = startPoint; i<=endPoint; i++) {
  69. List<String> links = DataHarvesterApp.extractLinks(firstField + i + lastField);
  70. for (String link : links) {
  71. // System.out.println(link);
  72. sb.append(link+"\n");
  73. }
  74. //
  75. }
  76.  
  77. DataHarvesterView.textLinks.setText(sb.toString());
  78.  
  79. }
  80.  
  81. public static void separateLinksDB() throws IOException{
  82. databaseCode();
  83.  
  84. }
  85.  
  86. public static void separateLinksLocal() throws IOException{
  87. String linksList = DataHarvesterView.fieldLinks.getText().toString();
  88. StringTokenizer st = new StringTokenizer(linksList);
  89. StringBuffer sb= new StringBuffer();
  90.  
  91. while (st.hasMoreTokens()) {
  92. //System.out.println(st.nextToken());
  93. List<String> links = DataHarvesterApp.extractLinks(st.nextToken());
  94.  
  95. for (String link : links) {
  96. // System.out.println(link);
  97. sb.append(link+"\n");
  98. }
  99. }
  100.  
  101. DataHarvesterView.textLinks.setText(sb.toString());
  102.  
  103. }
  104.  
  105. public static void databaseCode() {
  106. Connection con = null;
  107. Statement st = null;
  108. ResultSet rs = null;
  109.  
  110. try {
  111. Class.forName("com.mysql.jdbc.Driver").newInstance();
  112. con = DriverManager.getConnection("jdbc:mysql:///cyberia",
  113. "root", "churchill");
  114.  
  115. st = (Statement) con.createStatement();
  116. rs = st.executeQuery("SELECT * FROM items");
  117.  
  118. while(rs.next()) {
  119. int userId = rs.getInt(1);
  120. String name = rs.getString(2);
  121. String description = rs.getString(3);
  122. String licence = rs.getString(4);
  123.  
  124. System.out.println(userId + ". " + name + " - " +
  125. description + " (" + licence + ")");
  126. }
  127.  
  128. } catch (Exception e) {
  129. System.err.println("Exception: " + e.getMessage());
  130. } finally {
  131. try {
  132. if(rs != null)
  133. rs.close();
  134. if(st != null)
  135. st.close();
  136. if(con != null)
  137. con.close();
  138. } catch (SQLException e) {
  139. }
  140. }
  141. }
  142.  
  143. /**
  144. * At startup create and show the main frame of the application.
  145. */
  146. @Override protected void startup() {
  147. show(new DataHarvesterView(this));
  148. }
  149.  
  150. /**
  151. * This method is to initialize the specified window by injecting resources.
  152. * Windows shown in our application come fully initialized from the GUI
  153. * builder, so this additional configuration is not needed.
  154. */
  155. @Override protected void configureWindow(java.awt.Window root) {
  156. }
  157.  
  158. /**
  159. * A convenient static getter for the application instance.
  160. * @return the instance of DataHarvesterApp
  161. */
  162. public static DataHarvesterApp getApplication() {
  163. return Application.getInstance(DataHarvesterApp.class);
  164. }
  165.  
  166. /**
  167. * Main method launching the application.
  168. */
  169. public static void main(String[] args) throws Exception{
  170. launch(DataHarvesterApp.class, args);
  171.  
  172.  
  173. }
  174.  
  175. @Action
  176. public void startListing() throws IOException {
  177.  
  178. //case link counter radio buttooon
  179. if(DataHarvesterView.radioButtonCounter.isSelected()) {
  180. if(DataHarvesterView.buttonDB.isSelected()) {
  181. linkCounterDB();
  182. }
  183. else if(DataHarvesterView.buttonLocal.isSelected()) {
  184. linkCounterLocal();
  185. }
  186. }
  187. //case separate pages radio button
  188. else if(DataHarvesterView.radioButtonList.isSelected()){
  189. if(DataHarvesterView.buttonDB.isSelected()) {
  190. separateLinksDB();
  191. }
  192. else if(DataHarvesterView.buttonLocal.isSelected()) {
  193. separateLinksLocal();
  194. }
  195. }
  196.  
  197.  
  198.  
  199.  
  200. }
  201.  
  202.  
  203.  
  204.  
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement