|
| 1 | +/* |
| 2 | + This file is part of the iText (R) project. |
| 3 | + Copyright (c) 1998-2023 Apryse Group NV |
| 4 | + Authors: Apryse Software. |
| 5 | +
|
| 6 | + This program is offered under a commercial and under the AGPL license. |
| 7 | + For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below. |
| 8 | +
|
| 9 | + AGPL licensing: |
| 10 | + This program is free software: you can redistribute it and/or modify |
| 11 | + it under the terms of the GNU Affero General Public License as published by |
| 12 | + the Free Software Foundation, either version 3 of the License, or |
| 13 | + (at your option) any later version. |
| 14 | +
|
| 15 | + This program is distributed in the hope that it will be useful, |
| 16 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | + GNU Affero General Public License for more details. |
| 19 | +
|
| 20 | + You should have received a copy of the GNU Affero General Public License |
| 21 | + along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 22 | + */ |
| 23 | +package com.itextpdf.styledxmlparser.css.resolve.shorthand.impl; |
| 24 | + |
| 25 | +import com.itextpdf.commons.utils.MessageFormatUtil; |
| 26 | +import com.itextpdf.styledxmlparser.css.CommonCssConstants; |
| 27 | +import com.itextpdf.styledxmlparser.css.CssDeclaration; |
| 28 | +import com.itextpdf.styledxmlparser.css.resolve.shorthand.IShorthandResolver; |
| 29 | +import com.itextpdf.styledxmlparser.css.util.CssTypesValidationUtils; |
| 30 | +import com.itextpdf.styledxmlparser.css.util.CssUtils; |
| 31 | +import com.itextpdf.styledxmlparser.css.validate.impl.datatype.CssEnumValidator; |
| 32 | +import com.itextpdf.styledxmlparser.logs.StyledXmlParserLogMessageConstant; |
| 33 | + |
| 34 | +import java.util.ArrayList; |
| 35 | +import java.util.Arrays; |
| 36 | +import java.util.Collections; |
| 37 | +import java.util.List; |
| 38 | +import org.slf4j.Logger; |
| 39 | +import org.slf4j.LoggerFactory; |
| 40 | + |
| 41 | +/** |
| 42 | + * Shorthand resolver for the column-rule property. |
| 43 | + * This property is a shorthand for the column-rule-width, column-rule-style, and column-rule-color properties. |
| 44 | + */ |
| 45 | +public class ColumnRuleShortHandResolver implements IShorthandResolver { |
| 46 | + |
| 47 | + private static final Logger LOGGER = LoggerFactory.getLogger(ColumnRuleShortHandResolver.class); |
| 48 | + private final CssEnumValidator borderStyleValidators = new CssEnumValidator(CommonCssConstants.BORDER_STYLE_VALUES); |
| 49 | + private final CssEnumValidator borderWithValidators = new CssEnumValidator(CommonCssConstants.BORDER_WIDTH_VALUES); |
| 50 | + |
| 51 | + /** |
| 52 | + * Creates a new {@link ColumnsShorthandResolver} instance. |
| 53 | + */ |
| 54 | + public ColumnRuleShortHandResolver() { |
| 55 | + //empty constructor |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Resolves a shorthand expression. |
| 60 | + * |
| 61 | + * @param shorthandExpression the shorthand expression |
| 62 | + * |
| 63 | + * @return a list of CSS declaration |
| 64 | + */ |
| 65 | + @Override |
| 66 | + public List<CssDeclaration> resolveShorthand(String shorthandExpression) { |
| 67 | + shorthandExpression = shorthandExpression.trim(); |
| 68 | + if (CssTypesValidationUtils.isInitialOrInheritOrUnset(shorthandExpression)) { |
| 69 | + return Arrays.asList( |
| 70 | + new CssDeclaration(CommonCssConstants.COLUMN_RULE_COLOR, shorthandExpression), |
| 71 | + new CssDeclaration(CommonCssConstants.COLUMN_RULE_WIDTH, shorthandExpression), |
| 72 | + new CssDeclaration(CommonCssConstants.COLUMN_RULE_STYLE, shorthandExpression) |
| 73 | + ); |
| 74 | + } |
| 75 | + if (CssTypesValidationUtils.containsInitialOrInheritOrUnset(shorthandExpression)) { |
| 76 | + return handleExpressionError(StyledXmlParserLogMessageConstant.INVALID_CSS_PROPERTY_DECLARATION, |
| 77 | + CommonCssConstants.COLUMN_RULE, shorthandExpression); |
| 78 | + } |
| 79 | + if (shorthandExpression.isEmpty()) { |
| 80 | + return handleExpressionError(StyledXmlParserLogMessageConstant.SHORTHAND_PROPERTY_CANNOT_BE_EMPTY, |
| 81 | + CommonCssConstants.COLUMN_RULE, shorthandExpression); |
| 82 | + } |
| 83 | + |
| 84 | + final int maxProperties = 3; |
| 85 | + List<String> properties = CssUtils.extractShorthandProperties(shorthandExpression).get(0); |
| 86 | + if (properties.size() > maxProperties) { |
| 87 | + return handleExpressionError(StyledXmlParserLogMessageConstant.INVALID_CSS_PROPERTY_DECLARATION, |
| 88 | + CommonCssConstants.COLUMN_RULE, shorthandExpression); |
| 89 | + } |
| 90 | + List<CssDeclaration> result = new ArrayList<>(maxProperties); |
| 91 | + for (String property : properties) { |
| 92 | + String cleanProperty = property.trim(); |
| 93 | + CssDeclaration declaration = processProperty(cleanProperty); |
| 94 | + if (declaration != null) { |
| 95 | + result.add(declaration); |
| 96 | + } |
| 97 | + if (declaration == null) { |
| 98 | + return handleExpressionError(StyledXmlParserLogMessageConstant.INVALID_CSS_PROPERTY_DECLARATION, |
| 99 | + CommonCssConstants.COLUMN_RULE_STYLE, shorthandExpression); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + return result; |
| 104 | + } |
| 105 | + |
| 106 | + private CssDeclaration processProperty(String value) { |
| 107 | + if (CssTypesValidationUtils.isMetricValue(value) || CssTypesValidationUtils.isRelativeValue(value) |
| 108 | + || borderWithValidators.isValid(value)) { |
| 109 | + return new CssDeclaration(CommonCssConstants.COLUMN_RULE_WIDTH, value); |
| 110 | + } |
| 111 | + if (CssTypesValidationUtils.isColorProperty(value)) { |
| 112 | + return new CssDeclaration(CommonCssConstants.COLUMN_RULE_COLOR, value); |
| 113 | + } |
| 114 | + if (borderStyleValidators.isValid(value)) { |
| 115 | + return new CssDeclaration(CommonCssConstants.COLUMN_RULE_STYLE, value); |
| 116 | + } |
| 117 | + return null; |
| 118 | + } |
| 119 | + |
| 120 | + private static List<CssDeclaration> handleExpressionError(String logMessage, String attribute, |
| 121 | + String shorthandExpression) { |
| 122 | + LOGGER.warn(MessageFormatUtil.format(logMessage, attribute, shorthandExpression)); |
| 123 | + return Collections.<CssDeclaration>emptyList(); |
| 124 | + } |
| 125 | +} |
0 commit comments