Skip to content

Commit 6c0bbd5

Browse files
committed
Divide the CssUtils class into several classes
Divide into CssDimensionParsingUtils (new class), CssTypesValidationUtils (new class) and CssBackgroundUtils DEVSIX-4569
1 parent 2a2bcba commit 6c0bbd5

File tree

53 files changed

+1387
-537
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+1387
-537
lines changed

styled-xml-parser/src/main/java/com/itextpdf/styledxmlparser/css/media/MediaExpression.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ This file is part of the iText (R) project.
4242
*/
4343
package com.itextpdf.styledxmlparser.css.media;
4444

45+
import com.itextpdf.styledxmlparser.css.util.CssTypesValidationUtils;
46+
import com.itextpdf.styledxmlparser.css.util.CssDimensionParsingUtils;
4547
import com.itextpdf.styledxmlparser.css.util.CssUtils;
48+
4649
import java.util.Objects;
4750

4851
/**
@@ -108,7 +111,7 @@ public class MediaExpression {
108111
public boolean matches(MediaDeviceDescription deviceDescription) {
109112
switch (feature) {
110113
case MediaFeature.COLOR: {
111-
Integer val = CssUtils.parseInteger(value);
114+
Integer val = CssDimensionParsingUtils.parseInteger(value);
112115
if (minPrefix) {
113116
return val != null && deviceDescription.getBitsPerComponent() >= val;
114117
} else if (maxPrefix) {
@@ -118,7 +121,7 @@ public boolean matches(MediaDeviceDescription deviceDescription) {
118121
}
119122
}
120123
case MediaFeature.COLOR_INDEX: {
121-
Integer val = CssUtils.parseInteger(value);
124+
Integer val = CssDimensionParsingUtils.parseInteger(value);
122125
if (minPrefix) {
123126
return val != null && deviceDescription.getColorIndex() >= val;
124127
} else if (maxPrefix) {
@@ -128,7 +131,7 @@ public boolean matches(MediaDeviceDescription deviceDescription) {
128131
}
129132
}
130133
case MediaFeature.ASPECT_RATIO: {
131-
int[] aspectRatio = CssUtils.parseAspectRatio(value);
134+
int[] aspectRatio = CssDimensionParsingUtils.parseAspectRatio(value);
132135
if (minPrefix) {
133136
return aspectRatio != null && aspectRatio[0] * deviceDescription.getHeight() >= aspectRatio[1] * deviceDescription.getWidth();
134137
} else if (maxPrefix) {
@@ -138,7 +141,7 @@ public boolean matches(MediaDeviceDescription deviceDescription) {
138141
}
139142
}
140143
case MediaFeature.GRID: {
141-
Integer val = CssUtils.parseInteger(value);
144+
Integer val = CssDimensionParsingUtils.parseInteger(value);
142145
return val != null && val == 0 && !deviceDescription.isGrid() || deviceDescription.isGrid();
143146
}
144147
case MediaFeature.SCAN: {
@@ -148,7 +151,7 @@ public boolean matches(MediaDeviceDescription deviceDescription) {
148151
return Objects.equals(value, deviceDescription.getOrientation());
149152
}
150153
case MediaFeature.MONOCHROME: {
151-
Integer val = CssUtils.parseInteger(value);
154+
Integer val = CssDimensionParsingUtils.parseInteger(value);
152155
if (minPrefix) {
153156
return val != null && deviceDescription.getMonochrome() >= val;
154157
} else if (maxPrefix) {
@@ -178,7 +181,7 @@ public boolean matches(MediaDeviceDescription deviceDescription) {
178181
}
179182
}
180183
case MediaFeature.RESOLUTION: {
181-
float val = CssUtils.parseResolution(value);
184+
float val = CssDimensionParsingUtils.parseResolution(value);
182185
if (minPrefix) {
183186
return deviceDescription.getResolution() >= val;
184187
} else if (maxPrefix) {
@@ -199,11 +202,11 @@ public boolean matches(MediaDeviceDescription deviceDescription) {
199202
* @return the absolute length as a {@code float} value
200203
*/
201204
private static float parseAbsoluteLength(String value) {
202-
if (CssUtils.isRelativeValue(value)) {
205+
if (CssTypesValidationUtils.isRelativeValue(value)) {
203206
// TODO here should be used default font size of the browser, it probably should be fetched from the more generic place than private class constant
204-
return CssUtils.parseRelativeValue(value, DEFAULT_FONT_SIZE);
207+
return CssDimensionParsingUtils.parseRelativeValue(value, DEFAULT_FONT_SIZE);
205208
} else {
206-
return CssUtils.parseAbsoluteLength(value);
209+
return CssDimensionParsingUtils.parseAbsoluteLength(value);
207210
}
208211
}
209212
}

