diff --git a/ccm-sci-publications/src/com/arsdigita/cms/scipublications/exporter/bibtex/BibTeXExporter.java b/ccm-sci-publications/src/com/arsdigita/cms/scipublications/exporter/bibtex/BibTeXExporter.java index c44f966fc..7eeefa1f9 100644 --- a/ccm-sci-publications/src/com/arsdigita/cms/scipublications/exporter/bibtex/BibTeXExporter.java +++ b/ccm-sci-publications/src/com/arsdigita/cms/scipublications/exporter/bibtex/BibTeXExporter.java @@ -46,25 +46,10 @@ public class BibTeXExporter implements SciPublicationsExporter { } public String exportPublication(final Publication publication) { - BibTeXBuilder builder; - - builder = BibTeXBuilders.getInstance(). - getBibTeXBuilderForCcmPublicationtType(publication.getClass(). - getName()); - if ((builder == null) && publication instanceof PublicationWithPublisher) { - builder = BibTeXBuilders.getInstance(). - getBibTeXBuilderForCcmPublicationtType( - PublicationWithPublisher.class.getName()); - } else { - builder = BibTeXBuilders.getInstance(). - getBibTeXBuilderForCcmPublicationtType(Publication.class. - getName()); - } + throw new UnsupportedOperationException("Not implemented yet."); - - return builder.toBibTeX(); } } diff --git a/ccm-sci-publications/src/com/arsdigita/cms/scipublications/exporter/bibtex/builders/BibTeXBuilders.java b/ccm-sci-publications/src/com/arsdigita/cms/scipublications/exporter/bibtex/builders/BibTeXBuilders.java index c4f83df48..717e021f6 100644 --- a/ccm-sci-publications/src/com/arsdigita/cms/scipublications/exporter/bibtex/builders/BibTeXBuilders.java +++ b/ccm-sci-publications/src/com/arsdigita/cms/scipublications/exporter/bibtex/builders/BibTeXBuilders.java @@ -32,7 +32,7 @@ public class BibTeXBuilders { return Instance.INSTANCE; } - public BibTeXBuilder getBibTeXBuilderForCcmPublicationtType(final String type) { + public BibTeXBuilder getBibTeXBuilderForType(final String type) { return builders.get(type); } } diff --git a/ccm-sci-publications/src/com/arsdigita/cms/scipublications/exporter/bibtex/converters/AbstractBibTeXConverter.java b/ccm-sci-publications/src/com/arsdigita/cms/scipublications/exporter/bibtex/converters/AbstractBibTeXConverter.java new file mode 100644 index 000000000..d0578163e --- /dev/null +++ b/ccm-sci-publications/src/com/arsdigita/cms/scipublications/exporter/bibtex/converters/AbstractBibTeXConverter.java @@ -0,0 +1,90 @@ +package com.arsdigita.cms.scipublications.exporter.bibtex.converters; + +import com.arsdigita.cms.contenttypes.AuthorshipCollection; +import com.arsdigita.cms.contenttypes.Publication; +import com.arsdigita.cms.contenttypes.PublicationWithPublisher; +import com.arsdigita.cms.scipublications.exporter.bibtex.builders.BibTeXBuilder; +import com.arsdigita.cms.scipublications.exporter.bibtex.builders.BibTeXBuilders; +import com.arsdigita.cms.scipublications.exporter.bibtex.builders.BibTeXField; +import com.arsdigita.cms.scipublications.exporter.bibtex.builders.UnsupportedFieldException; + +/** + * + * @author jensp + */ +public abstract class AbstractBibTeXConverter implements BibTeXConverter { + + private BibTeXBuilder bibTeXBuilder; + + protected void convertAuthors(final Publication publication) { + AuthorshipCollection authors; + + authors = publication.getAuthors(); + if (authors != null) { + while (authors.next()) { + if (authors.isEditor()) { + getBibTeXBuilder().addEditor(authors.getAuthor()); + } else { + getBibTeXBuilder().addAuthor(authors.getAuthor()); + } + } + } + } + + protected void convertTitle(final Publication publication) + throws UnsupportedFieldException { + getBibTeXBuilder().setField(BibTeXField.TITLE, publication.getTitle()); + } + + protected void convertYear(final Publication publication) + throws UnsupportedFieldException { + if (publication.getYearOfPublication() != null) { + getBibTeXBuilder().setField(BibTeXField.YEAR, + publication.getYearOfPublication(). + toString()); + } + } + + protected void convertPublisher(final PublicationWithPublisher publication) + throws UnsupportedFieldException { + if (publication.getPublisher() != null) { + getBibTeXBuilder().setField(BibTeXField.PUBLISHER, + publication.getPublisher().getTitle()); + } + } + + protected void convertISBN(final PublicationWithPublisher publication) + throws UnsupportedFieldException { + if (publication.getISBN() != null) { + getBibTeXBuilder().setField(BibTeXField.ISBN, + publication.getISBN()); + } + } + + protected void convertVolume(final PublicationWithPublisher publication) + throws UnsupportedFieldException { + if (publication.getVolume() != null) { + getBibTeXBuilder().setField(BibTeXField.VOLUME, + publication.getVolume().toString()); + } + } + + protected void convertEdition(final PublicationWithPublisher publication) + throws UnsupportedFieldException { + if (publication.getEdition() != null) { + getBibTeXBuilder().setField(BibTeXField.EDITION, + publication.getEdition()); + } + } + + protected BibTeXBuilder getBibTeXBuilder() { + if (bibTeXBuilder == null) { + bibTeXBuilder = BibTeXBuilders.getInstance().getBibTeXBuilderForType( + getBibTeXType()); + } + + return bibTeXBuilder; + } + + protected abstract String getBibTeXType(); +} diff --git a/ccm-sci-publications/src/com/arsdigita/cms/scipublications/exporter/bibtex/converters/ArticleInCollectedVolumeConverter.java b/ccm-sci-publications/src/com/arsdigita/cms/scipublications/exporter/bibtex/converters/ArticleInCollectedVolumeConverter.java new file mode 100644 index 000000000..bb7b0066a --- /dev/null +++ b/ccm-sci-publications/src/com/arsdigita/cms/scipublications/exporter/bibtex/converters/ArticleInCollectedVolumeConverter.java @@ -0,0 +1,104 @@ +package com.arsdigita.cms.scipublications.exporter.bibtex.converters; + +import com.arsdigita.cms.contenttypes.Publication; +import com.arsdigita.cms.contenttypes.ArticleInCollectedVolume; +import com.arsdigita.cms.contenttypes.SeriesCollection; +import com.arsdigita.cms.scipublications.exporter.bibtex.builders.BibTeXBuilder; +import com.arsdigita.cms.scipublications.exporter.bibtex.builders.BibTeXField; +import com.arsdigita.cms.scipublications.exporter.bibtex.builders.UnsupportedFieldException; +import org.apache.log4j.Logger; + +/** + * + * @author jensp + */ +public class ArticleInCollectedVolumeConverter extends AbstractBibTeXConverter { + + private static final Logger logger = Logger.getLogger( + ArticleInCollectedVolumeConverter.class); + + @Override + protected String getBibTeXType() { + return "incollection"; + } + + public String convert(final Publication publication) { + ArticleInCollectedVolume article; + BibTeXBuilder builder; + + if (!(publication instanceof ArticleInCollectedVolume)) { + throw new UnsupportedCcmTypeException( + String.format("The ArticleInCollectedVolumeConverter only " + + "supports publication types which are of the" + + "type ArticleInCollectedVolume or which are " + + "extending " + + "ArticleInCollectedVolume. The " + + "provided publication is of type '%s' which " + + "is not of type " + + "ArticleInCollectedVolume and does not " + + "extends ArticleInCollectedVolume.", + publication.getClass().getName())); + } + + article = (ArticleInCollectedVolume) publication; + + convertAuthors(publication); + builder = getBibTeXBuilder(); + try { + convertTitle(publication); + convertYear(publication); + + builder.setField(BibTeXField.BOOKTITLE, + article.getCollectedVolume().getTitle()); + if (article.getCollectedVolume().getPublisher() == null) { + builder.setField(BibTeXField.PUBLISHER, ""); + } else { + builder.setField(BibTeXField.PUBLISHER, + article.getCollectedVolume().getPublisher(). + getTitle()); + } + + if (article.getCollectedVolume().getVolume() != null) { + builder.setField(BibTeXField.VOLUME, + article.getCollectedVolume().getVolume(). + toString()); + } + SeriesCollection seriesColl = + article.getCollectedVolume().getSeries(); + if ((seriesColl != null) && (seriesColl.size() > 0)) { + + seriesColl.next(); + + builder.setField(BibTeXField.SERIES, + seriesColl.getSeries().getTitle()); + + seriesColl.close(); + } + + if (article.getChapter() != null) { + builder.setField(BibTeXField.CHAPTER, article.getChapter()); + } + + if (article.getPagesFrom() != null) { + builder.setField(BibTeXField.PAGES, + String.format("%s - %s", + article.getPagesFrom(), + article.getPagesTo())); + } + + if (article.getCollectedVolume().getEdition() != null) { + builder.setField(BibTeXField.EDITION, + article.getCollectedVolume().getEdition()); + } + } catch (UnsupportedFieldException ex) { + logger.warn("Tried to set unsupported BibTeX field while " + + "converting a publication"); + } + + return builder.toBibTeX(); + } + + public String getCcmType() { + return ArticleInCollectedVolume.class.getName(); + } +} diff --git a/ccm-sci-publications/src/com/arsdigita/cms/scipublications/exporter/bibtex/converters/BibTeXConverters.java b/ccm-sci-publications/src/com/arsdigita/cms/scipublications/exporter/bibtex/converters/BibTeXConverters.java index 9df7cd7c0..07b3d8cfe 100644 --- a/ccm-sci-publications/src/com/arsdigita/cms/scipublications/exporter/bibtex/converters/BibTeXConverters.java +++ b/ccm-sci-publications/src/com/arsdigita/cms/scipublications/exporter/bibtex/converters/BibTeXConverters.java @@ -1,6 +1,7 @@ package com.arsdigita.cms.scipublications.exporter.bibtex.converters; import com.arsdigita.cms.contenttypes.Publication; +import com.arsdigita.cms.contenttypes.PublicationWithPublisher; import java.util.HashMap; import java.util.Map; @@ -11,13 +12,13 @@ import java.util.Map; public class BibTeXConverters { private Map converters = - new HashMap(); + new HashMap(); private BibTeXConverters() { - } private static class Instance { + private static BibTeXConverters INSTANCE = new BibTeXConverters(); } @@ -30,7 +31,16 @@ public class BibTeXConverters { converter = converters.get(publication.getClass().getName()); + if (converter == null) { + if (publication instanceof PublicationWithPublisher) { + converter = converters.get(PublicationWithPublisher.class. + getName()); + } else { + converter = converters.get(Publication.class.getName()); + + } + } + return converter.convert(publication); } - } diff --git a/ccm-sci-publications/src/com/arsdigita/cms/scipublications/exporter/bibtex/converters/MonographConverter.java b/ccm-sci-publications/src/com/arsdigita/cms/scipublications/exporter/bibtex/converters/MonographConverter.java new file mode 100644 index 000000000..a9080b04d --- /dev/null +++ b/ccm-sci-publications/src/com/arsdigita/cms/scipublications/exporter/bibtex/converters/MonographConverter.java @@ -0,0 +1,58 @@ +package com.arsdigita.cms.scipublications.exporter.bibtex.converters; + +import com.arsdigita.cms.contenttypes.Publication; +import com.arsdigita.cms.contenttypes.Monograph; +import com.arsdigita.cms.scipublications.exporter.bibtex.builders.UnsupportedFieldException; +import org.apache.log4j.Logger; + +/** + * + * @author jensp + */ +public class MonographConverter extends AbstractBibTeXConverter { + + private static final Logger logger = Logger.getLogger( + MonographConverter.class); + + @Override + protected String getBibTeXType() { + return "book"; + } + + public String convert(final Publication publication) { + Monograph monograph; + + if (!(publication instanceof Monograph)) { + throw new UnsupportedCcmTypeException( + String.format("The MonographConverter only " + + "supports publication types which are of the" + + "type Monograph or which are extending " + + "Monograh. The " + + "provided publication is of type '%s' which " + + "is not of type Monograph and does not " + + "extends Monograph.", + publication.getClass().getName())); + } + + monograph = (Monograph) publication; + + convertAuthors(publication); + try { + convertTitle(publication); + convertYear(publication); + + convertPublisher(monograph); + convertISBN(monograph); + convertEdition(monograph); + } catch (UnsupportedFieldException ex) { + logger.warn("Tried to set unsupported BibTeX field while " + + "converting a publication"); + } + + return getBibTeXBuilder().toBibTeX(); + } + + public String getCcmType() { + return Monograph.class.getName(); + } +} diff --git a/ccm-sci-publications/src/com/arsdigita/cms/scipublications/exporter/bibtex/converters/PublicationConverter.java b/ccm-sci-publications/src/com/arsdigita/cms/scipublications/exporter/bibtex/converters/PublicationConverter.java new file mode 100644 index 000000000..2ad8b69bd --- /dev/null +++ b/ccm-sci-publications/src/com/arsdigita/cms/scipublications/exporter/bibtex/converters/PublicationConverter.java @@ -0,0 +1,39 @@ +package com.arsdigita.cms.scipublications.exporter.bibtex.converters; + +import com.arsdigita.cms.contenttypes.Publication; +import com.arsdigita.cms.scipublications.exporter.bibtex.builders.UnsupportedFieldException; +import org.apache.log4j.Logger; + +/** + * + * @author jensp + */ +public class PublicationConverter extends AbstractBibTeXConverter { + + private static final Logger logger = Logger.getLogger( + PublicationConverter.class); + + @Override + public String convert(final Publication publication) { + convertAuthors(publication); + try { + convertTitle(publication); + convertYear(publication); + } catch (UnsupportedFieldException ex) { + logger.warn("Tried to set unsupported BibTeX field while " + + "converting a publication"); + } + + return getBibTeXBuilder().toBibTeX(); + } + + @Override + public String getBibTeXType() { + return "misc"; + } + + @Override + public String getCcmType() { + return Publication.class.getName(); + } +} diff --git a/ccm-sci-publications/src/com/arsdigita/cms/scipublications/exporter/bibtex/converters/PublicationWithPublisherConverter.java b/ccm-sci-publications/src/com/arsdigita/cms/scipublications/exporter/bibtex/converters/PublicationWithPublisherConverter.java new file mode 100644 index 000000000..231eaaeab --- /dev/null +++ b/ccm-sci-publications/src/com/arsdigita/cms/scipublications/exporter/bibtex/converters/PublicationWithPublisherConverter.java @@ -0,0 +1,57 @@ +package com.arsdigita.cms.scipublications.exporter.bibtex.converters; + +import com.arsdigita.cms.contenttypes.Publication; +import com.arsdigita.cms.contenttypes.PublicationWithPublisher; +import com.arsdigita.cms.scipublications.exporter.bibtex.builders.UnsupportedFieldException; +import org.apache.log4j.Logger; + +/** + * + * @author jensp + */ +public class PublicationWithPublisherConverter extends AbstractBibTeXConverter { + + private static final Logger logger = Logger.getLogger( + PublicationWithPublisherConverter.class); + + @Override + protected String getBibTeXType() { + return "misc"; + } + + public String convert(final Publication publication) { + PublicationWithPublisher _publication; + + if (!(publication instanceof PublicationWithPublisher)) { + throw new UnsupportedCcmTypeException( + String.format("The PublicationWithPublicationConverter only " + + "supports publication types which are " + + "extending PublicationWithPublisher. The " + + "provided publication is of type '%s' which " + + "does not extends " + + "PublicationWithPublisher.", + publication.getClass().getName())); + } + + _publication = (PublicationWithPublisher) publication; + + convertAuthors(publication); + try { + convertTitle(publication); + convertYear(publication); + + convertPublisher(_publication); + convertISBN(_publication); + convertEdition(_publication); + } catch (UnsupportedFieldException ex) { + logger.warn("Tried to set unsupported BibTeX field while " + + "converting a publication"); + } + + return getBibTeXBuilder().toBibTeX(); + } + + public String getCcmType() { + return PublicationWithPublisher.class.getName(); + } +} diff --git a/ccm-sci-publications/src/com/arsdigita/cms/scipublications/exporter/bibtex/converters/UnsupportedCcmTypeException.java b/ccm-sci-publications/src/com/arsdigita/cms/scipublications/exporter/bibtex/converters/UnsupportedCcmTypeException.java new file mode 100644 index 000000000..abd78ce24 --- /dev/null +++ b/ccm-sci-publications/src/com/arsdigita/cms/scipublications/exporter/bibtex/converters/UnsupportedCcmTypeException.java @@ -0,0 +1,32 @@ +package com.arsdigita.cms.scipublications.exporter.bibtex.converters; + +/** + * + * @author jensp + */ +public class UnsupportedCcmTypeException extends RuntimeException { + + private static final long serialVersionUID = 1L; + + /** + * Creates a new instance of UnsupportedCcmType without detail message. + */ + public UnsupportedCcmTypeException() { + } + + /** + * Constructs an instance of UnsupportedCcmType with the specified detail message. + * @param msg the detail message. + */ + public UnsupportedCcmTypeException(final String msg) { + super(msg); + } + + public UnsupportedCcmTypeException(final Throwable cause) { + super(cause); + } + + public UnsupportedCcmTypeException(final String msg, Throwable cause) { + super(msg, cause); + } +}