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

com.itextpdf.commons.actions.producer.CurrentDatePlaceholderPopulator Maven / Gradle / Ivy

There is a newer version: 9.0.0
Show newest version
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2023 Apryse Group NV
    Authors: Apryse Software.

    This program is offered under a commercial and under the AGPL license.
    For commercial licensing, contact us at https://itextpdf.com/sales.  For AGPL licensing, see below.

    AGPL licensing:
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is 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 Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see .
 */
package com.itextpdf.commons.actions.producer;

import com.itextpdf.commons.actions.confirmations.ConfirmedEventWrapper;
import com.itextpdf.commons.exceptions.CommonsExceptionMessageConstant;
import com.itextpdf.commons.utils.DateTimeUtil;
import com.itextpdf.commons.utils.MessageFormatUtil;

import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Class is used to populate currentDate placeholder. Placeholder should be configured
 * with parameter defining the format of date output. Within format strings, unquoted letters from
 * A to Z and from a to z are process as pattern
 * letters. Chain of equal pattern letters forms an appropriate component of
 * currentDate format. There following components are supported:
 *
 * 

*

    *
  • d is for the day of the month, from 1 through 31 *
  • dd is for the day of the month, from 01 through 31 *
  • M defines the month from 1 to 12 *
  • MM defines the month from 01 to 12 *
  • MMM defines the abbreviated name of the month *
  • MMMM defines the full name of month *
  • yy means the year from 00 to 99 *
  • yyyy means the year in for digits format *
  • s shows current second, from 0 through 59 *
  • ss shows current second, from 00 through 59 *
  • m is replaced with the current minute from 0 to 59 *
  • mm is replaced with the current minute from 00 to 59 *
  • H stands for the current hour, using a 24-hour clock from 0 to 23 *
  • HH stands for the current hour, using a 24-hour clock from 00 to 23 *
* *

* Text can be quoted using single quotes (') to avoid interpretation. All other characters are not * interpreted and just copied into the output string. String may contain escaped apostrophes * \' which processed as characters. Backslash is used for escaping so you need double * backslash to print it \\. All the rest backslashes (not followed by apostrophe or * one more backslash) are simply ignored. * *

* The result of the processing is current date representing in accordance with the provided format. */ class CurrentDatePlaceholderPopulator extends AbstractFormattedPlaceholderPopulator { private static final Set ALLOWED_PATTERNS = new HashSet<>(Arrays.asList( "dd", "MM", "MMM", "MMMM", "yy", "yyyy", "ss", "mm", "HH" )); public CurrentDatePlaceholderPopulator() { // Empty constructor. } /** * Builds a replacement for a placeholder currentDate in accordance with the * provided format. * * @param events is a list of event involved into document processing. It is not used during * the placeholder replacement * @param parameter defines output format in accordance with the description * * @return date of producer line creation in accordance with defined format * * @throws IllegalArgumentException if format of the date pattern is invalid */ @Override public String populate(List events, String parameter) { if (parameter == null) { throw new IllegalArgumentException(MessageFormatUtil.format( CommonsExceptionMessageConstant.INVALID_USAGE_FORMAT_REQUIRED, "currentDate") ); } final Date now = DateTimeUtil.getCurrentTimeDate(); return formatDate(now, parameter); } private String formatDate(Date date, String format) { final StringBuilder builder = new StringBuilder(); char[] formatArray = format.toCharArray(); for (int i = 0; i < formatArray.length; i++) { if (formatArray[i] == APOSTROPHE) { i = attachQuotedString(i, builder, formatArray); } else if (isLetter(formatArray[i])) { i = processDateComponent(i, date, builder, formatArray); } else { builder.append(formatArray[i]); } } return builder.toString(); } private int processDateComponent(int index, Date date, StringBuilder builder, char[] formatArray) { final StringBuilder peaceBuilder = new StringBuilder(); final char currentChar = formatArray[index]; peaceBuilder.append(currentChar); while (index + 1 < formatArray.length && currentChar == formatArray[index + 1]) { index++; peaceBuilder.append(formatArray[index]); } final String piece = peaceBuilder.toString(); if (ALLOWED_PATTERNS.contains(piece)) { builder.append(DateTimeUtil.format(date, piece)); } else { throw new IllegalArgumentException(MessageFormatUtil.format( CommonsExceptionMessageConstant.PATTERN_CONTAINS_UNEXPECTED_COMPONENT, piece) ); } return index; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy