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

org.sejda.conversion.PdfPageLabelAdapter Maven / Gradle / Ivy

/*
 * Created on Sep 15, 2011
 * Copyright 2010 by Eduard Weissmann ([email protected]).
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 */
package org.sejda.conversion;

import org.apache.commons.lang3.StringUtils;
import org.sejda.conversion.exception.ConversionException;
import org.sejda.model.exception.SejdaRuntimeException;
import org.sejda.model.pdf.label.PdfLabelNumberingStyle;
import org.sejda.model.pdf.label.PdfPageLabel;

/**
 * Adapter for a {@link PdfPageLabel} having a page number. Provides initialization from a String input, to be used in the cli interface
 * 
 * @author Eduard Weissmann
 * 
 */
public class PdfPageLabelAdapter {

    private static final int LABEL_PREFIX_INDEX = 3;
    private static final int MIN_TOKENS = 3;

    private Integer pageNumber;
    private PdfPageLabel pdfPageLabel;

    public PdfPageLabelAdapter(String input) {

        try {
            doParseInput(input);
        } catch (SejdaRuntimeException e) {
            throw new ConversionException("Could not parse input: '" + input + "'. " + e.getMessage(), e);
        }
    }

    private void doParseInput(String input) {
        String[] tokens = AdapterUtils.split(input);

        if (tokens.length < MIN_TOKENS) {
            throw new ConversionException(
                    "Format expected is: pageNumber(mandatory):numberingStyle(mandatory):logicalPageNumber(mandatory):labelPrefix(optional)'");
        }

        pageNumber = AdapterUtils.parseInt(tokens[0], "page number");
        PdfLabelNumberingStyle numberingStyle = EnumUtils.valueOf(PdfLabelNumberingStyle.class, tokens[1],
                "numbering style");
        int labelSuffixStartFromNumber = AdapterUtils.parseInt(tokens[2], "label suffix start number");
        String labelPrefix = tokens.length < LABEL_PREFIX_INDEX + 1 ? "" : tokens[LABEL_PREFIX_INDEX];

        if (StringUtils.isBlank(labelPrefix)) {
            pdfPageLabel = PdfPageLabel.newInstanceWithoutLabel(numberingStyle, labelSuffixStartFromNumber);
        } else {
            pdfPageLabel = PdfPageLabel.newInstanceWithLabel(labelPrefix, numberingStyle, labelSuffixStartFromNumber);
        }
    }

    public Integer getPageNumber() {
        return pageNumber;
    }

    public PdfPageLabel getPdfPageLabel() {
        return pdfPageLabel;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy