| 1 | /* |
| 2 | * Copyright 2005-2006 The RbUtils Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | * |
| 16 | */ |
| 17 | |
| 18 | // $Id: RbContentListerUtility.java,v 1.1 2007/07/10 13:29:19 ktcguru Exp $ |
| 19 | |
| 20 | package org.ktc.rbutils.rb.content; |
| 21 | |
| 22 | import java.io.PrintWriter; |
| 23 | import java.util.ArrayList; |
| 24 | import java.util.Collections; |
| 25 | import java.util.Enumeration; |
| 26 | import java.util.Iterator; |
| 27 | import java.util.List; |
| 28 | import java.util.Locale; |
| 29 | import java.util.ResourceBundle; |
| 30 | import org.ktc.rbutils.api.i18n.LocaleUtils; |
| 31 | import org.ktc.rbutils.rb.AbstractUtility; |
| 32 | import org.ktc.rbutils.rb.CommonProps; |
| 33 | import org.ktc.rbutils.rb.TaskType; |
| 34 | |
| 35 | /** |
| 36 | * Command line utility for ResourceBundle Content List. |
| 37 | * @since RbUtils 0.11 |
| 38 | * @version $Revision: 1.1 $ |
| 39 | * @author ktcguru |
| 40 | */ |
| 41 | public class RbContentListerUtility extends AbstractUtility { |
| 42 | /** The Writer to print outputs. */ |
| 43 | private PrintWriter pw; |
| 44 | /** The class name of the ResourceBundle to be retrieved. */ |
| 45 | private String rbClassName; |
| 46 | /** The locale of the ResourceBundle to be retrieved. */ |
| 47 | private Locale locale; |
| 48 | |
| 49 | /** |
| 50 | * Creates a new instance of ContentListerUtility. |
| 51 | * @param args the arguments to be passed to the utility. |
| 52 | * @param pw the Writer to print into. |
| 53 | */ |
| 54 | protected RbContentListerUtility(final String[] args, final PrintWriter pw) { |
| 55 | //taskName = "RbContentLister"; |
| 56 | internalArgs = args; |
| 57 | this.pw = pw; |
| 58 | //options = clone(GENERATOR_OPTS); |
| 59 | } |
| 60 | |
| 61 | |
| 62 | /** |
| 63 | * Launches the utility. Do not display anything. Call method should catch parse exceptions and |
| 64 | * display messages to user. |
| 65 | * @return the number of errors that occur during the generation (this does not count arguments |
| 66 | * parse error). |
| 67 | * @throws Exception the arguments to be passed to the utility. |
| 68 | */ |
| 69 | protected int process() throws Exception { |
| 70 | parseParameters(); |
| 71 | |
| 72 | final ResourceBundle bundle = ResourceBundle.getBundle(rbClassName, locale); |
| 73 | |
| 74 | // TODO duplicate with RbChecker, use an utility method |
| 75 | final List rbKeys = new ArrayList(); |
| 76 | for (final Enumeration rbKeysEnum = bundle.getKeys(); rbKeysEnum.hasMoreElements();) { |
| 77 | rbKeys.add(rbKeysEnum.nextElement()); |
| 78 | } |
| 79 | Collections.sort(rbKeys); |
| 80 | |
| 81 | pw.println("ResourceBundle " + rbClassName); |
| 82 | pw.println("Locale {" + bundle.getLocale() + "}"); |
| 83 | |
| 84 | for (final Iterator iter = rbKeys.iterator(); iter.hasNext();) { |
| 85 | final String key = (String) iter.next(); |
| 86 | final Object obj = bundle.getObject(key); |
| 87 | pw.println(" " + key + ": " + obj + " [" + obj.getClass().getName() + "]"); |
| 88 | } |
| 89 | |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | |
| 94 | /** |
| 95 | * {@inheritDoc} |
| 96 | */ |
| 97 | public void parseParameters() { |
| 98 | rbClassName = internalArgs[0]; |
| 99 | |
| 100 | if (internalArgs.length > 1) { |
| 101 | final String stringLocale = internalArgs[1]; |
| 102 | locale = LocaleUtils.toLocale(stringLocale); |
| 103 | } |
| 104 | else { |
| 105 | locale = LocaleUtils.getEmptyLocale(); |
| 106 | } |
| 107 | |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Display the usage of the utility. |
| 112 | */ |
| 113 | protected void internalUsage() { |
| 114 | final StringBuffer usage = new StringBuffer(); |
| 115 | usage.append("java " + getClass().getName()).append(" <ResourceBundleClassName> [locale]"); |
| 116 | pw.write(usage.toString()); |
| 117 | pw.flush(); |
| 118 | } |
| 119 | |
| 120 | // ============================================================================================ |
| 121 | // Main methods |
| 122 | // ============================================================================================ |
| 123 | |
| 124 | /** |
| 125 | * Lists the content of a ResourceBundle. See RbUtils documentation. |
| 126 | * <p> |
| 127 | * If an error occurs on initialization, prints usage. |
| 128 | * @param args the arguments to be passed to the utility. |
| 129 | */ |
| 130 | public static void main(final String[] args) { |
| 131 | final int exitValue = mainNoExit(args, new PrintWriter(System.out)); |
| 132 | System.exit(exitValue); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Launches the utility. Acts like the main method but does not exit. |
| 137 | * @param args the arguments to be passed to the utility. |
| 138 | * @param writer the Writer to be used to print messages. |
| 139 | * @return the number of error detected during generation. |
| 140 | */ |
| 141 | protected static int mainNoExit(final String[] args, final PrintWriter writer) { |
| 142 | // RFE add outputstream where log error output |
| 143 | // TODO use a new TaskType enum value |
| 144 | writer.println(CommonProps.getRunningTask(TaskType.RBCONTENTLISTER)); |
| 145 | int errorsNumber = -1; |
| 146 | |
| 147 | RbContentListerUtility util = null; |
| 148 | try { |
| 149 | util = new RbContentListerUtility(args, writer); |
| 150 | errorsNumber = util.process(); |
| 151 | writer.println("Errors: " + errorsNumber); |
| 152 | } |
| 153 | catch (final Exception exception) { |
| 154 | exception.printStackTrace(writer); |
| 155 | util.internalUsage(); |
| 156 | } |
| 157 | finally { |
| 158 | // TODO see how the defaultLogger flush to be sure it is done sometimes |
| 159 | writer.flush(); |
| 160 | } |
| 161 | |
| 162 | return errorsNumber; |
| 163 | } |
| 164 | |
| 165 | } |