1 23 24 package com.sun.enterprise.cli.framework; 25 26 import java.util.Locale ; 27 import java.io.File ; 28 import java.io.InputStream ; 29 import java.io.Reader ; 30 import java.util.Iterator ; 31 import java.util.NoSuchElementException ; 32 import java.util.List ; 33 import java.util.Collections ; 34 import java.util.ArrayList ; 35 import java.io.InputStreamReader ; 36 37 61 62 public class CLIManFileFinder 63 { 64 69 public Reader getCommandManFile(String commandName) { 70 return getCommandManFile(commandName, Locale.getDefault()); 71 } 72 73 78 public Reader getCommandManFile(String commandName, Locale currentLocale) { 79 return getCommandManFile(commandName, currentLocale, this.getClass().getClassLoader()); 80 } 81 82 83 92 public Reader getCommandManFile(String commandName, Locale locale, ClassLoader classLoader){ 93 if (commandName.length() == 0) { 94 throw new IllegalArgumentException ("Command name cannot be empty"); 95 } 96 97 98 InputStream s = null; 99 Iterator it = getPossibleLocations(commandName, locale); 100 while (s == null && it.hasNext()){ 101 s = classLoader.getResourceAsStream((String ) it.next()); 102 } 103 104 return (s == null ? (InputStreamReader ) null : new InputStreamReader (s)); 105 } 106 107 Iterator getPossibleLocations(final String commandName, final Locale locale){ 108 return new Iterator (){ 109 final String [] locales = getLocaleLocations(locale); 110 int i = 0; 111 int j = 0; 112 public boolean hasNext() { 113 return i < locales.length && j < sections.length; 114 } 115 public Object next() throws NoSuchElementException { 116 if (!hasNext()){ 117 throw new NoSuchElementException (); 118 } 119 final String result = HELPDIR + locales[i] + "/" + commandName+"." + sections[j++]; 120 if (j == sections.length) { 121 i++; 122 if (i < locales.length ){ 123 j = 0; 124 } 125 } 126 return result; 127 } 128 public void remove() { 129 throw new UnsupportedOperationException (); 130 } 131 }; 132 } 133 134 String [] getLocaleLocations(Locale locale){ 135 String language = locale.getLanguage(); 136 String country = locale.getCountry(); 137 String variant = locale.getVariant(); 138 ArrayList l = new ArrayList (); 139 l.add(""); 140 141 if (language != null && language.length() > 0) { 142 l.add("/" + language); 143 if (country != null && country.length() > 0){ 144 l.add("/" + language +"/"+country); 145 if (variant != null && variant.length() > 0){ 146 l.add("/" + language +"/" + country +"/" + variant); 147 } 148 } 149 } 150 Collections.reverse(l); 151 return (String []) l.toArray(new String [0]); 152 } 153 154 private static final String [] sections = { 155 "1", "1m", "2", "2m", "3", "3m", "4", "4m", "5", "5m", "6", "6m", "7", "7m", "8", "8m", "9", "9m" 156 }; 157 158 private static final String HELPDIR = "help"; 159 160 } 161 | Popular Tags |