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

📄 xmldatatypeutil.java

📁 这是外国一个开源推理机
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
		}		else if (value.equals("0")) {			return "false";		}		else if (value.equals("true") || value.equals("false")) {			return value;		}		else {			_throwIAE("Not a legal boolean value: " + value);			return null; // required by compiler, never reached		}	}	/**	 * Normalizes a decimal to its canonical representation.	 * For example: <tt>120</tt> becomes <tt>120.0</tt>,	 * <tt>+.3</tt> becomes <tt>0.3</tt>,	 * <tt>00012.45000</tt> becomes <tt>12.45</tt> and	 * <tt>-.0</tt> becomes <tt>0.0</tt>.	 *	 * @param decimal The decimal to normalize.	 * @return The canonical representation of <tt>decimal</tt>.	 * @throws IllegalArgumentException If one of the supplied strings	 * is not a legal decimal.	 **/	public static String normalizeDecimal(String decimal) {		decimal = collapseWhiteSpace(decimal);		String errMsg = "Not a legal decimal: " + decimal;		int decLength = decimal.length();		StringBuffer result = new StringBuffer(decLength + 2);		if (decLength == 0) {			_throwIAE(errMsg);		}		boolean isZeroPointZero = true;		// process any sign info		int idx = 0;		if (decimal.charAt(idx) == '-') {			result.append('-');			idx++;		}		else if (decimal.charAt(idx) == '+') {			idx++;		}		if (idx == decLength) {			_throwIAE(errMsg);		}		// skip any leading zeros		while (idx < decLength && decimal.charAt(idx) == '0') {			idx++;		}		// Process digits before the dot		if (idx == decLength) {			// decimal consists of zeros only			result.append('0');		}		else if (idx < decLength && decimal.charAt(idx) == '.') {			// no non-zero digit before the dot			result.append('0');		}		else {			isZeroPointZero = false;			// Copy any digits before the dot			while (idx < decLength) {				char c = decimal.charAt(idx);				if (c == '.') {					break;				}				if (!_isDigit(c)) {					_throwIAE(errMsg);				}				result.append(c);				idx++;			}		}		result.append('.');		// Process digits after the dot		if (idx == decLength) {			// No dot was found in the decimal			result.append('0');		}		else {			idx++;			// search last non-zero digit			int lastIdx = decLength - 1;			while (lastIdx >= 0 && decimal.charAt(lastIdx) == '0') {				lastIdx--;			}			if (idx > lastIdx) {				// No non-zero digits found				result.append('0');			}			else {				isZeroPointZero = false;				while (idx <= lastIdx) {					char c = decimal.charAt(idx);					if (!_isDigit(c)) {						_throwIAE(errMsg);					}					result.append(c);					idx++;				}			}		}		if (isZeroPointZero) {			// Make sure we don't return "-0.0"			return "0.0";		}		else {			return result.toString();		}	}	/**	 * Normalizes an integer to its canonical representation.	 * For example: <tt>+120</tt> becomes <tt>120</tt> and	 * <tt>00012</tt> becomes <tt>12</tt>.	 *	 * @param value The value to normalize.	 * @return The canonical representation of <tt>value</tt>.	 * @throws IllegalArgumentException If the supplied value	 * is not a legal integer.	 **/	public static String normalizeInteger(String value) {		return _normalizeIntegerValue(value, null, null);	}	/**	 * Normalizes an xsd:negativeInteger.	 **/	public static String normalizeNegativeInteger(String value) {		return _normalizeIntegerValue(value, null, "-1");	}	/**	 * Normalizes an xsd:nonPositiveInteger.	 **/	public static String normalizeNonPositiveInteger(String value) {		return _normalizeIntegerValue(value, null, "0");	}	/**	 * Normalizes an xsd:nonNegativeInteger.	 **/	public static String normalizeNonNegativeInteger(String value) {		return _normalizeIntegerValue(value, "0", null);	}	/**	 * Normalizes an xsd:positiveInteger.	 **/	public static String normalizePositiveInteger(String value) {		return _normalizeIntegerValue(value, "1", null);	}	/**	 * Normalizes an xsd:long.	 **/	public static String normalizeLong(String value) {		return _normalizeIntegerValue(value, "-9223372036854775808", "9223372036854775807");	}	/**	 * Normalizes an xsd:int.	 **/	public static String normalizeInt(String value) {		return _normalizeIntegerValue(value, "-2147483648", "2147483647");	}	/**	 * Normalizes an xsd:short.	 **/	public static String normalizeShort(String value) {		return _normalizeIntegerValue(value, "-32768", "32767");	}	/**	 * Normalizes an xsd:byte.	 **/	public static String normalizeByte(String value) {		return _normalizeIntegerValue(value, "-128", "127");	}	/**	 * Normalizes an xsd:unsignedLong.	 **/	public static String normalizeUnsignedLong(String value) {		return _normalizeIntegerValue(value, "0", "18446744073709551615");	}	/**	 * Normalizes an xsd:unsignedInt.	 **/	public static String normalizeUnsignedInt(String value) {		return _normalizeIntegerValue(value, "0", "4294967295");	}	/**	 * Normalizes an xsd:unsignedShort.	 **/	public static String normalizeUnsignedShort(String value) {		return _normalizeIntegerValue(value, "0", "65535");	}	/**	 * Normalizes an xsd:unsignedByte.	 **/	public static String normalizeUnsignedByte(String value) {		return _normalizeIntegerValue(value, "0", "255");	}	/**	 * Normalizes an integer to its canonical representation and	 * checks that the value is in the range [minValue, maxValue].	 **/	private static String _normalizeIntegerValue(String integer, String minValue, String maxValue) {		integer = collapseWhiteSpace(integer);		String errMsg = "Not a legal integer: " + integer;		int intLength = integer.length();		if (intLength == 0) {			_throwIAE(errMsg);		}		int idx = 0;		// process any sign info		boolean isNegative = false;		if (integer.charAt(idx) == '-') {			isNegative = true;			idx++;		}		else if (integer.charAt(idx) == '+') {			idx++;		}		if (idx == intLength) {			_throwIAE(errMsg);		}		if (integer.charAt(idx) == '0' && idx < intLength - 1) {			// integer starts with a zero followed by more characters,			// skip any leading zeros			idx++;			while (idx < intLength - 1 && integer.charAt(idx) == '0') {				idx++;			}		}		String norm = integer.substring(idx);		// Check that all characters in 'norm' are digits		for (int i = 0; i < norm.length(); i++) {			if (!_isDigit(norm.charAt(i))) {				_throwIAE(errMsg);			}		}		if (isNegative && norm.charAt(0) != '0') {			norm = "-" + norm;		}		// Check lower and upper bounds, if applicable		if (minValue != null) {			if (compareCanonicalIntegers(norm, minValue) < 0) {				_throwIAE("Value smaller than minimum value");			}		}		if (maxValue != null) {			if (compareCanonicalIntegers(norm, maxValue) > 0) {				_throwIAE("Value larger than maximum value");			}		}		return norm;	}	/**	 * Normalizes a float to its canonical representation.	 *	 * @param value The value to normalize.	 * @return The canonical representation of <tt>value</tt>.	 * @throws IllegalArgumentException If the supplied value	 * is not a legal float.	 **/	public static String normalizeFloat(String value) {		return _normalizeFPNumber(value, "-16777215.0", "16777215.0", "-149", "104");	}	/**	 * Normalizes a double to its canonical representation.	 *	 * @param value The value to normalize.	 * @return The canonical representation of <tt>value</tt>.	 * @throws IllegalArgumentException If the supplied value	 * is not a legal double.	 **/	public static String normalizeDouble(String value) {		return _normalizeFPNumber(value, "-9007199254740991.0", "9007199254740991.0", "-1075", "970");	}	/**	 * Normalizes a floating point number to its canonical	 * representation.	 *	 * @param value The value to normalize.	 * @return The canonical representation of <tt>value</tt>.	 * @throws IllegalArgumentException If the supplied value	 * is not a legal floating point number.	 **/	public static String normalizeFPNumber(String value) {		return _normalizeFPNumber(value, null, null, null, null);	}	/**	 * Normalizes a floating point number to its canonical	 * representation.	 *	 * @param value The value to normalize.	 * @param minMantissa A normalized decimal indicating the lowest	 * value that the mantissa may have.	 * @param maxMantissa A normalized decimal indicating the highest	 * value that the mantissa may have.	 * @param minExponent A normalized integer indicating the lowest	 * value that the exponent may have.	 * @param maxExponent A normalized integer indicating the highest	 * value that the exponent may have.	 * @return The canonical representation of <tt>value</tt>.	 * @throws IllegalArgumentException If the supplied value	 * is not a legal floating point number.	 **/	private static String _normalizeFPNumber(		String value,		String minMantissa, String maxMantissa,		String minExponent, String maxExponent)	{		value = collapseWhiteSpace(value);		// handle special values		if (value.equals("INF") || value.equals("-INF") || value.equals("NaN")) {			return value;		}		// Search for the exponent character E or e		int eIdx = value.indexOf('E');		if (eIdx == -1) {			// try lower case			eIdx = value.indexOf('e');		}		// Extract mantissa and exponent		String mantissa, exponent;		if (eIdx == -1) {			mantissa = normalizeDecimal(value);			exponent = "0";		}		else {			mantissa = normalizeDecimal(value.substring(0, eIdx));			exponent = normalizeInteger(value.substring(eIdx + 1));		}		// Check lower and upper bounds, if applicable		if (minMantissa != null) {			if (compareCanonicalDecimals(mantissa, minMantissa) < 0) {				_throwIAE("Mantissa smaller than minimum value (" + minMantissa + ")");			}		}		if (maxMantissa != null) {			if (compareCanonicalDecimals(mantissa, maxMantissa) > 0) {				_throwIAE("Mantissa larger than maximum value (" + maxMantissa + ")");			}		}		if (minExponent != null) {			if (compareCanonicalIntegers(exponent, minExponent) < 0) {				_throwIAE("Exponent smaller than minimum value (" + minExponent + ")");			}		}		if (maxExponent != null) {			if (compareCanonicalIntegers(exponent, maxExponent) > 0) {				_throwIAE("Exponent larger than maximum value (" + maxExponent + ")");			}		}		// Normalize mantissa to one non-zero digit before the dot		int shift = 0;		int dotIdx = mantissa.indexOf('.');		int digitCount = dotIdx;		if (mantissa.charAt(0) == '-') {			digitCount--;		}		if (digitCount > 1) {			// more than one digit before the dot, e.g 123.45, -10.0 or 100.0			StringBuffer buf = new StringBuffer(mantissa.length());			int firstDigitIdx = 0;			if (mantissa.charAt(0) == '-') {				buf.append('-');				firstDigitIdx = 1;			}			buf.append(mantissa.charAt(firstDigitIdx));			buf.append('.');			buf.append(mantissa.substring(firstDigitIdx + 1, dotIdx));			buf.append(mantissa.substring(dotIdx + 1));			mantissa = buf.toString();			// Check if the mantissa has excessive trailing zeros.			// For example, 100.0 will be normalize to 1.000 and			// -10.0 to -1.00.			int nonZeroIdx = mantissa.length() - 1;			while (nonZeroIdx >= 3 && mantissa.charAt(nonZeroIdx) == '0') {				nonZeroIdx--;			}			if (nonZeroIdx < 3 && mantissa.charAt(0) == '-') {				nonZeroIdx++;			}			if (nonZeroIdx < mantissa.length() - 1) {				mantissa = mantissa.substring(0, nonZeroIdx + 1);			}			shift = 1 - digitCount;		}		else if (mantissa.startsWith("0.") || mantissa.startsWith("-0.")) {			// Example mantissas: 0.0, -0.1, 0.00345 and 0.09			// search first non-zero digit			int nonZeroIdx = 2;			while (nonZeroIdx < mantissa.length() && mantissa.charAt(nonZeroIdx) == '0') {				nonZeroIdx++;			}			// 0.0 does not need any normalization:			if (nonZeroIdx < mantissa.length()) {				StringBuffer buf = new StringBuffer(mantissa.length());				buf.append(mantissa.charAt(nonZeroIdx));				buf.append('.');				if (nonZeroIdx == mantissa.length() - 1) {					// There was only one non-zero digit, e.g. as in 0.09					buf.append('0');				}				else {					buf.append(mantissa.substring(nonZeroIdx + 1));				}				mantissa = buf.toString();				shift = nonZeroIdx - 1;			}		}		if (shift != 0) {			try {				int exp = Integer.parseInt(exponent);				exponent = String.valueOf(exp - shift);			}			catch (NumberFormatException e) {				throw new RuntimeException("NumberFormatException: " + e.getMessage());			}		}		return mantissa + "E" + exponent;	}	/**	 * Normalizes an xsd:dateTime.	 *	 * @param value The value to normalize.	 * @return The normalized value.	 * @throws IllegalArgumentException If the supplied value is not a legal	 * xsd:dateTime value.	 */	public static String normalizeDateTime(String value) {		DateTime dt = new DateTime(value);		dt.normalize();		return dt.toString();	}	/**	 * Replaces all occurences of #x9 (tab), #xA (line feed) and #xD (carriage	 * return) with #x20 (space), as specified for whiteSpace facet	 * <tt>replace</tt>.	 **/	private static String _replaceWhiteSpace(String s) {		s = StringUtil.gsub("\t", " ", s);		s = StringUtil.gsub("\r", " ", s);		s = StringUtil.gsub("\n", " ", s);		return s;	}	/**	 * Replaces all contiguous sequences of #x9 (tab), #xA (line feed) and #xD	 * (carriage return) with a single #x20 (space) character, and removes any	 * leading and trailing whitespace characters, as specified for whiteSpace	 * facet <tt>collapse</tt>.	 **/	public static String collapseWhiteSpace(String s) {		// Note: the following code is optimized for the case where no white		// space collapsing is necessary, which (hopefully) is often the case.

⌨️ 快捷键说明

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