Diff of /jakarta/commons/sandbox/i18n/trunk/src/examples/org/apache/i18n/examples/LocalizedExceptionExample.java
Parent Directory
| Revision Log
| Patch
--- jakarta/commons/sandbox/i18n/trunk/src/examples/org/apache/i18n/examples/LocalizedExceptionExample.java 2005/05/06 10:19:21 168589
+++ jakarta/commons/sandbox/i18n/trunk/src/examples/org/apache/i18n/examples/LocalizedExceptionExample.java 2005/05/06 10:36:38 168590
@@ -1,22 +1,22 @@
/*
-*
-* ====================================================================
-*
-* Copyright 2004 The Apache Software Foundation
-*
-* Licensed under the Apache License, Version 2.0 (the "License");
-* you may not use this file except in compliance with the License.
-* You may obtain a copy of the License at
-*
-* http://www.apache.org/licenses/LICENSE-2.0
-*
-* Unless required by applicable law or agreed to in writing, software
-* distributed under the License is distributed on an "AS IS" BASIS,
-* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-* See the License for the specific language governing permissions and
-* limitations under the License.
-*
-*/
+ *
+ * ====================================================================
+ *
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
package org.apache.i18n.examples;
import java.util.Locale;
@@ -29,30 +29,46 @@ import org.apache.commons.i18n.bundles.E
/**
* @author Daniel Florey
- *
+ *
*/
public class LocalizedExceptionExample {
- private static final Logger logger = Logger.getLogger(LocalizedExceptionExample.class.getName());
+ private static final Logger logger = Logger
+ .getLogger(LocalizedExceptionExample.class.getName());
public static void main(String[] args) {
- XMLMessageProvider.install("org.apache.commons-i18n.test", Thread.currentThread().getContextClassLoader().getResourceAsStream("testMessages.xml"));
+ // Install the file providing the required messages for this example
+ XMLMessageProvider.install("org.apache.commons-i18n.examples", Thread
+ .currentThread().getContextClassLoader().getResourceAsStream(
+ "exampleMessages.xml"));
+ // Simulate the locale of the current user in a multi-user environment
+ // such as a web application
Locale currentUsersLocale = Locale.GERMAN;
-
- // Dealing with localized exceptions
+
+ // This is the real part dealing with localized exceptions
try {
someMethodThrowingAnException();
- } catch ( LocalizedException exception ) {
+ } catch (LocalizedException exception) {
+ // Retrieve the detailed localized error message
ErrorBundle errorMessage = exception.getErrorMessage();
+
// Print the summary of this error to the log with level SEVERE
// using the VM default locale:
- logger.log(Level.SEVERE, errorMessage.getSummary(Locale.getDefault()));
+ logger.log(Level.SEVERE, errorMessage.getSummary(Locale
+ .getDefault()));
+
// Print the details of this error to the log with level FINE
// using the VM default locale:
- logger.log(Level.FINE, errorMessage.getDetails(Locale.getDefault()));
- // Provide the title of this error to the user in a highly visible way
+ logger
+ .log(Level.FINE, errorMessage.getDetails(Locale
+ .getDefault()));
+
+ // Provide the title of this error to the user in a highly visible
+ // way
// using the current users locale:
- System.out.println("#### "+errorMessage.getTitle(currentUsersLocale)+" ####");
+ System.out.println("#### "
+ + errorMessage.getTitle(currentUsersLocale) + " ####");
+
// Provide the text of this error to the user
// using the current users locale:
System.out.println(errorMessage.getText(currentUsersLocale));
@@ -60,12 +76,14 @@ public class LocalizedExceptionExample {
}
/**
- * @throws LocalizedException is thrown just to show the capabilities of LocalizedExceptions
+ * @throws LocalizedException
+ * is thrown just to show the capabilities of
+ * LocalizedExceptions
*/
- private static void someMethodThrowingAnException() throws LocalizedException {
+ private static void someMethodThrowingAnException()
+ throws LocalizedException {
String userCausingTheException = "Daniel";
- throw new LocalizedException(
- new ErrorBundle("theCauseOfThisException",
- new String[] { userCausingTheException } ));
+ throw new LocalizedException(new ErrorBundle("theCauseOfThisException",
+ new String[]{userCausingTheException}));
}
}
\ No newline at end of file