Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,11 @@ public final class LocalizedResource implements java.security.PrivilegedAction<
private DecimalFormat formatDecimal;

private LocalizedResource(){

init();
}
public LocalizedResource(String msgF){

init(null, null, msgF, true);
}

Expand All @@ -79,11 +81,14 @@ public LocalizedResource(String msgF){
* @param msgF message file
*/
public LocalizedResource(String encStr, String locStr, String msgF){

init(encStr,locStr,msgF, false);
}

public static LocalizedResource getInstance(){

if (local == null){

local = new LocalizedResource();
}
return local;
Expand All @@ -106,13 +111,17 @@ public void init (String encStr, String locStr, String msgF) {
}

private void init (String encStr, String locStr, String msgF, boolean readEnv){

if (encStr != null){

encode = encStr;
}
//then get encoding string from environment
if (encode == null && readEnv) {

String eEncode = getEnvProperty(ENV_CODESET);
if ( eEncode != null ){

encode = eEncode;
}
}
Expand All @@ -125,25 +134,31 @@ private void init (String encStr, String locStr, String msgF, boolean readEnv){

//if null, get locale again from the environment variable
if (locale==null && readEnv) {

String s = getEnvProperty(ENV_LOCALE);
locale = getNewLocale(s);
}
//get the default locale if forced
if (locale==null){

locale = Locale.getDefault();
}
if (msgF != null) {

messageFileName = msgF;
}
else {

messageFileName = MESSAGE_FILE;
}

//create default in/out
out = getNewOutput(System.out);
in = getNewInput(System.in);

//for faster code: get the format objs
if (enableLocalized && locale != null){

formatDecimal = (DecimalFormat)DecimalFormat.getInstance(locale);
formatNumber = NumberFormat.getInstance(locale);
formatDate = DateFormat.getDateInstance(DateFormat.LONG,locale);
Expand All @@ -152,6 +167,7 @@ private void init (String encStr, String locStr, String msgF, boolean readEnv){
DateFormat.LONG, locale);
}
else {

formatDecimal = (DecimalFormat)DecimalFormat.getInstance();
formatNumber = NumberFormat.getInstance();
formatDate = DateFormat.getDateInstance(DateFormat.LONG);
Expand All @@ -166,16 +182,20 @@ private void init (String encStr, String locStr, String msgF, boolean readEnv){
//fall back to English message file if locale message file is not found
private void setResource(){
if (res != null){

return;
}
if ( locale == null || locale.toString().equals("none") ){

res = ResourceBundle.getBundle(messageFileName);
}
else
try {

res = ResourceBundle.getBundle(messageFileName,locale);
}
catch(java.util.MissingResourceException e){

res = ResourceBundle.getBundle(messageFileName,Locale.ENGLISH);
}
}
Expand All @@ -190,6 +210,7 @@ private void initMaxSizes2(){
// 3900/01/28 !! original devloper thought they were getting 2000/01/28
Date d = new Date(60907276800000L);
Timestamp t = new Timestamp(d.getTime());

for(int month = 0 ; month <=11 ; month++, d.setTime(d.getTime() + (30L * 24L * 60L * 60L * 1000L))) {
len=getDateAsString(d).length();

Expand Down Expand Up @@ -227,6 +248,7 @@ private void initMaxSizes2(){

public LocalizedInput getNewInput(InputStream i) {
try {

if (encode != null)
return new LocalizedInput(i,encode);
}
Expand All @@ -237,6 +259,7 @@ public LocalizedInput getNewInput(InputStream i) {
}

public LocalizedInput getNewEncodedInput(InputStream i, String encoding) {

try {
return new LocalizedInput(i,encoding);
}
Expand All @@ -248,6 +271,7 @@ public LocalizedInput getNewEncodedInput(InputStream i, String encoding) {

public LocalizedOutput getNewOutput(OutputStream o){
try {

if (encode != null)
return new LocalizedOutput(o,encode);
}
Expand All @@ -262,11 +286,13 @@ public LocalizedOutput getNewOutput(OutputStream o){
public LocalizedOutput getNewEncodedOutput(OutputStream o,
String encoding) throws UnsupportedEncodingException{
out = new LocalizedOutput(o, encoding);

return out;
}
private Locale getNewLocale(String locStr){
String l="", r="", v="";
StringTokenizer st;

if (locStr==null) {
return null;
}
Expand All @@ -283,94 +309,140 @@ private Locale getNewLocale(String locStr){
}
}
public String getTextMessage(String key, Object... objectArr) {

if (res == null){

setResource();
}

if(key=="IJ_UnabToEsta") {
try{

return "No connection available";
}
catch(Exception e){
String tmpFormat = key;

for (int i=0; objectArr != null && i<objectArr.length; i++)
tmpFormat = tmpFormat + ", <{" + (i) + "}>";
return MessageFormat.format(tmpFormat, objectArr);
}

}

else{

try{

return MessageFormat.format(res.getString(key), objectArr);
} catch (Exception e) {
String tmpFormat = key;

for (int i=0; objectArr != null && i<objectArr.length; i++)
tmpFormat = tmpFormat + ", <{" + (i) + "}>";
return MessageFormat.format(tmpFormat, objectArr);
}
}

}
public String getLocalizedString(ResultSet rs,
ResultSetMetaData rsm,
int columnNumber) throws SQLException{
if (!enableLocalized){

return rs.getString(columnNumber);
}
int type = rsm.getColumnType(columnNumber);
if ( type == Types.DATE ) {

return getDateAsString(rs.getDate(columnNumber));
}
else if ( type == Types.INTEGER || type == Types.SMALLINT ||
type == Types.BIGINT || type == Types.TINYINT ) {

return getNumberAsString(rs.getLong(columnNumber));
}
else if (type == Types.REAL || type == Types.FLOAT ||
type == Types.DOUBLE ) {

return getNumberAsString(rs.getDouble(columnNumber));
}
else if (type == Types.NUMERIC || type == Types.DECIMAL) {

return getNumberAsString(rs.getBigDecimal(columnNumber));
}
else if (type == Types.TIME ) {

return getTimeAsString(rs.getTime(columnNumber));
}
else if (type == Types.TIMESTAMP ) {
return getTimestampAsString(rs.getTimestamp(columnNumber));

return getTimestampAsString(rs.getTimestamp(columnNumber));
}
return rs.getString(columnNumber);
}

public String getDateAsString(Date d){

if (!enableLocalized){

return d.toString();
}

return formatDate.format(d);
}
public String getTimeAsString(Date t){

if (!enableLocalized){

return t.toString();
}
return formatTime.format(t, new StringBuffer(),
new java.text.FieldPosition(0)).toString();
}
public String getNumberAsString(int o){

if (enableLocalized){

return formatNumber.format(o);
}
else {

return String.valueOf(o);
}
}
public String getNumberAsString(long o){
if (enableLocalized){

return formatNumber.format(o);
}
else{

return String.valueOf(o);
}
}
public String getNumberAsString(Object o){
if (enableLocalized){

return formatNumber.format(o, new StringBuffer(),
new FieldPosition(0)).toString();
}
else {

return o.toString();
}
}
public String getNumberAsString(double o){
if (!enableLocalized) {

return String.valueOf(o);
}
return formatDecimal.format(o);
}
public String getTimestampAsString(Timestamp t){

if (!enableLocalized){

return t.toString();
}
return formatTimestamp.format
Expand All @@ -380,6 +452,7 @@ public String getTimestampAsString(Timestamp t){
public int getColumnDisplaySize(ResultSetMetaData rsm,
int columnNumber) throws SQLException{
if (!enableLocalized){

return rsm.getColumnDisplaySize(columnNumber);
}
int type = rsm.getColumnType(columnNumber);
Expand All @@ -394,13 +467,15 @@ public int getColumnDisplaySize(ResultSetMetaData rsm,
public String getStringFromDate(String dateStr)
throws ParseException{
if (!enableLocalized){

return dateStr;
}
Date d = formatDate.parse(dateStr);
return new java.sql.Date(d.getTime()).toString();
}
public String getStringFromTime(String timeStr)
throws ParseException{

if (!enableLocalized){
return timeStr;
}
Expand All @@ -409,6 +484,7 @@ public String getStringFromTime(String timeStr)
}
public String getStringFromValue(String val)
throws ParseException{

if (!enableLocalized){
return val;
}
Expand All @@ -417,57 +493,70 @@ public String getStringFromValue(String val)
public String getStringFromTimestamp(String timestampStr)
throws ParseException{
if (!enableLocalized){

return timestampStr;
}
Date ts = formatTimestamp.parse(timestampStr);
return new java.sql.Timestamp(ts.getTime()).toString();
}
public Locale getLocale(){
;
return locale;
}

private final synchronized String getEnvProperty(String key) {
String s;

try
{
resourceKey = key;
s = java.security.AccessController.doPrivileged(this);
}
catch (SecurityException se) {

s = null;
}
//System.out.println("{"+resourceKey+"="+s+"}");
return s;
}
public final String run() {

String s = System.getProperty(resourceKey);
return s;
}
public static boolean enableLocalization(boolean mode) {

getInstance().enableLocalized = mode;
//re-initialized locale
getInstance().init();
return mode;
}
public boolean isLocalized(){

return getInstance().enableLocalized;
}
public static String getMessage(String key, Object... args) {

return getInstance().getTextMessage(key, args);
}
public static LocalizedOutput OutputWriter(){

return getInstance().out;
}
public static LocalizedInput InputReader(){

return getInstance().in;
}
public static String getNumber(long o){

return getInstance().getNumberAsString(o);
}
public static String getNumber(int o){

return getInstance().getNumberAsString(o);
}
public String toString(){

String s = "toString(){\n" +
"locale=" + (locale==null?"null":locale.toString()) + "\n" +
"encode=" + encode + "\n" +
Expand Down