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

org.xbib.net.template.parse.LiteralParser Maven / Gradle / Ivy

package org.xbib.net.template.parse;

import org.xbib.net.matcher.CharMatcher;
import org.xbib.net.template.expression.TemplateLiteral;
import org.xbib.net.template.expression.URITemplateExpression;

import java.nio.CharBuffer;

/**
 * Literal parser.
 */
public
class LiteralParser implements TemplateParser {

    @Override
    public URITemplateExpression parse(CharBuffer buffer) {
        StringBuilder sb = new StringBuilder();
        char c;
        while (buffer.hasRemaining()) {
            c = buffer.charAt(0);
            if (!CharMatcher.LITERALS.matches(c)) {
                break;
            }
            sb.append(buffer.get());
            if (CharMatcher.PERCENT.matches(c)) {
                parsePercentEncoded(buffer, sb);
            }
        }
        return new TemplateLiteral(sb.toString());
    }

    private static void parsePercentEncoded(CharBuffer buffer, StringBuilder sb) {
        if (buffer.remaining() < 2) {
            throw new IllegalArgumentException("short read");
        }
        char first = buffer.get();
        if (!CharMatcher.HEXDIGIT.matches(first)) {
            throw new IllegalArgumentException("illegal percent encoding");
        }
        char second = buffer.get();
        if (!CharMatcher.HEXDIGIT.matches(second)) {
            throw new IllegalArgumentException("illegal percent encoding");
        }
        sb.append(first).append(second);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy