From 812f7e9fd8b1d16b0886e9efb20357e3433bba44 Mon Sep 17 00:00:00 2001 From: Dennis Tschechlov Date: Mon, 19 Sep 2016 19:38:33 +0200 Subject: [PATCH 1/5] rewrite exporter with jaxb parser --- .../logic/exporter/BibTeXMLExportFormat.java | 249 ++++++++++++++++++ .../jabref/logic/exporter/ExportFormats.java | 2 +- 2 files changed, 250 insertions(+), 1 deletion(-) create mode 100644 src/main/java/net/sf/jabref/logic/exporter/BibTeXMLExportFormat.java diff --git a/src/main/java/net/sf/jabref/logic/exporter/BibTeXMLExportFormat.java b/src/main/java/net/sf/jabref/logic/exporter/BibTeXMLExportFormat.java new file mode 100644 index 00000000000..80426a43233 --- /dev/null +++ b/src/main/java/net/sf/jabref/logic/exporter/BibTeXMLExportFormat.java @@ -0,0 +1,249 @@ +package net.sf.jabref.logic.exporter; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.math.BigInteger; +import java.nio.charset.Charset; +import java.util.Arrays; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.stream.Collectors; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBElement; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Marshaller; +import javax.xml.datatype.DatatypeConfigurationException; +import javax.xml.datatype.DatatypeFactory; +import javax.xml.datatype.XMLGregorianCalendar; +import javax.xml.namespace.QName; + +import net.sf.jabref.logic.importer.fileformat.bibtexml.Article; +import net.sf.jabref.logic.importer.fileformat.bibtexml.Booklet; +import net.sf.jabref.logic.importer.fileformat.bibtexml.Book; +import net.sf.jabref.logic.importer.fileformat.bibtexml.Conference; +import net.sf.jabref.logic.importer.fileformat.bibtexml.Entry; +import net.sf.jabref.logic.importer.fileformat.bibtexml.File; +import net.sf.jabref.logic.importer.fileformat.bibtexml.Inbook; +import net.sf.jabref.logic.importer.fileformat.bibtexml.Incollection; +import net.sf.jabref.logic.importer.fileformat.bibtexml.Inproceedings; +import net.sf.jabref.logic.importer.fileformat.bibtexml.Manual; +import net.sf.jabref.logic.importer.fileformat.bibtexml.Mastersthesis; +import net.sf.jabref.logic.importer.fileformat.bibtexml.Misc; +import net.sf.jabref.logic.importer.fileformat.bibtexml.Phdthesis; +import net.sf.jabref.logic.importer.fileformat.bibtexml.Proceedings; +import net.sf.jabref.logic.importer.fileformat.bibtexml.Techreport; +import net.sf.jabref.logic.importer.fileformat.bibtexml.Unpublished; +import net.sf.jabref.model.database.BibDatabaseContext; +import net.sf.jabref.model.entry.BibEntry; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +public class BibTeXMLExportFormat extends ExportFormat { + + private static final String BIBTEXML_NAMESPACE_URI = "http://bibtexml.sf.net/"; + private static final Locale ENGLISH = Locale.ENGLISH; + private static final Log LOGGER = LogFactory.getLog(BibTeXMLExportFormat.class); + + + public BibTeXMLExportFormat() { + super("BibTeXML", "bibtexml", null, null, ".xml"); + } + + @Override + public void performExport(final BibDatabaseContext databaseContext, final String resultFile, final Charset encoding, + List entries) throws SaveException { + Objects.requireNonNull(databaseContext); + Objects.requireNonNull(entries); + if (entries.isEmpty()) { // Only export if entries exist + return; + } + + File file = new File(); + for (BibEntry bibEntry : entries) { + Entry entry = new Entry(); + + Optional citeKey = bibEntry.getCiteKeyOptional(); + if (citeKey.isPresent()) { + entry.setId(citeKey.get()); + } + + String type = bibEntry.getType().toLowerCase(ENGLISH); + switch (type) { + case "article": + Article article = new Article(); + parse(article, bibEntry); + entry.setArticle(article); + break; + case "book": + Book book = new Book(); + parse(book, bibEntry); + entry.setBook(book); + break; + case "booklet": + Booklet booklet = new Booklet(); + parse(booklet, bibEntry); + entry.setBooklet(booklet); + break; + case "conference": + Conference conference = new Conference(); + parse(conference, bibEntry); + entry.setConference(conference); + break; + case "inbook": + Inbook inbook = new Inbook(); + parseInbook(inbook, bibEntry); + entry.setInbook(inbook); + break; + case "incollection": + Incollection incollection = new Incollection(); + parse(incollection, bibEntry); + entry.setIncollection(incollection); + break; + case "inproceedings": + Inproceedings inproceedings = new Inproceedings(); + parse(inproceedings, bibEntry); + entry.setInproceedings(inproceedings); + break; + case "mastersthesis": + Mastersthesis mastersthesis = new Mastersthesis(); + parse(mastersthesis, bibEntry); + entry.setMastersthesis(mastersthesis); + break; + case "manual": + Manual manual = new Manual(); + parse(manual, bibEntry); + entry.setManual(manual); + break; + case "misc": + Misc misc = new Misc(); + parse(misc, bibEntry); + entry.setMisc(misc); + break; + case "phdthesis": + Phdthesis phdthesis = new Phdthesis(); + parse(phdthesis, bibEntry); + entry.setPhdthesis(phdthesis); + break; + case "proceedings": + Proceedings proceedings = new Proceedings(); + parse(proceedings, bibEntry); + entry.setProceedings(proceedings); + break; + case "techreport": + Techreport techreport = new Techreport(); + parse(techreport, bibEntry); + entry.setTechreport(techreport); + break; + case "unpublished": + Unpublished unpublished = new Unpublished(); + parse(unpublished, bibEntry); + entry.setUnpublished(unpublished); + break; + } + + file.getEntry().add(entry); + } + + try { + JAXBContext context = JAXBContext.newInstance(File.class); + Marshaller marshaller = context.createMarshaller(); + marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); + + marshaller.marshal(file, new java.io.File(resultFile)); + } catch (JAXBException e) { + throw new SaveException(e); + } + } + + private void parseInbook(Inbook inbook, BibEntry bibEntry) { + Map fieldMap = bibEntry.getFieldMap(); + for (java.util.Map.Entry entryField : fieldMap.entrySet()) { + String value = entryField.getValue(); + String key = entryField.getKey(); + if ("year".equals(key)) { + XMLGregorianCalendar calendar; + try { + calendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(value); + + JAXBElement year = new JAXBElement<>( + new QName(BIBTEXML_NAMESPACE_URI, key), + XMLGregorianCalendar.class, calendar); + inbook.getContent().add(year); + } catch (DatatypeConfigurationException e) { + LOGGER.error("A configuration error occured"); + } + } else if ("number".equals(key)) { + JAXBElement number = new JAXBElement<>(new QName(BIBTEXML_NAMESPACE_URI, "number"), + BigInteger.class, + new BigInteger(value)); + inbook.getContent().add(number); + } else { + JAXBElement element = new JAXBElement<>(new QName(BIBTEXML_NAMESPACE_URI, key), String.class, + value); + inbook.getContent().add(element); + } + } + } + + private void parse(T entryType, BibEntry bibEntry) { + List declaredSetMethods = getListOfSetMethods(entryType); + Map fieldMap = bibEntry.getFieldMap(); + for (java.util.Map.Entry entryField : fieldMap.entrySet()) { + String value = entryField.getValue(); + String key = entryField.getKey(); + for (Method method : declaredSetMethods) { + String methodNameWithoutSet = method.getName().replace("set", "").toLowerCase(ENGLISH); + try { + + if ("year".equals(key) && key.equals(methodNameWithoutSet)) { + try { + + XMLGregorianCalendar calendar = DatatypeFactory.newInstance() + .newXMLGregorianCalendar(value); + method.invoke(entryType, calendar); + } catch (DatatypeConfigurationException e) { + LOGGER.error("A configuration error occured"); + } + break; + } else if ("number".equals(key) && key.equals(methodNameWithoutSet)) { + method.invoke(entryType, new BigInteger(value)); + break; + } else if (key.equals(methodNameWithoutSet)) { + method.invoke(entryType, value); + break; + } + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + LOGGER.error("Could not invoke method", e); + } + } + } + + // //set the entryType to the entry + // List entryMethods = Arrays.asList(entry.getClass().getDeclaredMethods()).stream() + // .filter(method -> method.getName().startsWith("set")).collect(Collectors.toList()); + // for (Method method : entryMethods) { + // String methodWithoutSet = method.getName().replace("set", ""); + // String simpleClassName = entryType.getClass().getSimpleName().replaceAll("[", "").replaceAll("]", ""); + // if(methodWithoutSet.equals(simpleClassName)) { + // try { + // method.invoke(entry, entryType); + // } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + // // TODO Auto-generated catch block + // e.printStackTrace(); + // } + // } + // } + + } + + private List getListOfSetMethods(T entryType) { + return Arrays.asList(entryType.getClass().getDeclaredMethods()).stream() + .filter(method -> method.getName().startsWith("set")).collect(Collectors.toList()); + } + +} diff --git a/src/main/java/net/sf/jabref/logic/exporter/ExportFormats.java b/src/main/java/net/sf/jabref/logic/exporter/ExportFormats.java index 7b59204c00d..6a6578f9b79 100644 --- a/src/main/java/net/sf/jabref/logic/exporter/ExportFormats.java +++ b/src/main/java/net/sf/jabref/logic/exporter/ExportFormats.java @@ -31,7 +31,7 @@ public static void initAllExports(Map customFormats, ExportFormats.putFormat(new ExportFormat("DIN 1505", "din1505", "din1505winword", "din1505", ".rtf", layoutPreferences, savePreferences)); ExportFormats.putFormat( - new ExportFormat("BibTeXML", "bibtexml", "bibtexml", null, ".xml", layoutPreferences, savePreferences)); +new BibTeXMLExportFormat()); ExportFormats.putFormat( new ExportFormat("BibO RDF", "bibordf", "bibordf", null, ".rdf", layoutPreferences, savePreferences)); ExportFormats.putFormat(new ExportFormat(Localization.lang("HTML table"), "tablerefs", "tablerefs", "tablerefs", From f45b46245565c915604b1755d4f565218c10d843 Mon Sep 17 00:00:00 2001 From: Dennis Tschechlov Date: Tue, 20 Sep 2016 12:09:56 +0200 Subject: [PATCH 2/5] add test --- .../logic/exporter/BibTeXMLExportFormat.java | 159 ++++++++---------- .../exporter/BibTeXMLExporterTestFiles.java | 89 ++++++++++ .../exporter/BibTeXMLExporterTestArticle.bib | 11 ++ .../exporter/BibTeXMLExporterTestArticle.xml | 14 ++ .../BibTeXMLExporterTestArticleWithoutID.bib | 11 ++ .../BibTeXMLExporterTestArticleWithoutID.xml | 14 ++ .../exporter/BibTeXMLExporterTestAuthor.bib | 5 + .../exporter/BibTeXMLExporterTestAuthor.xml | 8 + .../exporter/BibTeXMLExporterTestBook.bib | 11 ++ .../exporter/BibTeXMLExporterTestBook.xml | 14 ++ .../exporter/BibTeXMLExporterTestBooklet.bib | 10 ++ .../exporter/BibTeXMLExporterTestBooklet.xml | 13 ++ .../BibTeXMLExporterTestConference.bib | 18 ++ .../BibTeXMLExporterTestConference.xml | 21 +++ .../exporter/BibTeXMLExporterTestInBook.bib | 13 ++ .../BibTeXMLExporterTestInCollection.bib | 16 ++ .../BibTeXMLExporterTestInCollection.xml | 18 ++ .../BibTeXMLExporterTestInProceedings.bib | 12 ++ .../BibTeXMLExporterTestInProceedings.xml | 15 ++ .../exporter/BibTeXMLExporterTestInbook.xml | 17 ++ .../BibTeXMLExporterTestInbookLessFields.bib | 15 ++ .../BibTeXMLExporterTestInbookLessFields.xml | 21 +++ .../BibTeXMLExporterTestInvalidInbook.bib | 3 + .../BibTeXMLExporterTestInvalidInbook.xml | 8 + .../exporter/BibTeXMLExporterTestManual.bib | 11 ++ .../exporter/BibTeXMLExporterTestManual.xml | 14 ++ .../BibTeXMLExporterTestMasterThesis.bib | 12 ++ .../BibTeXMLExporterTestMasterThesis.xml | 15 ++ .../exporter/BibTeXMLExporterTestMisc.bib | 10 ++ .../exporter/BibTeXMLExporterTestMisc.xml | 13 ++ .../BibTeXMLExporterTestPhdThesis.bib | 12 ++ .../BibTeXMLExporterTestPhdThesis.xml | 15 ++ .../BibTeXMLExporterTestProceedings.bib | 11 ++ .../BibTeXMLExporterTestProceedings.xml | 14 ++ .../BibTeXMLExporterTestTechReport.bib | 11 ++ .../BibTeXMLExporterTestTechReport.xml | 16 ++ .../BibTeXMLExporterTestUnpublished.bib | 9 + .../BibTeXMLExporterTestUnpublished.xml | 12 ++ 38 files changed, 623 insertions(+), 88 deletions(-) create mode 100644 src/test/java/net/sf/jabref/logic/exporter/BibTeXMLExporterTestFiles.java create mode 100644 src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestArticle.bib create mode 100644 src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestArticle.xml create mode 100644 src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestArticleWithoutID.bib create mode 100644 src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestArticleWithoutID.xml create mode 100644 src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestAuthor.bib create mode 100644 src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestAuthor.xml create mode 100644 src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestBook.bib create mode 100644 src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestBook.xml create mode 100644 src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestBooklet.bib create mode 100644 src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestBooklet.xml create mode 100644 src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestConference.bib create mode 100644 src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestConference.xml create mode 100644 src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestInBook.bib create mode 100644 src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestInCollection.bib create mode 100644 src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestInCollection.xml create mode 100644 src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestInProceedings.bib create mode 100644 src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestInProceedings.xml create mode 100644 src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestInbook.xml create mode 100644 src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestInbookLessFields.bib create mode 100644 src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestInbookLessFields.xml create mode 100644 src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestInvalidInbook.bib create mode 100644 src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestInvalidInbook.xml create mode 100644 src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestManual.bib create mode 100644 src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestManual.xml create mode 100644 src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestMasterThesis.bib create mode 100644 src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestMasterThesis.xml create mode 100644 src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestMisc.bib create mode 100644 src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestMisc.xml create mode 100644 src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestPhdThesis.bib create mode 100644 src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestPhdThesis.xml create mode 100644 src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestProceedings.bib create mode 100644 src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestProceedings.xml create mode 100644 src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestTechReport.bib create mode 100644 src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestTechReport.xml create mode 100644 src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestUnpublished.bib create mode 100644 src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestUnpublished.xml diff --git a/src/main/java/net/sf/jabref/logic/exporter/BibTeXMLExportFormat.java b/src/main/java/net/sf/jabref/logic/exporter/BibTeXMLExportFormat.java index 80426a43233..6672ba7d711 100644 --- a/src/main/java/net/sf/jabref/logic/exporter/BibTeXMLExportFormat.java +++ b/src/main/java/net/sf/jabref/logic/exporter/BibTeXMLExportFormat.java @@ -74,76 +74,76 @@ public void performExport(final BibDatabaseContext databaseContext, final String String type = bibEntry.getType().toLowerCase(ENGLISH); switch (type) { - case "article": - Article article = new Article(); - parse(article, bibEntry); - entry.setArticle(article); - break; - case "book": - Book book = new Book(); - parse(book, bibEntry); - entry.setBook(book); - break; - case "booklet": - Booklet booklet = new Booklet(); - parse(booklet, bibEntry); - entry.setBooklet(booklet); - break; - case "conference": - Conference conference = new Conference(); - parse(conference, bibEntry); - entry.setConference(conference); - break; - case "inbook": - Inbook inbook = new Inbook(); - parseInbook(inbook, bibEntry); - entry.setInbook(inbook); - break; - case "incollection": - Incollection incollection = new Incollection(); - parse(incollection, bibEntry); - entry.setIncollection(incollection); - break; - case "inproceedings": - Inproceedings inproceedings = new Inproceedings(); - parse(inproceedings, bibEntry); - entry.setInproceedings(inproceedings); - break; - case "mastersthesis": - Mastersthesis mastersthesis = new Mastersthesis(); - parse(mastersthesis, bibEntry); - entry.setMastersthesis(mastersthesis); - break; - case "manual": - Manual manual = new Manual(); - parse(manual, bibEntry); - entry.setManual(manual); - break; - case "misc": - Misc misc = new Misc(); - parse(misc, bibEntry); - entry.setMisc(misc); - break; - case "phdthesis": - Phdthesis phdthesis = new Phdthesis(); - parse(phdthesis, bibEntry); - entry.setPhdthesis(phdthesis); - break; - case "proceedings": - Proceedings proceedings = new Proceedings(); - parse(proceedings, bibEntry); - entry.setProceedings(proceedings); - break; - case "techreport": - Techreport techreport = new Techreport(); - parse(techreport, bibEntry); - entry.setTechreport(techreport); - break; - case "unpublished": - Unpublished unpublished = new Unpublished(); - parse(unpublished, bibEntry); - entry.setUnpublished(unpublished); - break; + case "article": + Article article = new Article(); + parse(article, bibEntry); + entry.setArticle(article); + break; + case "book": + Book book = new Book(); + parse(book, bibEntry); + entry.setBook(book); + break; + case "booklet": + Booklet booklet = new Booklet(); + parse(booklet, bibEntry); + entry.setBooklet(booklet); + break; + case "conference": + Conference conference = new Conference(); + parse(conference, bibEntry); + entry.setConference(conference); + break; + case "inbook": + Inbook inbook = new Inbook(); + parseInbook(inbook, bibEntry); + entry.setInbook(inbook); + break; + case "incollection": + Incollection incollection = new Incollection(); + parse(incollection, bibEntry); + entry.setIncollection(incollection); + break; + case "inproceedings": + Inproceedings inproceedings = new Inproceedings(); + parse(inproceedings, bibEntry); + entry.setInproceedings(inproceedings); + break; + case "mastersthesis": + Mastersthesis mastersthesis = new Mastersthesis(); + parse(mastersthesis, bibEntry); + entry.setMastersthesis(mastersthesis); + break; + case "manual": + Manual manual = new Manual(); + parse(manual, bibEntry); + entry.setManual(manual); + break; + case "misc": + Misc misc = new Misc(); + parse(misc, bibEntry); + entry.setMisc(misc); + break; + case "phdthesis": + Phdthesis phdthesis = new Phdthesis(); + parse(phdthesis, bibEntry); + entry.setPhdthesis(phdthesis); + break; + case "proceedings": + Proceedings proceedings = new Proceedings(); + parse(proceedings, bibEntry); + entry.setProceedings(proceedings); + break; + case "techreport": + Techreport techreport = new Techreport(); + parse(techreport, bibEntry); + entry.setTechreport(techreport); + break; + case "unpublished": + Unpublished unpublished = new Unpublished(); + parse(unpublished, bibEntry); + entry.setUnpublished(unpublished); + break; } file.getEntry().add(entry); @@ -171,7 +171,7 @@ private void parseInbook(Inbook inbook, BibEntry bibEntry) { calendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(value); JAXBElement year = new JAXBElement<>( - new QName(BIBTEXML_NAMESPACE_URI, key), + new QName(BIBTEXML_NAMESPACE_URI, "year"), XMLGregorianCalendar.class, calendar); inbook.getContent().add(year); } catch (DatatypeConfigurationException e) { @@ -222,23 +222,6 @@ private void parse(T entryType, BibEntry bibEntry) { } } } - - // //set the entryType to the entry - // List entryMethods = Arrays.asList(entry.getClass().getDeclaredMethods()).stream() - // .filter(method -> method.getName().startsWith("set")).collect(Collectors.toList()); - // for (Method method : entryMethods) { - // String methodWithoutSet = method.getName().replace("set", ""); - // String simpleClassName = entryType.getClass().getSimpleName().replaceAll("[", "").replaceAll("]", ""); - // if(methodWithoutSet.equals(simpleClassName)) { - // try { - // method.invoke(entry, entryType); - // } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { - // // TODO Auto-generated catch block - // e.printStackTrace(); - // } - // } - // } - } private List getListOfSetMethods(T entryType) { diff --git a/src/test/java/net/sf/jabref/logic/exporter/BibTeXMLExporterTestFiles.java b/src/test/java/net/sf/jabref/logic/exporter/BibTeXMLExporterTestFiles.java new file mode 100644 index 00000000000..e4216f77800 --- /dev/null +++ b/src/test/java/net/sf/jabref/logic/exporter/BibTeXMLExporterTestFiles.java @@ -0,0 +1,89 @@ +package net.sf.jabref.logic.exporter; + +import java.io.File; +import java.io.IOException; +import java.net.URISyntaxException; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Collection; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import net.sf.jabref.logic.importer.fileformat.BibtexImporter; +import net.sf.jabref.model.database.BibDatabaseContext; +import net.sf.jabref.model.entry.BibEntry; +import net.sf.jabref.preferences.JabRefPreferences; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameter; +import org.junit.runners.Parameterized.Parameters; +import org.xmlunit.builder.Input; +import org.xmlunit.builder.Input.Builder; +import org.xmlunit.diff.DefaultNodeMatcher; +import org.xmlunit.diff.ElementSelectors; +import org.xmlunit.matchers.CompareMatcher; + +import static org.junit.Assert.*; + +@RunWith(Parameterized.class) +public class BibTeXMLExporterTestFiles { + + public BibDatabaseContext databaseContext; + public Charset charset; + public File tempFile; + public BibTeXMLExportFormat bibtexmlExportFormat; + public BibtexImporter testImporter; + + @Parameter + public String filename; + public Path resourceDir; + + @Rule + public TemporaryFolder testFolder = new TemporaryFolder(); + + + @Parameters(name = "{0}") + public static Collection fileNames() throws IOException, URISyntaxException { + try (Stream stream = Files.list(Paths.get(BibTeXMLExporterTestFiles.class.getResource("").toURI()))) { + return stream.map(n -> n.getFileName().toString()).filter(n -> n.endsWith(".bib")) + .filter(n -> n.startsWith("BibTeXML")).collect(Collectors.toList()); + } + } + + @Before + public void setUp() throws Exception { + resourceDir = Paths.get(BibTeXMLExporterTestFiles.class.getResource("").toURI()); + databaseContext = new BibDatabaseContext(); + charset = StandardCharsets.UTF_8; + bibtexmlExportFormat = new BibTeXMLExportFormat(); + tempFile = testFolder.newFile(); + testImporter = new BibtexImporter(JabRefPreferences.getInstance().getImportFormatPreferences()); + } + + @Test + public final void testPerformExport() throws IOException, SaveException { + String xmlFileName = filename.replace(".bib", ".xml"); + Path importFile = resourceDir.resolve(filename); + String tempFilename = tempFile.getCanonicalPath(); + + List entries = testImporter.importDatabase(importFile, StandardCharsets.UTF_8).getDatabase() + .getEntries(); + + bibtexmlExportFormat.performExport(databaseContext, tempFile.getPath(), charset, entries); + + Builder control = Input.from(Files.newInputStream(resourceDir.resolve(xmlFileName))); + Builder test = Input.from(Files.newInputStream(Paths.get(tempFilename))); + + assertThat(test, CompareMatcher.isSimilarTo(control) + .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byNameAndText)).throwComparisonFailure()); + } +} \ No newline at end of file diff --git a/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestArticle.bib b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestArticle.bib new file mode 100644 index 00000000000..f170c7f5e10 --- /dev/null +++ b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestArticle.bib @@ -0,0 +1,11 @@ +% Encoding: UTF-8 + +@Article{Mustermann2016, + author = {Max Mustermann}, + title = {Java tricks}, + journal = {Java Journal}, + year = {2016}, + pages = {2}, + month = {February}, + keywords = {java} +} diff --git a/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestArticle.xml b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestArticle.xml new file mode 100644 index 00000000000..28010c40329 --- /dev/null +++ b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestArticle.xml @@ -0,0 +1,14 @@ + + + +
+ Max Mustermann + Java tricks + Java Journal + 2016 + 2 + February + java +
+
+
diff --git a/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestArticleWithoutID.bib b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestArticleWithoutID.bib new file mode 100644 index 00000000000..b759fbeebf0 --- /dev/null +++ b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestArticleWithoutID.bib @@ -0,0 +1,11 @@ +% Encoding: UTF-8 + +@Article{, + author = {Max Mustermann}, + title = {Java tricks}, + journal = {Java Journal}, + year = {2016}, + pages = {2}, + month = {February}, + keywords = {java} +} diff --git a/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestArticleWithoutID.xml b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestArticleWithoutID.xml new file mode 100644 index 00000000000..d1fbc686dcc --- /dev/null +++ b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestArticleWithoutID.xml @@ -0,0 +1,14 @@ + + + +
+ Max Mustermann + Java tricks + Java Journal + 2016 + 2 + February + java +
+
+
diff --git a/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestAuthor.bib b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestAuthor.bib new file mode 100644 index 00000000000..f4cdac61bf0 --- /dev/null +++ b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestAuthor.bib @@ -0,0 +1,5 @@ +% Encoding: UTF-8 + +@Misc{, + author = {Burri, Emanuel and Beglinger, Christoph and von Felten, Stefanie and Lehmann, Frank Serge} +} diff --git a/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestAuthor.xml b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestAuthor.xml new file mode 100644 index 00000000000..6a4e0c9b29f --- /dev/null +++ b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestAuthor.xml @@ -0,0 +1,8 @@ + + + + + Burri, Emanuel and Beglinger, Christoph and von Felten, Stefanie and Lehmann, Frank Serge + + + diff --git a/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestBook.bib b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestBook.bib new file mode 100644 index 00000000000..d4b21de4684 --- /dev/null +++ b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestBook.bib @@ -0,0 +1,11 @@ +% Encoding: UTF-8 + +@Book{Mustermann2016, + title = {Java Book}, + publisher = {Java Book Publisher}, + year = {2016}, + author = {Max Mustermann}, + volume = {1}, + month = {February}, + keywords = {java} +} diff --git a/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestBook.xml b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestBook.xml new file mode 100644 index 00000000000..4f167bda6cd --- /dev/null +++ b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestBook.xml @@ -0,0 +1,14 @@ + + + + + Max Mustermann + Java Book + Java Book Publisher + 2016 + 1 + February + java + + + diff --git a/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestBooklet.bib b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestBooklet.bib new file mode 100644 index 00000000000..3dad334bf5f --- /dev/null +++ b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestBooklet.bib @@ -0,0 +1,10 @@ +% Encoding: UTF-8 + +@Booklet{Mustermann2016, + title = {Java Booklet}, + author = {Max Mustermann}, + address = {Stuttgart}, + month = {February}, + year = {2016}, + keywords = {java} +} diff --git a/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestBooklet.xml b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestBooklet.xml new file mode 100644 index 00000000000..f95803734f4 --- /dev/null +++ b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestBooklet.xml @@ -0,0 +1,13 @@ + + + + + Max Mustermann + Java Booklet +
Stuttgart
+ February + 2016 + java +
+
+
diff --git a/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestConference.bib b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestConference.bib new file mode 100644 index 00000000000..e7709cedf11 --- /dev/null +++ b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestConference.bib @@ -0,0 +1,18 @@ +% Encoding: UTF-8 + +@Conference{Mustermann2016, + author = {Max Mustermann}, + title = {Java Conference}, + booktitle = {Java Booktitle}, + year = {2016}, + editor = {Maxima Musterfrau}, + volume = {1}, + number = {1}, + series = {1}, + pages = {3-9}, + address = {Stuttgart}, + month = {February}, + organization = {Java Org}, + publisher = {Java Publisher}, + keywords = {java} +} diff --git a/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestConference.xml b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestConference.xml new file mode 100644 index 00000000000..d363b41624f --- /dev/null +++ b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestConference.xml @@ -0,0 +1,21 @@ + + + + + Max Mustermann + Java Conference + Java Booktitle + 2016 + Maxima Musterfrau + 1 + 1 + 1 + 3-9 +
Stuttgart
+ February + Java Org + Java Publisher + java +
+
+
diff --git a/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestInBook.bib b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestInBook.bib new file mode 100644 index 00000000000..d0e1c4d6686 --- /dev/null +++ b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestInBook.bib @@ -0,0 +1,13 @@ +% Encoding: UTF-8 + +@InBook{Mustermann2016, + chapter = {8}, + pages = {18-25}, + title = {Java 8 Lambdas}, + publisher = {Java Book Publisher}, + year = {2016}, + author = {Max Mustermann}, + volume = {1}, + month = {February}, + keywords = {java} +} diff --git a/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestInCollection.bib b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestInCollection.bib new file mode 100644 index 00000000000..9fa735acd67 --- /dev/null +++ b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestInCollection.bib @@ -0,0 +1,16 @@ +% Encoding: UTF-8 + +@InCollection{Mustermann2016, + author = {Max Mustermann}, + title = {Java InCollection}, + booktitle = {Java Testing}, + publisher = {Java Publisher}, + year = {2016}, + editor = {Maxima Musterfrau}, + volume = {1}, + number = {1}, + chapter = {3}, + pages = {18-26}, + month = {February}, + keywords = {java} +} diff --git a/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestInCollection.xml b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestInCollection.xml new file mode 100644 index 00000000000..71a2ba457bd --- /dev/null +++ b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestInCollection.xml @@ -0,0 +1,18 @@ + + + + + Max Mustermann + Java InCollection + Java Testing + Java Publisher + 2016 + Maxima Musterfrau + 1 + 1 + 18-26 + February + java + + + diff --git a/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestInProceedings.bib b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestInProceedings.bib new file mode 100644 index 00000000000..0a9546563d9 --- /dev/null +++ b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestInProceedings.bib @@ -0,0 +1,12 @@ +% Encoding: UTF-8 + +@InProceedings{Mustermann2016, + author = {Max Mustermann}, + title = {Java InProceedings}, + booktitle = {Java Book}, + year = {2016}, + month = {February}, + organization = {Java Org}, + publisher = {Java Publisher}, + keywords = {java} +} diff --git a/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestInProceedings.xml b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestInProceedings.xml new file mode 100644 index 00000000000..e0dfe360ca8 --- /dev/null +++ b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestInProceedings.xml @@ -0,0 +1,15 @@ + + + + + Max Mustermann + Java InProceedings + Java Book + 2016 + February + Java Org + Java Publisher + java + + + diff --git a/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestInbook.xml b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestInbook.xml new file mode 100644 index 00000000000..bfce9880d66 --- /dev/null +++ b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestInbook.xml @@ -0,0 +1,17 @@ + + + + + 1 + 8 + 18-25 + February + java + 2016 + Max Mustermann + Mustermann2016 + Java Book Publisher + Java 8 Lambdas + + + diff --git a/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestInbookLessFields.bib b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestInbookLessFields.bib new file mode 100644 index 00000000000..4014194c1b0 --- /dev/null +++ b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestInbookLessFields.bib @@ -0,0 +1,15 @@ +@InBook{Mustermann2016, + chapter = {10}, + title = {Java Conference}, + publisher = {Java Publisher}, + author = {Max Mustermann}, + editor = {Maxima Musterfrau}, + volume = {1}, + number = {1}, + series = {1}, + address = {Stuttgart}, + edition = {10}, + month = {February}, + note = {some note}, + keywords = {java}, +} diff --git a/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestInbookLessFields.xml b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestInbookLessFields.xml new file mode 100644 index 00000000000..b45b5574a53 --- /dev/null +++ b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestInbookLessFields.xml @@ -0,0 +1,21 @@ + + + + + Maxima Musterfrau + 10 + some note +
Stuttgart
+ java + Max Mustermann + 10 + Java Conference + 1 + 1 + February + 1 + Java Publisher + Mustermann2016 +
+
+
diff --git a/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestInvalidInbook.bib b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestInvalidInbook.bib new file mode 100644 index 00000000000..84b1642b127 --- /dev/null +++ b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestInvalidInbook.bib @@ -0,0 +1,3 @@ +@inbook{Mustermann2016, + +} \ No newline at end of file diff --git a/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestInvalidInbook.xml b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestInvalidInbook.xml new file mode 100644 index 00000000000..964936a8255 --- /dev/null +++ b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestInvalidInbook.xml @@ -0,0 +1,8 @@ + + + + + Mustermann2016 + + + diff --git a/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestManual.bib b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestManual.bib new file mode 100644 index 00000000000..21a7961789e --- /dev/null +++ b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestManual.bib @@ -0,0 +1,11 @@ +% Encoding: UTF-8 + +@Manual{Mustermann2016, + title = {How to install Java}, + author = {Max Mustermann}, + organization = {Java Users}, + address = {Stuttgart}, + month = {February}, + year = {2016}, + keywords = {java} +} diff --git a/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestManual.xml b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestManual.xml new file mode 100644 index 00000000000..876f8f73ccc --- /dev/null +++ b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestManual.xml @@ -0,0 +1,14 @@ + + + + + Max Mustermann + How to install Java + Java Users +
Stuttgart
+ February + 2016 + java +
+
+
diff --git a/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestMasterThesis.bib b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestMasterThesis.bib new file mode 100644 index 00000000000..003c2879c6c --- /dev/null +++ b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestMasterThesis.bib @@ -0,0 +1,12 @@ +% Encoding: UTF-8 + +@MastersThesis{Mustermann2016, + author = {Max Mustermann}, + title = {Java MasterThesis}, + school = {University}, + year = {2016}, + type = {Thesis}, + address = {Stuttgart}, + month = {February}, + keywords = {java} +} diff --git a/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestMasterThesis.xml b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestMasterThesis.xml new file mode 100644 index 00000000000..6d7e0cf5b99 --- /dev/null +++ b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestMasterThesis.xml @@ -0,0 +1,15 @@ + + + + + Max Mustermann + Java MasterThesis + University + 2016 + Thesis +
Stuttgart
+ February + java +
+
+
diff --git a/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestMisc.bib b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestMisc.bib new file mode 100644 index 00000000000..9101bba68e2 --- /dev/null +++ b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestMisc.bib @@ -0,0 +1,10 @@ +% Encoding: UTF-8 + +@Misc{Mustermann2016, + author = {Max Mustermann}, + title = {Java Misc}, + howpublished = {Internet}, + month = {February}, + year = {2016}, + keywords = {java} +} diff --git a/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestMisc.xml b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestMisc.xml new file mode 100644 index 00000000000..8e84a02c4d8 --- /dev/null +++ b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestMisc.xml @@ -0,0 +1,13 @@ + + + + + Max Mustermann + Java Misc + Internet + February + 2016 + java + + + diff --git a/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestPhdThesis.bib b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestPhdThesis.bib new file mode 100644 index 00000000000..6121bc38d24 --- /dev/null +++ b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestPhdThesis.bib @@ -0,0 +1,12 @@ +% Encoding: UTF-8 + +@PhdThesis{Mustermann2016, + author = {Max Mustermann}, + title = {Java PhdThesis}, + school = {University}, + year = {2016}, + type = {Thesis}, + address = {Stuttgart}, + month = {February}, + keywords = {java} +} diff --git a/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestPhdThesis.xml b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestPhdThesis.xml new file mode 100644 index 00000000000..071250941db --- /dev/null +++ b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestPhdThesis.xml @@ -0,0 +1,15 @@ + + + + + Max Mustermann + Java PhdThesis + University + 2016 + Thesis +
Stuttgart
+ February + java +
+
+
diff --git a/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestProceedings.bib b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestProceedings.bib new file mode 100644 index 00000000000..8d6cbe4c661 --- /dev/null +++ b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestProceedings.bib @@ -0,0 +1,11 @@ +% Encoding: UTF-8 + +@Proceedings{Musterfrau2016, + title = {Java Proceedings}, + year = {2016}, + editor = {Maxima Musterfrau}, + address = {Stuttgart}, + publisher = {Java Pub}, + month = {February}, + organization = {Java Org} +} diff --git a/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestProceedings.xml b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestProceedings.xml new file mode 100644 index 00000000000..143e178a7d2 --- /dev/null +++ b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestProceedings.xml @@ -0,0 +1,14 @@ + + + + + Maxima Musterfrau + Java Proceedings + 2016 +
Stuttgart
+ February + Java Org + Java Pub +
+
+
diff --git a/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestTechReport.bib b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestTechReport.bib new file mode 100644 index 00000000000..1ab28ba275c --- /dev/null +++ b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestTechReport.bib @@ -0,0 +1,11 @@ +@TechReport{Mustermann2016, + author = {Max Mustermann}, + title = {Java TechReport}, + institution = {JIOT}, + year = {2016}, + type = {Report}, + number = {1}, + address = {Stuttgart}, + month = {February}, + keywords = {java}, +} diff --git a/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestTechReport.xml b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestTechReport.xml new file mode 100644 index 00000000000..b9e7f2edd0a --- /dev/null +++ b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestTechReport.xml @@ -0,0 +1,16 @@ + + + + + Max Mustermann + Java TechReport + JIOT + 2016 + Report + 1 +
Stuttgart
+ February + java +
+
+
diff --git a/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestUnpublished.bib b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestUnpublished.bib new file mode 100644 index 00000000000..f1720ddef2b --- /dev/null +++ b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestUnpublished.bib @@ -0,0 +1,9 @@ +% Encoding: UTF-8 + +@Unpublished{Mustermann2016, + author = {Max Mustermann}, + title = {Java Unpublished}, + month = {February}, + year = {2016}, + keywords = {java} +} diff --git a/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestUnpublished.xml b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestUnpublished.xml new file mode 100644 index 00000000000..37e7ebfdded --- /dev/null +++ b/src/test/resources/net/sf/jabref/logic/exporter/BibTeXMLExporterTestUnpublished.xml @@ -0,0 +1,12 @@ + + + + + Max Mustermann + Java Unpublished + February + 2016 + java + + + From b9980acbec77f919d899f6a1a60b0e7bc1e1fd72 Mon Sep 17 00:00:00 2001 From: Dennis Tschechlov Date: Wed, 21 Sep 2016 00:39:56 +0200 Subject: [PATCH 3/5] fix codacy and some code refactorings --- .../logic/exporter/BibTeXMLExportFormat.java | 121 ++++++++++-------- .../exporter/BibTeXMLExporterTestFiles.java | 4 +- 2 files changed, 70 insertions(+), 55 deletions(-) diff --git a/src/main/java/net/sf/jabref/logic/exporter/BibTeXMLExportFormat.java b/src/main/java/net/sf/jabref/logic/exporter/BibTeXMLExportFormat.java index 6672ba7d711..23a6d800803 100644 --- a/src/main/java/net/sf/jabref/logic/exporter/BibTeXMLExportFormat.java +++ b/src/main/java/net/sf/jabref/logic/exporter/BibTeXMLExportFormat.java @@ -22,8 +22,8 @@ import javax.xml.namespace.QName; import net.sf.jabref.logic.importer.fileformat.bibtexml.Article; -import net.sf.jabref.logic.importer.fileformat.bibtexml.Booklet; import net.sf.jabref.logic.importer.fileformat.bibtexml.Book; +import net.sf.jabref.logic.importer.fileformat.bibtexml.Booklet; import net.sf.jabref.logic.importer.fileformat.bibtexml.Conference; import net.sf.jabref.logic.importer.fileformat.bibtexml.Entry; import net.sf.jabref.logic.importer.fileformat.bibtexml.File; @@ -48,6 +48,7 @@ public class BibTeXMLExportFormat extends ExportFormat { private static final String BIBTEXML_NAMESPACE_URI = "http://bibtexml.sf.net/"; private static final Locale ENGLISH = Locale.ENGLISH; private static final Log LOGGER = LogFactory.getLog(BibTeXMLExportFormat.class); + private JAXBContext context; public BibTeXMLExportFormat() { @@ -75,82 +76,61 @@ public void performExport(final BibDatabaseContext databaseContext, final String String type = bibEntry.getType().toLowerCase(ENGLISH); switch (type) { case "article": - Article article = new Article(); - parse(article, bibEntry); - entry.setArticle(article); + parse(new Article(), bibEntry, entry); break; case "book": - Book book = new Book(); - parse(book, bibEntry); - entry.setBook(book); + parse(new Book(), bibEntry, entry); break; case "booklet": - Booklet booklet = new Booklet(); - parse(booklet, bibEntry); - entry.setBooklet(booklet); + parse(new Booklet(), bibEntry, entry); break; case "conference": - Conference conference = new Conference(); - parse(conference, bibEntry); - entry.setConference(conference); + parse(new Conference(), bibEntry, entry); break; case "inbook": - Inbook inbook = new Inbook(); - parseInbook(inbook, bibEntry); - entry.setInbook(inbook); + parseInbook(new Inbook(), bibEntry, entry); break; case "incollection": - Incollection incollection = new Incollection(); - parse(incollection, bibEntry); - entry.setIncollection(incollection); + parse(new Incollection(), bibEntry, entry); break; case "inproceedings": - Inproceedings inproceedings = new Inproceedings(); - parse(inproceedings, bibEntry); - entry.setInproceedings(inproceedings); + parse(new Inproceedings(), bibEntry, entry); break; case "mastersthesis": - Mastersthesis mastersthesis = new Mastersthesis(); - parse(mastersthesis, bibEntry); - entry.setMastersthesis(mastersthesis); + parse(new Mastersthesis(), bibEntry, entry); break; case "manual": - Manual manual = new Manual(); - parse(manual, bibEntry); - entry.setManual(manual); + parse(new Manual(), bibEntry, entry); break; case "misc": - Misc misc = new Misc(); - parse(misc, bibEntry); - entry.setMisc(misc); + parse(new Misc(), bibEntry, entry); break; case "phdthesis": - Phdthesis phdthesis = new Phdthesis(); - parse(phdthesis, bibEntry); - entry.setPhdthesis(phdthesis); + parse(new Phdthesis(), bibEntry, entry); break; case "proceedings": - Proceedings proceedings = new Proceedings(); - parse(proceedings, bibEntry); - entry.setProceedings(proceedings); + parse(new Proceedings(), bibEntry, entry); break; case "techreport": - Techreport techreport = new Techreport(); - parse(techreport, bibEntry); - entry.setTechreport(techreport); + parse(new Techreport(), bibEntry, entry); break; case "unpublished": - Unpublished unpublished = new Unpublished(); - parse(unpublished, bibEntry); - entry.setUnpublished(unpublished); + parse(new Unpublished(), bibEntry, entry); + break; + default: + LOGGER.warn("unexpected type appeared"); break; } - file.getEntry().add(entry); } + createMarshallerAndWriteToFile(file, resultFile); + } + private void createMarshallerAndWriteToFile(File file, String resultFile) throws SaveException { try { - JAXBContext context = JAXBContext.newInstance(File.class); + if (context == null) { + context = JAXBContext.newInstance(File.class); + } Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); @@ -160,9 +140,14 @@ public void performExport(final BibDatabaseContext databaseContext, final String } } - private void parseInbook(Inbook inbook, BibEntry bibEntry) { + /** + * Contains same logic as the parse method, but inbook needs a special treatment, because + * the contents of inbook are stored in a List of JAXBElements. So we first need to create + * a JAXBElement for every field and then add it to the content list. + */ + private void parseInbook(Inbook inbook, BibEntry bibEntry, Entry entry) { Map fieldMap = bibEntry.getFieldMap(); - for (java.util.Map.Entry entryField : fieldMap.entrySet()) { + for (Map.Entry entryField : fieldMap.entrySet()) { String value = entryField.getValue(); String key = entryField.getKey(); if ("year".equals(key)) { @@ -171,16 +156,14 @@ private void parseInbook(Inbook inbook, BibEntry bibEntry) { calendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(value); JAXBElement year = new JAXBElement<>( - new QName(BIBTEXML_NAMESPACE_URI, "year"), - XMLGregorianCalendar.class, calendar); + new QName(BIBTEXML_NAMESPACE_URI, "year"), XMLGregorianCalendar.class, calendar); inbook.getContent().add(year); } catch (DatatypeConfigurationException e) { LOGGER.error("A configuration error occured"); } } else if ("number".equals(key)) { JAXBElement number = new JAXBElement<>(new QName(BIBTEXML_NAMESPACE_URI, "number"), - BigInteger.class, - new BigInteger(value)); + BigInteger.class, new BigInteger(value)); inbook.getContent().add(number); } else { JAXBElement element = new JAXBElement<>(new QName(BIBTEXML_NAMESPACE_URI, key), String.class, @@ -188,12 +171,29 @@ private void parseInbook(Inbook inbook, BibEntry bibEntry) { inbook.getContent().add(element); } } + + //set the entryType to the entry + entry.setInbook(inbook); } - private void parse(T entryType, BibEntry bibEntry) { + /** + * Generic method that gets an instance of an entry type (article, book, booklet ...). It also + * gets one bibEntry. Then the method checks all fields of the entry and then for all fields the method + * uses the set method of the entry type with the fieldname. So for example if a bib entry has the field + * author and the value for it is "Max Mustermann" and the given type is an article, then this method + * will invoke article.setAuthor("Max Mustermann").
+ *
+ * The second part of this method is that the entry type will be set to the entry. So e.g., if the type is + * article then entry.setArticle(article) will be invoked. + * + * @param entryType The type parameterized type of the entry. + * @param bibEntry The bib entry, which fields will be set to the entryType. + * @param entry The bibtexml entry. The entryType will be set to this entry. + */ + private void parse(T entryType, BibEntry bibEntry, Entry entry) { List declaredSetMethods = getListOfSetMethods(entryType); Map fieldMap = bibEntry.getFieldMap(); - for (java.util.Map.Entry entryField : fieldMap.entrySet()) { + for (Map.Entry entryField : fieldMap.entrySet()) { String value = entryField.getValue(); String key = entryField.getKey(); for (Method method : declaredSetMethods) { @@ -221,6 +221,21 @@ private void parse(T entryType, BibEntry bibEntry) { LOGGER.error("Could not invoke method", e); } } + + //set the entryType to the entry + List entryMethods = getListOfSetMethods(entryType); + for (Method method : entryMethods) { + String methodWithoutSet = method.getName().replace("set", ""); + String simpleClassName = entryType.getClass().getSimpleName().replaceAll("\\[", "").replaceAll("\\]", + ""); + if (methodWithoutSet.equals(simpleClassName)) { + try { + method.invoke(entry, entryType); + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + LOGGER.warn("Could not set the type to the entry"); + } + } + } } } diff --git a/src/test/java/net/sf/jabref/logic/exporter/BibTeXMLExporterTestFiles.java b/src/test/java/net/sf/jabref/logic/exporter/BibTeXMLExporterTestFiles.java index e4216f77800..d9cc71dec5e 100644 --- a/src/test/java/net/sf/jabref/logic/exporter/BibTeXMLExporterTestFiles.java +++ b/src/test/java/net/sf/jabref/logic/exporter/BibTeXMLExporterTestFiles.java @@ -18,6 +18,7 @@ import net.sf.jabref.model.entry.BibEntry; import net.sf.jabref.preferences.JabRefPreferences; +import org.junit.Assert; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -32,7 +33,6 @@ import org.xmlunit.diff.ElementSelectors; import org.xmlunit.matchers.CompareMatcher; -import static org.junit.Assert.*; @RunWith(Parameterized.class) public class BibTeXMLExporterTestFiles { @@ -83,7 +83,7 @@ public final void testPerformExport() throws IOException, SaveException { Builder control = Input.from(Files.newInputStream(resourceDir.resolve(xmlFileName))); Builder test = Input.from(Files.newInputStream(Paths.get(tempFilename))); - assertThat(test, CompareMatcher.isSimilarTo(control) + Assert.assertThat(test, CompareMatcher.isSimilarTo(control) .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byNameAndText)).throwComparisonFailure()); } } \ No newline at end of file From 310857e3656eccf29f9cab8082722001874aa05e Mon Sep 17 00:00:00 2001 From: Dennis Tschechlov Date: Wed, 21 Sep 2016 13:16:46 +0200 Subject: [PATCH 4/5] delete old layout files --- .../resource/layout/bibtexml.begin.layout | 5 --- .../resource/layout/bibtexml.end.layout | 1 - .../resources/resource/layout/bibtexml.layout | 36 ------------------- 3 files changed, 42 deletions(-) delete mode 100644 src/main/resources/resource/layout/bibtexml.begin.layout delete mode 100644 src/main/resources/resource/layout/bibtexml.end.layout delete mode 100644 src/main/resources/resource/layout/bibtexml.layout diff --git a/src/main/resources/resource/layout/bibtexml.begin.layout b/src/main/resources/resource/layout/bibtexml.begin.layout deleted file mode 100644 index 140a9b87aea..00000000000 --- a/src/main/resources/resource/layout/bibtexml.begin.layout +++ /dev/null @@ -1,5 +0,0 @@ - - - - - diff --git a/src/main/resources/resource/layout/bibtexml.end.layout b/src/main/resources/resource/layout/bibtexml.end.layout deleted file mode 100644 index 0447a2fef1e..00000000000 --- a/src/main/resources/resource/layout/bibtexml.end.layout +++ /dev/null @@ -1 +0,0 @@ - diff --git a/src/main/resources/resource/layout/bibtexml.layout b/src/main/resources/resource/layout/bibtexml.layout deleted file mode 100644 index 2d582f69653..00000000000 --- a/src/main/resources/resource/layout/bibtexml.layout +++ /dev/null @@ -1,36 +0,0 @@ - - -\begin{author} \format[XMLChars,AuthorFirstFirst]{\author}\end{author} -\begin{editor} \format[XMLChars,AuthorFirstFirst]{\editor}\end{editor} -\begin{title} \format[XMLChars]{\title}\end{title} -\begin{booktitle} \format[XMLChars]{\booktitle}\end{booktitle} -\begin{journal} \format[XMLChars]{\journal}\end{journal} -\begin{publisher} \format[XMLChars]{\publisher}\end{publisher} -\begin{year} \year\end{year} -\begin{volume} \format[XMLChars]{\volume}\end{volume} -\begin{month} \format[XMLChars]{\month}\end{month} -\begin{chapter} \format[XMLChars]{\chapter}\end{chapter} -\begin{pages} \format[FormatPagesForXML]{\pages}\end{pages} -\begin{number} \format[XMLChars]{\number}\end{number} -\begin{edition} \format[XMLChars]{\edition}\end{edition} -\begin{series} \format[XMLChars]{\series}\end{series} -\begin{institution} \format[XMLChars]{\institution}\end{institution} -\begin{organization} \format[XMLChars]{\organization}\end{organization} -\begin{school} \format[XMLChars]{\school}\end{school} -\begin{address} \format[XMLChars]{\address}\end{address} -\begin{howpublished} \format[XMLChars]{\howpublished}\end{howpublished} -\begin{abstract} \format[XMLChars]{\abstract}\end{abstract} -\begin{url} \format[XMLChars]{\url}\end{url} -\begin{doi} \format[DOIStrip,XMLChars]{\doi}\end{doi} -\begin{eid} \format[XMLChars]{\eid}\end{eid} -\begin{type} \format[XMLChars]{\type}\end{type} -\begin{crossref} \format[XMLChars]{\crossref}\end{crossref} -\begin{annote} \format[XMLChars]{\annote}\end{annote} -\begin{keywords} \format[XMLChars]{\keywords}\end{keywords} -\begin{comment} \format[XMLChars]{\comment}\end{comment} -\begin{note} \format[XMLChars]{\note}\end{note} -\begin{key} \format[XMLChars]{\key}\end{key} -\begin{review} \format[XMLChars]{\review}\end{review} -\begin{file}\format[WrapFileLinks( \p\n)]{\file}\end{file} - - From 7f344ee8ca63ed18af880d5daac347f94ec62706 Mon Sep 17 00:00:00 2001 From: Dennis Tschechlov Date: Wed, 21 Sep 2016 20:05:39 +0200 Subject: [PATCH 5/5] adress comments --- .../logic/exporter/BibTeXMLExportFormat.java | 17 ++++++++--------- .../sf/jabref/logic/exporter/ExportFormats.java | 3 +-- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/src/main/java/net/sf/jabref/logic/exporter/BibTeXMLExportFormat.java b/src/main/java/net/sf/jabref/logic/exporter/BibTeXMLExportFormat.java index 23a6d800803..78e0556af78 100644 --- a/src/main/java/net/sf/jabref/logic/exporter/BibTeXMLExportFormat.java +++ b/src/main/java/net/sf/jabref/logic/exporter/BibTeXMLExportFormat.java @@ -9,7 +9,6 @@ import java.util.Locale; import java.util.Map; import java.util.Objects; -import java.util.Optional; import java.util.stream.Collectors; import javax.xml.bind.JAXBContext; @@ -43,6 +42,9 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +/** + * Export format for the BibTeXML format. + */ public class BibTeXMLExportFormat extends ExportFormat { private static final String BIBTEXML_NAMESPACE_URI = "http://bibtexml.sf.net/"; @@ -68,10 +70,7 @@ public void performExport(final BibDatabaseContext databaseContext, final String for (BibEntry bibEntry : entries) { Entry entry = new Entry(); - Optional citeKey = bibEntry.getCiteKeyOptional(); - if (citeKey.isPresent()) { - entry.setId(citeKey.get()); - } + bibEntry.getCiteKeyOptional().ifPresent(citeKey -> entry.setId(citeKey)); String type = bibEntry.getType().toLowerCase(ENGLISH); switch (type) { @@ -141,7 +140,7 @@ private void createMarshallerAndWriteToFile(File file, String resultFile) throws } /** - * Contains same logic as the parse method, but inbook needs a special treatment, because + * Contains same logic as the {@link parse()} method, but inbook needs a special treatment, because * the contents of inbook are stored in a List of JAXBElements. So we first need to create * a JAXBElement for every field and then add it to the content list. */ @@ -223,11 +222,11 @@ private void parse(T entryType, BibEntry bibEntry, Entry entry) { } //set the entryType to the entry - List entryMethods = getListOfSetMethods(entryType); + List entryMethods = getListOfSetMethods(entry); for (Method method : entryMethods) { String methodWithoutSet = method.getName().replace("set", ""); - String simpleClassName = entryType.getClass().getSimpleName().replaceAll("\\[", "").replaceAll("\\]", - ""); + String simpleClassName = entryType.getClass().getSimpleName(); + if (methodWithoutSet.equals(simpleClassName)) { try { method.invoke(entry, entryType); diff --git a/src/main/java/net/sf/jabref/logic/exporter/ExportFormats.java b/src/main/java/net/sf/jabref/logic/exporter/ExportFormats.java index 6a6578f9b79..ed9d2cf4fe0 100644 --- a/src/main/java/net/sf/jabref/logic/exporter/ExportFormats.java +++ b/src/main/java/net/sf/jabref/logic/exporter/ExportFormats.java @@ -30,8 +30,6 @@ public static void initAllExports(Map customFormats, savePreferences)); ExportFormats.putFormat(new ExportFormat("DIN 1505", "din1505", "din1505winword", "din1505", ".rtf", layoutPreferences, savePreferences)); - ExportFormats.putFormat( -new BibTeXMLExportFormat()); ExportFormats.putFormat( new ExportFormat("BibO RDF", "bibordf", "bibordf", null, ".rdf", layoutPreferences, savePreferences)); ExportFormats.putFormat(new ExportFormat(Localization.lang("HTML table"), "tablerefs", "tablerefs", "tablerefs", @@ -56,6 +54,7 @@ public static void initAllExports(Map customFormats, ExportFormats.putFormat( new ExportFormat("MIS Quarterly", "misq", "misq", "misq", ".rtf", layoutPreferences, savePreferences)); + ExportFormats.putFormat(new BibTeXMLExportFormat()); ExportFormats.putFormat(new OpenOfficeDocumentCreator()); ExportFormats.putFormat(new OpenDocumentSpreadsheetCreator()); ExportFormats.putFormat(new MSBibExportFormat());