📄 timeattribute.java
字号:
/*
* @(#)TimeAttribute.java
*
* Copyright 2003-2006 Sun Microsystems, Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistribution of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistribution in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* Neither the name of Sun Microsystems, Inc. or the names of contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* This software is provided "AS IS," without a warranty of any kind. ALL
* EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
* ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN")
* AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
* AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
* DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
* REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
* INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
* OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
* EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
*
* You acknowledge that this software is not designed or intended for use in
* the design, construction, operation or maintenance of any nuclear facility.
*/
package com.sun.xacml.attr;
import com.sun.xacml.ParsingException;
import com.sun.xacml.ProcessingException;
import java.net.URI;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.TimeZone;
import org.w3c.dom.Node;
/**
* Representation of an xs:time value. This class supports parsing
* xs:time values. All objects of this class are immutable and
* thread-safe. The <code>Date</code> objects returned are not, but
* these objects are cloned before being returned.
*
* @since 1.0
* @author Steve Hanna
* @author Seth Proctor
*/
public class TimeAttribute extends AttributeValue
{
/**
* Official name of this type
*/
public static final String identifier =
"http://www.w3.org/2001/XMLSchema#time";
/**
* URI version of name for this type
* <p>
* This field is initialized by a static initializer so that
* we can catch any exceptions thrown by URI(String) and
* transform them into a RuntimeException, since this should
* never happen but should be reported properly if it ever does.
* <p>
* This object is used for synchronization whenever we need
* protection across this whole class.
*/
private static URI identifierURI;
/**
* RuntimeException that wraps an Exception thrown during the
* creation of identifierURI, null if none.
*/
private static RuntimeException earlyException;
/**
* Static initializer that initializes the identifierURI
* class field so that we can catch any exceptions thrown
* by URI(String) and transform them into a RuntimeException.
* Such exceptions should never happen but should be reported
* properly if they ever do.
*/
static {
try {
identifierURI = new URI(identifier);
} catch (Exception e) {
earlyException = new IllegalArgumentException();
earlyException.initCause(e);
}
};
/**
* Time zone value that indicates that the time zone was not
* specified.
*/
public static final int TZ_UNSPECIFIED = -1000000;
/**
* The time that this object represents in second resolution, in
* milliseconds GMT, with zero being midnight. If no time zone was
* specified, the local time zone is used to convert to milliseconds
* relative to GMT.
*/
private long timeGMT;
/**
* The number of nanoseconds beyond the time given by the timeGMT
* field. The XML Query document says that fractional seconds
* must be supported down to at least 100 nanosecond resolution.
* The Date class only supports milliseconds, so we include here
* support for nanosecond resolution.
*/
private int nanoseconds;
// NOTE: now that we're not using a Date object, the above two variables
// could be condensed, and the interface could be changed so we don't
// need to worry about tracking the time values separately
/**
* The time zone specified for this object (or TZ_UNSPECIFIED if
* unspecified). The offset to GMT, in minutes.
*/
private int timeZone;
/**
* The time zone actually used for this object (if it was
* originally unspecified, the default time zone used).
* The offset to GMT, in minutes.
*/
private int defaultedTimeZone;
/**
* Cached encoded value (null if not cached yet).
*/
private String encodedValue = null;
/**
* Creates a new <code>TimeAttribute</code> that represents
* the current time in the current time zone.
*/
public TimeAttribute() {
this(new Date());
}
/**
* Creates a new <code>TimeAttribute</code> that represents
* the given time but uses the default timezone and offset values.
*
* @param time a <code>Date</code> object representing the
* specified time down to second resolution. This
* date should have a date of 01/01/1970. If it does
* not, such a date will be forced. If this object
* has non-zero milliseconds, they are combined
* with the nanoseconds parameter.
*/
public TimeAttribute(Date time) {
super(identifierURI);
int currOffset = DateTimeAttribute.getDefaultTZOffset(time);
init(time, 0, currOffset, currOffset);
}
/**
* Creates a new <code>TimeAttribute</code> that represents
* the time supplied.
*
* @param time a <code>Date</code> object representing the
* specified time down to second resolution. This
* date should have a date of 01/01/1970. If it does
* not, such a date will be forced. If this object
* has non-zero milliseconds, they are combined
* with the nanoseconds parameter.
* @param nanoseconds the number of nanoseconds beyond the
* Date specified in the date parameter
* @param timeZone the time zone specified for this object
* (or TZ_UNSPECIFIED if unspecified). The
* offset to GMT, in minutes.
* @param defaultedTimeZone the time zone actually used for this
* object, which must be specified.
* The offset to GMT, in minutes.
*/
public TimeAttribute(Date time, int nanoseconds, int timeZone,
int defaultedTimeZone) {
super(identifierURI);
// if the timezone is unspecified, it's illegal for the defaulted
// timezone to also be unspecified
if ((timeZone == TZ_UNSPECIFIED) &&
(defaultedTimeZone == TZ_UNSPECIFIED))
throw new ProcessingException("default timezone must be specified"
+ "when a timezone is provided");
init(time, nanoseconds, timeZone, defaultedTimeZone);
}
/**
* Initialization code shared by constructors.
*
* @param date a <code>Date</code> object representing the
* specified time down to second resolution. This
* date should have a date of 01/01/1970. If it does
* not, such a date will be forced. If this object
* has non-zero milliseconds, they are combined
* with the nanoseconds parameter.
* @param nanoseconds the number of nanoseconds beyond the
* Date specified in the date parameter
* @param timeZone the time zone specified for this object
* (or TZ_UNSPECIFIED if unspecified). The
* offset to GMT, in minutes.
* @param defaultedTimeZone the time zone actually used for this
* object (if it was originally unspecified,
* the default time zone used).
* The offset to GMT, in minutes.
*/
private void init(Date date, int nanoseconds, int timeZone,
int defaultedTimeZone) {
// Shouldn't happen, but just in case...
if (earlyException != null)
throw earlyException;
// get a temporary copy of the date
Date tmpDate = (Date)(date.clone());
// Combine the nanoseconds so they are between 0 and 999,999,999
this.nanoseconds =
DateTimeAttribute.combineNanos(tmpDate, nanoseconds);
// now that the date has been (potentially) updated, store the time
this.timeGMT = tmpDate.getTime();
// keep track of the timezone values
this.timeZone = timeZone;
this.defaultedTimeZone = defaultedTimeZone;
// Check that the date is normalized to 1/1/70
if ((timeGMT >= DateAttribute.MILLIS_PER_DAY) || (timeGMT < 0)) {
timeGMT = timeGMT % DateAttribute.MILLIS_PER_DAY;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -