public class CSL extends Object
The citation processor.
In order to use the processor in your application you first have to
create an ItemDataProvider that provides citation item data. For
example, the following dummy provider returns always the same data:
public class MyItemProvider implements ItemDataProvider {
@Override
public CSLItemData retrieveItem(String id) {
return new CSLItemDataBuilder()
.id(id)
.type(CSLType.ARTICLE_JOURNAL)
.title("A dummy journal article")
.author("John", "Smith")
.issued(2013, 9, 6)
.containerTitle("Dummy journal")
.build();
}
@Override
public String[] getIds() {
String ids[] = {"ID-0", "ID-1", "ID-2"};
return ids;
}
}
Now you can instantiate the CSL processor.
CSL citeproc = new CSL(new MyItemProvider(), "ieee");
citeproc.setOutputFormat("html");
You may also use makeAdhocBibliography(String, CSLItemData...) or
makeAdhocBibliography(String, String, CSLItemData...) to create
ad-hoc bibliographies from CSL items.
CSLItemData item = new CSLItemDataBuilder()
.type(CSLType.WEBPAGE)
.title("citeproc-java: A Citation Style Language (CSL) processor for Java")
.author("Michel", "Kraemer")
.issued(2014, 7, 13)
.URL("http://michel-kraemer.github.io/citeproc-java/")
.accessed(2014, 7, 13)
.build();
String bibl = CSL.makeAdhocBibliography("ieee", item).makeString();
Please note that this class is not thread-safe. However, shared resources
are held in memory, so constructing new instances is generally rather cheap.
If your settings do not change you may cache instances of this class in a
ThreadLocal.
ThreadLocal<CSL> csl = new ThreadLocal<CSL>() {
@Override
protected CSL initialValue() {
return new CSL(itemDataProvider, style, lang);
}
};
Please remember to reset the ThreadLocal if you don't need
the CSL instance anymore to avoid memory leaks.| Constructor and Description |
|---|
CSL(ItemDataProvider itemDataProvider,
AbbreviationProvider abbreviationProvider,
String style)
Constructs a new citation processor
|
CSL(ItemDataProvider itemDataProvider,
AbbreviationProvider abbreviationProvider,
String style,
String lang)
Constructs a new citation processor
|
CSL(ItemDataProvider itemDataProvider,
LocaleProvider localeProvider,
AbbreviationProvider abbreviationProvider,
String style,
String lang,
boolean forceLang)
Constructs a new citation processor
|
CSL(ItemDataProvider itemDataProvider,
LocaleProvider localeProvider,
AbbreviationProvider abbreviationProvider,
VariableWrapper variableWrapper,
String style,
String lang,
boolean forceLang)
Constructs a new citation processor
|
CSL(ItemDataProvider itemDataProvider,
LocaleProvider localeProvider,
String style,
String lang,
boolean forceLang)
Constructs a new citation processor
|
CSL(ItemDataProvider itemDataProvider,
String style)
Constructs a new citation processor
|
CSL(ItemDataProvider itemDataProvider,
String style,
String lang)
Constructs a new citation processor
|
| Modifier and Type | Method and Description |
|---|---|
protected Object |
getEngine() |
protected ScriptRunner |
getScriptRunner() |
static Set<String> |
getSupportedLocales()
Calculates a list of available citation locales
|
static List<String> |
getSupportedOutputFormats()
Calculates a list of supported output formats
|
static Set<String> |
getSupportedStyles()
Calculates a list of available citation styles
|
static Bibliography |
makeAdhocBibliography(String style,
CSLItemData... items)
Creates an ad hoc bibliography from the given citation items using the
"html" output format. |
static Bibliography |
makeAdhocBibliography(String style,
String outputFormat,
CSLItemData... items)
Creates an ad hoc bibliography from the given citation items.
|
Bibliography |
makeBibliography()
Generates a bibliography for the registered citations
|
Bibliography |
makeBibliography(SelectionMode mode,
CSLItemData... selection)
Generates a bibliography for the registered citations.
|
Bibliography |
makeBibliography(SelectionMode mode,
CSLItemData[] selection,
CSLItemData[] quash)
Generates a bibliography for the registered citations.
|
List<Citation> |
makeCitation(CSLCitation citation)
Generates citation strings that can be inserted into the text.
|
List<Citation> |
makeCitation(CSLCitation citation,
List<CitationIDIndexPair> citationsPre,
List<CitationIDIndexPair> citationsPost)
Generates citation strings that can be inserted into the text.
|
List<Citation> |
makeCitation(String... ids)
Generates citation strings that can be inserted into the text.
|
void |
registerCitationItems(String... ids)
Introduces the given citation IDs to the processor.
|
void |
registerCitationItems(String[] ids,
boolean unsorted)
Introduces the given citation IDs to the processor.
|
void |
reset()
Resets the processor's state
|
void |
setAbbreviations(String name)
Enables the abbreviation list with the given name.
|
void |
setConvertLinks(boolean convert)
Specifies if the processor should convert URLs and DOIs in the output
to links.
|
void |
setOutputFormat(String format)
Sets the processor's output format
|
static boolean |
supportsStyle(String style)
Checks if a given citation style is supported
|
public CSL(ItemDataProvider itemDataProvider, String style) throws IOException
itemDataProvider - an object that provides citation item datastyle - the citation style to use. May either be a serialized
XML representation of the style or a style's name such as ieee.
In the latter case, the processor loads the style from the classpath (e.g.
/ieee.csl)IOException - if the underlying JavaScript files or the CSL style
could not be loadedpublic CSL(ItemDataProvider itemDataProvider, AbbreviationProvider abbreviationProvider, String style) throws IOException
itemDataProvider - an object that provides citation item dataabbreviationProvider - an object that provides abbreviationsstyle - the citation style to use. May either be a serialized
XML representation of the style or a style's name such as ieee.
In the latter case, the processor loads the style from the classpath (e.g.
/ieee.csl)IOException - if the underlying JavaScript files or the CSL style
could not be loadedpublic CSL(ItemDataProvider itemDataProvider, String style, String lang) throws IOException
itemDataProvider - an object that provides citation item datastyle - the citation style to use. May either be a serialized
XML representation of the style or a style's name such as ieee.
In the latter case, the processor loads the style from the classpath (e.g.
/ieee.csl)lang - an RFC 4646 identifier for the citation locale (e.g. en-US)IOException - if the underlying JavaScript files or the CSL style
could not be loadedpublic CSL(ItemDataProvider itemDataProvider, AbbreviationProvider abbreviationProvider, String style, String lang) throws IOException
itemDataProvider - an object that provides citation item dataabbreviationProvider - an object that provides abbreviationsstyle - the citation style to use. May either be a serialized
XML representation of the style or a style's name such as ieee.
In the latter case, the processor loads the style from the classpath (e.g.
/ieee.csl)lang - an RFC 4646 identifier for the citation locale (e.g. en-US)IOException - if the underlying JavaScript files or the CSL style
could not be loadedpublic CSL(ItemDataProvider itemDataProvider, LocaleProvider localeProvider, String style, String lang, boolean forceLang) throws IOException
itemDataProvider - an object that provides citation item datalocaleProvider - an object that provides CSL localesstyle - the citation style to use. May either be a serialized
XML representation of the style or a style's name such as ieee.
In the latter case, the processor loads the style from the classpath (e.g.
/ieee.csl)lang - an RFC 4646 identifier for the citation locale (e.g. en-US)forceLang - true if the given locale should overwrite any default localeIOException - if the underlying JavaScript files or the CSL style
could not be loadedpublic CSL(ItemDataProvider itemDataProvider, LocaleProvider localeProvider, AbbreviationProvider abbreviationProvider, String style, String lang, boolean forceLang) throws IOException
itemDataProvider - an object that provides citation item datalocaleProvider - an object that provides CSL localesabbreviationProvider - an object that provides abbreviationsstyle - the citation style to use. May either be a serialized
XML representation of the style or a style's name such as ieee.
In the latter case, the processor loads the style from the classpath (e.g.
/ieee.csl)lang - an RFC 4646 identifier for the citation locale (e.g. en-US)forceLang - true if the given locale should overwrite any default localeIOException - if the underlying JavaScript files or the CSL style
could not be loadedpublic CSL(ItemDataProvider itemDataProvider, LocaleProvider localeProvider, AbbreviationProvider abbreviationProvider, VariableWrapper variableWrapper, String style, String lang, boolean forceLang) throws IOException
itemDataProvider - an object that provides citation item datalocaleProvider - an object that provides CSL localesabbreviationProvider - an object that provides abbreviationsvariableWrapper - an object that decorates rendered itemsstyle - the citation style to use. May either be a serialized
XML representation of the style or a style's name such as ieee.
In the latter case, the processor loads the style from the classpath (e.g.
/ieee.csl)lang - an RFC 4646 identifier for the citation locale (e.g. en-US)forceLang - true if the given locale should overwrite any default localeIOException - if the underlying JavaScript files or the CSL style
could not be loadedpublic static List<String> getSupportedOutputFormats() throws IOException
IOException - if the underlying JavaScript files could not be loadedpublic static Set<String> getSupportedStyles() throws IOException
IOException - if the citation styles could not be loadedpublic static boolean supportsStyle(String style)
style - the citation style's namepublic static Set<String> getSupportedLocales() throws IOException
IOException - if the citation locales could not be loadedprotected ScriptRunner getScriptRunner()
protected Object getEngine()
public void setOutputFormat(String format)
format - the format (one of "html",
"text", "asciidoc", "fo",
or "rtf")public void setConvertLinks(boolean convert)
setOutputFormat(String)convert - true if URLs and DOIs should be converted to linkspublic void setAbbreviations(String name)
AbbreviationProvider.getAbbreviations(String) with the
given String to get the abbreviations that should be used from here on.name - the name of the abbreviation list to enablepublic void registerCitationItems(String... ids)
ItemDataProvider.retrieveItem(String) for each ID to get
the respective citation item. The retrieved items will be added to the
bibliography, so you don't have to call makeCitation(String...)
for each of them anymore.ids - the IDs to registerIllegalArgumentException - if one of the given IDs refers to
citation item data that does not existpublic void registerCitationItems(String[] ids, boolean unsorted)
ItemDataProvider.retrieveItem(String) for each ID to get
the respective citation item. The retrieved items will be added to the
bibliography, so you don't have to call makeCitation(String...)
for each of them anymore.ids - the IDs to registerunsorted - true if items should not be sorted in the bibliographyIllegalArgumentException - if one of the given IDs refers to
citation item data that does not existpublic List<Citation> makeCitation(String... ids)
ItemDataProvider.retrieveItem(String) for each of the given
IDs to request the corresponding citation item. Additionally, it saves
the IDs, so makeBibliography() will generate a bibliography
that only consists of the retrieved citation items.ids - IDs of citation items for which strings should be generatedIllegalArgumentException - if one of the given IDs refers to
citation item data that does not existpublic List<Citation> makeCitation(CSLCitation citation)
ItemDataProvider.retrieveItem(String) for each item in the
given set to request the corresponding citation item data. Additionally,
it saves the requested citation IDs, so makeBibliography() will
generate a bibliography that only consists of the retrieved items.citation - a set of citation items for which strings should be generatedIllegalArgumentException - if the given set of citation items
refers to citation item data that does not existpublic List<Citation> makeCitation(CSLCitation citation, List<CitationIDIndexPair> citationsPre, List<CitationIDIndexPair> citationsPost)
ItemDataProvider.retrieveItem(String) for each item in the
given set to request the corresponding citation item data. Additionally,
it saves the requested citation IDs, so makeBibliography() will
generate a bibliography that only consists of the retrieved items.citation - a set of citation items for which strings should be generatedcitationsPre - citations that precede citationcitationsPost - citations that come after citationIllegalArgumentException - if the given set of citation items
refers to citation item data that does not existpublic Bibliography makeBibliography()
public Bibliography makeBibliography(SelectionMode mode, CSLItemData... selection)
mode - the selection modeselection - the example item data objects that contain
the fields and field values to matchpublic Bibliography makeBibliography(SelectionMode mode, CSLItemData[] selection, CSLItemData[] quash)
mode - the selection modeselection - the example item data objects that contain
the fields and field values to matchquash - regardless of the item data in selection
skip items if all fields/values from this list matchpublic void reset()
public static Bibliography makeAdhocBibliography(String style, CSLItemData... items) throws IOException
"html" output format. Calling this method is rather
expensive as it initializes the CSL processor. If you need to create
bibliographies multiple times in your application you should create
the processor yourself and cache it if necessary.style - the citation style to use. May either be a serialized
XML representation of the style or a style's name such as ieee.
In the latter case, the processor loads the style from the classpath (e.g.
/ieee.csl)items - the citation items to add to the bibliographyIOException - if the underlying JavaScript files or the CSL style
could not be loadedmakeAdhocBibliography(String, String, CSLItemData...)public static Bibliography makeAdhocBibliography(String style, String outputFormat, CSLItemData... items) throws IOException
style - the citation style to use. May either be a serialized
XML representation of the style or a style's name such as ieee.
In the latter case, the processor loads the style from the classpath (e.g.
/ieee.csl)outputFormat - the processor's output format (one of
"html", "text", "asciidoc",
"fo", or "rtf")items - the citation items to add to the bibliographyIOException - if the underlying JavaScript files or the CSL style
could not be loaded