-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Split MsBib functionality into Converter classes * Put all mappings inside a mapping class * Fixes #421 Remove LaTeX commands from all BibTeX fields when exporting to Word Bibliography
- Loading branch information
1 parent
34e8c94
commit a6769dd
Showing
28 changed files
with
644 additions
and
1,594 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
src/main/java/net/sf/jabref/logic/msbib/BibTeXConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package net.sf.jabref.logic.msbib; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import java.util.stream.Collectors; | ||
|
||
import net.sf.jabref.importer.fileformat.ImportFormat; | ||
import net.sf.jabref.logic.mods.PersonName; | ||
import net.sf.jabref.model.entry.BibEntry; | ||
|
||
public class BibTeXConverter { | ||
private static final String MSBIB_PREFIX = "msbib-"; | ||
|
||
public static BibEntry convert(MSBibEntry entry) { | ||
BibEntry result; | ||
Map<String, String> fieldValues = new HashMap<>(); | ||
|
||
if (entry.getCiteKey() == null) { | ||
result = new BibEntry(ImportFormat.DEFAULT_BIBTEXENTRY_ID, MSBibMapping.getBibTeXEntryType(entry.getType())); | ||
} else { | ||
// TODO: the cite key should not be the ID?! | ||
// id assumes an existing database so don't | ||
result = new BibEntry(entry.getCiteKey(), MSBibMapping.getBibTeXEntryType(entry.getType())); | ||
} | ||
|
||
// 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("language")) { | ||
int lcid = Integer.valueOf(fieldValues.get("language")); | ||
fieldValues.put("language", MSBibMapping.getLanguage(lcid)); | ||
} | ||
|
||
addAuthor(fieldValues, "author", entry.authors); | ||
addAuthor(fieldValues, MSBIB_PREFIX + "bookauthor", entry.bookAuthors); | ||
addAuthor(fieldValues, "editor", entry.editors); | ||
addAuthor(fieldValues, MSBIB_PREFIX + "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("pages", entry.pages.toString("--")); | ||
} | ||
|
||
parseStandardNumber(entry.standardNumber, fieldValues); | ||
|
||
if (entry.address != null) { | ||
fieldValues.put("address", entry.address); | ||
} | ||
// TODO: ConferenceName is saved as booktitle when converting from MSBIB to BibTeX | ||
if (entry.conferenceName != null) { | ||
fieldValues.put("organization", entry.conferenceName); | ||
} | ||
|
||
if (entry.dateAccessed != null) { | ||
fieldValues.put(MSBIB_PREFIX + "accessed", entry.dateAccessed); | ||
} | ||
|
||
// set all fields | ||
result.setField(fieldValues); | ||
|
||
return result; | ||
} | ||
|
||
private static void addAuthor(Map<String, String> map, String type, List<PersonName> authors) { | ||
if (authors == null) { | ||
return; | ||
} | ||
String allAuthors = authors.stream().map(PersonName::getFullname).collect(Collectors.joining(" and ")); | ||
|
||
map.put(type, allAuthors); | ||
} | ||
|
||
private static void parseSingleStandardNumber(String type, String bibtype, String standardNum, Map<String, String> map) { | ||
Pattern pattern = Pattern.compile(':' + type + ":(.[^:]+)"); | ||
Matcher matcher = pattern.matcher(standardNum); | ||
if (matcher.matches()) { | ||
map.put(bibtype, matcher.group(1)); | ||
} | ||
} | ||
|
||
private static void parseStandardNumber(String standardNum, Map<String, String> map) { | ||
if (standardNum == null) { | ||
return; | ||
} | ||
parseSingleStandardNumber("ISBN", "isbn", standardNum, map); | ||
parseSingleStandardNumber("ISSN", "issn", standardNum, map); | ||
parseSingleStandardNumber("LCCN", "lccn", standardNum, map); | ||
parseSingleStandardNumber("MRN", "mrnumber", standardNum, map); | ||
parseSingleStandardNumber("DOI", "doi", standardNum, map); | ||
} | ||
} |
143 changes: 143 additions & 0 deletions
143
src/main/java/net/sf/jabref/logic/msbib/MSBibConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
package net.sf.jabref.logic.msbib; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import net.sf.jabref.logic.layout.format.LatexToUnicodeFormatter; | ||
import net.sf.jabref.logic.layout.format.RemoveBrackets; | ||
import net.sf.jabref.logic.mods.PageNumbers; | ||
import net.sf.jabref.logic.mods.PersonName; | ||
import net.sf.jabref.model.entry.BibEntry; | ||
|
||
public class MSBibConverter { | ||
private static final String MSBIB_PREFIX = "msbib-"; | ||
private static final String BIBTEX_PREFIX = "BIBTEX_"; | ||
|
||
public static MSBibEntry convert(BibEntry entry) { | ||
MSBibEntry result = new MSBibEntry(); | ||
|
||
// memorize original type | ||
result.fields.put(BIBTEX_PREFIX + "Entry", entry.getType()); | ||
// define new type | ||
String msbibType = result.fields.put("SourceType", MSBibMapping.getMSBibEntryType(entry.getType()).name()); | ||
|
||
for (String field : entry.getFieldNames()) { | ||
// clean field | ||
String unicodeField = removeLaTeX(entry.getField(field)); | ||
|
||
if (MSBibMapping.getMSBibField(field) != null) { | ||
result.fields.put(MSBibMapping.getMSBibField(field), unicodeField); | ||
} | ||
} | ||
|
||
// Duplicate: also added as BookTitle | ||
if (entry.hasField("booktitle")) { | ||
result.conferenceName = entry.getField("booktitle"); | ||
} | ||
|
||
if (entry.hasField("pages")) { | ||
result.pages = new PageNumbers(entry.getField("pages")); | ||
} | ||
|
||
if (entry.hasField(MSBIB_PREFIX + "accessed")) { | ||
result.dateAccessed = entry.getField(MSBIB_PREFIX + "accessed"); | ||
} | ||
|
||
// TODO: currently this can never happen | ||
if ("SoundRecording".equals(msbibType) && (entry.hasField("title"))) { | ||
result.albumTitle = entry.getField("title"); | ||
} | ||
|
||
// TODO: currently this can never happen | ||
if ("Interview".equals(msbibType) && (entry.hasField("title"))) { | ||
result.broadcastTitle = entry.getField("title"); | ||
} | ||
|
||
// Value must be converted | ||
if (entry.hasField("language")) { | ||
result.fields.put("LCID", String.valueOf(MSBibMapping.getLCID(entry.getField("language")))); | ||
} | ||
|
||
result.standardNumber = ""; | ||
if (entry.hasField("isbn")) { | ||
result.standardNumber += " ISBN: " + entry.getField("isbn"); | ||
} | ||
if (entry.hasField("issn")) { | ||
result.standardNumber += " ISSN: " + entry.getField("issn"); | ||
} | ||
if (entry.hasField("lccn")) { | ||
result.standardNumber += " LCCN: " + entry.getField("lccn"); | ||
} | ||
if (entry.hasField("mrnumber")) { | ||
result.standardNumber += " MRN: " + entry.getField("mrnumber"); | ||
} | ||
if (entry.hasField("doi")) { | ||
result.standardNumber += " DOI: " + entry.getField("doi"); | ||
} | ||
if (result.standardNumber.isEmpty()) { | ||
result.standardNumber = null; | ||
} | ||
|
||
if (entry.hasField("address")) { | ||
result.address = entry.getField("address"); | ||
} | ||
|
||
if (entry.hasField("type")) { | ||
result.thesisType = entry.getField("type"); | ||
} else { | ||
if ("techreport".equalsIgnoreCase(entry.getType())) { | ||
result.thesisType = "Tech. rep."; | ||
} else if ("mastersthesis".equalsIgnoreCase(entry.getType())) { | ||
result.thesisType = "Master's thesis"; | ||
} else if ("phdthesis".equalsIgnoreCase(entry.getType())) { | ||
result.thesisType = "Ph.D. dissertation"; | ||
} else if ("unpublished".equalsIgnoreCase(entry.getType())) { | ||
result.thesisType = "unpublished"; | ||
} | ||
} | ||
|
||
// TODO: currently this can never happen | ||
if (("InternetSite".equals(msbibType) || "DocumentFromInternetSite".equals(msbibType)) | ||
&& (entry.hasField("title"))) { | ||
result.internetSiteTitle = entry.getField("title"); | ||
} | ||
|
||
// TODO: currently only Misc can happen | ||
if (("ElectronicSource".equals(msbibType) || "Art".equals(msbibType) || "Misc".equals(msbibType)) | ||
&& (entry.hasField("title"))) { | ||
result.publicationTitle = entry.getField("title"); | ||
} | ||
|
||
if (entry.hasField("author")) { | ||
result.authors = getAuthors(entry.getField("author")); | ||
} | ||
if (entry.hasField("editor")) { | ||
result.editors = getAuthors(entry.getField("editor")); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
private static List<PersonName> getAuthors(String authors) { | ||
List<PersonName> result = new ArrayList<>(); | ||
|
||
// TODO: case-insensitive?! | ||
if (authors.contains(" and ")) { | ||
String[] names = authors.split(" and "); | ||
for (String name : names) { | ||
result.add(new PersonName(name)); | ||
} | ||
} else { | ||
result.add(new PersonName(authors)); | ||
} | ||
return result; | ||
} | ||
|
||
private static String removeLaTeX(String text) { | ||
// TODO: just use latex free version everywhere in the future | ||
String result = new RemoveBrackets().format(text); | ||
result = new LatexToUnicodeFormatter().format(result); | ||
|
||
return result; | ||
} | ||
} |
Oops, something went wrong.