KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > cli > framework > CLIManFileFinder


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.cli.framework;
25
26 import java.util.Locale JavaDoc;
27 import java.io.File JavaDoc;
28 import java.io.InputStream JavaDoc;
29 import java.io.Reader JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.NoSuchElementException JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Collections JavaDoc;
34 import java.util.ArrayList JavaDoc;
35 import java.io.InputStreamReader JavaDoc;
36
37 /**
38  * A utility class which gets the I18N xml help file name for the
39  * given command. It searches (using Class.getSystemResource()) for
40  * the pages, and returns the first one found.
41  *
42  * For any given man page multiple instances of that page can
43  * exist. Man pages are come in sections (1 through 9, 1m through
44  * 9m), locales (language, country, variant), and by command
45  * version. These instances are ordered by section number (1 - 9, 1m
46  * - 9m), local
47  * specificity (most specific to least specific) and then by version
48  * (later versions before earlier versions).
49  *
50  * This is probably <em>not</em> what is wanted (I think what is
51  * wanted is versions before sections before language specificity),
52  * but is this way because of the way the Java Class.getResource()
53  * mechanism works.
54  *
55  * All methods will throw a NullPointerException if given null object
56  * arguments.
57  *
58  * All methods will throw an IllegalArgumentException if their
59  * arguments are non-null but are otherwise meaningless.
60  */

61
62 public class CLIManFileFinder
63 {
64       /**
65        * Get the man page for the given command, using the default locale
66        *
67        * @param commandName the name of the command
68        */

69   public Reader JavaDoc getCommandManFile(String JavaDoc commandName) {
70     return getCommandManFile(commandName, Locale.getDefault());
71   }
72
73       /**
74        * Get the man page for the given command for the given locale
75        * @param commandName the name of the command
76        * @param currentLocale the locale to be used to find the man page
77        */

78   public Reader JavaDoc getCommandManFile(String JavaDoc commandName, Locale JavaDoc currentLocale) {
79     return getCommandManFile(commandName, currentLocale, this.getClass().getClassLoader());
80   }
81
82
83       /**
84        * Get the man page for the given command for the given locale
85        * using the give classloader.
86        *
87        * @param commandName the name of the command
88        * @param locale the locale to be used to find the man page
89        * @param classLoader the class loader to be used to find the
90        * man page
91        */

92   public Reader JavaDoc getCommandManFile(String JavaDoc commandName, Locale JavaDoc locale, ClassLoader JavaDoc classLoader){
93     if (commandName.length() == 0) {
94       throw new IllegalArgumentException JavaDoc ("Command name cannot be empty");
95     }
96
97
98     InputStream JavaDoc s = null;
99     Iterator JavaDoc it = getPossibleLocations(commandName, locale);
100     while (s == null && it.hasNext()){
101       s = classLoader.getResourceAsStream((String JavaDoc) it.next());
102     }
103     
104     return (s == null ? (InputStreamReader JavaDoc) null : new InputStreamReader JavaDoc(s));
105   }
106
107   Iterator JavaDoc getPossibleLocations(final String JavaDoc commandName, final Locale JavaDoc locale){
108     return new Iterator JavaDoc(){
109         final String JavaDoc[] 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 JavaDoc next() throws NoSuchElementException JavaDoc{
116           if (!hasNext()){
117             throw new NoSuchElementException JavaDoc();
118           }
119           final String JavaDoc 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 JavaDoc();
130         }
131       };
132   }
133
134   String JavaDoc[] getLocaleLocations(Locale JavaDoc locale){
135     String JavaDoc language = locale.getLanguage();
136     String JavaDoc country = locale.getCountry();
137     String JavaDoc variant = locale.getVariant();
138     ArrayList JavaDoc l = new ArrayList JavaDoc();
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 JavaDoc[]) l.toArray(new String JavaDoc[0]);
152   }
153   
154   private static final String JavaDoc[] 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 JavaDoc HELPDIR = "help";
159   
160 }
161
Popular Tags