Skip to content

Commit

Permalink
Export single corporate author to ms office xml (#2688)
Browse files Browse the repository at this point in the history
* Export single corporate author to ms office xml
#1497

* Add test files for corporate Authors export

* Add changelog entry

* Improved author handling in MS-Office Import/Export
  • Loading branch information
Siedlerchr authored and tobiasdiez committed Mar 30, 2017
1 parent 41decf4 commit 0c34fa4
Show file tree
Hide file tree
Showing 13 changed files with 244 additions and 193 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#
- We added an option to copy the title of BibTeX entries to the clipboard through `Edit -> Copy title` (implements [#210](https://github.com/koppor/jabref/issues/210))
- Several scrollbars were added to the preference dialog which show up when content is too large [#2559](https://github.com/JabRef/jabref/issues/2559)
- We fixed and improved the auto detection of the OpenOffice and LibreOffice connection
- Entries with a single corporate author are now correclty exported to the corresponding `corporate` author field in MS-Office XML. [#1497](https://github.com/JabRef/jabref/issues/1497)
- Improved author handling in MS-Office Import/Export

### Fixed
- We fixed an issue where authors with multiple surnames were not presented correctly in the main table. [#2534](https://github.com/JabRef/jabref/issues/2534)
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/jabref/logic/msbib/BibTeXConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ public static BibEntry convert(MSBibEntry entry) {
return result;
}

private static void addAuthor(Map<String, String> map, String type, List<PersonName> authors) {
private static void addAuthor(Map<String, String> map, String type, List<MsBibAuthor> authors) {
if (authors == null) {
return;
}
String allAuthors = authors.stream().map(PersonName::getFullname).collect(Collectors.joining(" and "));
String allAuthors = authors.stream().map(MsBibAuthor::getLastFirst).collect(Collectors.joining(" and "));

map.put(type, allAuthors);
}
Expand Down
27 changes: 15 additions & 12 deletions src/main/java/org/jabref/logic/msbib/MSBibConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import org.jabref.model.entry.Author;
import org.jabref.model.entry.AuthorList;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.FieldName;

Expand Down Expand Up @@ -103,23 +104,25 @@ public static MSBibEntry convert(BibEntry entry) {
result.publicationTitle = entry.getField(FieldName.TITLE).orElse(null);
}

entry.getLatexFreeField(FieldName.AUTHOR).ifPresent(authors -> result.authors = getAuthors(authors));
entry.getLatexFreeField(FieldName.EDITOR).ifPresent(editors -> result.editors = getAuthors(editors));
entry.getField(FieldName.AUTHOR).ifPresent(authors -> result.authors = getAuthors(authors));
entry.getField(FieldName.EDITOR).ifPresent(editors -> result.editors = getAuthors(editors));

return result;
}

private static List<PersonName> getAuthors(String authors) {
List<PersonName> result = new ArrayList<>();
private static List<MsBibAuthor> getAuthors(String authors) {
List<MsBibAuthor> result = new ArrayList<>();
boolean corporate = false;
//Only one corporate authors is supported
if (authors.startsWith("{") && authors.endsWith("}")) {
corporate = true;
}
AuthorList authorList = AuthorList.parse(authors);

if (authors.toUpperCase(Locale.ENGLISH).contains(" AND ")) {
String[] names = authors.split(" (?i)and ");
for (String name : names) {
result.add(new PersonName(name));
}
} else {
result.add(new PersonName(authors));
for (Author author : authorList.getAuthors()) {
result.add(new MsBibAuthor(author, corporate));
}

return result;
}

Expand Down
89 changes: 58 additions & 31 deletions src/main/java/org/jabref/logic/msbib/MSBibEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jabref.model.entry.Author;
import org.jabref.model.entry.AuthorList;
import org.jabref.model.strings.StringUtil;

import org.w3c.dom.Document;
Expand All @@ -26,21 +29,21 @@ class MSBibEntry {
// MSBib fields and values
public Map<String, String> fields = new HashMap<>();

public List<PersonName> authors;
public List<PersonName> bookAuthors;
public List<PersonName> editors;
public List<PersonName> translators;
public List<PersonName> producerNames;
public List<PersonName> composers;
public List<PersonName> conductors;
public List<PersonName> performers;
public List<PersonName> writers;
public List<PersonName> directors;
public List<PersonName> compilers;
public List<PersonName> interviewers;
public List<PersonName> interviewees;
public List<PersonName> inventors;
public List<PersonName> counsels;
public List<MsBibAuthor> authors;
public List<MsBibAuthor> bookAuthors;
public List<MsBibAuthor> editors;
public List<MsBibAuthor> translators;
public List<MsBibAuthor> producerNames;
public List<MsBibAuthor> composers;
public List<MsBibAuthor> conductors;
public List<MsBibAuthor> performers;
public List<MsBibAuthor> writers;
public List<MsBibAuthor> directors;
public List<MsBibAuthor> compilers;
public List<MsBibAuthor> interviewers;
public List<MsBibAuthor> interviewees;
public List<MsBibAuthor> inventors;
public List<MsBibAuthor> counsels;

public PageNumbers pages;

Expand Down Expand Up @@ -203,8 +206,8 @@ private void getAuthors(Element authorsElem) {
counsels = getSpecificAuthors("Counsel", authorsElem);
}

private List<PersonName> getSpecificAuthors(String type, Element authors) {
List<PersonName> result = null;
private List<MsBibAuthor> getSpecificAuthors(String type, Element authors) {
List<MsBibAuthor> result = null;
NodeList nodeLst = authors.getElementsByTagNameNS("*", type);
if (nodeLst.getLength() <= 0) {
return result;
Expand All @@ -223,17 +226,25 @@ private List<PersonName> getSpecificAuthors(String type, Element authors) {
NodeList firstName = ((Element) person.item(i)).getElementsByTagNameNS("*", "First");
NodeList lastName = ((Element) person.item(i)).getElementsByTagNameNS("*", "Last");
NodeList middleName = ((Element) person.item(i)).getElementsByTagNameNS("*", "Middle");
PersonName name = new PersonName();

StringBuilder sb = new StringBuilder();

if (firstName.getLength() > 0) {
name.setFirstname(firstName.item(0).getTextContent());
sb.append(firstName.item(0).getTextContent());
sb.append(" ");
}
if (middleName.getLength() > 0) {
name.setMiddlename(middleName.item(0).getTextContent());
sb.append(middleName.item(0).getTextContent());
sb.append(" ");
}
if (lastName.getLength() > 0) {
name.setSurname(lastName.item(0).getTextContent());
sb.append(lastName.item(0).getTextContent());
}

AuthorList authorList = AuthorList.parse(sb.toString());
for (Author author : authorList.getAuthors()) {
result.add(new MsBibAuthor(author));
}
result.add(name);
}

return result;
Expand Down Expand Up @@ -316,21 +327,37 @@ private void addField(Document document, Element parent, String name, String val
parent.appendChild(elem);
}

private void addAuthor(Document document, Element allAuthors, String entryName, List<PersonName> authorsLst) {
//Add authors for export
private void addAuthor(Document document, Element allAuthors, String entryName, List<MsBibAuthor> authorsLst) {
if (authorsLst == null) {
return;
}
Element authorTop = document.createElementNS(MSBibDatabase.NAMESPACE, MSBibDatabase.PREFIX + entryName);
Element nameList = document.createElementNS(MSBibDatabase.NAMESPACE, MSBibDatabase.PREFIX + "NameList");
for (PersonName name : authorsLst) {
Element person = document.createElementNS(MSBibDatabase.NAMESPACE, MSBibDatabase.PREFIX + "Person");
addField(document, person, "Last", name.getSurname());
addField(document, person, "Middle", name.getMiddlename());
addField(document, person, "First", name.getFirstname());
nameList.appendChild(person);

Optional<MsBibAuthor> personName = authorsLst.stream().filter(MsBibAuthor::isCorporate)
.findFirst();
if (personName.isPresent()) {
MsBibAuthor person = personName.get();

Element corporate = document.createElementNS(MSBibDatabase.NAMESPACE,
MSBibDatabase.PREFIX + "Corporate");
corporate.setTextContent(person.getFirstLast());
authorTop.appendChild(corporate);
} else {

Element nameList = document.createElementNS(MSBibDatabase.NAMESPACE, MSBibDatabase.PREFIX + "NameList");
for (MsBibAuthor name : authorsLst) {
Element person = document.createElementNS(MSBibDatabase.NAMESPACE, MSBibDatabase.PREFIX + "Person");
addField(document, person, "Last", name.getLastName());
addField(document, person, "Middle", name.getMiddleName());
addField(document, person, "First", name.getFirstName());
nameList.appendChild(person);

}
authorTop.appendChild(nameList);
}
authorTop.appendChild(nameList);
allAuthors.appendChild(authorTop);

}

private void addAddress(Document document, Element parent, String addressToSplit) {
Expand Down
66 changes: 66 additions & 0 deletions src/main/java/org/jabref/logic/msbib/MsBibAuthor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package org.jabref.logic.msbib;

import org.jabref.model.entry.Author;

public class MsBibAuthor {

private String firstName;
private String middleName;
private final Author author;
private boolean isCorporate;

public MsBibAuthor(Author author) {
this.author = author;

StringBuilder sb = new StringBuilder();
author.getFirst().ifPresent(firstNames -> {

String[] names = firstNames.split(" ");
for (int i = 1; i < names.length; i++) {
sb.append(names[i]);
sb.append(" ");
}
this.middleName = sb.toString().trim();
this.firstName = names[0];
});

}

public MsBibAuthor(Author author, boolean isCorporate) {
this(author);
this.isCorporate = isCorporate;

}

public String getFirstName() {

if (!"".equals(firstName)) {
return firstName;
}
return author.getFirst().orElse(null);
}

public String getMiddleName() {
if ("".equals(middleName)) {
return null;
}
return middleName;
}

public String getLastName() {
return author.getLast().orElse(null);
}

public String getFirstLast() {
return author.getFirstLast(false);
}

public String getLastFirst() {
return author.getLastFirst(false);
}

public boolean isCorporate() {
return isCorporate;
}

}
Loading

0 comments on commit 0c34fa4

Please sign in to comment.