NlmToDocumentWithBasicMetadataConverter { public static ExtractedDocumentMetadata convertFull(String id, Document source, String text) throws TransformationException { if (id == null) { throw new RuntimeException("unable to set null id"); } ExtractedDocumentMetadata.Builder builder = ExtractedDocumentMetadata.newBuilder(); builder.setId(id); builder.setText(text != null ? text : ""); if (source == null) { return builder.build(); } Element rootElement = source.getRootElement(); convertMeta(id, rootElement, builder); Map<String, Affiliation> affiliationMap = convertAffiliations(rootElement); List<String> affiliationIds = new ArrayList<String>(); if (!affiliationMap.isEmpty()) { List<Affiliation> affiliations = new ArrayList<Affiliation>(); for (Entry<String, Affiliation> entry : affiliationMap.entrySet()) { affiliationIds.add(entry.getKey()); affiliations.add(entry.getValue()); } builder.setAffiliations(affiliations); } List<Author> authors = convertAuthors(rootElement, affiliationIds); if (!authors.isEmpty()) { builder.setAuthors(authors); } List<ReferenceMetadata> refs = convertReferences(rootElement); if (!refs.isEmpty()) { builder.setReferences(refs); } return builder.build(); } private  NlmToDocumentWithBasicMetadataConverter(); static ExtractedDocumentMetadata convertFull(String id, Document source, String text); static ExtractedDocumentMetadata createEmpty(String id); static final String EMPTY_META; }
@Test public void testConvertFull() throws Exception { SAXBuilder builder = new SAXBuilder(); Document document = builder.build(ClassPathResourceProvider.getResourceInputStream(testXML)); String id = "predefinedId"; ExtractedDocumentMetadata result = NlmToDocumentWithBasicMetadataConverter.convertFull(id, document, "text"); assertNotNull(result); assertEquals(id, result.getId()); testAuthorsAndAffiliations(result.getAuthors(), result.getAffiliations()); testReferences(result.getReferences()); assertEquals("Hindawi Publishing Corporation", result.getPublisher()); assertEquals("International Journal of Digital Multimedia Broadcasting", result.getJournal()); assertEquals( "Video Quality Prediction Models Based on Video Content Dynamics for H.264 Video over UMTS Networks", result.getTitle()); assertEquals(2, result.getExternalIdentifiers().size()); assertEquals(new Utf8("10.1155/2010/608138"), result.getExternalIdentifiers().get(new Utf8("doi"))); assertEquals(new Utf8("608138"), result.getExternalIdentifiers().get(new Utf8("hindawi-id"))); assertNotNull(result.getAbstract$()); assertFalse(result.getAbstract$().toString().isEmpty()); assertEquals(2, result.getKeywords().size()); assertTrue(result.getKeywords().contains("keyword 1")); assertTrue(result.getKeywords().contains("keyword 2")); assertEquals(2010, (int) result.getYear()); assertEquals("2010", result.getVolume()); assertEquals("issue-5", result.getIssue()); assertNotNull(result.getPages()); assertEquals("50", result.getPages().getStart()); assertEquals("60", result.getPages().getEnd()); }
@Test(expected=RuntimeException.class) public void testConvertFullNoId() throws Exception { SAXBuilder builder = new SAXBuilder(); Document document = (Document) builder.build(ClassPathResourceProvider.getResourceInputStream(testXML)); NlmToDocumentWithBasicMetadataConverter.convertFull(null, document, "text"); }
@Test public void testConvertFullNoDocument() throws Exception { String id = "id"; ExtractedDocumentMetadata result = NlmToDocumentWithBasicMetadataConverter.convertFull(id, null, "text"); assertNotNull(result); assertEquals(id, result.getId()); assertEquals("text", result.getText()); }