use of org.jabref.model.entry.Month 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;
}
use of org.jabref.model.entry.Month in project jabref by JabRef.
the class RisImporter method importDatabase.
@Override
public ParserResult importDatabase(BufferedReader reader) throws IOException {
List<BibEntry> bibitems = new ArrayList<>();
//use optional here, so that no exception will be thrown if the file is empty
Optional<String> OptionalLines = reader.lines().reduce((line, nextline) -> line + "\n" + nextline);
String linesAsString = OptionalLines.isPresent() ? OptionalLines.get() : "";
String[] entries = linesAsString.replace("–", "-").replace("—", "--").replace("―", "--").split("ER -.*\\n");
for (String entry1 : entries) {
String type = "";
String author = "";
String editor = "";
String startPage = "";
String endPage = "";
String comment = "";
Optional<Month> month = Optional.empty();
Map<String, String> fields = new HashMap<>();
String[] lines = entry1.split("\n");
for (int j = 0; j < lines.length; j++) {
StringBuilder current = new StringBuilder(lines[j]);
boolean done = false;
while (!done && (j < (lines.length - 1))) {
if ((lines[j + 1].length() >= 6) && !" - ".equals(lines[j + 1].substring(2, 6))) {
if ((current.length() > 0) && !Character.isWhitespace(current.charAt(current.length() - 1)) && !Character.isWhitespace(lines[j + 1].charAt(0))) {
current.append(' ');
}
current.append(lines[j + 1]);
j++;
} else {
done = true;
}
}
String entry = current.toString();
if (entry.length() < 6) {
continue;
} else {
String tag = entry.substring(0, 2);
String value = entry.substring(6).trim();
if ("TY".equals(tag)) {
if ("BOOK".equals(value)) {
type = "book";
} else if ("JOUR".equals(value) || "MGZN".equals(value)) {
type = "article";
} else if ("THES".equals(value)) {
type = "phdthesis";
} else if ("UNPB".equals(value)) {
type = "unpublished";
} else if ("RPRT".equals(value)) {
type = "techreport";
} else if ("CONF".equals(value)) {
type = "inproceedings";
} else if ("CHAP".equals(value)) {
//"inbook";
type = "incollection";
} else if ("PAT".equals(value)) {
type = "patent";
} else {
type = "other";
}
} else if ("T1".equals(tag) || "TI".equals(tag)) {
String oldVal = fields.get(FieldName.TITLE);
if (oldVal == null) {
fields.put(FieldName.TITLE, value);
} else {
if (oldVal.endsWith(":") || oldVal.endsWith(".") || oldVal.endsWith("?")) {
fields.put(FieldName.TITLE, oldVal + " " + value);
} else {
fields.put(FieldName.TITLE, oldVal + ": " + value);
}
}
// Normalize whitespaces
fields.put(FieldName.TITLE, fields.get(FieldName.TITLE).replaceAll("\\s+", " "));
} else if ("BT".equals(tag)) {
fields.put(FieldName.BOOKTITLE, value);
} else if ("T2".equals(tag) && (fields.get(FieldName.JOURNAL) == null || "".equals(fields.get(FieldName.JOURNAL)))) {
//if there is no journal title, then put second title as journal title
fields.put(FieldName.JOURNAL, value);
} else if ("JO".equals(tag)) {
//if this field appears then this should be the journal title
fields.put(FieldName.JOURNAL, value);
} else if ("T3".equals(tag)) {
fields.put(FieldName.SERIES, value);
} else if ("AU".equals(tag) || "A1".equals(tag)) {
if ("".equals(author)) {
author = value;
} else {
author += " and " + value;
}
} else if ("A2".equals(tag) || "A3".equals(tag) || "A4".equals(tag)) {
if (editor.isEmpty()) {
editor = value;
} else {
editor += " and " + value;
}
} else if ("JA".equals(tag) || "JF".equals(tag)) {
if ("inproceedings".equals(type)) {
fields.put(FieldName.BOOKTITLE, value);
} else {
fields.put(FieldName.JOURNAL, value);
}
} else if ("LA".equals(tag)) {
fields.put(FieldName.LANGUAGE, value);
} else if ("CA".equals(tag)) {
fields.put("caption", value);
} else if ("DB".equals(tag)) {
fields.put("database", value);
} else if ("IS".equals(tag)) {
fields.put(FieldName.NUMBER, value);
} else if ("SP".equals(tag)) {
startPage = value;
} else if ("PB".equals(tag)) {
if ("phdthesis".equals(type)) {
fields.put(FieldName.SCHOOL, value);
} else {
fields.put(FieldName.PUBLISHER, value);
}
} else if ("AD".equals(tag) || "CY".equals(tag)) {
fields.put(FieldName.ADDRESS, value);
} else if ("EP".equals(tag)) {
endPage = value;
if (!endPage.isEmpty()) {
endPage = "--" + endPage;
}
} else if ("ET".equals(tag)) {
fields.put(FieldName.EDITION, value);
} else if ("SN".equals(tag)) {
fields.put(FieldName.ISSN, value);
} else if ("VL".equals(tag)) {
fields.put(FieldName.VOLUME, value);
} else if ("N2".equals(tag) || "AB".equals(tag)) {
String oldAb = fields.get(FieldName.ABSTRACT);
if (oldAb == null) {
fields.put(FieldName.ABSTRACT, value);
} else {
fields.put(FieldName.ABSTRACT, oldAb + OS.NEWLINE + value);
}
} else if ("UR".equals(tag)) {
fields.put(FieldName.URL, value);
} else if (("Y1".equals(tag) || "PY".equals(tag) || "DA".equals(tag)) && (value.length() >= 4)) {
fields.put(FieldName.YEAR, value.substring(0, 4));
String[] parts = value.split("/");
if ((parts.length > 1) && !parts[1].isEmpty()) {
try {
int monthNumber = Integer.parseInt(parts[1]);
month = Month.getMonthByNumber(monthNumber);
} catch (NumberFormatException ex) {
// The month part is unparseable, so we ignore it.
}
}
} else if ("KW".equals(tag)) {
if (fields.containsKey(FieldName.KEYWORDS)) {
String kw = fields.get(FieldName.KEYWORDS);
fields.put(FieldName.KEYWORDS, kw + ", " + value);
} else {
fields.put(FieldName.KEYWORDS, value);
}
} else if ("U1".equals(tag) || "U2".equals(tag) || "N1".equals(tag)) {
if (!comment.isEmpty()) {
comment = comment + " ";
}
comment = comment + value;
} else // Added ID import 2005.12.01, Morten Alver:
if ("ID".equals(tag)) {
fields.put("refid", value);
} else if ("M3".equals(tag) || "DO".equals(tag)) {
addDoi(fields, value);
}
}
// fix authors
if (!author.isEmpty()) {
author = AuthorList.fixAuthorLastNameFirst(author);
fields.put(FieldName.AUTHOR, author);
}
if (!editor.isEmpty()) {
editor = AuthorList.fixAuthorLastNameFirst(editor);
fields.put(FieldName.EDITOR, editor);
}
if (!comment.isEmpty()) {
fields.put(FieldName.COMMENT, comment);
}
fields.put(FieldName.PAGES, startPage + endPage);
}
// Remove empty fields:
fields.entrySet().removeIf(key -> (key.getValue() == null) || key.getValue().trim().isEmpty());
// create one here
// type is set in the loop above
BibEntry b = new BibEntry(type);
b.setField(fields);
// month has a special treatment as we use the separate method "setMonth" of BibEntry instead of directly setting the value
month.ifPresent(parsedMonth -> b.setMonth(parsedMonth));
bibitems.add(b);
}
return new ParserResult(bibitems);
}
use of org.jabref.model.entry.Month in project jabref by JabRef.
the class JSONEntryParser method parseSpringerJSONtoBibtex.
/**
* Convert a JSONObject obtained from http://api.springer.com/metadata/json to a BibEntry
*
* @param springerJsonEntry the JSONObject from search results
* @return the converted BibEntry
*/
public static BibEntry parseSpringerJSONtoBibtex(JSONObject springerJsonEntry) {
// Fields that are directly accessible at the top level Json object
String[] singleFieldStrings = { FieldName.ISSN, FieldName.VOLUME, FieldName.ABSTRACT, FieldName.DOI, FieldName.TITLE, FieldName.NUMBER, FieldName.PUBLISHER };
BibEntry entry = new BibEntry();
String nametype;
// Guess publication type
String isbn = springerJsonEntry.optString("isbn");
if (com.google.common.base.Strings.isNullOrEmpty(isbn)) {
// Probably article
entry.setType("article");
nametype = FieldName.JOURNAL;
} else {
// Probably book chapter or from proceeding, go for book chapter
entry.setType("incollection");
nametype = FieldName.BOOKTITLE;
entry.setField(FieldName.ISBN, isbn);
}
// Authors
if (springerJsonEntry.has("creators")) {
JSONArray authors = springerJsonEntry.getJSONArray("creators");
List<String> authorList = new ArrayList<>();
for (int i = 0; i < authors.length(); i++) {
if (authors.getJSONObject(i).has("creator")) {
authorList.add(authors.getJSONObject(i).getString("creator"));
} else {
LOGGER.info("Empty author name.");
}
}
entry.setField(FieldName.AUTHOR, String.join(" and ", authorList));
} else {
LOGGER.info("No author found.");
}
// Direct accessible fields
for (String field : singleFieldStrings) {
if (springerJsonEntry.has(field)) {
String text = springerJsonEntry.getString(field);
if (!text.isEmpty()) {
entry.setField(field, text);
}
}
}
// Page numbers
if (springerJsonEntry.has("startingPage") && !(springerJsonEntry.getString("startingPage").isEmpty())) {
if (springerJsonEntry.has("endPage") && !(springerJsonEntry.getString("endPage").isEmpty())) {
entry.setField(FieldName.PAGES, springerJsonEntry.getString("startingPage") + "--" + springerJsonEntry.getString("endPage"));
} else {
entry.setField(FieldName.PAGES, springerJsonEntry.getString("startingPage"));
}
}
// Journal
if (springerJsonEntry.has("publicationName")) {
entry.setField(nametype, springerJsonEntry.getString("publicationName"));
}
// URL
if (springerJsonEntry.has("url")) {
JSONArray urlarray = springerJsonEntry.optJSONArray("url");
if (urlarray == null) {
entry.setField(FieldName.URL, springerJsonEntry.optString("url"));
} else {
entry.setField(FieldName.URL, urlarray.getJSONObject(0).optString("value"));
}
}
// Date
if (springerJsonEntry.has("publicationDate")) {
String date = springerJsonEntry.getString("publicationDate");
// For biblatex
entry.setField(FieldName.DATE, date);
String[] dateparts = date.split("-");
entry.setField(FieldName.YEAR, dateparts[0]);
Optional<Month> month = Month.getMonthByNumber(Integer.parseInt(dateparts[1]));
month.ifPresent(entry::setMonth);
}
// Clean up abstract (often starting with Abstract)
entry.getField(FieldName.ABSTRACT).ifPresent(abstractContents -> {
if (abstractContents.startsWith("Abstract")) {
entry.setField(FieldName.ABSTRACT, abstractContents.substring(8));
}
});
return entry;
}
use of org.jabref.model.entry.Month in project jabref by JabRef.
the class BibTeXConverter method convert.
/**
* Converts an {@link MSBibEntry} to a {@link BibEntry} for import
* @param entry The MsBibEntry to convert
* @return The bib entry
*/
public static BibEntry convert(MSBibEntry entry) {
BibEntry result;
Map<String, String> fieldValues = new HashMap<>();
String bibTexEntryType = MSBibMapping.getBiblatexEntryType(entry.getType());
result = new BibEntry(bibTexEntryType);
// add String fields
for (Map.Entry<String, String> field : entry.fields.entrySet()) {
String msField = field.getKey();
String value = field.getValue();
if ((value != null) && (MSBibMapping.getBibTeXField(msField) != null)) {
fieldValues.put(MSBibMapping.getBibTeXField(msField), value);
}
}
// Value must be converted
if (fieldValues.containsKey(FieldName.LANGUAGE)) {
int lcid = Integer.valueOf(fieldValues.get(FieldName.LANGUAGE));
fieldValues.put(FieldName.LANGUAGE, MSBibMapping.getLanguage(lcid));
}
addAuthor(fieldValues, FieldName.AUTHOR, entry.authors);
addAuthor(fieldValues, MSBIB_PREFIX + FieldName.BOOKAUTHOR, entry.bookAuthors);
addAuthor(fieldValues, FieldName.EDITOR, entry.editors);
addAuthor(fieldValues, MSBIB_PREFIX + FieldName.TRANSLATOR, entry.translators);
addAuthor(fieldValues, MSBIB_PREFIX + "producername", entry.producerNames);
addAuthor(fieldValues, MSBIB_PREFIX + "composer", entry.composers);
addAuthor(fieldValues, MSBIB_PREFIX + "conductor", entry.conductors);
addAuthor(fieldValues, MSBIB_PREFIX + "performer", entry.performers);
addAuthor(fieldValues, MSBIB_PREFIX + "writer", entry.writers);
addAuthor(fieldValues, MSBIB_PREFIX + "director", entry.directors);
addAuthor(fieldValues, MSBIB_PREFIX + "compiler", entry.compilers);
addAuthor(fieldValues, MSBIB_PREFIX + "interviewer", entry.interviewers);
addAuthor(fieldValues, MSBIB_PREFIX + "interviewee", entry.interviewees);
addAuthor(fieldValues, MSBIB_PREFIX + "inventor", entry.inventors);
addAuthor(fieldValues, MSBIB_PREFIX + "counsel", entry.counsels);
if (entry.pages != null) {
fieldValues.put(FieldName.PAGES, entry.pages.toString("--"));
}
parseStandardNumber(entry.standardNumber, fieldValues);
if (entry.address != null) {
fieldValues.put(FieldName.LOCATION, entry.address);
}
// TODO: ConferenceName is saved as booktitle when converting from MSBIB to BibTeX
if (entry.conferenceName != null) {
fieldValues.put(FieldName.ORGANIZATION, entry.conferenceName);
}
if (entry.dateAccessed != null) {
fieldValues.put(MSBIB_PREFIX + "accessed", entry.dateAccessed);
}
if (entry.journalName != null) {
fieldValues.put(FieldName.JOURNAL, entry.journalName);
}
if (entry.month != null) {
Optional<Month> month = Month.parse(entry.month);
month.ifPresent(parsedMonth -> result.setMonth(parsedMonth));
}
if (entry.number != null) {
fieldValues.put(FieldName.NUMBER, entry.number);
}
// set all fields
result.setField(fieldValues);
return result;
}
use of org.jabref.model.entry.Month in project jabref by JabRef.
the class XMPUtil method getBibtexEntryFromDublinCore.
/**
* Helper function for retrieving a BibEntry from the DublinCore metadata
* in a PDF file.
*
* To understand how to get hold of a XMPSchemaDublinCore have a look in the
* test cases for XMPUtil.
*
* The BibEntry is build by mapping individual fields in the dublin core
* (like creator, title, subject) to fields in a bibtex entry.
*
* @param dcSchema The document information from which to build a BibEntry.
* @return The bibtex entry found in the document information.
*/
public static Optional<BibEntry> getBibtexEntryFromDublinCore(XMPSchemaDublinCore dcSchema, XMPPreferences xmpPreferences) {
BibEntry entry = new BibEntry();
/*
* Contributor -> Editor
*/
List<String> contributors = dcSchema.getContributors();
if ((contributors != null) && !contributors.isEmpty()) {
entry.setField(FieldName.EDITOR, String.join(" and ", contributors));
}
/*
* Author -> Creator
*/
List<String> creators = dcSchema.getCreators();
if ((creators != null) && !creators.isEmpty()) {
entry.setField(FieldName.AUTHOR, String.join(" and ", creators));
}
/*
* Year + Month -> Date
*/
List<String> dates = dcSchema.getSequenceList("dc:date");
if ((dates != null) && !dates.isEmpty()) {
String date = dates.get(0).trim();
Calendar c = null;
try {
c = DateConverter.toCalendar(date);
} catch (IOException ignored) {
// Ignored
}
if (c != null) {
entry.setField(FieldName.YEAR, String.valueOf(c.get(Calendar.YEAR)));
if (date.length() > 4) {
Optional<Month> month = Month.getMonthByNumber(c.get(Calendar.MONTH) + 1);
month.ifPresent(entry::setMonth);
}
}
}
/*
* Abstract -> Description
*/
String s = dcSchema.getDescription();
if (s != null) {
entry.setField(FieldName.ABSTRACT, s);
}
/*
* Identifier -> DOI
*/
s = dcSchema.getIdentifier();
if (s != null) {
entry.setField(FieldName.DOI, s);
}
/*
* Publisher -> Publisher
*/
List<String> publishers = dcSchema.getPublishers();
if ((publishers != null) && !publishers.isEmpty()) {
entry.setField(FieldName.PUBLISHER, String.join(" and ", publishers));
}
/*
* Relation -> bibtexkey
*
* We abuse the relationship attribute to store all other values in the
* bibtex document
*/
List<String> relationships = dcSchema.getRelationships();
if (relationships != null) {
for (String r : relationships) {
if (r.startsWith("bibtex/")) {
r = r.substring("bibtex/".length());
int i = r.indexOf('/');
if (i != -1) {
entry.setField(r.substring(0, i), r.substring(i + 1));
}
}
}
}
/*
* Rights -> Rights
*/
s = dcSchema.getRights();
if (s != null) {
entry.setField("rights", s);
}
/*
* Source -> Source
*/
s = dcSchema.getSource();
if (s != null) {
entry.setField("source", s);
}
/*
* Subject -> Keywords
*/
List<String> subjects = dcSchema.getSubjects();
if (subjects != null) {
entry.addKeywords(subjects, xmpPreferences.getKeywordSeparator());
}
/*
* Title -> Title
*/
s = dcSchema.getTitle();
if (s != null) {
entry.setField(FieldName.TITLE, s);
}
/*
* Type -> Type
*/
List<String> l = dcSchema.getTypes();
if ((l != null) && !l.isEmpty()) {
s = l.get(0);
if (s != null) {
entry.setType(s);
}
}
return entry.getFieldNames().isEmpty() ? Optional.empty() : Optional.of(entry);
}
Aggregations