⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 language.java

📁 這是一個油Java實作的資料庫系統 是個入門的好材料
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* =============================================================
 * SmallSQL : a free Java DBMS library for the Java(tm) platform
 * =============================================================
 *
 * (C) Copyright 2004-2007, by Volker Berlin.
 *
 * Project Info:  http://www.smallsql.de/
 *
 * This library is free software; you can redistribute it and/or modify it 
 * under the terms of the GNU Lesser General Public License as published by 
 * the Free Software Foundation; either version 2.1 of the License, or 
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
 * USA.  
 *
 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
 * in the United States and other countries.]
 *
 * ---------------
 * Language.java
 * ---------------
 * Author: Volker Berlin
 * Author: Saverio Miroddi 
 */
package smallsql.database.language;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

/**
 * Base localization class.<br>
 * Contains English default messages and ResourceBundle interface
 * implementation.
 */
public class Language {
	//////////////////////////////////////////////////////////////////////
	// NAMED CONSTANTS 
	//////////////////////////////////////////////////////////////////////
	
	/* CUSTOM_MESSAGE doesn't need to be copied to subclasses. Also, it's
	 * not checked by the difference() method. */
	public static final String CUSTOM_MESSAGE			= "SS-0000";
	
	public static final String UNSUPPORTED_OPERATION 	= "SS-0001";
    public static final String CANT_LOCK_FILE           = "SS-0003";

	public static final String DB_EXISTENT 				= "SS-0030";
	public static final String DB_NONEXISTENT 			= "SS-0031";
	public static final String DB_NOT_DIRECTORY 		= "SS-0032";
	public static final String DB_NOTCONNECTED 			= "SS-0033";

	public static final String CONNECTION_CLOSED 		= "SS-0070";

	public static final String VIEW_INSERT 				= "SS-0100";
	public static final String VIEWDROP_NOT_VIEW 		= "SS-0101";
	public static final String VIEW_CANTDROP 			= "SS-0102";

	public static final String RSET_NOT_PRODUCED 		= "SS-0130";
	public static final String RSET_READONLY 			= "SS-0131";
	public static final String RSET_FWDONLY				= "SS-0132";
	public static final String RSET_CLOSED				= "SS-0133";
	public static final String RSET_NOT_INSERT_ROW		= "SS-0134";
	public static final String RSET_ON_INSERT_ROW		= "SS-0135";
	public static final String ROWSOURCE_READONLY		= "SS-0136";
    
    public static final String STMT_IS_CLOSED           = "SS-0140";

	public static final String SUBQUERY_COL_COUNT		= "SS-0160";
	public static final String JOIN_DELETE				= "SS-0161";
	public static final String JOIN_INSERT				= "SS-0162";
	public static final String DELETE_WO_FROM			= "SS-0163";
	public static final String INSERT_WO_FROM			= "SS-0164";

	public static final String TABLE_CANT_RENAME		= "SS-0190";
	public static final String TABLE_CANT_DROP			= "SS-0191";
	public static final String TABLE_CANT_DROP_LOCKED	= "SS-0192";
	public static final String TABLE_CORRUPT_PAGE		= "SS-0193";
	public static final String TABLE_MODIFIED			= "SS-0194";
	public static final String TABLE_DEADLOCK			= "SS-0195";
	public static final String TABLE_OR_VIEW_MISSING	= "SS-0196";
	public static final String TABLE_FILE_INVALID		= "SS-0197";
	public static final String TABLE_OR_VIEW_FILE_INVALID = "SS-0198";
	public static final String TABLE_EXISTENT			= "SS-0199";

	public static final String FK_NOT_TABLE				= "SS-0220";
	public static final String PK_ONLYONE				= "SS-0221";
	public static final String KEY_DUPLICATE			= "SS-0222";

	public static final String MONTH_TOOLARGE 			= "SS-0251";
	public static final String DAYS_TOOLARGE 			= "SS-0252";
	public static final String HOURS_TOOLARGE 			= "SS-0253";
	public static final String MINUTES_TOOLARGE 		= "SS-0254";
	public static final String SECS_TOOLARGE 			= "SS-0255";
	public static final String MILLIS_TOOLARGE 			= "SS-0256";
	public static final String DATETIME_INVALID 		= "SS-0257";

