EMMA Coverage Report (generated Fri Jun 19 09:16:10 CEST 2009)
[all classes][org.ktc.rbutils.rb.content]

COVERAGE SUMMARY FOR SOURCE FILE [RbContentListerUtility.java]

nameclass, %method, %block, %line, %
RbContentListerUtility.java0%   (0/1)0%   (0/6)0%   (0/210)0%   (0/45)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class RbContentListerUtility0%   (0/1)0%   (0/6)0%   (0/210)0%   (0/45)
RbContentListerUtility (String [], PrintWriter): void 0%   (0/1)0%   (0/9)0%   (0/4)
internalUsage (): void 0%   (0/1)0%   (0/28)0%   (0/5)
main (String []): void 0%   (0/1)0%   (0/10)0%   (0/3)
mainNoExit (String [], PrintWriter): int 0%   (0/1)0%   (0/46)0%   (0/12)
parseParameters (): void 0%   (0/1)0%   (0/25)0%   (0/7)
process (): int 0%   (0/1)0%   (0/92)0%   (0/14)

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 
20package org.ktc.rbutils.rb.content;
21 
22import java.io.PrintWriter;
23import java.util.ArrayList;
24import java.util.Collections;
25import java.util.Enumeration;
26import java.util.Iterator;
27import java.util.List;
28import java.util.Locale;
29import java.util.ResourceBundle;
30import org.ktc.rbutils.api.i18n.LocaleUtils;
31import org.ktc.rbutils.rb.AbstractUtility;
32import org.ktc.rbutils.rb.CommonProps;
33import 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 */
41public 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}

[all classes][org.ktc.rbutils.rb.content]
EMMA 2.0.5312 (C) Vladimir Roubtsov