Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide Java implementation to list available icons #16

Merged
merged 3 commits into from
Sep 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions src/main/java/io/jenkins/plugins/ionicons/Ionicons.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package io.jenkins.plugins.ionicons;

import java.io.File;
import java.net.URL;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Map;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;

/**
* Utility to work with icons provided by the ionicons-api-plugin.
*
* @author strangelookingnerd
*
*/
public final class Ionicons {

private static final Logger LOGGER = Logger.getLogger(Ionicons.class.getName());

private static final String[] SVG_FILE_FORMAT = { "svg" };
private static final String SVG_FILE_ENDING = "." + SVG_FILE_FORMAT[0];
private static final String IMAGES_SYMBOLS_PATH = "images/symbols/";
private static final String IONICONS_API_PLUGIN = "ionicons-api-plugin";
private static final String ICON_CLASS_NAME_PATTERN = "symbol-%s plugin-ionicons-api";

private static final Ionicons INSTANCE = new Ionicons();

private final Map<String, String> availableIcons = new ConcurrentSkipListMap<>();

private Ionicons() {
try {
Enumeration<URL> urls = getClass().getClassLoader().getResources(IMAGES_SYMBOLS_PATH);

while (urls.hasMoreElements()) {
URL url = urls.nextElement();

if (StringUtils.contains(url.toExternalForm(), IONICONS_API_PLUGIN)) {
Collection<File> icons = FileUtils.listFiles(new File(url.toURI()), SVG_FILE_FORMAT, false);

for (File icon : icons) {
String iconName = StringUtils.removeEnd(icon.getName(), SVG_FILE_ENDING);
availableIcons.put(iconName, getIconClassName(iconName));
}
break;
}
}
} catch (Exception ex) {
LOGGER.log(Level.WARNING, "Unable to read available icons: Resource unavailable.", ex);
}
}

/**
* Takes an icon name and generates a icon class name from it.
*
* @param icon the icon name
* @return the icon class name
*/
public static String getIconClassName(String icon) {
return String.format(ICON_CLASS_NAME_PATTERN, icon);
}

/**
* Get all available icons provided by the ionicons-api-plugin.
*
* @return a sorted map of available icons with icon name as key and the icon
* class name as value.
*/
public static Map<String, String> getAvailableIcons() {
return INSTANCE.availableIcons;
}

}
53 changes: 53 additions & 0 deletions src/test/java/io/jenkins/plugins/ionicons/IoniconsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package io.jenkins.plugins.ionicons;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertIterableEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import java.io.File;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.junit.jupiter.api.Test;

/**
* Test Class for {@link Ionicons}.
*
* @author strangelookingnerd
*
*/
class IoniconsTest {

@Test
void testGetIconClassName() {
assertEquals("symbol-test plugin-ionicons-api", Ionicons.getIconClassName("test"));
}

@Test
void testGetAvailableIcons() {
Map<String, String> availableIcons = Ionicons.getAvailableIcons();

assertNotNull(availableIcons);
assertFalse(availableIcons.isEmpty());

Collection<File> icons = FileUtils.listFiles(new File("./src/main/resources/images/symbols/"),
new String[] { "svg" }, false);
assertEquals(icons.size(), availableIcons.size());

List<String> iconNames = icons.stream().map(icon -> StringUtils.removeEnd(icon.getName(), ".svg"))
.sorted(Comparator.naturalOrder()).collect(Collectors.toList());
assertIterableEquals(iconNames, availableIcons.keySet());

for (Entry<String, String> icon : availableIcons.entrySet()) {
assertEquals(icon.getValue(), Ionicons.getIconClassName(icon.getKey()));
}
}

}