|
| 1 | +/** |
| 2 | + * // This is the HtmlParser's API interface. |
| 3 | + * // You should not implement it, or speculate about its implementation |
| 4 | + * interface HtmlParser { |
| 5 | + * public List<String> getUrls(String url) {} |
| 6 | + * } |
| 7 | + */ |
| 8 | +importjava.net.URI; |
| 9 | +importjava.net.URISyntaxException; |
| 10 | + |
| 11 | + |
| 12 | +classSolution { |
| 13 | +publicList<String>crawl(StringstartUrl,HtmlParserhtmlParser) { |
| 14 | +ResultRecordresultRecord =newResultRecord(); |
| 15 | +CrawlTasktask =newCrawlTask( |
| 16 | +startUrl,htmlParser,resultRecord,UrlUtil.parseHostname(startUrl)); |
| 17 | +try { |
| 18 | +task.start(); |
| 19 | +task.join(); |
| 20 | + }catch (InterruptedExceptione) { |
| 21 | +e.printStackTrace(); |
| 22 | + } |
| 23 | +returnresultRecord.getResultList(); |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +classCrawlTaskextendsThread { |
| 28 | + |
| 29 | +privatefinalStringurl; |
| 30 | +privatefinalHtmlParserhtmlParser; |
| 31 | +privatefinalResultRecordresultRecord; |
| 32 | +privatefinalStringparentHost; |
| 33 | + |
| 34 | +publicCrawlTask(Stringurl, |
| 35 | +HtmlParserhtmlParser, |
| 36 | +ResultRecordresultRecord, |
| 37 | +StringparentHost) { |
| 38 | +this.url =url; |
| 39 | +this.htmlParser =htmlParser; |
| 40 | +this.resultRecord =resultRecord; |
| 41 | +this.parentHost =parentHost; |
| 42 | + } |
| 43 | + |
| 44 | +publicvoidrun() { |
| 45 | +Stringhostname =UrlUtil.parseHostname(url); |
| 46 | +if (!hostname.equals(parentHost)) { |
| 47 | +return; |
| 48 | + } |
| 49 | +if (resultRecord.addIfNotExists(url)) { |
| 50 | +List<String>childUrls =htmlParser.getUrls(url); |
| 51 | +List<CrawlTask>tasks =newArrayList<>(); |
| 52 | +for (StringchildUrl :childUrls) { |
| 53 | +tasks.add(newCrawlTask( |
| 54 | +childUrl,htmlParser,resultRecord,parentHost)); |
| 55 | + } |
| 56 | +try { |
| 57 | +for (CrawlTasktask :tasks) { |
| 58 | +task.start(); |
| 59 | + } |
| 60 | +for (CrawlTasktask :tasks) { |
| 61 | +task.join(); |
| 62 | + } |
| 63 | + }catch (InterruptedExceptione) { |
| 64 | +e.printStackTrace(); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +classUrlUtil { |
| 71 | + |
| 72 | +publicstaticStringparseHostname(Stringurl) { |
| 73 | +try { |
| 74 | +URIuri =newURI(url); |
| 75 | +returnuri.getHost(); |
| 76 | + }catch(URISyntaxExceptione) { |
| 77 | +e.printStackTrace(); |
| 78 | + } |
| 79 | +returnnull; |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +classResultRecord { |
| 84 | + |
| 85 | +privateSet<String>urls; |
| 86 | +privateSemaphoremutex; |
| 87 | + |
| 88 | +publicResultRecord() { |
| 89 | +this.urls =newHashSet<>(); |
| 90 | +this.mutex =newSemaphore(1); |
| 91 | + } |
| 92 | + |
| 93 | +publicbooleanaddIfNotExists(Stringurl) { |
| 94 | +try { |
| 95 | +this.mutex.acquire(); |
| 96 | +booleanadded =this.urls.add(url); |
| 97 | +this.mutex.release(); |
| 98 | +returnadded; |
| 99 | + }catch (InterruptedExceptione) { |
| 100 | +e.printStackTrace(); |
| 101 | + } |
| 102 | +returnfalse; |
| 103 | + } |
| 104 | + |
| 105 | +publicList<String>getResultList() { |
| 106 | +returnnewArrayList<>(urls); |
| 107 | + } |
| 108 | +} |