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

com.openhtmltopdf.layout.counter.RootCounterContext Maven / Gradle / Ivy

Go to download

Open HTML to PDF is a CSS 2.1 renderer written in Java. This artifact contains the core rendering and layout code.

There is a newer version: 1.1.4
Show newest version
package com.openhtmltopdf.layout.counter;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.openhtmltopdf.css.parser.CounterData;
import com.openhtmltopdf.css.style.CalculatedStyle;

public class RootCounterContext implements AbstractCounterContext {
    private final Map counterMap = new HashMap<>();

    public void resetCounterValue(CalculatedStyle style) {
        List resets = style.getCounterReset();
        if (resets != null) {
            resets.forEach(cd -> counterMap.put(cd.getName(), cd.getValue()));
        }
    }

    public void incrementCounterValue(CalculatedStyle style) {
        List incs = style.getCounterIncrement();
        if (incs != null) {
            for (CounterData cd : incs) {
                counterMap.merge(cd.getName(), cd.getValue(), (old, newVal) -> old + newVal);
            }
        }
    }

    @Override
    public int getCurrentCounterValue(String name) {
        Integer current = counterMap.get(name);

        if (current != null) {
            return current;
        } else {
            counterMap.put(name, 0);
            return 0;
        }
    }

    @Override
    public List getCurrentCounterValues(String name) {
        return Collections.singletonList(getCurrentCounterValue(name));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy