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

javax0.jamal.snippet.Format Maven / Gradle / Ivy

The newest version!
package javax0.jamal.snippet;

import javax0.jamal.api.BadSyntax;
import javax0.jamal.api.Input;
import javax0.jamal.api.Macro;
import javax0.jamal.api.Processor;
import javax0.jamal.tools.Cast;
import javax0.jamal.tools.InputHandler;

import java.util.IllegalFormatException;

public class Format implements Macro {
    @Override
    public String evaluate(Input in, Processor processor) throws BadSyntax {
        final String[] parts = InputHandler.getParts(in);
        final String format = parts[0];
        try {
            final Object[] value = new Object[parts.length - 1];
            for (int i = 0; i < value.length; i++) {
                value[i] = Cast.cast(parts[i + 1]);
            }
            return String.format(format, value);
        } catch (IllegalFormatException e) {
            throw new BadSyntax("The format string '" + format + "'in macro '" + getId() + "' is incorrect.", e);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy