/**
 * This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. 
 * If a copy of the MPL was not distributed with this file, You can obtain one at 
 * http://mozilla.org/MPL/2.0/.
 * 
 * This Source Code Form is also subject to the terms of the Health-Related Additional
 * Disclaimer of Warranty and Limitation of Liability available at
 * http://www.carewebframework.org/licensing/disclaimer.
 */
package org.carewebframework.ui.icons;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.io.Resource;

/**
 * A base implementation for an icon library.
 */
public class IconLibraryBase implements IIconLibrary, ApplicationContextAware {
    
    private static final Log log = LogFactory.getLog(IconLibraryBase.class);
    
    private final String id;
    
    private final String[] dimensions;
    
    private final String resourcePath;
    
    private ApplicationContext applicationContext;
    
    protected IconLibraryBase(String id, String resourcePath, String dimensions) {
        this.id = id;
        this.resourcePath = resourcePath;
        this.dimensions = StringUtils.split(dimensions, ',');
    }
    
    @Override
    public String getId() {
        return id;
    }
    
    @Override
    public String getIconUrl(String iconName, String dimensions) {
        return formatURL(iconName, dimensions, "~.");
    }
    
    @Override
    public List<String> getMatching(String iconName, String dimensions) {
        List<String> urls = new ArrayList<String>();
        
        try {
            for (Resource resource : applicationContext.getResources(formatURL(iconName, dimensions, "classpath:web"))) {
                String path = resource.getURL().getPath();
                int i = path.indexOf(resourcePath);
                urls.add("~." + path.substring(i));
            }
        } catch (IOException e) {
            log.error("Error enumerating icons.", e);
        }
        
        return urls;
    }
    
    @Override
    public String[] supportedDimensions() {
        return dimensions;
    }
    
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
    
    protected String formatURL(String iconName, String dims, String prefix) {
        if (dims == null) {
            if (dimensions != null && dimensions.length > 0) {
                dims = dimensions[0];
            } else {
                dims = "";
            }
        }
        
        return prefix + resourcePath + getId() + "/" + dims + "/" + iconName;
    }
    
}

@@ -84,17 +84,16 @@
  v. 2.0.
- 
 %0A * If a
@@ -166,17 +166,16 @@
 n one at
- 
 %0A * http
@@ -1217,24 +1217,66 @@
 ePath;%0A    %0A
+    private final String urlPattern;%0A    %0A
     private 
@@ -1394,32 +1394,212 @@
 g dimensions) %7B%0A
+        this(id, resourcePath, dimensions, %22%251$s/%252$s/%253$s/%254$s%22);%0A    %7D%0A    %0A    protected IconLibraryBase(String id, String resourcePath, String dimensions, String urlPattern) %7B%0A
         this.id 
@@ -1708,16 +1708,54 @@
 , ',');%0A
+        this.urlPattern = urlPattern;%0A
     %7D%0A  
@@ -3169,60 +3169,86 @@
 x + 
-resourcePath + getId() + %22/%22 + dims + %22/%22 + iconName
+String.format(urlPattern, resourcePath, id, dims, iconName).replace(%22//%22, %22/%22)
 ;%0A  

Make url pattern for icons more generalizable.