// IPTemplatesAttribute.java
// $Id: IPTemplatesAttribute.java,v 1.5 1997/01/17 09:48:47 abaird Exp $
// (c) COPYRIGHT MIT and INRIA, 1996.
// Please first read the full copyright statement in file COPYRIGHT.html

package w3c.jigsaw.auth ;

import java.io.*;

import w3c.tools.store.*;
import w3c.jigsaw.resources.* ;
import w3c.jigsaw.formedit.FormFieldRegistry;

/**
 * The IPTemplates attribute description.
 * Maintains a list of IP templates (short arrays, to allow for the splash).
 */

public class IPTemplatesAttribute extends Attribute {

    static {
	// Register this attribute's editor:
	FormFieldRegistry.registerField("w3c.jigsaw.auth.IPTemplatesAttribute"
					, "w3c.jigsaw.auth.IPTemplatesField");
    }

    /**
     * Is the given value a valid IPTemplates value ?
     * @param obj The object to test.
     * @exception IllegalAttributeAccess If the provided value doesn't pass the
     *    test.
     */

    public boolean checkValue(Object obj) {
	return ((obj == null) || (obj instanceof short[][])) ;
    }

    /**
     * Get the number of bytes required to save that attribute value.
     * @param The value about to be pickled.
     * @return The number of bytes needed to pickle that value.
     */

    public final int getPickleLength(Object value) {
	return ((short[][]) value).length * 2 + 4;
    }

    /**
     * Pickle an array of IP templates  to the given output stream.
     * @param out The output stream to pickle to.
     * @param obj The object to pickle.
     * @exception IOException If some IO error occured.
     */

    public void pickle(DataOutputStream out, Object obj) 
	throws IOException
    {
	short ips[][] = (short[][]) obj ;
	out.writeInt(ips.length) ;
	for (int i = 0 ; i < ips.length ; i++) {
	    out.writeShort(ips[i][0]) ;
	    out.writeShort(ips[i][1]) ;
	    out.writeShort(ips[i][2]) ;
	    out.writeShort(ips[i][3]) ;
	}
    }

    /**
     * Unpickle an array of IP templates from the given input stream.
     * @param in The input stream to unpickle from.
     * @return An instance of short[][].
     * @exception IOException If some IO error occured.
     */

    public Object unpickle (DataInputStream in) 
	throws IOException
    {
	int   cnt     = in.readInt() ;
	short ips[][] = new short[cnt][] ;

	for (int i = 0 ; i < cnt ; i++) {
	    ips[i] = new short[4] ;
	    ips[i][0] = in.readShort() ;
	    ips[i][1] = in.readShort() ;
	    ips[i][2] = in.readShort() ;
	    ips[i][3] = in.readShort() ;
	}
	return ips ;
    }
     
    public IPTemplatesAttribute(String name, short defs[][], int flags) {
	super(name, defs, flags) ;
	this.type = "short[][]";
    }



}
