📄 xsdabstractdatetimetype.java
字号:
/******************************************************************
* File: XSDAbstractDateTimeType.java
* Created by: Dave Reynolds
* Created on: 04-Dec-2003
*
* (c) Copyright 2003, 2004, 2005, 2006, 2007 Hewlett-Packard Development Company, LP
* All rights reserved.
* [See end of file]
* $Id: XSDAbstractDateTimeType.java,v 1.8 2007/01/02 11:48:24 andy_seaborne Exp $
*****************************************************************/
package com.hp.hpl.jena.datatypes.xsd.impl;
import com.hp.hpl.jena.datatypes.RDFDatatype;
import com.hp.hpl.jena.datatypes.xsd.XSDDatatype;
import com.hp.hpl.jena.datatypes.xsd.XSDDateTime;
import com.hp.hpl.jena.graph.impl.LiteralLabel;
/**
* Base class for all date/time/duration type representations.
* Includes support functions for parsing and comparing dates.
*
* @author <a href="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
* @version $Revision: 1.8 $ on $Date: 2007/01/02 11:48:24 $
*/
public class XSDAbstractDateTimeType extends XSDDatatype {
/**
* Constructor
*/
public XSDAbstractDateTimeType(String typename) {
super(typename);
}
/**
* Compares two instances of values of the given datatype.
* This ignores lang tags and just uses the java.lang.Number
* equality.
*/
public boolean isEqual(LiteralLabel value1, LiteralLabel value2) {
return value1.getValue().equals(value2.getValue());
}
/** Mask to indicate whether year is present */
public static final short YEAR_MASK = 0x1;
/** Mask to indicate whether month is present */
public static final short MONTH_MASK = 0x2;
/** Mask to indicate whether day is present */
public static final short DAY_MASK = 0x4;
/** Mask to indicate whether time is present */
public static final short TIME_MASK = 0x8;
/** Mask to indicate all date/time are present */
public static final short FULL_MASK = 0xf;
// --------------------------------------------------------------------
// This code is adapated from Xerces 2.6.0 AbstractDateTimeDV.
// Copyright (c) 1999-2003 The Apache Software Foundation. All rights
// reserved.
// --------------------------------------------------------------------
//define constants
protected final static int CY = 0, M = 1, D = 2, h = 3,
m = 4, s = 5, ms = 6, msscale=8, utc=7, hh=0, mm=1;
//size for all objects must have the same fields:
//CCYY, MM, DD, h, m, s, ms + timeZone
protected final static int TOTAL_SIZE = 9;
//define constants to be used in assigning default values for
//all date/time excluding duration
protected final static int YEAR=2000;
protected final static int MONTH=01;
protected final static int DAY = 15;
/**
* Parses time hh:mm:ss.sss and time zone if any
*
* @param start
* @param end
* @param data
* @exception RuntimeException
*/
protected void getTime (String buffer, int start, int end, int[] data, int[] timeZone) throws RuntimeException{
int stop = start+2;
//get hours (hh)
data[h]=parseInt(buffer, start,stop);
//get minutes (mm)
if (buffer.charAt(stop++)!=':') {
throw new RuntimeException("Error in parsing time zone" );
}
start = stop;
stop = stop+2;
data[m]=parseInt(buffer, start,stop);
//get seconds (ss)
if (buffer.charAt(stop++)!=':') {
throw new RuntimeException("Error in parsing time zone" );
}
start = stop;
stop = stop+2;
data[s]=parseInt(buffer, start,stop);
if (stop == end)
return;
//get miliseconds (ms)
start = stop;
int milisec = buffer.charAt(start) == '.' ? start : -1;
//find UTC sign if any
int sign = findUTCSign(buffer, start, end);
//parse miliseconds
if ( milisec != -1 ) {
// The end of millisecond part is between . and
// either the end of the UTC sign
start = sign < 0 ? end : sign;
int msEnd = start;
while (buffer.charAt(msEnd-1) == '0') msEnd--;
data[ms]=parseInt(buffer, milisec+1, msEnd);
data[msscale] = msEnd - milisec - 1;
}
//parse UTC time zone (hh:mm)
if ( sign>0 ) {
if (start != sign)
throw new RuntimeException("Error in parsing time zone" );
getTimeZone(buffer, data, sign, end, timeZone);
}
else if (start != end) {
throw new RuntimeException("Error in parsing time zone" );
}
}
/**
* Parses date CCYY-MM-DD
*
* @param start
* @param end
* @param data
* @exception RuntimeException
*/
protected int getDate (String buffer, int start, int end, int[] date) throws RuntimeException{
start = getYearMonth(buffer, start, end, date);
if (buffer.charAt(start++) !='-') {
throw new RuntimeException("CCYY-MM must be followed by '-' sign");
}
int stop = start + 2;
date[D]=parseInt(buffer, start, stop);
return stop;
}
/**
* Parses date CCYY-MM
*
* @param start
* @param end
* @param data
* @exception RuntimeException
*/
protected int getYearMonth (String buffer, int start, int end, int[] date) throws RuntimeException{
if ( buffer.charAt(0)=='-' ) {
// REVISIT: date starts with preceding '-' sign
// do we have to do anything with it?
//
start++;
}
int i = indexOf(buffer, start, end, '-');
if ( i==-1 ) throw new RuntimeException("Year separator is missing or misplaced");
int length = i-start;
if (length<4) {
throw new RuntimeException("Year must have 'CCYY' format");
}
else if (length > 4 && buffer.charAt(start)=='0'){
throw new RuntimeException("Leading zeros are required if the year value would otherwise have fewer than four digits; otherwise they are forbidden");
}
date[CY]= parseIntYear(buffer, i);
if (buffer.charAt(i)!='-') {
throw new RuntimeException("CCYY must be followed by '-' sign");
}
start = ++i;
i = start +2;
date[M]=parseInt(buffer, start, i);
return i; //fStart points right after the MONTH
}
/**
* Shared code from Date and YearMonth datatypes.
* Finds if time zone sign is present
*
* @param end
* @param date
* @exception RuntimeException
*/
protected void parseTimeZone (String buffer, int start, int end, int[] date, int[] timeZone) throws RuntimeException{
//fStart points right after the date
if ( start<end ) {
int sign = findUTCSign(buffer, start, end);
if ( sign<0 ) {
throw new RuntimeException ("Error in month parsing");
}
else {
getTimeZone(buffer, date, sign, end, timeZone);
}
}
}
/**
* Parses time zone: 'Z' or {+,-} followed by hh:mm
*
* @param data
* @param sign
* @exception RuntimeException
*/
protected void getTimeZone (String buffer, int[] data, int sign, int end, int[] timeZone) throws RuntimeException{
data[utc]=buffer.charAt(sign);
if ( buffer.charAt(sign) == 'Z' ) {
if (end>(++sign)) {
throw new RuntimeException("Error in parsing time zone");
}
return;
}
if ( sign<=(end-6) ) {
//parse [hh]
int stop = ++sign+2;
timeZone[hh]=parseInt(buffer, sign, stop);
if (buffer.charAt(stop++)!=':') {
throw new RuntimeException("Error in parsing time zone" );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -