Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2014
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.07 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package mhd;
  7.  
  8. import java.io.IOException;
  9. import java.net.MalformedURLException;
  10. import java.net.URL;
  11. import java.util.ArrayList;
  12. import java.util.List;
  13. import org.jsoup.Jsoup;
  14. import org.jsoup.nodes.Document;
  15. import org.jsoup.nodes.Element;
  16. import org.jsoup.select.Elements;
  17.  
  18. /**
  19.  *
  20.  * @author martrin100
  21.  */
  22. public class Parser2 {
  23.      public static void main(String[] args) throws IOException {
  24.  
  25.         String url19 = "http://imhd.zoznam.sk/ke/cestovny-poriadok/linka/19/smer/Nad-jazerom-Napajadla/zastavka/KVP-klastor/60436.html";
  26.         Parser2 p = new Parser2(url19);
  27.         List<String> columnTitles = p.getColumnTitles();
  28.         System.out.println(p.parseColumn(1));
  29.         System.out.println("titles:" + columnTitles);
  30.         for (int i = 0; i < columnTitles.size(); i++) {
  31.             System.out.printf("stlpec:%s casy:%s\n", columnTitles.get(i),p.parseColumn(i));
  32.         }
  33.      }
  34.  
  35.     private final String url;
  36.     private Document doc;
  37.  
  38.     public Parser2(String url) {
  39.         this.url = url;
  40.         try {
  41.             doc = Jsoup.parse(new URL(url), 1000 * 5);
  42.         } catch (MalformedURLException ex) {
  43.             throw new IllegalArgumentException(String.format("Mallformed URL:%s", url));
  44.         } catch (IOException ex) {
  45.             throw new IllegalArgumentException(String.format("IO excception occured while downloading:%s", url));
  46.         }
  47.     }
  48.  
  49.     public static class Zastavka {
  50.  
  51.         public final String meno;
  52.         public final int cas;
  53.  
  54.         public Zastavka(String meno, int cas) {
  55.             this.meno = meno;
  56.             this.cas = cas;
  57.         }
  58.  
  59.     }
  60.  
  61.     public static class Cas {
  62.  
  63.         public final int hodina;
  64.         public final int minuta;
  65.  
  66.         public Cas(int hodina, int minuta) {
  67.             this.hodina = hodina;
  68.             this.minuta = minuta;
  69.         }
  70.  
  71.         @Override
  72.         public String toString() {
  73.            return String.format("%02d:%02d",hodina, minuta);
  74.         }
  75.        
  76.        
  77.  
  78.     }
  79.  
  80.     private Element getBase(int index) {
  81.         return doc.getElementsByClass("cp_obsah").get(0).getElementsByTag("tr").get(index);
  82.     }
  83.  
  84.     public List<String> getColumnTitles() {
  85.         List<String> result = new ArrayList<>();
  86.         for (Element el : getBase(0).getElementsByTag("td")) {
  87.             result.add(el.text());
  88.         }
  89.         return result;
  90.     }
  91.  
  92.     public List<Cas> parseColumn(int column) {
  93.         Element base = getBase(1).getElementsByClass("cp_odchody_tabulka").get(column);
  94.         List<Cas> result = new ArrayList<>();
  95.         for (Element hodina : base.getElementsByClass("cp_odchody")) {
  96.             result.addAll(parseHodina(hodina));
  97.         }
  98.         return result;
  99.     }
  100.  
  101.     private List<Cas> parseHodina(Element cpOdchody) {
  102.         Elements odchody = cpOdchody.getElementsByTag("td");
  103.         Element odchod = cpOdchody.getElementsByClass("cp_hodina").get(0);
  104.         odchody.remove(odchod);
  105.         int hodina = Integer.parseInt(odchod.text());
  106.         List<Cas> result = new ArrayList<>();
  107.         for (Element element : filterMinuty(odchody)) {
  108.             int minuta = Integer.parseInt(element.text());
  109.             result.add(new Cas(hodina, minuta));
  110.         }
  111.         return result;
  112.     }
  113.  
  114.     private List<Element> filterMinuty(List<Element> elements) {
  115.         List<Element> result = new ArrayList<>();
  116.         for (Element element : elements) {
  117.             if (!isEmpty(element.text())) {
  118.                 result.add(element);
  119.             }
  120.         }
  121.         return result;
  122.     }
  123.  
  124.     private boolean isEmpty(String str) {
  125.         if (str == null || str.isEmpty()) {
  126.             return true;
  127.         }
  128.         boolean whitespacesOnly = true;
  129.         for (Character ch : str.toCharArray()) {
  130.             whitespacesOnly = whitespacesOnly && Character.isWhitespace(ch);
  131.         }
  132.         return whitespacesOnly;
  133.     }
  134.  
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement