@Test public void testExtractHyperlinks() { logger.info("extractHyperlinks"); String text = "some text <a href=\"url1\">text1</a> <a href=\"url2\"><img /></a> some other text"; Map<HTMLParser.HyperlinkPart, List<String>> expResult = new HashMap<>(); expResult.put(HTMLParser.HyperlinkPart.HTMLTAG, new ArrayList<>()); expResult.put(HTMLParser.HyperlinkPart.URL, new ArrayList<>()); expResult.put(HTMLParser.HyperlinkPart.ANCHORTEXT, new ArrayList<>()); expResult.get(HTMLParser.HyperlinkPart.HTMLTAG).add("<a href=\"url1\">text1</a>"); expResult.get(HTMLParser.HyperlinkPart.URL).add("url1"); expResult.get(HTMLParser.HyperlinkPart.ANCHORTEXT).add("text1"); expResult.get(HTMLParser.HyperlinkPart.HTMLTAG).add("<a href=\"url2\"><img /></a>"); expResult.get(HTMLParser.HyperlinkPart.URL).add("url2"); expResult.get(HTMLParser.HyperlinkPart.ANCHORTEXT).add("<img />"); Map<HTMLParser.HyperlinkPart, List<String>> result = HTMLParser.extractHyperlinks(text); assertEquals(expResult, result); }
public static Map<HyperlinkPart, List<String>> extractHyperlinks(String html) { Map<HyperlinkPart, List<String>> hyperlinksMap = new HashMap<>(); hyperlinksMap.put(HyperlinkPart.HTMLTAG, new ArrayList<>()); hyperlinksMap.put(HyperlinkPart.URL, new ArrayList<>()); hyperlinksMap.put(HyperlinkPart.ANCHORTEXT, new ArrayList<>()); Matcher m = HYPERLINK_PATTERN.matcher(html); while (m.find()) { if(m.groupCount()==2) { String tag = m.group(0); String url = m.group(1); String anchortext = m.group(2); hyperlinksMap.get(HyperlinkPart.HTMLTAG).add(tag); hyperlinksMap.get(HyperlinkPart.URL).add(url); hyperlinksMap.get(HyperlinkPart.ANCHORTEXT).add(anchortext); } } return hyperlinksMap; }
HTMLParser { public static Map<HyperlinkPart, List<String>> extractHyperlinks(String html) { Map<HyperlinkPart, List<String>> hyperlinksMap = new HashMap<>(); hyperlinksMap.put(HyperlinkPart.HTMLTAG, new ArrayList<>()); hyperlinksMap.put(HyperlinkPart.URL, new ArrayList<>()); hyperlinksMap.put(HyperlinkPart.ANCHORTEXT, new ArrayList<>()); Matcher m = HYPERLINK_PATTERN.matcher(html); while (m.find()) { if(m.groupCount()==2) { String tag = m.group(0); String url = m.group(1); String anchortext = m.group(2); hyperlinksMap.get(HyperlinkPart.HTMLTAG).add(tag); hyperlinksMap.get(HyperlinkPart.URL).add(url); hyperlinksMap.get(HyperlinkPart.ANCHORTEXT).add(anchortext); } } return hyperlinksMap; } }
HTMLParser { public static Map<HyperlinkPart, List<String>> extractHyperlinks(String html) { Map<HyperlinkPart, List<String>> hyperlinksMap = new HashMap<>(); hyperlinksMap.put(HyperlinkPart.HTMLTAG, new ArrayList<>()); hyperlinksMap.put(HyperlinkPart.URL, new ArrayList<>()); hyperlinksMap.put(HyperlinkPart.ANCHORTEXT, new ArrayList<>()); Matcher m = HYPERLINK_PATTERN.matcher(html); while (m.find()) { if(m.groupCount()==2) { String tag = m.group(0); String url = m.group(1); String anchortext = m.group(2); hyperlinksMap.get(HyperlinkPart.HTMLTAG).add(tag); hyperlinksMap.get(HyperlinkPart.URL).add(url); hyperlinksMap.get(HyperlinkPart.ANCHORTEXT).add(anchortext); } } return hyperlinksMap; }  }
HTMLParser { public static Map<HyperlinkPart, List<String>> extractHyperlinks(String html) { Map<HyperlinkPart, List<String>> hyperlinksMap = new HashMap<>(); hyperlinksMap.put(HyperlinkPart.HTMLTAG, new ArrayList<>()); hyperlinksMap.put(HyperlinkPart.URL, new ArrayList<>()); hyperlinksMap.put(HyperlinkPart.ANCHORTEXT, new ArrayList<>()); Matcher m = HYPERLINK_PATTERN.matcher(html); while (m.find()) { if(m.groupCount()==2) { String tag = m.group(0); String url = m.group(1); String anchortext = m.group(2); hyperlinksMap.get(HyperlinkPart.HTMLTAG).add(tag); hyperlinksMap.get(HyperlinkPart.URL).add(url); hyperlinksMap.get(HyperlinkPart.ANCHORTEXT).add(anchortext); } } return hyperlinksMap; }  static String replaceImgWithAlt(String html); static String removeComments(String html); static String unsafeRemoveAllTags(String html); static String safeRemoveAllTags(String html); static String removeNonTextTagsAndAttributes(String html); static String extractText(String html); static String extractTitle(String html); static Map<HyperlinkPart, List<String>> extractHyperlinks(String html); static Map<String, String> extractMetatags(String html); static Map<String, List<String>> extractHTMLheaders(String html); }
HTMLParser { public static Map<HyperlinkPart, List<String>> extractHyperlinks(String html) { Map<HyperlinkPart, List<String>> hyperlinksMap = new HashMap<>(); hyperlinksMap.put(HyperlinkPart.HTMLTAG, new ArrayList<>()); hyperlinksMap.put(HyperlinkPart.URL, new ArrayList<>()); hyperlinksMap.put(HyperlinkPart.ANCHORTEXT, new ArrayList<>()); Matcher m = HYPERLINK_PATTERN.matcher(html); while (m.find()) { if(m.groupCount()==2) { String tag = m.group(0); String url = m.group(1); String anchortext = m.group(2); hyperlinksMap.get(HyperlinkPart.HTMLTAG).add(tag); hyperlinksMap.get(HyperlinkPart.URL).add(url); hyperlinksMap.get(HyperlinkPart.ANCHORTEXT).add(anchortext); } } return hyperlinksMap; }  static String replaceImgWithAlt(String html); static String removeComments(String html); static String unsafeRemoveAllTags(String html); static String safeRemoveAllTags(String html); static String removeNonTextTagsAndAttributes(String html); static String extractText(String html); static String extractTitle(String html); static Map<HyperlinkPart, List<String>> extractHyperlinks(String html); static Map<String, String> extractMetatags(String html); static Map<String, List<String>> extractHTMLheaders(String html);  }