Advertisement
utroz

Untitled

May 21st, 2011
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.54 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.Desktop;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5.  
  6. public class Testxl {  
  7.     static String menu;
  8.     static String title = "DownsLink 0.3";
  9.  
  10.     static void openUrl(String link) {
  11.         Desktop desk = java.awt.Desktop.getDesktop();    
  12.         try {    
  13.             desk.browse(new java.net.URI(link));    
  14.         } catch (Exception e) {    
  15.             e.printStackTrace();    
  16.         }
  17.     }
  18.  
  19.     static void startSystem(){
  20.  
  21.             String user = System.getProperty("user.name");
  22.  
  23.     try {
  24.         menu = "Software Info:\nDeveloped by: Utroz(RSC)\nE-mail: [email protected]\nBlog: www.GCoders.info\n---------------------\n\nHello "+user+",\n\n[Menu Options]:\nSearch link and show: [1]\nSearch link and open: [2]\nText reverse: [3]\nCatch Links: [4]\nExit system: [5]\n---------------------\n\nPlease input a option:";
  25.  
  26.         JTextArea area = new JTextArea(menu);
  27.  
  28.         JScrollPane scroll = new JScrollPane(area, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);  
  29.         scroll.setPreferredSize(new Dimension(300,300));
  30.         int options = Integer.parseInt(JOptionPane.showInputDialog(null,scroll, title, JOptionPane.PLAIN_MESSAGE));
  31.  
  32.  
  33.         switch(options){
  34.             case 1:
  35.                 newUrl(false);
  36.                 break;
  37.             case 2:
  38.                 newUrl(true);
  39.                 break;
  40.             case 3: reverseText();
  41.                 break;
  42.             case 4: searchLink();
  43.                 break;
  44.             case 5:
  45.                 JOptionPane.showMessageDialog(null, "Thanks for use the system!");
  46.                 System.exit(0);    
  47.            
  48.         }
  49.     } catch (NumberFormatException ex){
  50.         startSystem();
  51.     }
  52.     }
  53.  
  54.     static void reverseText(){
  55.         String reverse = "";
  56.  
  57.         String link = JOptionPane.showInputDialog(null, "Developed by: Utroz(RSC)\nE-mail: [email protected]\n\nPlease input a Text to be reverse:", title, JOptionPane.PLAIN_MESSAGE);
  58.  
  59.         for(int i = link.length()-1; i >= 0; i--){
  60.             reverse += link.charAt(i);
  61.         }
  62.  
  63.         JTextArea area = new JTextArea("Reverse Text: \n"+reverse+"");  
  64.         JScrollPane scroll = new JScrollPane(area, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);  
  65.         scroll.setPreferredSize(new Dimension(300,300));
  66.         JOptionPane.showMessageDialog(null,scroll);
  67.         startSystem();
  68.     }
  69.  
  70.     static void searchLink () {
  71.         String linkx = JOptionPane.showInputDialog(null, "Developed by: Utroz(RSC)\nE-mail: [email protected]\n\nPlease input a URL to Links catch:\nExample Format: [http://www.google.com.br]", title, JOptionPane.PLAIN_MESSAGE);
  72.    
  73.         HTMLUtils.start(linkx);
  74.         startSystem();
  75.     }      
  76.          
  77.  
  78.     static void newUrl(boolean arg){
  79.     try {
  80.         String link;
  81.  
  82.         if (arg == true){
  83.             link = JOptionPane.showInputDialog(null, "Developed by: Utroz(RSC)\nE-mail: [email protected]\n\nPlease input a Link (Browser) to be open:", title, JOptionPane.PLAIN_MESSAGE);
  84.         } else {
  85.             link = JOptionPane.showInputDialog(null, "Developed by: Utroz(RSC)\nE-mail: [email protected]\n\nPlease input a Link (Browser) to be revert:", title, JOptionPane.PLAIN_MESSAGE);
  86.         }          
  87.  
  88.         boolean isTrue = false;
  89.  
  90.         String url = "", newLink = "", revertLink = "", urlB = "";
  91.         for(int i = 0; i < link.length(); i++){
  92.  
  93.             if(link.charAt(i) == 117){
  94.                 url += link.charAt(i);
  95.                 url += link.charAt(i + 1);
  96.                 url += link.charAt(i + 2);
  97.                 url += link.charAt(i + 3);
  98.  
  99.             }
  100.             if(url.equals("url=")){ // search for 'url='.
  101.                 i = i + 4;
  102.  
  103.                 for(int x = i; x < link.length(); x++){
  104.                     if(link.charAt(x) == 61){
  105.                         urlB += link.charAt(x);
  106.                         urlB += link.charAt(x + 1);
  107.                         urlB += link.charAt(x + 2);
  108.                         urlB += link.charAt(x + 3);    
  109.                     }
  110.                     if(urlB.equals("=lru")){ // search for '=lru'.
  111.                         break;
  112.                     }
  113.                     else {
  114.                         urlB = "";
  115.                     }
  116.                     newLink += link.charAt(x); // catch link after 'url'.
  117.                 }
  118.                 isTrue = true;             
  119.                 break;
  120.             } else {
  121.                 url = "";
  122.                 isTrue = false;
  123.             }
  124.         }
  125.  
  126.         if(isTrue == true){
  127.             for(int revert = newLink.length()-1; revert >= 0; revert--){ // reverse link.
  128.                 revertLink += newLink.charAt(revert);
  129.             }
  130.  
  131.             if(arg == true){
  132.                 JOptionPane.showMessageDialog(null, "Link was opened sucessfully!");
  133.                 openUrl(revertLink);
  134.             } else {
  135.                 JOptionPane.showMessageDialog(null, "Link was reverted sucessfully!");
  136.  
  137.                 JTextArea area = new JTextArea("Reverse Link: \n"+revertLink+"");  
  138.                 JScrollPane scroll = new JScrollPane(area, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);  
  139.                 scroll.setPreferredSize(new Dimension(300,300));
  140.                 JOptionPane.showMessageDialog(null,scroll);
  141.             }
  142.  
  143.         } else if (isTrue == false){
  144.             JOptionPane.showMessageDialog(null, "Error: Wrong Link!", "Message:", JOptionPane.ERROR_MESSAGE);
  145.         }
  146.         startSystem();
  147.     } catch(IndexOutOfBoundsException ex){
  148.             JOptionPane.showMessageDialog(null, "Error: Wrong Link!", "Message:", JOptionPane.ERROR_MESSAGE);  
  149.             startSystem(); 
  150.     }
  151.     }
  152.  
  153.     public static void main (String[] args) {
  154.  
  155.             startSystem();
  156.  
  157.     }
  158. }
  159.  
  160.  
  161. import java.io.IOException;
  162. import java.util.ArrayList;
  163. import javax.swing.*;
  164.  
  165. import org.jsoup.nodes.Document;
  166. import org.jsoup.Jsoup;
  167. import org.jsoup.nodes.Element;
  168. import org.jsoup.select.Elements;
  169.  
  170. import java.awt.*;
  171. import java.awt.event.*;
  172.  
  173. public class HTMLUtils {
  174.  
  175.     public static void start(String url) {
  176.  
  177.         extractLinks(url);
  178.     }
  179.  
  180.     static void extractLinks (String url){
  181.  
  182.     try{
  183.         ArrayList <String> pageLinks = new ArrayList<String>();
  184.  
  185.         Document doc = Jsoup.connect(url).get();       
  186.         Elements links = doc.select("a[href]");
  187.  
  188.         String codeFont;
  189.         StringBuffer newStr = new StringBuffer ();
  190.         newStr.setLength(0);
  191.  
  192.         for(Element link : links){
  193.             codeFont = link.toString(); // take Code Font on the page.
  194.            
  195.             StringBuffer str = new StringBuffer();
  196.             str.setLength(0);          
  197.  
  198.             for(int i = 0; i < codeFont.length(); i++){
  199.                 if(codeFont.charAt(i) == 'h'){
  200.                     str.append(codeFont.charAt(i)); // h
  201.                     str.append(codeFont.charAt(i+1)); // r
  202.                     str.append(codeFont.charAt(i+2)); // e
  203.                     str.append(codeFont.charAt(i+3)); // f
  204.        
  205.                     if(str.toString().equals("href")){
  206.                         i += 6; /* salt 3 positions on string. href=" */
  207.                         for(int it = i; it < codeFont.length(); it++){
  208.  
  209.                             if((codeFont.charAt(it) == '"') || (codeFont.charAt(it) == ' ')){
  210.                                
  211.                                 if(codeFont.charAt(it) == '"'){
  212.                                     str.setLength(0);
  213.                                     str.append(codeFont.charAt(it));
  214.                                     str.append(codeFont.charAt(it + 1));
  215.  
  216.                                     if(str.toString().equals("\">")){  // ">
  217.                                    
  218.                                         pageLinks.add(newStr.toString()); // add to arrayList.
  219.                                         newStr.setLength(0);
  220.                                         str.setLength(0);
  221.                                         break;
  222.                                     }
  223.                                 } else if(codeFont.charAt(it) == ' '){
  224.                                     pageLinks.add(newStr.toString()); // add to arrayList.
  225.                                     newStr.setLength(0);
  226.                                     str.setLength(0);
  227.                                     break; 
  228.                                 }                      
  229.                             } else {
  230.                                 newStr.append(codeFont.charAt(it));
  231.                             }
  232.  
  233.                         }
  234.  
  235.                     } else {
  236.                         str.setLength(0);
  237.                         continue;
  238.                     }
  239.                 } else {
  240.                     continue;
  241.                 }
  242.             } // for end.
  243.  
  244.  
  245.         } // for end.          
  246.         printArray(pageLinks); // send arrayList
  247.     }catch(IOException ex){
  248.  
  249.     }
  250.  
  251.     }
  252.  
  253.     static void printArray (ArrayList <String> arg){
  254.         StringBuffer print = new StringBuffer(); // final print.
  255.        
  256.         for(int i = 0; i < arg.size(); i++){
  257.            
  258.             print.append(arg.get(i));
  259.             print.append("\n");
  260.         }
  261.  
  262.         JTextArea area = new JTextArea(print.toString());  
  263.         JScrollPane scroll = new JScrollPane(area, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);  
  264.         scroll.setPreferredSize(new Dimension(640,480));
  265.         JOptionPane.showMessageDialog(null,scroll);
  266.            
  267.     }
  268.  
  269. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement