Search in sources :

Example 1 with OAI2Handler

use of org.jabref.logic.importer.util.OAI2Handler in project jabref by JabRef.

the class OAI2HandlerFetcherTest method setUp.

@Before
public void setUp() throws ParserConfigurationException, SAXException {
    parserFactory = SAXParserFactory.newInstance();
    saxParser = parserFactory.newSAXParser();
    be = new BibEntry("article");
    handler = new OAI2Handler(be);
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) OAI2Handler(org.jabref.logic.importer.util.OAI2Handler) Before(org.junit.Before)

Example 2 with OAI2Handler

use of org.jabref.logic.importer.util.OAI2Handler in project jabref by JabRef.

the class OAI2Fetcher method importOai2Entry.

/**
     * Import an entry from an OAI2 archive. The BibEntry provided has to
     * have the field OAI2_IDENTIFIER_FIELD set to the search string.
     *
     * @param key
     *            The OAI2 key to fetch from ArXiv.
     * @return The imported BibEntry or null if none.
     */
protected BibEntry importOai2Entry(String key) throws IOException, SAXException {
    /**
         * Fix for problem reported in mailing-list:
         *   https://sourceforge.net/forum/message.php?msg_id=4087158
         */
    String fixedKey = OAI2Fetcher.fixKey(key);
    String url = constructUrl(fixedKey);
    URL oai2Url = new URL(url);
    HttpURLConnection oai2Connection = (HttpURLConnection) oai2Url.openConnection();
    oai2Connection.setRequestProperty("User-Agent", "JabRef");
    /* create an empty BibEntry and set the oai2identifier field */
    BibEntry entry = new BibEntry("article");
    entry.setField(OAI2Fetcher.OAI2_IDENTIFIER_FIELD, fixedKey);
    DefaultHandler handlerBase = new OAI2Handler(entry);
    try (InputStream inputStream = oai2Connection.getInputStream()) {
        /* parse the result */
        saxParser.parse(inputStream, handlerBase);
        /* Correct line breaks and spacing */
        for (String name : entry.getFieldNames()) {
            entry.getField(name).ifPresent(content -> entry.setField(name, OAI2Handler.correctLineBreaks(content)));
        }
        if (fixedKey.matches("\\d\\d\\d\\d\\..*")) {
            entry.setField(FieldName.YEAR, "20" + fixedKey.substring(0, 2));
            int monthNumber = Integer.parseInt(fixedKey.substring(2, 4));
            Optional<Month> month = Month.getMonthByNumber(monthNumber);
            month.ifPresent(entry::setMonth);
        }
    }
    return entry;
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) Month(org.jabref.model.entry.Month) HttpURLConnection(java.net.HttpURLConnection) OAI2Handler(org.jabref.logic.importer.util.OAI2Handler) InputStream(java.io.InputStream) URL(java.net.URL) DefaultHandler(org.xml.sax.helpers.DefaultHandler)

Aggregations

OAI2Handler (org.jabref.logic.importer.util.OAI2Handler)2 BibEntry (org.jabref.model.entry.BibEntry)2 InputStream (java.io.InputStream)1 HttpURLConnection (java.net.HttpURLConnection)1 URL (java.net.URL)1 Month (org.jabref.model.entry.Month)1 Before (org.junit.Before)1 DefaultHandler (org.xml.sax.helpers.DefaultHandler)1