All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.undercouch.citeproc.csl.internal.format.FoFormat Maven / Gradle / Ivy

package de.undercouch.citeproc.csl.internal.format;

import de.undercouch.citeproc.csl.internal.RenderContext;
import de.undercouch.citeproc.csl.internal.SBibliography;
import de.undercouch.citeproc.csl.internal.TokenBuffer;
import de.undercouch.citeproc.output.Bibliography;
import de.undercouch.citeproc.output.SecondFieldAlign;
import org.apache.commons.text.StringEscapeUtils;

import static de.undercouch.citeproc.csl.internal.behavior.FormattingAttributes.FS_ITALIC;
import static de.undercouch.citeproc.csl.internal.behavior.FormattingAttributes.FW_BOLD;
import static de.undercouch.citeproc.csl.internal.behavior.FormattingAttributes.VA_SUP;

/**
 * The XSL-FO output format
 * @author Michel Kraemer
 */
public class FoFormat extends BaseFormat {
    private final String columnWidth;

    /**
     * Default constructor
     */
    public FoFormat() {
        this("2.5em");
    }

    /**
     * Constructs a new output format
     * @param columnWidth the width of the first column if
     * {@link SecondFieldAlign} is enabled
     */
    public FoFormat(String columnWidth) {
        this.columnWidth = columnWidth;
    }

    @Override
    public String getName() {
        return "fo";
    }

    @Override
    protected String doFormatCitation(TokenBuffer buffer, RenderContext ctx) {
        return format(buffer);
    }

    @Override
    protected String doFormatBibliographyEntry(TokenBuffer buffer,
            RenderContext ctx, int index) {
        String result;

        SecondFieldAlign sfa = ctx.getStyle().getBibliography().getSecondFieldAlign();
        if (sfa != SecondFieldAlign.FALSE && !buffer.getTokens().isEmpty()) {
            // find tokens that are part of the first field
            int i = 0;
            while (buffer.getTokens().get(i).isFirstField()) {
                ++i;
            }
            TokenBuffer firstBuffer = buffer.copy(0, i);
            TokenBuffer restBuffer = buffer.copy(i, buffer.getTokens().size());

            // render first field and rest independently
            result = "\n  \n    " +
                    "\n    " +
                    "\n    " +
                    "\n      " +
                    "\n        " +
                    "\n          " +
                    "" + format(firstBuffer) + "\n        " +
                    "\n        " +
                    "\n          " +
                    "" + format(restBuffer) + "\n        " +
                    "\n      " +
                    "\n    " +
                    "\n  " +
                    "\n";
        } else {
            result = format(buffer);
        }

        return "" + result + "\n";
    }

    @Override
    protected String doFormatLink(String text, String uri) {
        return "" + text + "";
    }

    @Override
    public Bibliography makeBibliography(String[] entries,
            SBibliography bibliographyElement) {
        SecondFieldAlign sfa = bibliographyElement.getSecondFieldAlign();
        return new Bibliography(entries, null, null, null, null, null, null,
                null, null, sfa);
    }

    @Override
    protected String escape(String str) {
        return StringEscapeUtils.escapeXml11(str);
    }

    @Override
    protected String openFontStyle(int fontStyle) {
        if (fontStyle == FS_ITALIC) {
            return "";
        } else {
            return "";
        }
    }

    @Override
    protected String closeFontStyle(int fontStyle) {
        return "";
    }

    @Override
    protected String openFontVariant(int fontVariant) {
        return "";
    }

    @Override
    protected String closeFontVariant(int fontVariant) {
        return "";
    }

    @Override
    protected String openFontWeight(int fontWeight) {
        if (fontWeight == FW_BOLD) {
            return "";
        } else {
            return "";
        }
    }

    @Override
    protected String closeFontWeight(int fontWeight) {
        return "";
    }

    @Override
    protected String openTextDecoration(int textDecoration) {
        return "";
    }

    @Override
    protected String closeTextDecoration(int textDecoration) {
        return "";
    }

    @Override
    protected String openVerticalAlign(int verticalAlign) {
        if (verticalAlign == VA_SUP) {
            return "";
        } else {
            return "";
        }
    }

    @Override
    protected String closeVerticalAlign(int verticalAlign) {
        return "";
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy