de.undercouch.citeproc.BibliographyFileReader Maven / Gradle / Ivy
package de.undercouch.citeproc;
import de.undercouch.citeproc.bibtex.BibTeXConverter;
import de.undercouch.citeproc.bibtex.BibTeXItemDataProvider;
import de.undercouch.citeproc.csl.CSLItemData;
import de.undercouch.citeproc.endnote.EndNoteConverter;
import de.undercouch.citeproc.endnote.EndNoteItemDataProvider;
import de.undercouch.citeproc.endnote.EndNoteLibrary;
import de.undercouch.citeproc.helper.json.JsonLexer;
import de.undercouch.citeproc.helper.json.JsonParser;
import de.undercouch.citeproc.ris.RISConverter;
import de.undercouch.citeproc.ris.RISItemDataProvider;
import de.undercouch.citeproc.ris.RISLibrary;
import org.jbibtex.BibTeXDatabase;
import org.jbibtex.ParseException;
import org.yaml.snakeyaml.Yaml;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
/**
* A convenience class providing methods to load any supported kind of
* bibliography files. The class automatically detects the correct file
* format and returns a {@link ItemDataProvider} that holds all
* bibliography items read from the file.
* @author Michel Kraemer
*/
public class BibliographyFileReader {
/**
* Supported file formats for bibliography files
*/
public enum FileFormat {
/**
* A BibTeX file
*/
BIBTEX,
/**
* A CSL JSON object
*/
JSON_OBJECT,
/**
* An array of CSL JSON objects
*/
JSON_ARRAY,
/**
* A YAML document
* @since 1.1.0
*/
YAML,
/**
* An EndNote file
*/
ENDNOTE,
/**
* An RIS file
*/
RIS,
/**
* Unknown file format
*/
UNKNOWN
}
/**
* Reads all items from an input bibliography file and returns a provider
* serving these items
* @param bibfile the input file
* @return the provider
* @throws FileNotFoundException if the input file was not found
* @throws IOException if the input file could not be read
*/
public ItemDataProvider readBibliographyFile(File bibfile)
throws FileNotFoundException, IOException {
// open buffered input stream to bibliography file
if (!bibfile.exists()) {
throw new FileNotFoundException("Bibliography file `" +
bibfile.getName() + "' does not exist");
}
try (BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(bibfile))) {
return readBibliographyFile(bis, bibfile.getName());
}
}
/**
* Reads all items from an input stream and returns a provider
* serving these items. Note that you can supply an additional file
* name to help the method to determine the exact bibliography file format.
* If you don't know the file name you can pass null, but in this case the
* method's result might try to read the input stream using the wrong
* file format (depending on the input stream's contents). Also note
* that the caller is responsible for closing the given input stream.
* @param bibstream the input stream
* @param filename the name of the input file (can be null if you don't
* know the name)
* @return the provider
* @throws IOException if the input stream could not be read
*/
public ItemDataProvider readBibliographyFile(InputStream bibstream,
String filename) throws IOException {
BufferedInputStream bis;
if (bibstream instanceof BufferedInputStream) {
bis = (BufferedInputStream)bibstream;
} else {
bis = new BufferedInputStream(bibstream);
}
// determine file format
FileFormat ff = determineFileFormat(bis, filename);
// read stream
return readBibliographyFile(bis, ff);
}
/**
* Reads all items from an input stream using the given file format and
* returns a provider serving these items.
* @param bibstream the input stream
* @param format the bibliography file format
* @return the provider
* @throws IOException if the input stream could not be read
*/
public ItemDataProvider readBibliographyFile(InputStream bibstream,
FileFormat format) throws IOException {
ItemDataProvider provider;
try {
// load bibliography file
if (format == FileFormat.BIBTEX) {
BibTeXDatabase db = new BibTeXConverter().loadDatabase(bibstream);
BibTeXItemDataProvider bibtexprovider = new BibTeXItemDataProvider();
bibtexprovider.addDatabase(db);
provider = bibtexprovider;
} else if (format == FileFormat.JSON_ARRAY ||
format == FileFormat.JSON_OBJECT) {
JsonParser parser = new JsonParser(new JsonLexer(
new InputStreamReader(bibstream, StandardCharsets.UTF_8)));
List