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);
}
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;
}
Aggregations