001/**
002 * Copyright (c) 2011, The University of Southampton and the individual contributors.
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification,
006 * are permitted provided that the following conditions are met:
007 *
008 *   *  Redistributions of source code must retain the above copyright notice,
009 *      this list of conditions and the following disclaimer.
010 *
011 *   *  Redistributions in binary form must reproduce the above copyright notice,
012 *      this list of conditions and the following disclaimer in the documentation
013 *      and/or other materials provided with the distribution.
014 *
015 *   *  Neither the name of the University of Southampton nor the names of its
016 *      contributors may be used to endorse or promote products derived from this
017 *      software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030package org.openimaj.citation.annotation.processor;
031
032import java.io.IOException;
033import java.io.PrintWriter;
034import java.io.Writer;
035import java.util.HashSet;
036import java.util.Set;
037
038import javax.annotation.processing.AbstractProcessor;
039import javax.annotation.processing.Processor;
040import javax.annotation.processing.RoundEnvironment;
041import javax.annotation.processing.SupportedAnnotationTypes;
042import javax.lang.model.element.Element;
043import javax.lang.model.element.TypeElement;
044import javax.tools.Diagnostic.Kind;
045import javax.tools.FileObject;
046import javax.tools.StandardLocation;
047
048import org.openimaj.citation.annotation.Reference;
049import org.openimaj.citation.annotation.References;
050import org.openimaj.citation.annotation.output.StandardFormatters;
051
052/**
053 * {@link Processor} implementation that is capable of finding {@link Reference}
054 * and {@link References} annotations and generating lists which are then
055 * written.
056 * <p>
057 * Currently the processor produces a bibliography in BibTeX, HTML and Plain
058 * Text formats containing all references in the project.
059 *
060 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
061 */
062@SupportedAnnotationTypes(
063                value = { "org.openimaj.citation.annotation.Reference", "org.openimaj.citation.annotation.References" })
064public class ReferenceProcessor extends AbstractProcessor {
065        private static final String[] extensions = { "bib", "html", "txt" };
066        private static final String[] names = { "BibTeX", "HTML", "text" };
067        private static final StandardFormatters[] types = { StandardFormatters.BIBTEX, StandardFormatters.HTML,
068                        StandardFormatters.STRING };
069
070        Set<Reference> references = new HashSet<Reference>();
071
072        @Override
073        public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
074
075                for (final TypeElement te : annotations) {
076                        for (final Element e : roundEnv.getElementsAnnotatedWith(te)) {
077                                final Reference ann1 = e.getAnnotation(Reference.class);
078                                if (ann1 != null) {
079                                        references.add(ann1);
080                                }
081
082                                final References ann2 = e.getAnnotation(References.class);
083                                if (ann2 != null) {
084                                        for (final Reference r : ann2.references()) {
085                                                references.add(r);
086                                        }
087                                }
088                        }
089                }
090
091                if (roundEnv.processingOver()) {
092                        processingEnv.getMessager().printMessage(Kind.NOTE, "Creating project bibliography");
093
094                        for (int i = 0; i < types.length; i++) {
095                                try {
096                                        final String data = types[i].format(references);
097                                        final FileObject file = processingEnv.getFiler().createResource(StandardLocation.SOURCE_OUTPUT, "",
098                                                        "bibliography." + extensions[i]);
099
100                                        final Writer writer = new PrintWriter(file.openOutputStream());
101                                        writer.append(data);
102                                        writer.close();
103
104                                } catch (final IOException e) {
105                                        processingEnv.getMessager().printMessage(Kind.ERROR, "Error writing " + names[i] + " " + e);
106                                }
107                        }
108                }
109
110                return true;
111        }
112}