PriorityClaims extends DOMFragmentReader<List<DocumentId>> { @Override public List<DocumentId> read() { List<DocumentId> priorityDocIds = new ArrayList<DocumentId>(); Node provisionalNode = document.selectSingleNode(PROVISIONAL_PATH); if (provisionalNode != null) { DocumentId provisionalDocId = new DocumentIdNode(provisionalNode).read(); provisionalDocId.setType(DocumentIdType.PROVISIONAL); priorityDocIds.add(provisionalDocId); } List<Node> fragmentNodes = document.selectNodes(FRAGMENT_PATH); for (Node fragNode : fragmentNodes) { if (fragNode != null) { Element element = (Element) fragNode; String priorityKind = element.attributeValue("kind"); DocumentIdType docIdType; switch (priorityKind) { case "national": docIdType = DocumentIdType.NATIONAL_FILING; break; case "regional": docIdType = DocumentIdType.REGIONAL_FILING; break; case "international": docIdType = DocumentIdType.INTERNATIONAL_FILING; break; default: docIdType = DocumentIdType.NATIONAL_FILING; break; } Node docNumN = fragNode.selectSingleNode("doc-number"); if (docNumN == null) { continue; } Node countryN = fragNode.selectSingleNode("country"); CountryCode countryCode = CountryCode.UNDEFINED; String country = countryN != null ? countryN.getText() : null; if (country == null || country.trim().isEmpty()) { LOGGER.warn("Invalid CountryCode missing, using default countryCode 'US': {}", fragNode.asXML()); countryCode = CountryCode.US; } else { try { countryCode = CountryCode.fromString(country); } catch (InvalidDataException e2) { LOGGER.warn("{} : {}", e2.getMessage(), fragNode.asXML()); } } String docNumber = docNumN.getText(); if (docNumber.substring(0,2).toLowerCase().equals(countryCode.toString().toLowerCase())) { docNumber = docNumber.substring(2).trim(); LOGGER.debug("Removed duplicate CountryCode '{}' doc-number: {} => {}", countryCode.toString(), docNumN.getText(), docNumber); } Matcher matcher = SHORT_YEAR.matcher(docNumber); if (matcher.matches()) { if (matcher.group(1).equals("0")) { docNumber = "20" + docNumber; LOGGER.debug("Expanded Short Year, doc-number: {} => {}{}", matcher.group(0), countryCode, docNumber); } else if (matcher.group(1).equals("9")) { docNumber = "19" + docNumber; LOGGER.debug("Expanded Short Year, doc-number: {} => {}{}", matcher.group(0), countryCode, docNumber); } } if (!docNumber.startsWith("PCT/")) { docNumber = docNumber.replace("/", ""); } docNumber = docNumber.replaceAll("[\\s-]", ""); Node kindN = fragNode.selectSingleNode("kind"); String kindCode = kindN != null ? kindN.getText() : null; DocumentId documentId = new DocumentId(countryCode, docNumber, kindCode); documentId.setRawText(docNumN.getText()); documentId.setType(docIdType); Node dateN = fragNode.selectSingleNode("date"); if (dateN != null) { try { documentId.setDate(new DocumentDate(dateN.getText())); } catch (InvalidDataException e) { LOGGER.warn("{} : {}", e.getMessage(), fragNode.asXML()); } } priorityDocIds.add(documentId); } } return priorityDocIds; } PriorityClaims(Document document); @Override List<DocumentId> read();  }
@Test public void test() throws DocumentException, InvalidDataException { String xml = "<xml><biblio><priority-claims>\r\n" + "<priority-claim kind=\"regional\" sequence=\"01\">\r\n" + "<country>EM</country>\r\n" + "<doc-number>002705756-0001</doc-number>\r\n" + "<date>20150522</date>\r\n" + "</priority-claim>\r\n" + "<priority-claim kind=\"regional\" sequence=\"02\">\r\n" + "<country>EM</country>\r\n" + "<doc-number>002705756-0002</doc-number>\r\n" + "<date>20150522</date>\r\n" + "</priority-claim>\r\n" + "<priority-claim kind=\"regional\" sequence=\"03\">\r\n" + "<country>EM</country>\r\n" + "<doc-number>002705756-0003</doc-number>\r\n" + "<date>20150522</date>\r\n" + "</priority-claim>\r\n" + "</priority-claims></biblio></xml>"; Document doc = DocumentHelper.parseText(xml); List<DocumentId> docIds = new PriorityClaims(doc).read(); assertEquals("Expect 3 docIds", 3, docIds.size()); DocumentId id1 = new DocumentId(CountryCode.EM, "0027057560001"); id1.setType(DocumentIdType.REGIONAL_FILING); id1.setDate(new DocumentDate("20150522")); assertEquals(id1, docIds.get(0)); DocumentId id2 = new DocumentId(CountryCode.EM, "0027057560002"); id2.setType(DocumentIdType.REGIONAL_FILING); id2.setDate(new DocumentDate("20150522")); assertEquals(id2, docIds.get(1)); DocumentId id3 = new DocumentId(CountryCode.EM, "0027057560003"); id3.setType(DocumentIdType.REGIONAL_FILING); id3.setDate(new DocumentDate("20150522")); assertEquals(id3, docIds.get(2)); }