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

Include all standard columns with citationkey when exporting to old OpenOffice Calc format #8176

Merged
merged 5 commits into from
Oct 26, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
237 changes: 92 additions & 145 deletions src/main/java/org/jabref/logic/exporter/OOCalcDatabase.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package org.jabref.logic.exporter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.jabref.logic.bibtex.comparator.FieldComparator;
Expand All @@ -15,6 +15,7 @@
import org.jabref.model.database.BibDatabase;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.field.Field;
import org.jabref.model.entry.field.FieldFactory;
import org.jabref.model.entry.field.InternalField;
import org.jabref.model.entry.field.StandardField;
import org.jabref.model.entry.field.UnknownField;
Expand All @@ -28,178 +29,124 @@
class OOCalcDatabase {

private static final Logger LOGGER = LoggerFactory.getLogger(OOCalcDatabase.class);
private static final Field REPORT_TYPE_FIELD = new UnknownField("reporttype");

private final List<BibEntry> entries;
private final List<BibEntry> entries = new ArrayList<>();
private final List<Field> toExportFields = Stream.concat(FieldFactory.getStandardFieldsWithCitationKey().stream(), Stream.of(REPORT_TYPE_FIELD))
.collect(Collectors.toList());

public OOCalcDatabase(BibDatabase bibtex, List<BibEntry> entries) {
// Make a list of comparators for sorting the entries:
this.entries.addAll(entries != null ? entries : bibtex.getEntries());

List<FieldComparator> comparators = new ArrayList<>();
comparators.add(new FieldComparator(StandardField.AUTHOR));
comparators.add(new FieldComparator(StandardField.YEAR));
comparators.add(new FieldComparator(InternalField.KEY_FIELD));
// Use glazed lists to get a sorted view of the entries:
List<BibEntry> entryList = new ArrayList<>();
// Set up a list of all entries, if keySet==null, or the entries whose
// ids are in keySet, otherwise:
if (entries == null) {
entryList.addAll(bibtex.getEntries());
} else {
entryList.addAll(entries);
}
Collections.sort(entryList, new FieldComparatorStack<>(comparators));
this.entries = entryList;

this.entries.sort(new FieldComparatorStack<>(comparators));
}

private static String getField(BibEntry e, Field field) {
return e.getField(field).orElse("");
}

public Document getDOMrepresentation() {
Document result = null;
Document document = null;
try {
DocumentBuilder dbuild = DocumentBuilderFactory.newInstance().newDocumentBuilder();
result = dbuild.newDocument();
Element collection = result.createElement("office:document-content");
// collection.setAttribute("xmlns", "http://openoffice.org/2000/office");
collection.setAttribute("xmlns:office", "http://openoffice.org/2000/office");
collection.setAttribute("xmlns:style", "http://openoffice.org/2000/style");
collection.setAttribute("xmlns:text", "http://openoffice.org/2000/text");
collection.setAttribute("xmlns:table", "http://openoffice.org/2000/table");
collection.setAttribute("xmlns:office:class", "spreadsheet");
collection.setAttribute("xmlns:office:version", "1.0");
collection.setAttribute("xmlns:fo", "http://www.w3.org/1999/XSL/Format");
Element el = result.createElement("office:script");
collection.appendChild(el);

el = result.createElement("office:automatic-styles");
Element el2 = result.createElement("style:style");
el2.setAttribute("style:name", "ro1");
el2.setAttribute("style:family", "table-row");
Element el3 = result.createElement("style.properties");
el3.setAttribute("style:row-height", "0.1681inch");
el3.setAttribute("fo:break-before", "auto");
el3.setAttribute("style:use-optimal-row-height", "true");
el2.appendChild(el3);
el.appendChild(el2);
el2 = result.createElement("style:style");
el2.setAttribute("style:name", "ta1");
el2.setAttribute("style:family", "table");
el2.setAttribute("style:master-page-name", "Default");
el3 = result.createElement("style:properties");
el3.setAttribute("table:display", "true");
el2.appendChild(el3);
el.appendChild(el2);
collection.appendChild(el);

Element body = result.createElement("office:body");
Element table = result.createElement("table:table");
table.setAttribute("table:name", "biblio");
table.setAttribute("table.style-name", "ta1");

Element row = result.createElement("table:table-row");
row.setAttribute("table.style-name", "ro1");
addTableCell(result, row, "Type");
addTableCell(result, row, "ISBN");
addTableCell(result, row, "Identifier");
addTableCell(result, row, "Author");
addTableCell(result, row, "Title");
addTableCell(result, row, "Journal");
addTableCell(result, row, "Volume");
addTableCell(result, row, "Number");
addTableCell(result, row, "Month");
addTableCell(result, row, "Pages");
addTableCell(result, row, "Year");
addTableCell(result, row, "Address");
addTableCell(result, row, "Note");
addTableCell(result, row, "URL");
addTableCell(result, row, "Booktitle");
addTableCell(result, row, "Chapter");
addTableCell(result, row, "Edition");
addTableCell(result, row, "Series");
addTableCell(result, row, "Editor");
addTableCell(result, row, "Publisher");
addTableCell(result, row, "ReportType");
addTableCell(result, row, "Howpublished");
addTableCell(result, row, "Institution");
addTableCell(result, row, "Organization");
addTableCell(result, row, "School");
addTableCell(result, row, "Annote");
addTableCell(result, row, "Assignee");
addTableCell(result, row, "Day");
addTableCell(result, row, "Dayfiled");
addTableCell(result, row, "Monthfiled");
addTableCell(result, row, "Yearfiled");
addTableCell(result, row, "Language");
addTableCell(result, row, "Nationality");
addTableCell(result, row, "Revision");
addTableCell(result, row, "Custom1");
addTableCell(result, row, "Custom2");
addTableCell(result, row, "Custom3");
addTableCell(result, row, "Custom4");
addTableCell(result, row, "Custom5");
table.appendChild(row);

for (BibEntry e : entries) {
row = result.createElement("table:table-row");
addTableCell(result, row, new GetOpenOfficeType().format(e.getType().getName()));
addTableCell(result, row, getField(e, StandardField.ISBN));
addTableCell(result, row, getField(e, InternalField.KEY_FIELD));
addTableCell(result, row, getField(e, StandardField.AUTHOR)); // new AuthorLastFirst().format(getField(e, StandardField.AUTHOR_FIELD)));
addTableCell(result, row, new RemoveWhitespace().format(new RemoveBrackets().format(getField(e, StandardField.TITLE))));
addTableCell(result, row, getField(e, StandardField.JOURNAL));
addTableCell(result, row, getField(e, StandardField.VOLUME));
addTableCell(result, row, getField(e, StandardField.NUMBER));
addTableCell(result, row, getField(e, StandardField.MONTH));
addTableCell(result, row, getField(e, StandardField.PAGES));
addTableCell(result, row, getField(e, StandardField.YEAR));
addTableCell(result, row, getField(e, StandardField.ADDRESS));
addTableCell(result, row, getField(e, StandardField.NOTE));
addTableCell(result, row, getField(e, StandardField.URL));
addTableCell(result, row, getField(e, StandardField.BOOKTITLE));
addTableCell(result, row, getField(e, StandardField.CHAPTER));
addTableCell(result, row, getField(e, StandardField.EDITION));
addTableCell(result, row, getField(e, StandardField.SERIES));
addTableCell(result, row, getField(e, StandardField.EDITOR)); // new AuthorLastFirst().format(getField(e, StandardField.EDITOR_FIELD)));
addTableCell(result, row, getField(e, StandardField.PUBLISHER));
addTableCell(result, row, getField(e, new UnknownField("reporttype")));
addTableCell(result, row, getField(e, StandardField.HOWPUBLISHED));
addTableCell(result, row, getField(e, StandardField.INSTITUTION));
addTableCell(result, row, getField(e, StandardField.ORGANIZATION));
addTableCell(result, row, getField(e, StandardField.SCHOOL));
addTableCell(result, row, getField(e, StandardField.ANNOTE));
addTableCell(result, row, getField(e, StandardField.ASSIGNEE));
addTableCell(result, row, getField(e, StandardField.DAY));
addTableCell(result, row, getField(e, StandardField.DAYFILED));
addTableCell(result, row, getField(e, StandardField.MONTHFILED));
addTableCell(result, row, getField(e, StandardField.YEARFILED));
addTableCell(result, row, getField(e, StandardField.LANGUAGE));
addTableCell(result, row, getField(e, StandardField.NATIONALITY));
addTableCell(result, row, getField(e, StandardField.REVISION));
addTableCell(result, row, "");
addTableCell(result, row, "");
addTableCell(result, row, "");
addTableCell(result, row, "");
addTableCell(result, row, "");
table.appendChild(row);
}
document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
Element root = createRootElement(document);
Element body = document.createElement("office:body");
Element table = createTableElement(document);

body.appendChild(table);
collection.appendChild(body);
root.appendChild(body);
document.appendChild(root);

addTableHeader(table, document);

result.appendChild(collection);
for (BibEntry entry : entries) {
addEntryRow(entry, table, document);
}
} catch (Exception e) {
LOGGER.warn("Exception caught...", e);
}
return result;
return document;
}

private void addEntryRow(BibEntry entry, Element table, Document document) {
final Element row = document.createElement("table:table-row");

addTableCell(document, row, new GetOpenOfficeType().format(entry.getType().getName()));
toExportFields.forEach(field -> {
if (field.equals(StandardField.TITLE)) {
addTableCell(document, row, new RemoveWhitespace().format(new RemoveBrackets().format(getField(entry, StandardField.TITLE))));
} else {
addTableCell(document, row, getField(entry, field));
}
});

table.appendChild(row);
}

private Element createTableElement(Document document) {
Element table = document.createElement("table:table");
table.setAttribute("table:name", "biblio");
table.setAttribute("table.style-name", "ta1");
return table;
}

private Element createRootElement(Document document) {
Element root = document.createElement("office:document-content");
root.setAttribute("xmlns:office", "http://openoffice.org/2000/office");
root.setAttribute("xmlns:style", "http://openoffice.org/2000/style");
root.setAttribute("xmlns:text", "http://openoffice.org/2000/text");
root.setAttribute("xmlns:table", "http://openoffice.org/2000/table");
root.setAttribute("xmlns:office:class", "spreadsheet");
root.setAttribute("xmlns:office:version", "1.0");
root.setAttribute("xmlns:fo", "http://www.w3.org/1999/XSL/Format");
Element el = document.createElement("office:script");
root.appendChild(el);

el = document.createElement("office:automatic-styles");
Element el2 = document.createElement("style:style");
el2.setAttribute("style:name", "ro1");
el2.setAttribute("style:family", "table-row");
Element el3 = document.createElement("style.properties");
el3.setAttribute("style:row-height", "0.1681inch");
el3.setAttribute("fo:break-before", "auto");
el3.setAttribute("style:use-optimal-row-height", "true");
el2.appendChild(el3);
el.appendChild(el2);
el2 = document.createElement("style:style");
el2.setAttribute("style:name", "ta1");
el2.setAttribute("style:family", "table");
el2.setAttribute("style:master-page-name", "Default");
el3 = document.createElement("style:properties");
el3.setAttribute("table:display", "true");
el2.appendChild(el3);
el.appendChild(el2);
root.appendChild(el);

return root;
}

private static void addTableCell(Document doc, Element parent, String content) {
Element cell = doc.createElement("table:table-cell");
Element text = doc.createElement("text:p");
Text textNode = doc.createTextNode(content);
text.appendChild(textNode);
// text.setTextContent(content);
cell.appendChild(text);
parent.appendChild(cell);
}

private void addTableHeader(Element table, Document document) {
Element firstRow = document.createElement("table:table-row");
firstRow.setAttribute("table.style-name", "ro1");
addTableCell(document, firstRow, "Type");
for (Field field : toExportFields) {
addTableCell(document, firstRow, field.getDisplayName());
}

table.appendChild(firstRow);
}
}
Loading