KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > tax > util > AbstractUtil


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.xml.tax.util;
20
21 import org.openide.ErrorManager;
22 import org.openide.util.Lookup;
23 import org.openide.util.NbBundle;
24
25 /**
26  * This provides package scope utilities for debugging and code
27  * internationalization. It is designed to have a subclass,
28  * <code>ErrorManager</code> instance and bundle in each client's package.
29  * <p>
30  * It ensures localized strings will be loaded from <code>Bundle.properties</code>
31  * (branded) in same package as subclass belongs.
32  * <p>
33  * Debugging methods use {@link org.openide.ErrorManager} to log messages.
34  * <code>ErrorManager</code> instance also belongs to sub-class's package.
35  *
36  * @author Libor Kramolis
37  */

38 public abstract class AbstractUtil {
39     /** Instance package ErrorManager. */
40     private ErrorManager packageErrorManager;
41     /** Default debug severity used with ErrorManager. */
42     private static final int DEBUG_SEVERITY = ErrorManager.INFORMATIONAL;
43     
44     
45     //
46
// String localizing purposes
47
//
48

49
50     /**
51      * Get localized string from package bundle.
52      * @param key Key identifing localized value.
53      * @return localized value.
54      */

55     public final String JavaDoc getString (String JavaDoc key) {
56         if (key == null) throw new NullPointerException JavaDoc();
57     return NbBundle.getMessage (this.getClass(), key);
58     }
59     
60     /**
61      * Get localized string from package bundle.
62      * @param key Key identifing localized value (<code>MessageFormat</code>).
63      * @param param An argument <code>{0}</code> used for message parametrization.
64      * @return localized value.
65      */

66     public final String JavaDoc getString (String JavaDoc key, Object JavaDoc param) {
67         if (key == null) throw new NullPointerException JavaDoc();
68     return NbBundle.getMessage (this.getClass(), key, param);
69     }
70     
71     /**
72      * Get localized string from package bundle.
73      * @param key Key identifing localized value (<code>MessageFormat</code>).
74      * @param param1 An argument <code>{0}</code> used for message parametrization.
75      * @param param2 An argument <code>{1}</code> used for message parametrization.
76      * @return Localized value.
77      */

78     public final String JavaDoc getString (String JavaDoc key, Object JavaDoc param1, Object JavaDoc param2) {
79         if (key == null) throw new NullPointerException JavaDoc();
80     return NbBundle.getMessage (this.getClass(), key, param1, param2);
81     }
82     
83     /**
84      * Get localized character from package bundle. Usually used on mnemonic.
85      * @param key Key identifing localized value.
86      * @return localized value.
87      */

88     public final char getChar (String JavaDoc key) {
89         if (key == null) throw new NullPointerException JavaDoc();
90     return NbBundle.getMessage (this.getClass(), key).charAt (0);
91     }
92     
93     
94     //
95
// Debugging purposes
96
//
97

98     /**
99      * Check whether running at loggable level.
100      * @return true if <code>debug (...)</code> will log something.
101      */

102     public final boolean isLoggable () {
103         return getErrorManager().isLoggable (DEBUG_SEVERITY);
104     }
105
106     /**
107      * Log a message if package log level passes.
108      * @param message Message to log down. <code>null</code> is allowed
109      * but is not logged.
110      */

111     public final void debug (String JavaDoc message) {
112         if (message == null) return;
113         getErrorManager().log (DEBUG_SEVERITY, message);
114     }
115
116     /**
117      * Always log a exception.
118      * @param ex Exception to log down. <code>null</code> is allowed
119      * but is not logged.
120      */

121     public final void debug (Throwable JavaDoc ex) {
122         if (ex == null) return;
123         getErrorManager().notify (DEBUG_SEVERITY, ex);
124     }
125
126     /**
127      * Always log an annotated exception.
128      * @param message Message used for exception annotation or <code>null</code>.
129      * @param ex Exception to log down. <code>null</code> is allowed
130      * but is not logged.
131      */

132     public final void debug (String JavaDoc message, Throwable JavaDoc ex) {
133         if (ex == null) return;
134         if (message != null) {
135             ex = getErrorManager().annotate(ex, DEBUG_SEVERITY, message, null, null, null);
136         }
137         debug (ex);
138     }
139
140     /**
141      * Provide an <code>ErrorManager</code> instance named per subclass package.
142      * @return ErrorManager which is default for package where is class
143      * declared .
144      */

145     public final synchronized ErrorManager getErrorManager () {
146         if ( packageErrorManager == null ) {
147             String JavaDoc pack = "org.netbeans.modules.xml.tax.util"; // NOI18N
148
packageErrorManager = ErrorManager.getDefault().getInstance(pack);
149         }
150         return packageErrorManager;
151     }
152
153 }
154
Popular Tags