www.pudn.com > cryptix-asn1-0.1.11.zip > GeneralizedTime.java


/* $Id: GeneralizedTime.java,v 1.4 2001/05/26 07:10:24 raif Exp $ 
 * 
 * Copyright (C) 1997-2001 The Cryptix Foundation Limited. All rights reserved. 
 * 
 * Use, modification, copying and distribution of this software is subject to 
 * the terms and conditions of the Cryptix General Licence. You should have 
 * received a copy of the Cryptix General Licence along with this library; if 
 * not, you can download a copy from http://www.cryptix.org/ 
 */ 
package cryptix.asn1.lang; 
 
import cryptix.asn1.io.ASNReader; 
import cryptix.asn1.io.ASNWriter; 
import cryptix.asn1.io.BlankElementException; 
import cryptix.asn1.io.ElementNotFoundException; 
 
import org.apache.log4j.Category; 
 
import java.io.EOFException; 
import java.io.IOException; 
import java.util.Date; 
 
/** 
 * The basic implementation of an ASN.1 GeneralizedTime type. The value of such 
 * a type is a java.util.Date. A convenience method to return the value 
 * as a java.util.Date is supplied.

* * @version $Revision: 1.4 $ * @author Raif S. Naffah */ public class GeneralizedTime extends Type implements IType { // Constants and vars // ....................................................................... static Category cat = Category.getInstance(UTCTime.class.getName()); // Constructor(s) // ....................................................................... public GeneralizedTime() { super("", new Tag(Tag.GENERALIZED_TIME)); } public GeneralizedTime(String name) { super(name, new Tag(Tag.GENERALIZED_TIME)); } public GeneralizedTime(String name, Tag tag) { super(name, tag); } public GeneralizedTime(String name, Date value) { this(name, new Tag(Tag.GENERALIZED_TIME), value); } /** * Constructs a new instance of a GeneralizedTime type, given a designated * Tag and a designated initial value. * * @param name the name of this instance. * @param tag the designated Tag value. * @param value the designated initial value. * @exception ClassCastException if the designated value is not a Date. */ public GeneralizedTime(String name, Tag tag, Object value) { super(name, tag); if (value != null) if (value instanceof Date) value(new Date(((Date) value).getTime())); else throw new ClassCastException(); if (this.value != null) defaultValue(this.value); } // Class methods // ....................................................................... /** * Returns a new instance of this type with a trivial name and the * designated value. * * @param value a designated initial value for the new instance. * @return a new instance with the designated value. */ public static GeneralizedTime getInstance(Date value) { return new GeneralizedTime("", value); } // Redefinition of methods in superclass Type // ....................................................................... /** * Decodes a GeneralizedTime from an input stream. * * @param is the ASN.1 stream to read from. * @exception IOException if an exception occurs during the operation. */ public void decode(ASNReader is) throws IOException { String cn = this.getClass().getName(); cat.debug("==> "+cn+".decode()"); is.mark(Integer.MAX_VALUE); try { Date result = is.decodeGeneralizedTime(this); if (result == null) throw new ElementNotFoundException(cn); else this.value(result); } catch (IOException x) { cat.warn("Exception ("+String.valueOf(x)+") encountered while decoding a "+cn); if (x instanceof ASNException || x instanceof EOFException) { cat.warn("Resetting stream..."); is.reset(); } throw x; } cat.debug("<== "+cn+".decode()"); } /** * Encodes a GeneralizedTime to an output stream. * * @param os the ASN.1 stream to write to. * @exception IOException if an exception occurs during the operation. */ public void encode(ASNWriter os) throws IOException { String cn = this.getClass().getName(); cat.debug("==> "+cn+".encode()"); Object val = this.value(); if (val != null) os.encodeGeneralizedTime(this, (Date) val); else { val = this.defaultValue(); if (val != null) { cat.info("Encoding default value for "+cn); os.encodeGeneralizedTime(this, (Date) val); } else if (!this.isOptional()) throw new BlankElementException(cn); // else { // cat.warn("Encoding a NULL for "+cn); // new Null(this.name()).encode(os); // } } cat.debug("<== "+cn+".encode()"); } // other instance methods // ....................................................................... /** * Convenience method to facilitate type casting. * * @return the current value as a java.math.BigInteger. */ public Date dateValue() { return (Date) this.value; } /** * Returns true if the designated value is equal to the value of * this instance.

* * @param obj a value to compare with the value of this instance. * @return true if this instance has an equal, non-null value to * the designated one; false otherwise. */ protected boolean sameValue(Object obj) { Date val = this.dateValue(); return (val == null) ? false : val.equals(obj); } // java.lang.Object overloaded methods // ....................................................................... /** * Returns a string representation of this instance. * * @return a string representation of this instance. */ public String toString() { StringBuffer sb = new StringBuffer("-- "); if (value != null) sb.append(String.valueOf(dateValue())); else sb.append("N/A"); return sb.toString(); } }