Skip to content

Commit

Permalink
Strip trailing slashes from resource directory names
Browse files Browse the repository at this point in the history
This strips trailing slashes from resource directory names.

Fix: #2
  • Loading branch information
io7m committed Apr 4, 2021
1 parent 4f5a9fb commit d831cd0
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 9 deletions.
10 changes: 8 additions & 2 deletions README-CHANGES.xml
Expand Up @@ -4,8 +4,14 @@
<c:release date="2021-01-25T00:00:00+00:00" is-open="false" ticket-system="com.github.io7m.jxtrand" version="1.0.0">
<c:changes/>
</c:release>
<c:release date="2021-01-25T10:22:59+00:00" is-open="true" ticket-system="com.github.io7m.jxtrand" version="1.1.0">
<c:changes/>
<c:release date="2021-04-04T09:58:45+00:00" is-open="true" ticket-system="com.github.io7m.jxtrand" version="1.1.0">
<c:changes>
<c:change date="2021-04-04T09:58:45+00:00" summary="Strip trailing slashes from resource directory names">
<c:tickets>
<c:ticket id="2"/>
</c:tickets>
</c:change>
</c:changes>
</c:release>
</c:releases>
<c:ticket-systems>
Expand Down
@@ -0,0 +1,36 @@
/*
* Copyright © 2020 Mark Raynsford <code@io7m.com> http://io7m.com
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
* IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

package com.io7m.jxtrand.examples;

import com.io7m.jxtrand.vanilla.JXTAbstractStrings;

import java.io.IOException;
import java.util.Locale;

/**
* An example where the resource has specializations.
*/

public final class ExampleStrings4 extends JXTAbstractStrings
{
public ExampleStrings4(
final Locale locale)
throws IOException
{
super(locale, ExampleStrings4.class, "/com/io7m/jxtrand/examples/", "Red");
}
}
Expand Up @@ -19,7 +19,7 @@
*/

@Export
@Version("1.0.0")
@Version("1.1.0")
package com.io7m.jxtrand.examples;

import org.osgi.annotation.bundle.Export;
Expand Down
Expand Up @@ -20,11 +20,11 @@
import com.io7m.jxtrand.examples.ExampleStrings1;
import com.io7m.jxtrand.examples.ExampleStrings2;
import com.io7m.jxtrand.examples.ExampleStrings3;
import com.io7m.jxtrand.examples.ExampleStrings4;
import org.junit.jupiter.api.Test;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.NoSuchFileException;
import java.util.Locale;
import java.util.Spliterator;
import java.util.Spliterators;
Expand Down Expand Up @@ -105,4 +105,22 @@ public void testExample3English()
assertEquals("red", strings.format("Red"));
assertNotNull(strings.resources());
}

@Test
public void testExample4German()
throws IOException
{
final var strings = new ExampleStrings4(Locale.GERMAN);
assertEquals("rot", strings.format("Red"));
assertNotNull(strings.resources());
}

@Test
public void testExample4English()
throws IOException
{
final var strings = new ExampleStrings4(Locale.ENGLISH);
assertEquals("red", strings.format("Red"));
assertNotNull(strings.resources());
}
}
Expand Up @@ -26,13 +26,16 @@
import java.util.Locale;
import java.util.Objects;
import java.util.ResourceBundle;
import java.util.regex.Pattern;

/**
* The default provider of XML resource bundles.
*/

public final class JXTXMLResourceBundles implements JXTXMLResourceBundlesType
{
private static final Pattern TRAILING_SLASHES = Pattern.compile("/+$");

/**
* Construct a provider.
*/
Expand Down Expand Up @@ -63,16 +66,20 @@ public ResourceBundle ofResource(
Objects.requireNonNull(base, "directory");
Objects.requireNonNull(name, "name");

final var baseN =
TRAILING_SLASHES.matcher(base)
.replaceAll("");

final var lang = locale.getLanguage();
final var country = locale.getCountry();
final var ex = locale.getVariant();

final var possibleNames =
List.of(
String.format("%s/%s_%s_%s_%s.xml", base, name, lang, country, ex),
String.format("%s/%s_%s_%s.xml", base, name, lang, country),
String.format("%s/%s_%s.xml", base, name, lang),
String.format("%s/%s.xml", base, name)
String.format("%s/%s_%s_%s_%s.xml", baseN, name, lang, country, ex),
String.format("%s/%s_%s_%s.xml", baseN, name, lang, country),
String.format("%s/%s_%s.xml", baseN, name, lang),
String.format("%s/%s.xml", baseN, name)
);

for (final var possibleName : possibleNames) {
Expand All @@ -95,7 +102,7 @@ public ResourceBundle ofResource(
message.append(clazz);
message.append(System.lineSeparator());
message.append(" Base: ");
message.append(base);
message.append(baseN);
message.append(System.lineSeparator());
message.append(" Name: ");
message.append(name);
Expand Down

0 comments on commit d831cd0

Please sign in to comment.