Skip to content

Commit

Permalink
WICKET-6993 - Use new encoding mechanism
Browse files Browse the repository at this point in the history
We can now use the encoding mechanism introduced in the previous commit
to simplify the encoding and decoding of the entire resource attributes.
  • Loading branch information
Peter Lamby committed Jun 30, 2022
1 parent 4911b27 commit 4f15c45
Showing 1 changed file with 17 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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();
}
}

/**
Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit 4f15c45

Please sign in to comment.