Skip to content
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

Merged
merged 6 commits into from
Aug 25, 2016
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#
- Fixed [#1531](https://github.com/JabRef/jabref/issues/1531): `\relax` can be used for abbreviation of author names
- Fixed [#1771](https://github.com/JabRef/jabref/issues/1771): Show all supported import types as default
- Fixed [#1804](https://github.com/JabRef/jabref/issues/1804): Integrity check no longer removes URL field by mistake


- Fixed [#1750](https://github.com/JabRef/jabref/issues/1750): BibLaTeX `date` field is now correctly exported as `year` in MS-Office 2007 xml format
- Fixed: LaTeX characters in author names are now converted to Unicode before export in MS-Office 2007 xml format
- Fixed: `Volume`, `Journaltitle`, `Issue` and `number`(for Patents) fields are now exported correctly in MS-Office 2007 xml format
Copy link
Member

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"


### Removed
- It is not longer possible to choose to convert HTML sub- and superscripts to equations
Expand Down
22 changes: 20 additions & 2 deletions src/main/java/net/sf/jabref/logic/msbib/BibTeXConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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-";
Expand All @@ -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
Expand Down Expand Up @@ -77,12 +81,26 @@ public static BibEntry convert(MSBibEntry entry) {
fieldValues.put(MSBIB_PREFIX + "accessed", entry.dateAccessed);
}

if(entry.journalName != null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't forget to add the space after the if and move the bracket in the same line.

{
Copy link
Member

Choose a reason for hiding this comment

The 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;
}


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why another emtpy line?

Copy link
Member Author

Choose a reason for hiding this comment

The 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;
Expand Down
100 changes: 44 additions & 56 deletions src/main/java/net/sf/jabref/logic/msbib/MSBibConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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_";


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Empty line

public static MSBibEntry convert(BibEntry entry) {
MSBibEntry result = new MSBibEntry();

Expand All @@ -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.";
Expand All @@ -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));
}
Expand All @@ -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);
}
}
47 changes: 37 additions & 10 deletions src/main/java/net/sf/jabref/logic/msbib/MSBibEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<>();

Expand Down Expand Up @@ -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
Expand All @@ -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() {

Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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 = "";
Copy link
Contributor

Choose a reason for hiding this comment

The 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)) {
Expand Down Expand Up @@ -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);

Expand Down
Loading