~ubuntu-branches/ubuntu/gutsy/libpgjava/gutsy

« back to all changes in this revision

Viewing changes to src/interfaces/jdbc/org/postgresql/util/MessageTranslator.java

  • Committer: Bazaar Package Importer
  • Author(s): Stefan Gybas
  • Date: 2002-02-06 23:43:06 UTC
  • Revision ID: james.westby@ubuntu.com-20020206234306-hsg7suqr8q56qg40
Tags: upstream-7.2
ImportĀ upstreamĀ versionĀ 7.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.postgresql.util;
 
2
 
 
3
import java.util.*;
 
4
import java.text.*;
 
5
 
 
6
/*
 
7
 * A singleton class to translate JDBC driver messages in SQLException's.
 
8
 */
 
9
public class MessageTranslator
 
10
{
 
11
 
 
12
        // The singleton instance.
 
13
        private static MessageTranslator instance = null;
 
14
 
 
15
        private ResourceBundle bundle;
 
16
 
 
17
        private MessageTranslator()
 
18
        {
 
19
                try
 
20
                {
 
21
                        bundle = ResourceBundle.getBundle("org.postgresql.errors");
 
22
                }
 
23
                catch (MissingResourceException e)
 
24
                {
 
25
                        // translation files have not been installed.
 
26
                        bundle = null;
 
27
                }
 
28
        }
 
29
 
 
30
        // Synchronized, otherwise multiple threads may perform the test and
 
31
        // assign to the singleton instance simultaneously.
 
32
        private synchronized final static MessageTranslator getInstance()
 
33
        {
 
34
                if (instance == null)
 
35
                {
 
36
                        instance = new MessageTranslator();
 
37
                }
 
38
                return instance;
 
39
        }
 
40
 
 
41
        public final static String translate(String id, Object[] args)
 
42
        {
 
43
 
 
44
                MessageTranslator translator = MessageTranslator.getInstance();
 
45
 
 
46
                return translator._translate(id, args);
 
47
        }
 
48
 
 
49
        private final String _translate(String id, Object[] args)
 
50
        {
 
51
                String message;
 
52
 
 
53
                if (bundle != null && id != null)
 
54
                {
 
55
                        // Now look up a localized message. If one is not found, then use
 
56
                        // the supplied message instead.
 
57
                        try
 
58
                        {
 
59
                                message = bundle.getString(id);
 
60
                        }
 
61
                        catch (MissingResourceException e)
 
62
                        {
 
63
                                message = id;
 
64
                        }
 
65
                }
 
66
                else
 
67
                {
 
68
                        message = id;
 
69
                }
 
70
 
 
71
                // Expand any arguments
 
72
                if (args != null && message != null)
 
73
                {
 
74
                        message = MessageFormat.format(message, args);
 
75
                }
 
76
 
 
77
                return message;
 
78
        }
 
79
}