styled-xml-parser/src/main/java/com/itextpdf/styledxmlparser/css/parse/CssRuleSetParser.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ This file is part of the iText (R) project.
4848
import com.itextpdf.styledxmlparser.css.CssRuleSet;
4949
import com.itextpdf.styledxmlparser.css.selector.CssSelector;
5050
import com.itextpdf.styledxmlparser.css.util.CssUtils;
51+
5152
import org.slf4j.Logger;
5253
import org.slf4j.LoggerFactory;
5354

styled-xml-parser/src/main/java/com/itextpdf/styledxmlparser/css/parse/syntax/CssParserStateController.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@ This file is part of the iText (R) project.
5353
import com.itextpdf.styledxmlparser.css.CssStyleSheet;
5454
import com.itextpdf.styledxmlparser.css.parse.CssDeclarationValueTokenizer;
5555
import com.itextpdf.styledxmlparser.css.parse.CssRuleSetParser;
56-
import com.itextpdf.styledxmlparser.css.util.CssUtils;
56+
import com.itextpdf.styledxmlparser.css.util.CssTypesValidationUtils;
5757
import com.itextpdf.styledxmlparser.resolver.resource.UriResolver;
58+
5859
import org.slf4j.LoggerFactory;
5960

6061
import java.net.MalformedURLException;
@@ -433,7 +434,7 @@ private void normalizeDeclarationURIs(List<CssDeclaration> declarations) {
433434
if (token.getType() == CssDeclarationValueTokenizer.TokenType.FUNCTION && token.getValue().startsWith("url(")) {
434435
String url = token.getValue().trim();
435436
url = url.substring(4, url.length() - 1).trim();
436-
if (CssUtils.isBase64Data(url)) {
437+
if (CssTypesValidationUtils.isBase64Data(url)) {
437438
strToAppend = token.getValue().trim();
438439
} else {
439440
if (url.startsWith("'") && url.endsWith("'") || url.startsWith("\"") && url.endsWith("\"")) {

styled-xml-parser/src/main/java/com/itextpdf/styledxmlparser/css/resolve/shorthand/impl/AbstractBorderShorthandResolver.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ This file is part of the iText (R) project.
4747
import com.itextpdf.styledxmlparser.css.CommonCssConstants;
4848
import com.itextpdf.styledxmlparser.css.CssDeclaration;
4949
import com.itextpdf.styledxmlparser.css.resolve.shorthand.IShorthandResolver;
50-
import com.itextpdf.styledxmlparser.css.util.CssUtils;
50+
import com.itextpdf.styledxmlparser.css.util.CssTypesValidationUtils;
51+
5152
import org.slf4j.Logger;
5253
import org.slf4j.LoggerFactory;
5354

@@ -105,12 +106,12 @@ public List<CssDeclaration> resolveShorthand(String shorthandExpression) {
105106
logger.warn(MessageFormatUtil.format(LogMessageConstant.INVALID_CSS_PROPERTY_DECLARATION, shorthandExpression));
106107
return Collections.<CssDeclaration>emptyList();
107108
}
108-
if (CommonCssConstants.BORDER_WIDTH_VALUES.contains(value) || CssUtils.isNumericValue(value)
109-
|| CssUtils.isMetricValue(value) || CssUtils.isRelativeValue(value)) {
109+
if (CommonCssConstants.BORDER_WIDTH_VALUES.contains(value) || CssTypesValidationUtils.isNumericValue(value)
110+
|| CssTypesValidationUtils.isMetricValue(value) || CssTypesValidationUtils.isRelativeValue(value)) {
110111
borderWidthValue = value;
111112
} else if (CommonCssConstants.BORDER_STYLE_VALUES.contains(value) || value.equals(CommonCssConstants.AUTO)) { // AUTO property value is needed for outline property only
112113
borderStyleValue = value;
113-
} else if (CssUtils.isColorProperty(value)) {
114+
} else if (CssTypesValidationUtils.isColorProperty(value)) {
114115
borderColorValue = value;
115116
}
116117
}

styled-xml-parser/src/main/java/com/itextpdf/styledxmlparser/css/resolve/shorthand/impl/BackgroundPositionShorthandResolver.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ This file is part of the iText (R) project.
2727
import com.itextpdf.styledxmlparser.css.CommonCssConstants;
2828
import com.itextpdf.styledxmlparser.css.CssDeclaration;
2929
import com.itextpdf.styledxmlparser.css.resolve.shorthand.IShorthandResolver;
30+
import com.itextpdf.styledxmlparser.css.util.CssTypesValidationUtils;
3031
import com.itextpdf.styledxmlparser.css.util.CssUtils;
3132
import com.itextpdf.styledxmlparser.css.validate.CssDeclarationValidationMaster;
33+
3234
import org.slf4j.Logger;
3335
import org.slf4j.LoggerFactory;
3436

@@ -51,7 +53,7 @@ public class BackgroundPositionShorthandResolver implements IShorthandResolver {
5153
*/
5254
@Override
5355
public List<CssDeclaration> resolveShorthand(String shorthandExpression) {
54-
if (CssUtils.isInitialOrInheritOrUnset(shorthandExpression)) {
56+
if (CssTypesValidationUtils.isInitialOrInheritOrUnset(shorthandExpression)) {
5557
return Arrays.asList(
5658
new CssDeclaration(CommonCssConstants.BACKGROUND_POSITION_X, shorthandExpression),
5759
new CssDeclaration(CommonCssConstants.BACKGROUND_POSITION_Y, shorthandExpression)

styled-xml-parser/src/main/java/com/itextpdf/styledxmlparser/css/resolve/shorthand/impl/BackgroundShorthandResolver.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,10 @@ This file is part of the iText (R) project.
5151
import com.itextpdf.styledxmlparser.css.resolve.shorthand.ShorthandResolverFactory;
5252
import com.itextpdf.styledxmlparser.css.util.CssBackgroundUtils;
5353
import com.itextpdf.styledxmlparser.css.util.CssBackgroundUtils.BackgroundPropertyType;
54+
import com.itextpdf.styledxmlparser.css.util.CssTypesValidationUtils;
5455
import com.itextpdf.styledxmlparser.css.util.CssUtils;
55-
5656
import com.itextpdf.styledxmlparser.css.validate.CssDeclarationValidationMaster;
57+
5758
import org.slf4j.Logger;
5859
import org.slf4j.LoggerFactory;
5960

@@ -81,7 +82,7 @@ public class BackgroundShorthandResolver implements IShorthandResolver {
8182
*/
8283
@Override
8384
public List<CssDeclaration> resolveShorthand(final String shorthandExpression) {
84-
if (CssUtils.isInitialOrInheritOrUnset(shorthandExpression)) {
85+
if (CssTypesValidationUtils.isInitialOrInheritOrUnset(shorthandExpression)) {
8586
return Arrays.asList(
8687
new CssDeclaration(CommonCssConstants.BACKGROUND_COLOR, shorthandExpression),
8788
new CssDeclaration(CommonCssConstants.BACKGROUND_IMAGE, shorthandExpression),

styled-xml-parser/src/main/java/com/itextpdf/styledxmlparser/css/resolve/shorthand/impl/FontShorthandResolver.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ This file is part of the iText (R) project.
4646
import com.itextpdf.styledxmlparser.css.CommonCssConstants;
4747
import com.itextpdf.styledxmlparser.css.CssDeclaration;
4848
import com.itextpdf.styledxmlparser.css.resolve.shorthand.IShorthandResolver;
49-
import com.itextpdf.styledxmlparser.css.util.CssUtils;
49+
import com.itextpdf.styledxmlparser.css.util.CssTypesValidationUtils;
5050

51-
import java.util.Collections;
5251
import org.slf4j.Logger;
5352
import org.slf4j.LoggerFactory;
5453

54+
import java.util.Collections;
5555
import java.util.ArrayList;
5656
import java.util.Arrays;
5757
import java.util.HashSet;
@@ -137,8 +137,8 @@ public List<CssDeclaration> resolveShorthand(String shorthandExpression) {
137137
} else if (slashSymbolIndex > 0) {
138138
fontSizeValue = value.substring(0, slashSymbolIndex);
139139
lineHeightValue = value.substring(slashSymbolIndex + 1, value.length());
140-
} else if (FONT_SIZE_VALUES.contains(value) || CssUtils.isMetricValue(value)
141-
|| CssUtils.isNumericValue(value) || CssUtils.isRelativeValue(value)) {
140+
} else if (FONT_SIZE_VALUES.contains(value) || CssTypesValidationUtils.isMetricValue(value)
141+
|| CssTypesValidationUtils.isNumericValue(value) || CssTypesValidationUtils.isRelativeValue(value)) {
142142
fontSizeValue = value;
143143
} else {
144144
fontFamilyValue = value;

styled-xml-parser/src/main/java/com/itextpdf/styledxmlparser/css/util/CssBackgroundUtils.java

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,12 @@ This file is part of the iText (R) project.
2222
*/
2323
package com.itextpdf.styledxmlparser.css.util;
2424

25+
import com.itextpdf.layout.property.BackgroundRepeat.BackgroundRepeatValue;
2526
import com.itextpdf.styledxmlparser.css.CommonCssConstants;
2627

28+
/**
29+
* Utilities class for CSS background parsing.
30+
*/
2731
public final class CssBackgroundUtils {
2832

2933
/**
@@ -32,6 +36,26 @@ public final class CssBackgroundUtils {
3236
private CssBackgroundUtils() {
3337
}
3438

39+
/**
40+
* Parses the background repeat string value.
41+
*
42+
* @param value the string which stores the background repeat value
43+
* @return the background repeat as a {@link BackgroundRepeatValue} instance
44+
*/
45+
public static BackgroundRepeatValue parseBackgroundRepeat(String value) {
46+
switch (value) {
47+
case CommonCssConstants.NO_REPEAT:
48+
return BackgroundRepeatValue.NO_REPEAT;
49+
case CommonCssConstants.ROUND:
50+
return BackgroundRepeatValue.ROUND;
51+
case CommonCssConstants.SPACE:
52+
return BackgroundRepeatValue.SPACE;
53+
case CommonCssConstants.REPEAT:
54+
default:
55+
return BackgroundRepeatValue.REPEAT;
56+
}
57+
}
58+
3559
/**
3660
* Gets background property name corresponding to its type.
3761
*
@@ -97,14 +121,14 @@ public static BackgroundPropertyType resolveBackgroundPropertyType(final String
97121
if (CommonCssConstants.CENTER.equals(value)) {
98122
return BackgroundPropertyType.BACKGROUND_POSITION;
99123
}
100-
if (((Integer) 0).equals(CssUtils.parseInteger(value))
101-
|| CssUtils.isMetricValue(value) || CssUtils.isRelativeValue(value)) {
124+
if (((Integer) 0).equals(CssDimensionParsingUtils.parseInteger(value))
125+
|| CssTypesValidationUtils.isMetricValue(value) || CssTypesValidationUtils.isRelativeValue(value)) {
102126
return BackgroundPropertyType.BACKGROUND_POSITION_OR_SIZE;
103127
}
104128
if (CommonCssConstants.BACKGROUND_SIZE_VALUES.contains(value)) {
105129
return BackgroundPropertyType.BACKGROUND_SIZE;
106130
}
107-
if (CssUtils.isColorProperty(value)) {
131+
if (CssTypesValidationUtils.isColorProperty(value)) {
108132
return BackgroundPropertyType.BACKGROUND_COLOR;
109133
}
110134
if (CommonCssConstants.BACKGROUND_ORIGIN_OR_CLIP_VALUES.contains(value)) {

0 commit comments

Comments
 (0)