📄 xmldatatypeutil.java
字号:
/* Sesame - Storage and Querying architecture for RDF and RDF Schema * Copyright (C) 2001-2005 Aduna * * Contact: * Aduna * Prinses Julianaplein 14 b * 3817 CS Amersfoort * The Netherlands * tel. +33 (0)33 465 99 87 * fax. +33 (0)33 465 99 87 * * http://aduna.biz/ * http://www.openrdf.org/ * * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */package org.openrdf.util.xml;import java.util.Calendar;import java.util.GregorianCalendar;import java.util.StringTokenizer;import java.util.TimeZone;import org.openrdf.util.StringUtil;import org.openrdf.util.xml.datatypes.DateTime;import org.openrdf.vocabulary.XmlSchema;/** * Provides methods for handling the standard XML Schema datatypes. **/public class XmlDatatypeUtil {/*--------------------+| Datatype checking |+--------------------*/ /** * Checks whether the supplied datatype is a primitive XML Schema * datatype. **/ public static boolean isPrimitiveDatatype(String datatype) { return datatype.equals(XmlSchema.DURATION) || datatype.equals(XmlSchema.DATETIME) || datatype.equals(XmlSchema.TIME) || datatype.equals(XmlSchema.DATE) || datatype.equals(XmlSchema.GYEARMONTH) || datatype.equals(XmlSchema.GYEAR) || datatype.equals(XmlSchema.GMONTHDAY) || datatype.equals(XmlSchema.GDAY) || datatype.equals(XmlSchema.GMONTH) || datatype.equals(XmlSchema.STRING) || datatype.equals(XmlSchema.BOOLEAN) || datatype.equals(XmlSchema.BASE64BINARY) || datatype.equals(XmlSchema.HEXBINARY) || datatype.equals(XmlSchema.FLOAT) || datatype.equals(XmlSchema.DECIMAL) || datatype.equals(XmlSchema.DOUBLE) || datatype.equals(XmlSchema.ANYURI) || datatype.equals(XmlSchema.QNAME) || datatype.equals(XmlSchema.NOTATION); } /** * Checks whether the supplied datatype is a derived XML Schema * datatype. **/ public static boolean isDerivedDatatype(String datatype) { return datatype.equals(XmlSchema.NORMALIZEDSTRING) || datatype.equals(XmlSchema.TOKEN) || datatype.equals(XmlSchema.LANGUAGE) || datatype.equals(XmlSchema.NMTOKEN) || datatype.equals(XmlSchema.NMTOKENS) || datatype.equals(XmlSchema.NAME) || datatype.equals(XmlSchema.NCNAME) || datatype.equals(XmlSchema.ID) || datatype.equals(XmlSchema.IDREF) || datatype.equals(XmlSchema.IDREFS) || datatype.equals(XmlSchema.ENTITY) || datatype.equals(XmlSchema.ENTITIES) || datatype.equals(XmlSchema.INTEGER) || datatype.equals(XmlSchema.LONG) || datatype.equals(XmlSchema.INT) || datatype.equals(XmlSchema.SHORT) || datatype.equals(XmlSchema.BYTE) || datatype.equals(XmlSchema.NON_POSITIVE_INTEGER) || datatype.equals(XmlSchema.NEGATIVE_INTEGER) || datatype.equals(XmlSchema.NON_NEGATIVE_INTEGER) || datatype.equals(XmlSchema.POSITIVE_INTEGER) || datatype.equals(XmlSchema.UNSIGNED_LONG) || datatype.equals(XmlSchema.UNSIGNED_INT) || datatype.equals(XmlSchema.UNSIGNED_SHORT) || datatype.equals(XmlSchema.UNSIGNED_BYTE); } /** * Checks whether the supplied datatype is a built-in XML Schema * datatype. **/ public static boolean isBuiltInDatatype(String datatype) { return isPrimitiveDatatype(datatype) || isDerivedDatatype(datatype); } /** * Checks whether the supplied datatype is equal to xsd:decimal or * one of the built-in datatypes that is derived from xsd:decimal. **/ public static boolean isDecimalDatatype(String datatype) { return datatype.equals(XmlSchema.DECIMAL) || isIntegerDatatype(datatype); } /** * Checks whether the supplied datatype is equal to xsd:integer * or one of the built-in datatypes that is derived from * xsd:integer. **/ public static boolean isIntegerDatatype(String datatype) { return datatype.equals(XmlSchema.INTEGER) || datatype.equals(XmlSchema.LONG) || datatype.equals(XmlSchema.INT) || datatype.equals(XmlSchema.SHORT) || datatype.equals(XmlSchema.BYTE) || datatype.equals(XmlSchema.NON_POSITIVE_INTEGER) || datatype.equals(XmlSchema.NEGATIVE_INTEGER) || datatype.equals(XmlSchema.NON_NEGATIVE_INTEGER) || datatype.equals(XmlSchema.POSITIVE_INTEGER) || datatype.equals(XmlSchema.UNSIGNED_LONG) || datatype.equals(XmlSchema.UNSIGNED_INT) || datatype.equals(XmlSchema.UNSIGNED_SHORT) || datatype.equals(XmlSchema.UNSIGNED_BYTE); } /** * Checks whether the supplied datatype is equal to xsd:float or * xsd:double. **/ public static boolean isFloatingPointDatatype(String datatype) { return datatype.equals(XmlSchema.FLOAT) || datatype.equals(XmlSchema.DOUBLE); } /** * Checks whether the supplied datatype is equal to xsd:dateTime. */ public static boolean isDateTimeDatatype(String datatype) { return datatype.equals(XmlSchema.DATETIME); } /** * Checks whether the supplied datatype is ordered. The values of * an ordered datatype can be compared to eachother using * operators like <tt><</tt> and <tt>></tt>. **/ public static boolean isOrderedDatatype(String datatype) { return isDecimalDatatype(datatype) || isFloatingPointDatatype(datatype) || isDateTimeDatatype(datatype); }/*--------------------+| Value checking |+--------------------*/ public static boolean isValidValue(String value, String datatype) { boolean result = true; if (datatype.equals(XmlSchema.DECIMAL)) { result = isValidDecimal(value); } else if (datatype.equals(XmlSchema.INTEGER)) { result = isValidInteger(value); } else if (datatype.equals(XmlSchema.NEGATIVE_INTEGER)) { result = isValidNegativeInteger(value); } else if (datatype.equals(XmlSchema.NON_POSITIVE_INTEGER)) { result = isValidNonPositiveInteger(value); } else if (datatype.equals(XmlSchema.NON_NEGATIVE_INTEGER)) { result = isValidNonNegativeInteger(value); } else if (datatype.equals(XmlSchema.POSITIVE_INTEGER)) { result = isValidPositiveInteger(value); } else if (datatype.equals(XmlSchema.LONG)) { result = isValidLong(value); } else if (datatype.equals(XmlSchema.INT)) { result = isValidInt(value); } else if (datatype.equals(XmlSchema.SHORT)) { result = isValidShort(value); } else if (datatype.equals(XmlSchema.BYTE)) { result = isValidByte(value); } else if (datatype.equals(XmlSchema.UNSIGNED_LONG)) { result = isValidUnsignedLong(value); } else if (datatype.equals(XmlSchema.UNSIGNED_INT)) { result = isValidUnsignedInt(value); } else if (datatype.equals(XmlSchema.UNSIGNED_SHORT)) { result = isValidUnsignedShort(value); } else if (datatype.equals(XmlSchema.UNSIGNED_BYTE)) { result = isValidUnsignedByte(value); } else if (datatype.equals(XmlSchema.FLOAT)) { result = isValidFloat(value); } else if (datatype.equals(XmlSchema.DOUBLE)) { result = isValidDouble(value); } else if (datatype.equals(XmlSchema.BOOLEAN)) { result = isValidBoolean(value); } else if (datatype.equals(XmlSchema.DATETIME)) { result = isValidDateTime(value); } return result; } public static boolean isValidDecimal(String value) { try { normalizeDecimal(value); return true; } catch (IllegalArgumentException e) { return false; } } public static boolean isValidInteger(String value) { try { normalizeInteger(value); return true; } catch (IllegalArgumentException e) { return false; } } public static boolean isValidNegativeInteger(String value) { try { normalizeNegativeInteger(value); return true; } catch (IllegalArgumentException e) { return false; } } public static boolean isValidNonPositiveInteger(String value) { try { normalizeNonPositiveInteger(value); return true; } catch (IllegalArgumentException e) { return false; } } public static boolean isValidNonNegativeInteger(String value) { try { normalizeNonNegativeInteger(value); return true; } catch (IllegalArgumentException e) { return false; } } public static boolean isValidPositiveInteger(String value) { try { normalizePositiveInteger(value); return true; } catch (IllegalArgumentException e) { return false; } } public static boolean isValidLong(String value) { try { normalizeLong(value); return true; } catch (IllegalArgumentException e) { return false; } } public static boolean isValidInt(String value) { try { normalizeInt(value); return true; } catch (IllegalArgumentException e) { return false; } } public static boolean isValidShort(String value) { try { normalizeShort(value); return true; } catch (IllegalArgumentException e) { return false; } } public static boolean isValidByte(String value) { try { normalizeByte(value); return true; } catch (IllegalArgumentException e) { return false; } } public static boolean isValidUnsignedLong(String value) { try { normalizeUnsignedLong(value); return true; } catch (IllegalArgumentException e) { return false; } } public static boolean isValidUnsignedInt(String value) { try { normalizeUnsignedInt(value); return true; } catch (IllegalArgumentException e) { return false; } } public static boolean isValidUnsignedShort(String value) { try { normalizeUnsignedShort(value); return true; } catch (IllegalArgumentException e) { return false; } } public static boolean isValidUnsignedByte(String value) { try { normalizeUnsignedByte(value); return true; } catch (IllegalArgumentException e) { return false; } } public static boolean isValidFloat(String value) { try { normalizeFloat(value); return true; } catch (IllegalArgumentException e) { return false; } } public static boolean isValidDouble(String value) { try { normalizeDouble(value); return true; } catch (IllegalArgumentException e) { return false; } } public static boolean isValidBoolean(String value) { try { normalizeBoolean(value); return true; } catch (IllegalArgumentException e) { return false; } } public static boolean isValidDateTime(String value) { try { DateTime dt = new DateTime(value); return true; } catch (IllegalArgumentException e) { return false; } }/*--------------------+| Value normalization |+--------------------*/ /** * Normalizes the supplied value according to the normalization * rules for the supplied datatype. * * @param value The value to normalize. * @param datatype The value's datatype. * @return The normalized value if there are any (supported) * normalization rules for the supplied datatype, or the original * supplied value otherwise. * @exception IllegalArgumentException If the supplied value is * illegal considering the supplied datatype. **/ public static String normalize(String value, String datatype) { String result = value; if (datatype.equals(XmlSchema.DECIMAL)) { result = normalizeDecimal(value); } else if (datatype.equals(XmlSchema.INTEGER)) { result = normalizeInteger(value); } else if (datatype.equals(XmlSchema.NEGATIVE_INTEGER)) { result = normalizeNegativeInteger(value); } else if (datatype.equals(XmlSchema.NON_POSITIVE_INTEGER)) { result = normalizeNonPositiveInteger(value); } else if (datatype.equals(XmlSchema.NON_NEGATIVE_INTEGER)) { result = normalizeNonNegativeInteger(value); } else if (datatype.equals(XmlSchema.POSITIVE_INTEGER)) { result = normalizePositiveInteger(value); } else if (datatype.equals(XmlSchema.LONG)) { result = normalizeLong(value); } else if (datatype.equals(XmlSchema.INT)) { result = normalizeInt(value); } else if (datatype.equals(XmlSchema.SHORT)) { result = normalizeShort(value); } else if (datatype.equals(XmlSchema.BYTE)) { result = normalizeByte(value); } else if (datatype.equals(XmlSchema.UNSIGNED_LONG)) { result = normalizeUnsignedLong(value); } else if (datatype.equals(XmlSchema.UNSIGNED_INT)) { result = normalizeUnsignedInt(value); } else if (datatype.equals(XmlSchema.UNSIGNED_SHORT)) { result = normalizeUnsignedShort(value); } else if (datatype.equals(XmlSchema.UNSIGNED_BYTE)) { result = normalizeUnsignedByte(value); } else if (datatype.equals(XmlSchema.FLOAT)) { result = normalizeFloat(value); } else if (datatype.equals(XmlSchema.DOUBLE)) { result = normalizeDouble(value); } else if (datatype.equals(XmlSchema.BOOLEAN)) { result = normalizeBoolean(value); } else if (datatype.equals(XmlSchema.DATETIME)) { result = normalizeDateTime(value); } return result; } /** * Normalizes a boolean value to its canonical representation. * More specifically, the values <tt>1</tt> and <tt>0</tt> will be * normalized to the canonical values <tt>true</tt> and * <tt>false</tt>, respectively. Supplied canonical values will * remain as is. * * @param value The boolean value to normalize. * @return The normalized value. * @exception IllegalArgumentException If the supplied value is * not a legal boolean. **/ public static String normalizeBoolean(String value) { value = collapseWhiteSpace(value); if (value.equals("1")) { return "true";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -