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

io.lindstrom.m3u8.parser.AbstractLineParser Maven / Gradle / Ivy

There is a newer version: 0.28
Show newest version
package io.lindstrom.m3u8.parser;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

abstract class AbstractLineParser {
    static final Pattern ATTRIBUTE_LIST_PATTERN = Pattern.compile("([A-Z0-9\\-]+)=(?:(?:\"([^\"]+)\")|([^,]+))");
    private final String tag;

    AbstractLineParser(String tag) {
        this.tag = tag;
    }

    void write(T value, StringBuilder stringBuilder) {
        stringBuilder.append(tag).append(':');
        stringBuilder.append(writeAttributes(value));
        stringBuilder.append('\n');
    }

    T parse(String attributes) throws PlaylistParserException {
        return parse(attributes, Collections.emptyMap());
    }

    T parse(String attributes, Map moreAttributes) throws PlaylistParserException {
        if (attributes.isEmpty()) {
            return parseAttributes(Collections.emptyMap());
        } else {
            Map attributeMap = parseAttributes(attributes);
            attributeMap.putAll(moreAttributes);
            return parseAttributes(attributeMap);
        }
    }

    abstract T parseAttributes(Map attributes) throws PlaylistParserException;

    abstract String writeAttributes(T value);

    private Map parseAttributes(String attributeList) {
        Matcher matcher = ATTRIBUTE_LIST_PATTERN.matcher(attributeList);
        Map attributes = new HashMap<>();
        while (matcher.find()) {
            attributes.put(matcher.group(1), matcher.group(2) != null ? matcher.group(2) : matcher.group(3));
        }
        return attributes;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy