Search in sources :

Example 1 with PubmedBookArticle

use of org.jabref.logic.importer.fileformat.medline.PubmedBookArticle in project jabref by JabRef.

the class MedlineImporter method importDatabase.

@Override
public ParserResult importDatabase(BufferedReader reader) throws IOException {
    Objects.requireNonNull(reader);
    List<BibEntry> bibItems = new ArrayList<>();
    try {
        JAXBContext context = JAXBContext.newInstance("org.jabref.logic.importer.fileformat.medline");
        XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory();
        XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(reader);
        //go to the root element
        while (!xmlStreamReader.isStartElement()) {
            xmlStreamReader.next();
        }
        Unmarshaller unmarshaller = context.createUnmarshaller();
        Object unmarshalledObject = unmarshaller.unmarshal(xmlStreamReader);
        //check whether we have an article set, an article, a book article or a book article set
        if (unmarshalledObject instanceof PubmedArticleSet) {
            PubmedArticleSet articleSet = (PubmedArticleSet) unmarshalledObject;
            for (Object article : articleSet.getPubmedArticleOrPubmedBookArticle()) {
                if (article instanceof PubmedArticle) {
                    PubmedArticle currentArticle = (PubmedArticle) article;
                    parseArticle(currentArticle, bibItems);
                }
                if (article instanceof PubmedBookArticle) {
                    PubmedBookArticle currentArticle = (PubmedBookArticle) article;
                    parseBookArticle(currentArticle, bibItems);
                }
            }
        } else if (unmarshalledObject instanceof PubmedArticle) {
            PubmedArticle article = (PubmedArticle) unmarshalledObject;
            parseArticle(article, bibItems);
        } else if (unmarshalledObject instanceof PubmedBookArticle) {
            PubmedBookArticle currentArticle = (PubmedBookArticle) unmarshalledObject;
            parseBookArticle(currentArticle, bibItems);
        } else {
            PubmedBookArticleSet bookArticleSet = (PubmedBookArticleSet) unmarshalledObject;
            for (PubmedBookArticle bookArticle : bookArticleSet.getPubmedBookArticle()) {
                parseBookArticle(bookArticle, bibItems);
            }
        }
    } catch (JAXBException | XMLStreamException e) {
        LOGGER.debug("could not parse document", e);
        return ParserResult.fromError(e);
    }
    return new ParserResult(bibItems);
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) XMLStreamReader(javax.xml.stream.XMLStreamReader) PubmedBookArticleSet(org.jabref.logic.importer.fileformat.medline.PubmedBookArticleSet) JAXBException(javax.xml.bind.JAXBException) ArrayList(java.util.ArrayList) JAXBContext(javax.xml.bind.JAXBContext) ParserResult(org.jabref.logic.importer.ParserResult) PubmedBookArticle(org.jabref.logic.importer.fileformat.medline.PubmedBookArticle) XMLStreamException(javax.xml.stream.XMLStreamException) PubmedArticleSet(org.jabref.logic.importer.fileformat.medline.PubmedArticleSet) Unmarshaller(javax.xml.bind.Unmarshaller) PubmedArticle(org.jabref.logic.importer.fileformat.medline.PubmedArticle) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Aggregations

ArrayList (java.util.ArrayList)1 JAXBContext (javax.xml.bind.JAXBContext)1 JAXBException (javax.xml.bind.JAXBException)1 Unmarshaller (javax.xml.bind.Unmarshaller)1 XMLInputFactory (javax.xml.stream.XMLInputFactory)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 XMLStreamReader (javax.xml.stream.XMLStreamReader)1 ParserResult (org.jabref.logic.importer.ParserResult)1 PubmedArticle (org.jabref.logic.importer.fileformat.medline.PubmedArticle)1 PubmedArticleSet (org.jabref.logic.importer.fileformat.medline.PubmedArticleSet)1 PubmedBookArticle (org.jabref.logic.importer.fileformat.medline.PubmedBookArticle)1 PubmedBookArticleSet (org.jabref.logic.importer.fileformat.medline.PubmedBookArticleSet)1 BibEntry (org.jabref.model.entry.BibEntry)1