SubDomainDynamicExtractor extends DomainValueDynamicExtractor { public String extract(AttributeDetail attributeDetail, String url, Document articleDoc) throws Exception { String domain = super.extract(attributeDetail, url, articleDoc); if(domain.equals(UNKOWN_DOMAN)){ return null; } else { String[] suffixList = {".com", ".co.uk", ".cat", ".it"}; String domainWithoutSuffix = null; for(String suffix : suffixList){ if(domain.contains(suffix)){ domainWithoutSuffix = domain.replaceFirst(suffix, ""); break; } } String[] prefixList = {"www."}; for(String prefix : prefixList){ if(domain.contains(prefix)){ domainWithoutSuffix = domainWithoutSuffix.replaceFirst(prefix, ""); break; } } if(domainWithoutSuffix == null || domainWithoutSuffix.isEmpty()) return null; String[] domainSplit = domainWithoutSuffix.split("\\."); int subdomainPosition = 1; if(!attributeDetail.extractor_args.isEmpty()) subdomainPosition = Integer.parseInt(attributeDetail.extractor_args.get(0)); if(domainSplit.length <= subdomainPosition) return null; return domainSplit[subdomainPosition-1]; } }  String extract(AttributeDetail attributeDetail, String url, Document articleDoc);  }
@Test public void shouldReturnNullForNormalURL() throws Exception{ String url = "www.google.com"; AttributeDetail detail = new AttributeDetail(); detail.extractor_args = Arrays.asList("1"); SubDomainDynamicExtractor extr = new SubDomainDynamicExtractor(); assertNull(extr.extract(detail, url, null)); }
@Test public void shouldReturnFirstPartOfDomainWhenAskedFor() throws Exception{ String url = "abc.somesite.com/aStory"; AttributeDetail detail = new AttributeDetail(); detail.extractor_args = Arrays.asList("1"); SubDomainDynamicExtractor extr = new SubDomainDynamicExtractor(); assertEquals("abc", extr.extract(detail, url, null)); }