	public static final String UNSUPPORTED_CONVERSION_OPER = "SS-0280";
	public static final String UNSUPPORTED_DATATYPE_OPER = "SS-0281";
	public static final String UNSUPPORTED_DATATYPE_FUNC = "SS-0282";
	public static final String UNSUPPORTED_CONVERSION_FUNC = "SS-0283";
	public static final String UNSUPPORTED_TYPE_CONV 	= "SS-0284";
	public static final String UNSUPPORTED_TYPE_SUM 	= "SS-0285";
	public static final String UNSUPPORTED_TYPE_MAX 	= "SS-0286";
	public static final String UNSUPPORTED_CONVERSION 	= "SS-0287";
	public static final String INSERT_INVALID_LEN 		= "SS-0288";
	public static final String SUBSTR_INVALID_LEN 		= "SS-0289";

	public static final String VALUE_STR_TOOLARGE 		= "SS-0310";
	public static final String VALUE_BIN_TOOLARGE 		= "SS-0311";
	public static final String VALUE_NULL_INVALID 		= "SS-0312";
	public static final String VALUE_CANT_CONVERT 		= "SS-0313";

	public static final String BYTEARR_INVALID_SIZE 	= "SS-0340";
	public static final String LOB_DELETED 				= "SS-0341";

	public static final String PARAM_CLASS_UNKNOWN 		= "SS-0370";
	public static final String PARAM_EMPTY 				= "SS-0371";
	public static final String PARAM_IDX_OUT_RANGE 		= "SS-0372";

	public static final String COL_DUPLICATE 			= "SS-0400";
	public static final String COL_MISSING 				= "SS-0401";
	public static final String COL_VAL_UNMATCH 			= "SS-0402";
	public static final String COL_INVALID_SIZE 		= "SS-0403";
	public static final String COL_WRONG_PREFIX 		= "SS-0404";
	public static final String COL_READONLY 			= "SS-0405";
	public static final String COL_INVALID_NAME 		= "SS-0406";
	public static final String COL_IDX_OUT_RANGE 		= "SS-0407";
	public static final String COL_AMBIGUOUS 			= "SS-0408";
	
	public static final String GROUP_AGGR_INVALID 		= "SS-0430";
	public static final String GROUP_AGGR_NOTPART 		= "SS-0431";
	public static final String ORDERBY_INTERNAL 		= "SS-0432";
	public static final String UNION_DIFFERENT_COLS 	= "SS-0433";

	public static final String INDEX_EXISTS 			= "SS-0460";
	public static final String INDEX_MISSING 			= "SS-0461";
	public static final String INDEX_FILE_INVALID 		= "SS-0462";
	public static final String INDEX_CORRUPT 			= "SS-0463";
	public static final String INDEX_TOOMANY_EQUALS 	= "SS-0464";

	public static final String FILE_TOONEW 				= "SS-0490";
	public static final String FILE_TOOOLD 				= "SS-0491";

	public static final String ROW_0_ABSOLUTE 			= "SS-0520";
	public static final String ROW_NOCURRENT 			= "SS-0521";
	public static final String ROWS_WRONG_MAX 			= "SS-0522";
	public static final String ROW_LOCKED 				= "SS-0523";
	public static final String ROW_DELETED 				= "SS-0524";

	public static final String SAVEPT_INVALID_TRANS 	= "SS-0550";
	public static final String SAVEPT_INVALID_DRIVER 	= "SS-0551";

	public static final String ALIAS_UNSUPPORTED 		= "SS-0580";
	public static final String ISOLATION_UNKNOWN 		= "SS-0581";
	public static final String FLAGVALUE_INVALID 		= "SS-0582";
	public static final String ARGUMENT_INVALID 		= "SS-0583";
	public static final String GENER_KEYS_UNREQUIRED 	= "SS-0584";
	public static final String SEQUENCE_HEX_INVALID 	= "SS-0585";
	public static final String SEQUENCE_HEX_INVALID_STR = "SS-0586";
	
