CommandPrintBetween.java

/* ========================================================================
 * PlantUML : a free UML diagram generator
 * ========================================================================
 *
 * (C) Copyright 2009-2024, Arnaud Roques
 *
 * Project Info:  https://plantuml.com
 * 
 * If you like this project or if you find it useful, you can support us at:
 * 
 * https://plantuml.com/patreon (only 1$ per month!)
 * https://plantuml.com/paypal
 * 
 * This file is part of PlantUML.
 *
 * PlantUML is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * PlantUML distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
 * License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
 * USA.
 *
 *
 * Original Author:  Arnaud Roques
 * 
 *
 */
package net.sourceforge.plantuml.gantt.command;

import java.time.LocalDate;

import com.plantuml.ubrex.Capture;
import com.plantuml.ubrex.UnicodeBracketedExpression;
import com.plantuml.ubrex.builder.UBrexConcat;
import com.plantuml.ubrex.builder.UBrexLeaf;
import com.plantuml.ubrex.builder.UBrexNamed;

import net.sourceforge.plantuml.annotation.Explain;
import net.sourceforge.plantuml.command.CommandExecutionResult;
import net.sourceforge.plantuml.command.ParserPass;
import net.sourceforge.plantuml.command.UBrexSingleLineCommand2;
import net.sourceforge.plantuml.gantt.GanttDiagram;
import net.sourceforge.plantuml.gantt.lang.ComplementDate;
import net.sourceforge.plantuml.regex.RegexResult;
import net.sourceforge.plantuml.utils.LineLocation;

public class CommandPrintBetween extends UBrexSingleLineCommand2<GanttDiagram> {

	private static final ComplementDate pattern = ComplementDate.any();

	public CommandPrintBetween() {
		super(getRegexConcat());
	}

	static UnicodeBracketedExpression getRegexConcat() {
		return UBrexConcat.build(
				// Print between 2020/02/14 and 2020/03/04
				new UBrexLeaf("print"), //
				UBrexLeaf.spaceOneOrMore(), //
				new UBrexLeaf("between"), //
				UBrexLeaf.spaceOneOrMore(), //
				new UBrexNamed("START", pattern.toUnicodeBracketedExpressionComplement()), //
				UBrexLeaf.spaceOneOrMore(), //
				new UBrexLeaf("and"), //
				UBrexLeaf.spaceOneOrMore(), //
				new UBrexNamed("END", pattern.toUnicodeBracketedExpressionComplement()), //
				UBrexLeaf.end());
	}

	@Override
	@Explain
	protected String explainArg(LineLocation location, RegexResult arg) {
		// The dates are resolved by ComplementDate.getMe against the gantt calendar state;
		// describe the raw date expressions only
		final String start = arg.get("START", 0);
		final String end = arg.get("END", 0);
		return "Restricting the printed interval to between " + start + " and " + end;
	}

	@Override
	protected CommandExecutionResult executeArg(GanttDiagram diagram, LineLocation location, RegexResult arg,
			ParserPass currentPass) {
		final Capture startCapture = arg.getUMatcher().extractByPrefix("START");
		final Capture endCapture = arg.getUMatcher().extractByPrefix("END");
		final LocalDate start = pattern.getMe(diagram, startCapture).get();
		final LocalDate end = pattern.getMe(diagram, endCapture).get();
		diagram.setPrintInterval(start, end);
		return CommandExecutionResult.ok();
	}

}