FileDocCategorySizeDatePackage
CLIManFileFinder.javaAPI DocGlassfish v2 API6214Wed Jul 25 01:03:20 BST 2007com.sun.enterprise.cli.framework

CLIManFileFinder

public class CLIManFileFinder extends Object
A utility class which gets the I18N xml help file name for the given command. It searches (using Class.getSystemResource()) for the pages, and returns the first one found. For any given man page multiple instances of that page can exist. Man pages are come in sections (1 through 9, 1m through 9m), locales (language, country, variant), and by command version. These instances are ordered by section number (1 - 9, 1m - 9m), local specificity (most specific to least specific) and then by version (later versions before earlier versions). This is probably not what is wanted (I think what is wanted is versions before sections before language specificity), but is this way because of the way the Java Class.getResource() mechanism works. All methods will throw a NullPointerException if given null object arguments. All methods will throw an IllegalArgumentException if their arguments are non-null but are otherwise meaningless.

Fields Summary
private static final String[]
sections
private static final String
HELPDIR
Constructors Summary
Methods Summary
public java.io.ReadergetCommandManFile(java.lang.String commandName)
Get the man page for the given command, using the default locale

param
commandName the name of the command

	return getCommandManFile(commandName, Locale.getDefault());
  
public java.io.ReadergetCommandManFile(java.lang.String commandName, java.util.Locale currentLocale)
Get the man page for the given command for the given locale

param
commandName the name of the command
param
currentLocale the locale to be used to find the man page

	return getCommandManFile(commandName, currentLocale, this.getClass().getClassLoader());
  
public java.io.ReadergetCommandManFile(java.lang.String commandName, java.util.Locale locale, java.lang.ClassLoader classLoader)
Get the man page for the given command for the given locale using the give classloader.

param
commandName the name of the command
param
locale the locale to be used to find the man page
param
classLoader the class loader to be used to find the man page

	if (commandName.length() == 0) {
	  throw new IllegalArgumentException ("Command name cannot be empty");
	}


	InputStream s = null;
	Iterator it = getPossibleLocations(commandName, locale);
	while (s == null && it.hasNext()){
	  s = classLoader.getResourceAsStream((String) it.next());
	}
	
	return (s == null ? (InputStreamReader) null : new InputStreamReader(s));
  
java.lang.String[]getLocaleLocations(java.util.Locale locale)

	String language = locale.getLanguage();
	String country = locale.getCountry();
	String variant = locale.getVariant();
	List<String> l = new ArrayList<String>();
	l.add("");

	if (language != null && language.length() > 0) {
	  l.add("/" + language);
	  if (country != null && country.length() > 0){
		l.add("/" + language +"/"+country);
		if (variant != null && variant.length() > 0){
		  l.add("/" + language +"/" + country +"/" + variant);
		}
	  }
	}
	Collections.reverse(l);
	return (String[]) l.toArray(new String[0]);
  
java.util.IteratorgetPossibleLocations(java.lang.String commandName, java.util.Locale locale)

	return new Iterator(){
		final String[] locales = getLocaleLocations(locale);
		int i = 0;
		int j = 0;
		public boolean hasNext() {
		  return i < locales.length && j < sections.length;
		}
		public Object next() throws NoSuchElementException{
		  if (!hasNext()){
			throw new NoSuchElementException();
		  }
		  final String result = HELPDIR + locales[i] + "/" + commandName+"." + sections[j++];
		  if (j == sections.length) {
			i++;
			if (i < locales.length ){
			  j = 0;
			}
		  }
		  return result;
		}
		public void remove() {
		  throw new UnsupportedOperationException();
		}
	  };