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

org.sejda.impl.itext.component.ITextOutlineUtils Maven / Gradle / Ivy

/*
 * Created on 09/giu/2013
 * Copyright 2011 by Andrea Vacondio ([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.impl.itext.component;

import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;

/**
 * Utility class providing outline handling helper methods
 * 
 * @author Andrea Vacondio
 * 
 */
public final class ITextOutlineUtils {

    public static final String GOTO_VALUE = "GoTo";
    public static final String ACTION_KEY = "Action";
    public static final String PAGE_KEY = "Page";
    public static final String KIDS_KEY = "Kids";
    public static final String TITLE_KEY = "Title";
    private static final Pattern PAGE_NUMBER_MATCHING_PATTERN = Pattern.compile("(\\d+)(.*)");

    private ITextOutlineUtils() {
        // utility
    }

    static int getMaxBookmarkLevel(List> bookmarks, int parentLevel) {
        int maxLevel = parentLevel;
        if (bookmarks != null) {
            for (Map bookmark : bookmarks) {
                if (isGoToAction(bookmark)) {
                    @SuppressWarnings("unchecked")
                    int maxBookmarkBranchLevel = getMaxBookmarkLevel(
                            (List>) bookmark.get(KIDS_KEY), parentLevel + 1);
                    if (maxBookmarkBranchLevel > maxLevel) {
                        maxLevel = maxBookmarkBranchLevel;
                    }
                }
            }
        }
        return maxLevel;
    }

    static String nullSafeGetTitle(Map bookmark) {
        if (bookmark != null) {
            return ObjectUtils.toString(bookmark.get(TITLE_KEY));
        }
        return StringUtils.EMPTY;
    }

    static int getPageNumber(Map bookmark) {
        Object page = bookmark.get(PAGE_KEY);
        if (page == null) {
            return -1;
        }
        Matcher matcher = PAGE_NUMBER_MATCHING_PATTERN.matcher(page.toString());
        if (matcher.matches()) {
            return Integer.parseInt(matcher.group(1));
        }
        return -1;
    }

    static boolean isGoToAction(Map bookmark) {
        return bookmark != null && GOTO_VALUE.equals(bookmark.get(ACTION_KEY));
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy