/*
@license.text@
 */
package com.hammurapi.common;

import java.util.ArrayList;
import java.util.List;


/**
 * Utility class which searches class hierarchy for a resource.
 * Resource shall be named after class name, profile name is separated from
 * class name by ! and locale separated from class name/profile by _.
 * @author Pavel Vlasov
 * @revision $Revision$
 */
public class ClassResourceLoader extends ResourceLoaderImpl {
	
	public static String[] inheritancePath(Class<?> clazz) {
		final List<String> accumulator = new ArrayList<String>();
		
		new ClassHierarchyVisitable(clazz).accept(new Visitor<Class<?>>() {
			
			@Override
			public boolean visit(Class<?> target) {
				String cPath = target.getName().replace('.', '/');
				if (accumulator.contains(cPath)) {
					return false;
				}
				accumulator.add(cPath);
				return true;
			}
		});
		
		return accumulator.toArray(new String[accumulator.size()]);
	}

	private Class<?> clazz;
	
	public ClassResourceLoader(ResourceSet resourceSet, Class<?> clazz) {		
		super(resourceSet, inheritancePath(clazz));
		this.clazz = clazz;
	}
	
	@Override
	public String toString() {
		return "Class resource loader for "+clazz.getName();
	}
		
}
