DocumentIdNode extends ItemReader<DocumentId> { @Override public DocumentId read() { if (itemNode == null) { return null; } Node docNumN = itemNode.selectSingleNode("doc-number"); if (docNumN == null) { LOGGER.warn("Invalid doc-number can not be Null, from: {}", itemNode.asXML()); return null; } Node countryN = itemNode.selectSingleNode("country"); CountryCode countryCode = fallbackCountryCode; String country = countryN != null ? countryN.getText() : null; try { countryCode = CountryCode.fromString(country); } catch (InvalidDataException e2) { LOGGER.warn("{} : {}", e2.getMessage(), itemNode.asXML()); } String docNumber = docNumN.getText(); if (docNumber.length() > 2 && docNumber.substring(0, 2).equalsIgnoreCase(countryCode.toString())) { docNumber = docNumber.substring(2).trim(); if (docNumber.startsWith("/")) { docNumber = docNumber.substring(1); } LOGGER.debug("Removed duplicate CountryCode '{}' -- from: '{}' doc-number: {} => {}", countryCode.toString(), itemNode.getParent().getName(), docNumN.getText(), docNumber); } Node dateN = itemNode.selectSingleNode("date"); DocumentDate docDate = null; if (dateN != null && dateN.getText().trim().isEmpty()) { LOGGER.warn("Invalid Date, Empty or Missing : {}", itemNode.asXML()); } if (dateN != null) { try { docDate = new DocumentDate(dateN.getText()); } catch (InvalidDataException e) { LOGGER.warn("{} : {}", e.getMessage(), itemNode.asXML()); } } Matcher matcher = SHORT_YEAR.matcher(docNumber); if (matcher.matches()) { if (matcher.group(1).equals("0")) { if (docDate != null && docDate.getYear() <= 2000) { LOGGER.debug("Expand of possible Short Year, skipped due to year mismatch; doc-number: {} year: {}", matcher.group(0), docDate.getYear()); } else { docNumber = "20" + docNumber; LOGGER.debug("Expanded Short Year, doc-number: {} => {}{}", matcher.group(0), countryCode, docNumber); } } else if (matcher.group(1).equals("9")) { if (docDate != null && docDate.getYear() <= 1900 && docDate.getYear() > 2000) { LOGGER.debug("Expand of possible Short Year, skipped due to year mismatch; doc-number: {} year: {}", matcher.group(0), docDate.getYear()); } else { 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 = itemNode.selectSingleNode("kind"); String kindCode = kindN != null ? kindN.getText().trim() : null; if (countryCode == CountryCode.US && "00".equals(kindCode)) { kindCode = null; } DocumentId documentId = new DocumentId(countryCode, docNumber, kindCode); documentId.setRawText(docNumN.getText()); if (dateN != null && docDate != null) { documentId.setDate(docDate); } return documentId; } DocumentIdNode(Node itemNode);  DocumentIdNode(Node itemNode, CountryCode fallbackCountryCode); @Override DocumentId read();  }
@Test public void SmallId() throws DocumentException { String xml = "<xml><document-id><country>US</country><doc-number>1</doc-number><kind>A</kind><date>18360700</date></document-id></xml>"; Document xmlDoc = createDocument(xml); DocumentId docId = new DocumentIdNode(xmlDoc.getRootElement()).read(); assertEquals(CountryCode.US, docId.getCountryCode()); assertEquals("1", docId.getDocNumber()); assertEquals("18360700", docId.getDate().getDateText(DateTextType.RAW)); assertEquals("US1A", docId.toText()); }
@Test public void SimpleAppId() throws DocumentException { String xml = "<xml><document-id><country>US</country><doc-number>12345678</doc-number><kind>A1</kind><date>20010101</date></document-id></xml>"; Document xmlDoc = createDocument(xml); DocumentId docId = new DocumentIdNode(xmlDoc.getRootElement()).read(); assertEquals(CountryCode.US, docId.getCountryCode()); assertEquals("12345678", docId.getDocNumber()); assertEquals("A1", docId.getKindCode()); assertEquals("20010101", docId.getDate().getDateText(DateTextType.RAW)); assertEquals("US12345678A1", docId.toText()); }
@Test public void RemoveRepeatCountryCode() throws DocumentException { String xml = "<xml><document-id><country>WO</country><doc-number>WO 2004/12345678</doc-number><date>20010101</date></document-id></xml>"; Document xmlDoc = createDocument(xml); DocumentId docId = new DocumentIdNode(xmlDoc.getRootElement()).read(); assertEquals(CountryCode.WO, docId.getCountryCode()); assertEquals("200412345678", docId.getDocNumber()); assertEquals("20010101", docId.getDate().getDateText(DateTextType.RAW)); assertEquals("WO200412345678", docId.toText()); }
@Test public void RemoveRepeatCountryCode2() throws DocumentException { String xml = "<xml><document-id><country>WO</country><doc-number>WO/03/001333</doc-number><date>20030101</date></document-id></xml>"; Document xmlDoc = createDocument(xml); DocumentId docId = new DocumentIdNode(xmlDoc.getRootElement()).read(); assertEquals(CountryCode.WO, docId.getCountryCode()); assertEquals("2003001333", docId.getDocNumber()); assertEquals("20030101", docId.getDate().getDateText(DateTextType.RAW)); assertEquals("WO2003001333", docId.toText()); }
@Test public void WO_PCT() throws DocumentException { String xml = "<xml><document-id><country>WO</country><doc-number>PCT/EP/12345678</doc-number><date>20010101</date></document-id></xml>"; Document xmlDoc = createDocument(xml); DocumentId docId = new DocumentIdNode(xmlDoc.getRootElement()).read(); assertEquals(CountryCode.WO, docId.getCountryCode()); assertEquals("PCT/EP/12345678", docId.getDocNumber()); assertEquals("20010101", docId.getDate().getDateText(DateTextType.RAW)); assertEquals("PCT/EP/12345678", docId.toText()); }
@Test public void FixShortYear() throws DocumentException { String xml = "<xml><document-id><country>WO</country><doc-number>WO 04/12345678</doc-number><date>20010101</date></document-id></xml>"; Document xmlDoc = createDocument(xml); DocumentId docId = new DocumentIdNode(xmlDoc.getRootElement()).read(); assertEquals(CountryCode.WO, docId.getCountryCode()); assertEquals("200412345678", docId.getDocNumber()); assertEquals("20010101", docId.getDate().getDateText(DateTextType.RAW)); assertEquals("WO200412345678", docId.toText()); }
@Test public void JP_JAPAN() throws DocumentException { String xml = "<xml><document-id><country>JP</country><doc-number>99-123456</doc-number></document-id></xml>"; Document xmlDoc = createDocument(xml); DocumentId docId = new DocumentIdNode(xmlDoc.getRootElement()).read(); assertEquals(CountryCode.JP, docId.getCountryCode()); assertEquals("1999123456", docId.getDocNumber()); assertEquals("JP1999123456", docId.toText()); String xml2 = "<xml><document-id><country>JP</country><doc-number>2000-123456</doc-number></document-id></xml>"; Document xmlDoc2 = createDocument(xml2); DocumentId docId2 = new DocumentIdNode(xmlDoc2.getRootElement()).read(); assertEquals(CountryCode.JP, docId2.getCountryCode()); assertEquals("2000123456", docId2.getDocNumber()); assertEquals("JP2000123456", docId2.toText()); }
@Test public void KR_S_KOREA() throws DocumentException { String xml = "<xml><document-id><country>KR</country><doc-number>93-1234567</doc-number></document-id></xml>"; Document xmlDoc = createDocument(xml); DocumentId docId = new DocumentIdNode(xmlDoc.getRootElement()).read(); assertEquals(CountryCode.KR, docId.getCountryCode()); assertEquals("19931234567", docId.getDocNumber()); assertEquals("KR19931234567", docId.toText()); String xml2 = "<xml><document-id><country>KR</country><doc-number>1997-1234567</doc-number></document-id></xml>"; Document xmlDoc2 = createDocument(xml2); DocumentId docId2 = new DocumentIdNode(xmlDoc2.getRootElement()).read(); assertEquals(CountryCode.KR, docId2.getCountryCode()); assertEquals("19971234567", docId2.getDocNumber()); assertEquals("KR19971234567", docId2.toText()); String xml3 = "<xml><document-id><country>KR</country><doc-number>10-2004-1234567</doc-number></document-id></xml>"; Document xmlDoc3 = createDocument(xml3); DocumentId docId3 = new DocumentIdNode(xmlDoc3.getRootElement()).read(); assertEquals(CountryCode.KR, docId3.getCountryCode()); assertEquals("1020041234567", docId3.getDocNumber()); assertEquals("KR1020041234567", docId3.toText()); }
@Test public void RU_RUSSIA() throws DocumentException { String xml2 = "<xml><document-id><country>RU</country><doc-number>2000123456</doc-number><kind>U1</kind></document-id></xml>"; Document xmlDoc2 = createDocument(xml2); DocumentId docId2 = new DocumentIdNode(xmlDoc2.getRootElement()).read(); assertEquals(CountryCode.RU, docId2.getCountryCode()); assertEquals("2000123456", docId2.getDocNumber()); assertEquals("RU2000123456U1", docId2.toText()); }
@Test public void IN_INDIA() throws DocumentException { String xml = "<xml><document-id><country>IN</country><doc-number>12345/LLL/1997</doc-number></document-id></xml>"; Document xmlDoc = createDocument(xml); DocumentId docId = new DocumentIdNode(xmlDoc.getRootElement()).read(); assertEquals(CountryCode.IN, docId.getCountryCode()); assertEquals("12345LLL1997", docId.getDocNumber()); assertEquals("IN12345LLL1997", docId.toText()); String xml2 = "<xml><document-id><country>IN</country><doc-number>201611123456</doc-number></document-id></xml>"; Document xmlDoc2 = createDocument(xml2); DocumentId docId2 = new DocumentIdNode(xmlDoc2.getRootElement()).read(); assertEquals(CountryCode.IN, docId2.getCountryCode()); assertEquals("201611123456", docId2.getDocNumber()); assertEquals("IN201611123456", docId2.toText()); }
@Test public void RemoveSpaces() throws DocumentException { String xml = "<xml><document-id><country>DE</country><doc-number>299 06 432</doc-number><date>20010101</date><kind>U1</kind></document-id></xml>"; Document xmlDoc = createDocument(xml); DocumentId docId = new DocumentIdNode(xmlDoc.getRootElement()).read(); assertEquals(CountryCode.DE, docId.getCountryCode()); assertEquals("29906432", docId.getDocNumber()); assertEquals("20010101", docId.getDate().getDateText(DateTextType.RAW)); assertEquals("DE29906432U1", docId.toText()); }