	public static final String SYNTAX_BASE_OFS			= "SS-0610";
	public static final String SYNTAX_BASE_END			= "SS-0611";
	public static final String STXADD_ADDITIONAL_TOK	= "SS-0612";
	public static final String STXADD_IDENT_EXPECT		= "SS-0613";
	public static final String STXADD_IDENT_EMPTY		= "SS-0614";
	public static final String STXADD_IDENT_WRONG		= "SS-0615";
	public static final String STXADD_OPER_MINUS		= "SS-0616";	
	public static final String STXADD_FUNC_UNKNOWN		= "SS-0617";	
	public static final String STXADD_PARAM_INVALID_COUNT	= "SS-0618";
	public static final String STXADD_JOIN_INVALID		= "SS-0619";
	public static final String STXADD_FROM_PAR_CLOSE	= "SS-0620";
	public static final String STXADD_KEYS_REQUIRED		= "SS-0621";
	public static final String STXADD_NOT_NUMBER		= "SS-0622";
	public static final String STXADD_COMMENT_OPEN		= "SS-0623";

	//////////////////////////////////////////////////////////////////////
	// VARIABLE, CONSTRUCTOR AND NON-RESOURCEBUNDLE METHODS. 
	//////////////////////////////////////////////////////////////////////
	
	private Map messages;
	private Map sqlStates;
	
	/**
	 * Return a Language instance.<br>
	 * Defaults to ENGLISH language.<br>
	 * Warning: there is difference between locale strings like 'en_EN' and
	 * 'en_UK': it's advised to pass only the first two characters.
	 * 
	 * @param localeStr
	 *            Locale.toString() value for Language. Nullable, for ENGLISH.
	 * @return Language instance.
	 * @throws InstantiationException
	 *             Error during instantiation, i.e. duplicate entry found.
	 */
	public static Language getLanguage(String localeStr) {
		try {
			return getFromLocaleTree(localeStr);
		}
		catch (IllegalArgumentException e) {
			return getDefaultLanguage();
		}
	}
	
	/**
	 * Gets the language for the default locale; if not found, returns the
	 * ENGLISH language.
	 */
	public static Language getDefaultLanguage() {		
		String dfltLocaleStr = Locale.getDefault().toString();

		try {
			return getFromLocaleTree(dfltLocaleStr);
		}
		catch (IllegalArgumentException e) {
			return new Language(); // default to English
		}
	}
	
	/**
	 * Searches a language in the Locale tree, for example:<br>
	 * first 'en_UK', then 'en'
	 * 
	 * @param localeStr
	 *            locale string.
	 * @return Language instance, if found.
	 * @throws IllegalArgumentException
	 *             Language not found in the tree.
	 */
	private static Language getFromLocaleTree(String localeStr) 
	throws IllegalArgumentException {
		String part = localeStr;
		while (true) {
			String langClassName = Language.class.getName() + '_' + part;
			
			try {
				return (Language) Class.forName(langClassName).newInstance();
			}
			catch (IllegalAccessException e) {
				 assert(false): "Internal error: must never happen.";
			}
			catch (ClassNotFoundException e) { 
				// do nothing
			}
			catch (InstantiationException e) { 
				assert(false): "Error during Language instantiation: " + e.getMessage();
			}
			
			int lastUndsc = part.lastIndexOf("_");
			
			if (lastUndsc > -1) part = part.substring(0, lastUndsc);
			else break;			
		}
		
		throw new IllegalArgumentException("Locale not found in the tree: " + localeStr);
	}

	/**
	 * Base language constructor; fills the internal map with the English
	 * messages
	 */
	protected Language() {
		messages = new HashMap((int)(MESSAGES.length / 0.7)); //avoid rehashing ;-)
		sqlStates = new HashMap((int)(MESSAGES.length / 0.7)); //avoid rehashing ;-)
		addMessages(MESSAGES);
		setSqlStates();
	}
	
	/**
	 * Add entries to message map.<br>
	 * If duplicates entries are found the adding entries, an exception is
	 * thrown.
	 * 
	 * @param entries
	 *            adding language entries.
	 * @throws IllegalArgumentException
	 *             if duplicate entry is found.
	 */
	protected final void addMessages(String[][] entries) 
	throws IllegalArgumentException {
		Set inserted = new HashSet(); // for duplicates checking
		
		for (int i = 0; i < entries.length; i++) {
			String key = entries[i][0];
			
			if (! inserted.add(key)) {
				throw new IllegalArgumentException("Duplicate key: " + key);
			}
			else {
				String value = entries[i][1];
				messages.put(key, value);
			}
		}
	}
	
	/**
	 * Add entries to sql states map.<br>
	 * If duplicates entries are found the adding entries, an exception is
	 * thrown.
	 * 
	 * @param entries
	 *            adding language entries.
	 * @throws IllegalArgumentException

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -