From 4f15c45ac19ec91b6a5f408376701ea142d59125 Mon Sep 17 00:00:00 2001 From: Peter Lamby Date: Thu, 30 Jun 2022 14:55:40 +0200 Subject: [PATCH] WICKET-6993 - Use new encoding mechanism We can now use the encoding mechanism introduced in the previous commit to simplify the encoding and decoding of the entire resource attributes. --- .../apache/wicket/resource/ResourceUtil.java | 61 ++++++------------- 1 file changed, 17 insertions(+), 44 deletions(-) diff --git a/wicket-core/src/main/java/org/apache/wicket/resource/ResourceUtil.java b/wicket-core/src/main/java/org/apache/wicket/resource/ResourceUtil.java index 7bac677d41..94a279cee0 100644 --- a/wicket-core/src/main/java/org/apache/wicket/resource/ResourceUtil.java +++ b/wicket-core/src/main/java/org/apache/wicket/resource/ResourceUtil.java @@ -57,24 +57,12 @@ public class ResourceUtil */ public static ResourceReference.UrlAttributes decodeResourceReferenceAttributes(String encodedAttributes) { - Locale locale = null; - String style = null; - String variation = null; + String[] decodeAttributes = decodeStringParts(encodedAttributes); + + Locale locale = decodeAttributes.length > 0 ? parseLocale(decodeAttributes[0]) : null; + String style = decodeAttributes.length > 1 ? decodeAttributes[1] : null; + String variation = decodeAttributes.length > 2 ? decodeAttributes[2] : null; - if (Strings.isEmpty(encodedAttributes) == false) - { - String split[] = Strings.split(encodedAttributes, '-'); - locale = parseLocale(split[0]); - if (split.length == 2) - { - style = Strings.defaultIfEmpty(unescapeAttributesSeparator(split[1]), null); - } - else if (split.length == 3) - { - style = Strings.defaultIfEmpty(unescapeAttributesSeparator(split[1]), null); - variation = Strings.defaultIfEmpty(unescapeAttributesSeparator(split[2]), null); - } - } return new ResourceReference.UrlAttributes(locale, style, variation); } @@ -114,37 +102,18 @@ public static ResourceReference.UrlAttributes decodeResourceReferenceAttributes( public static String encodeResourceReferenceAttributes(ResourceReference.UrlAttributes attributes) { if (attributes == null || - (attributes.getLocale() == null && attributes.getStyle() == null && attributes.getVariation() == null)) - { - return null; - } - else - { - StringBuilder res = new StringBuilder(32); - if (attributes.getLocale() != null) - { - res.append(attributes.getLocale()); - } - boolean styleEmpty = Strings.isEmpty(attributes.getStyle()); - if (!styleEmpty) + (attributes.getLocale() == null && attributes.getStyle() == null && attributes.getVariation() == null)) { - res.append('-'); - res.append(escapeAttributesSeparator(attributes.getStyle())); + return null; } - if (!Strings.isEmpty(attributes.getVariation())) + else { - if (styleEmpty) - { - res.append("--"); - } - else - { - res.append('-'); - } - res.append(escapeAttributesSeparator(attributes.getVariation())); + StringBuilder res = new StringBuilder(32); + res.append(encodeStringPart(attributes.getLocale() == null ? null : attributes.getLocale().toString())); + res.append(encodeStringPart(attributes.getStyle())); + res.append(encodeStringPart(attributes.getVariation())); + return res.toString(); } - return res.toString(); - } } /** @@ -332,6 +301,10 @@ static String[] decodeStringParts(String encoded) if (c >= '0' && c <= '9') { lengthString.append(c); } else { + if (isAtStartOfPart || c != '~') { // Not a (valid) resource attribute string + return new String[0]; + } + int length = Integer.parseInt(lengthString.toString()); lengthString.setLength(0); // reset the length buffer result.add(String.valueOf(chars, i + 1, length));