-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Export to Office 2007 #1765
Export to Office 2007 #1765
Changes from 2 commits
a7f51e6
968d347
204c640
1a6abc2
934fc67
4afb70e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,8 @@ | |
import net.sf.jabref.logic.mods.PersonName; | ||
import net.sf.jabref.model.entry.BibEntry; | ||
import net.sf.jabref.model.entry.FieldName; | ||
import net.sf.jabref.model.entry.MonthUtil; | ||
import net.sf.jabref.model.entry.MonthUtil.Month; | ||
|
||
public class BibTeXConverter { | ||
private static final String MSBIB_PREFIX = "msbib-"; | ||
|
@@ -19,12 +21,14 @@ public static BibEntry convert(MSBibEntry entry) { | |
BibEntry result; | ||
Map<String, String> fieldValues = new HashMap<>(); | ||
|
||
String bibTexEntryType = MSBibMapping.getBibTeXEntryType(entry.getType()); | ||
if (entry.getCiteKey() == null) { | ||
result = new BibEntry(ImportFormat.DEFAULT_BIBTEXENTRY_ID, MSBibMapping.getBibTeXEntryType(entry.getType())); | ||
result = new BibEntry(ImportFormat.DEFAULT_BIBTEXENTRY_ID, bibTexEntryType); | ||
|
||
} 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())); | ||
result = new BibEntry(entry.getCiteKey(), bibTexEntryType); | ||
} | ||
|
||
// add String fields | ||
|
@@ -77,12 +81,26 @@ public static BibEntry convert(MSBibEntry entry) { | |
fieldValues.put(MSBIB_PREFIX + "accessed", entry.dateAccessed); | ||
} | ||
|
||
if(entry.journalName != null) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't forget to add the space after the |
||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. { in line above |
||
fieldValues.put(FieldName.JOURNALTITLE, entry.journalName); | ||
} | ||
if (entry.month != null) { | ||
Month month = MonthUtil.getMonth(entry.month); | ||
fieldValues.put(FieldName.MONTH, month.shortName); | ||
} | ||
if (entry.number != null) { | ||
fieldValues.put(FieldName.NUMBER, entry.number); | ||
} | ||
|
||
|
||
// set all fields | ||
result.setField(fieldValues); | ||
|
||
return result; | ||
} | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why another emtpy line? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe a result of the rebasing. Will fix it. |
||
private static void addAuthor(Map<String, String> map, String type, List<PersonName> authors) { | ||
if (authors == null) { | ||
return; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,18 +2,20 @@ | |
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Locale; | ||
|
||
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; | ||
import net.sf.jabref.model.entry.FieldName; | ||
|
||
public class MSBibConverter { | ||
|
||
private static final String MSBIB_PREFIX = "msbib-"; | ||
private static final String BIBTEX_PREFIX = "BIBTEX_"; | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Empty line |
||
public static MSBibEntry convert(BibEntry entry) { | ||
MSBibEntry result = new MSBibEntry(); | ||
|
||
|
@@ -24,67 +26,61 @@ public static MSBibEntry convert(BibEntry entry) { | |
|
||
for (String field : entry.getFieldNames()) { | ||
// clean field | ||
String unicodeField = removeLaTeX(entry.getField(field)); | ||
String unicodeField = removeLaTeX(entry.getFieldOptional(field).orElse("")); | ||
|
||
if (MSBibMapping.getMSBibField(field) != null) { | ||
result.fields.put(MSBibMapping.getMSBibField(field), unicodeField); | ||
} | ||
} | ||
|
||
// Duplicate: also added as BookTitle | ||
if (entry.hasField(FieldName.BOOKTITLE)) { | ||
result.conferenceName = entry.getField(FieldName.BOOKTITLE); | ||
} | ||
|
||
if (entry.hasField(FieldName.PAGES)) { | ||
result.pages = new PageNumbers(entry.getField(FieldName.PAGES)); | ||
} | ||
|
||
if (entry.hasField(MSBIB_PREFIX + "accessed")) { | ||
result.dateAccessed = entry.getField(MSBIB_PREFIX + "accessed"); | ||
} | ||
entry.getFieldOptional(FieldName.BOOKTITLE).ifPresent(booktitle -> result.conferenceName = booktitle); | ||
entry.getFieldOptional(FieldName.PAGES).ifPresent(pages -> result.pages = new PageNumbers(pages)); | ||
entry.getFieldOptional(MSBIB_PREFIX + "accessed").ifPresent(accesed -> result.dateAccessed = accesed); | ||
|
||
// TODO: currently this can never happen | ||
if ("SoundRecording".equals(msbibType) && (entry.hasField(FieldName.TITLE))) { | ||
result.albumTitle = entry.getField(FieldName.TITLE); | ||
if ("SoundRecording".equals(msbibType)) { | ||
result.albumTitle = entry.getFieldOptional(FieldName.TITLE).orElse(null); | ||
} | ||
|
||
// TODO: currently this can never happen | ||
if ("Interview".equals(msbibType) && (entry.hasField(FieldName.TITLE))) { | ||
result.broadcastTitle = entry.getField(FieldName.TITLE); | ||
if ("Interview".equals(msbibType)) { | ||
result.broadcastTitle = entry.getFieldOptional(FieldName.TITLE).orElse(null); | ||
} | ||
|
||
// Value must be converted | ||
if (entry.hasField(FieldName.LANGUAGE)) { | ||
result.fields.put("LCID", String.valueOf(MSBibMapping.getLCID(entry.getField(FieldName.LANGUAGE)))); | ||
if ("Patent".equalsIgnoreCase(entry.getType())) { | ||
result.patentNumber = entry.getFieldOptional(FieldName.NUMBER).orElse(null); | ||
} | ||
|
||
result.standardNumber = ""; | ||
if (entry.hasField(FieldName.ISBN)) { | ||
result.standardNumber += " ISBN: " + entry.getField(FieldName.ISBN); | ||
} | ||
if (entry.hasField(FieldName.ISSN)) { | ||
result.standardNumber += " ISSN: " + entry.getField(FieldName.ISSN); | ||
} | ||
if (entry.hasField("lccn")) { | ||
result.standardNumber += " LCCN: " + entry.getField("lccn"); | ||
} | ||
if (entry.hasField("mrnumber")) { | ||
result.standardNumber += " MRN: " + entry.getField("mrnumber"); | ||
result.journalName = entry.getFieldOrAlias(FieldName.JOURNAL).orElse(null); | ||
result.month = entry.getFieldOrAlias(FieldName.MONTH).orElse(null); | ||
|
||
if (!entry.getFieldOptional(FieldName.YEAR).isPresent()) { | ||
result.year = entry.getFieldOrAlias(FieldName.YEAR).orElse(null); | ||
} | ||
if (entry.hasField(FieldName.DOI)) { | ||
result.standardNumber += " DOI: " + entry.getField(FieldName.DOI); | ||
|
||
if (!entry.getFieldOptional(FieldName.ISSUE).isPresent()) { | ||
result.number = entry.getFieldOptional(FieldName.NUMBER).orElse(null); | ||
} | ||
// Value must be converted | ||
//Currently only english is supported | ||
entry.getFieldOptional(FieldName.LANGUAGE) | ||
.ifPresent(lang -> result.fields.put("LCID", String.valueOf(MSBibMapping.getLCID(lang)))); | ||
result.standardNumber = ""; | ||
entry.getFieldOptional(FieldName.ISBN).ifPresent(isbn -> result.standardNumber += " ISBN: " + isbn); | ||
entry.getFieldOptional(FieldName.ISSN).ifPresent(issn -> result.standardNumber += " ISSN: " + issn); | ||
entry.getFieldOptional("lccn").ifPresent(lccn -> result.standardNumber += " LCCN: " + lccn); | ||
entry.getFieldOptional("mrnumber").ifPresent(mrnumber -> result.standardNumber += " MRN: " + mrnumber); | ||
|
||
if (result.standardNumber.isEmpty()) { | ||
result.standardNumber = null; | ||
} | ||
|
||
if (entry.hasField(FieldName.ADDRESS)) { | ||
result.address = entry.getField(FieldName.ADDRESS); | ||
} | ||
result.address = entry.getFieldOrAlias(FieldName.ADDRESS).orElse(null); | ||
|
||
if (entry.getFieldOptional(FieldName.TYPE).isPresent()) { | ||
result.thesisType = entry.getFieldOptional(FieldName.TYPE).get(); | ||
|
||
if (entry.hasField(FieldName.TYPE)) { | ||
result.thesisType = entry.getField(FieldName.TYPE); | ||
} else { | ||
if ("techreport".equalsIgnoreCase(entry.getType())) { | ||
result.thesisType = "Tech. rep."; | ||
|
@@ -104,27 +100,23 @@ public static MSBibEntry convert(BibEntry entry) { | |
} | ||
|
||
// TODO: currently only Misc can happen | ||
if (("ElectronicSource".equals(msbibType) || "Art".equals(msbibType) || "Misc".equals(msbibType)) | ||
&& (entry.hasField(FieldName.TITLE))) { | ||
result.publicationTitle = entry.getField(FieldName.TITLE); | ||
if ("ElectronicSource".equals(msbibType) || "Art".equals(msbibType) || "Misc".equals(msbibType)) { | ||
result.publicationTitle = entry.getFieldOptional(FieldName.TITLE).orElse(null); | ||
} | ||
|
||
if (entry.hasField(FieldName.AUTHOR)) { | ||
result.authors = getAuthors(entry.getField(FieldName.AUTHOR)); | ||
} | ||
if (entry.hasField(FieldName.EDITOR)) { | ||
result.editors = getAuthors(entry.getField(FieldName.EDITOR)); | ||
} | ||
entry.getFieldOptional(FieldName.AUTHOR).ifPresent(authors -> result.authors = getAuthors(authors)); | ||
entry.getFieldOptional(FieldName.EDITOR).ifPresent(editors -> result.editors = getAuthors(editors)); | ||
|
||
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 "); | ||
authors = removeLaTeX(authors); | ||
|
||
if (authors.toUpperCase(Locale.ENGLISH).contains(" AND ")) { | ||
String[] names = authors.split(" (?i)and "); | ||
for (String name : names) { | ||
result.add(new PersonName(name)); | ||
} | ||
|
@@ -135,10 +127,6 @@ private static List<PersonName> getAuthors(String authors) { | |
} | ||
|
||
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; | ||
return new LatexToUnicodeFormatter().format(text); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ | |
* @see <a href="http://www.ecma-international.org/publications/standards/Ecma-376.htm">ECMA Standard</a> | ||
*/ | ||
class MSBibEntry { | ||
|
||
// MSBib fields and values | ||
public Map<String, String> fields = new HashMap<>(); | ||
|
||
|
@@ -54,6 +55,14 @@ class MSBibEntry { | |
public String publicationTitle; | ||
public String albumTitle; | ||
public String broadcastTitle; | ||
public String year; | ||
public String month; | ||
public String day; | ||
public String number; | ||
public String patentNumber; | ||
public String journalName; | ||
|
||
private String bibtexEntryType; | ||
|
||
// reduced subset, supports only "CITY , STATE, COUNTRY" | ||
// \b(\w+)\s?[,]?\s?(\w+)\s?[,]?\s?(\w+)\b | ||
|
@@ -65,7 +74,9 @@ class MSBibEntry { | |
// (\d{1,2})\s?[.,-/]\s?(\d{1,2})\s?[.,-/]\s?(\d{2,4}) | ||
// 1-2 DIGITS SPACE SEPERATOR SPACE 1-2 DIGITS SPACE SEPERATOR SPACE 2-4 DIGITS | ||
// tested using http://www.javaregex.com/test.html | ||
private static final Pattern DATE_PATTERN = Pattern.compile("(\\d{1,2})\\s*[.,-/]\\s*(\\d{1,2})\\s*[.,-/]\\s*(\\d{2,4})"); | ||
private static final Pattern DATE_PATTERN = Pattern | ||
.compile("(\\d{1,2})\\s*[.,-/]\\s*(\\d{1,2})\\s*[.,-/]\\s*(\\d{2,4})"); | ||
|
||
|
||
public MSBibEntry() { | ||
|
||
|
@@ -99,6 +110,9 @@ private void populateFromXml(Element entry) { | |
String key = node.getLocalName(); | ||
String value = node.getTextContent(); | ||
|
||
if ("SourceType".equals(key)) { | ||
this.bibtexEntryType = value; | ||
} | ||
fields.put(key, value); | ||
} | ||
} | ||
|
@@ -129,19 +143,26 @@ private void populateFromXml(Element entry) { | |
address = null; | ||
} | ||
|
||
if ("Patent".equalsIgnoreCase(bibtexEntryType)) { | ||
number = getXmlElementTextContent("PatentNumber", entry); | ||
} | ||
journalName = getXmlElementTextContent("JournalName", entry); | ||
month = getXmlElementTextContent("Month", entry); | ||
internetSiteTitle = getXmlElementTextContent("InternetSiteTitle", entry); | ||
String month = getXmlElementTextContent("MonthAccessed", entry); | ||
String day = getXmlElementTextContent("DayAccessed", entry); | ||
String year = getXmlElementTextContent("YearAccessed", entry); | ||
|
||
String monthAccessed = getXmlElementTextContent("MonthAccessed", entry); | ||
String dayAccessed = getXmlElementTextContent("DayAccessed", entry); | ||
String yearAccessed = getXmlElementTextContent("YearAccessed", entry); | ||
|
||
dateAccessed = ""; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. StringBuilder/StringBuffer? |
||
if (month != null) { | ||
dateAccessed += month + ' '; | ||
if (monthAccessed != null) { | ||
dateAccessed += monthAccessed + ' '; | ||
} | ||
if (day != null) { | ||
dateAccessed += day + ", "; | ||
if (dayAccessed != null) { | ||
dateAccessed += dayAccessed + ", "; | ||
} | ||
if (year != null) { | ||
dateAccessed += year; | ||
if (yearAccessed != null) { | ||
dateAccessed += yearAccessed; | ||
} | ||
dateAccessed = dateAccessed.trim(); | ||
if (dateAccessed.isEmpty() || ",".equals(dateAccessed)) { | ||
|
@@ -248,6 +269,12 @@ public Element getDOM(Document document) { | |
if (pages != null) { | ||
addField(document, rootNode, "Pages", pages.toString("-")); | ||
} | ||
addField(document, rootNode, "Year", year); | ||
addField(document, rootNode, "Month", month); | ||
|
||
addField(document, rootNode, "JournalName", journalName); | ||
addField(document, rootNode, "PatentNumber", patentNumber); | ||
|
||
addField(document, rootNode, "StandardNumber", standardNumber); | ||
addField(document, rootNode, "ConferenceName", conferenceName); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
all field names in lower case. Space after
number
. Patents with lower case "p"