Annotation of java/classes/org/w3c/tools/resources/PropertiesAttribute.java, revision 1.1

1.1     ! bmahe       1: // PropertiesAttribute.java
        !             2: // $Id: PropertiesAttribute.java,v 1.1 1997/10/24 12:42:30 bmahe Exp $
        !             3: // (c) COPYRIGHT MIT and INRIA, 1996.
        !             4: // Please first read the full copyright statement in file COPYRIGHT.html
        !             5: 
        !             6: package org.w3c.tools.resources ;
        !             7: 
        !             8: import java.io.* ;
        !             9: import java.util.*;
        !            10: 
        !            11: import org.w3c.util.*;
        !            12: 
        !            13: /**
        !            14:  * The generic description of an PropertiesAttribute.
        !            15:  * A PropertiesAttribute instance holds a String to String mapping, it
        !            16:  * should be used only with care, since people may act on a reference to
        !            17:  * it.
        !            18:  */
        !            19: 
        !            20: public class PropertiesAttribute extends Attribute {
        !            21: 
        !            22:     /**
        !            23:      * Get the pickled length of that string.
        !            24:      */
        !            25: 
        !            26:     private int getPickleLength(String str) {
        !            27:        int    strlen = str.length() ;
        !            28:        int    utflen = 0 ;
        !            29:        
        !            30:        for (int i = 0 ; i < strlen ; i++) {
        !            31:            int c = str.charAt(i);
        !            32:            if ((c >= 0x0001) && (c <= 0x007F)) {
        !            33:                utflen++;
        !            34:            } else if (c > 0x07FF) {
        !            35:                utflen += 3;
        !            36:            } else {
        !            37:                utflen += 2;
        !            38:            }
        !            39:        }
        !            40:        return utflen + 2 ;
        !            41:     }
        !            42: 
        !            43:     /**
        !            44:      * Is the given object a valid PropertiesAttribute value ?
        !            45:      * @param obj The object to test.
        !            46:      * @return A boolean <strong>true</strong> if value is valid.
        !            47:      */
        !            48: 
        !            49:     public boolean checkValue(Object obj) {
        !            50:        return (obj == null) || (obj instanceof ArrayDictionary);
        !            51:     }
        !            52:      
        !            53:     /**
        !            54:      * Get the number of bytes required to save that attribute value.
        !            55:      * @param The value about to be pickled.
        !            56:      * @return The number of bytes needed to pickle that value.
        !            57:      */
        !            58: 
        !            59:     public final int getPickleLength(Object value) {
        !            60:        ArrayDictionary a = (ArrayDictionary) value;
        !            61:        Enumeration     e = a.keys();
        !            62:        int             s = 4;
        !            63:        while ( e.hasMoreElements() ) {
        !            64:            String key = (String) e.nextElement();
        !            65:            s += getPickleLength(key);
        !            66:            s += getPickleLength((String) a.get(key));
        !            67:        }
        !            68:        return s;
        !            69:     }
        !            70: 
        !            71:     /**
        !            72:      * Pickle a property list to the given output stream.
        !            73:      * @param out The output stream to pickle to.
        !            74:      * @param obj The object to pickle.
        !            75:      * @exception IOException If some IO error occured.
        !            76:      */
        !            77: 
        !            78:     public void pickle(DataOutputStream out, Object o) 
        !            79:        throws IOException
        !            80:     {
        !            81:        ArrayDictionary a = (ArrayDictionary) o;
        !            82:        Enumeration     e = a.keys();
        !            83:        int             s = a.size();
        !            84:        out.writeInt(s);
        !            85:        while (--s >= 0) {
        !            86:            String key = (String) e.nextElement();
        !            87:            out.writeUTF(key);
        !            88:            out.writeUTF((String) a.get(key));
        !            89:        }
        !            90:     }
        !            91: 
        !            92:     /**
        !            93:      * Unpickle an string from the given input stream.
        !            94:      * @param in The input stream to unpickle from.
        !            95:      * @return An instance of String.
        !            96:      * @exception IOException If some IO error occured.
        !            97:      */
        !            98: 
        !            99:     public Object unpickle (DataInputStream in) 
        !           100:        throws IOException
        !           101:     {
        !           102:        int             s = in.readInt();
        !           103:        ArrayDictionary a = new ArrayDictionary(s, 5);
        !           104:        while (--s >= 0) {
        !           105:            String k = in.readUTF();
        !           106:            String v = in.readUTF();
        !           107:            a.put(k, v);
        !           108:        }
        !           109:        return a;
        !           110:     }
        !           111:      
        !           112:     /**
        !           113:      * Create a description for a generic property list attribute.
        !           114:      * @param name The attribute name.
        !           115:      * @param def The default value for these attributes.
        !           116:      * @param flags The associated flags.
        !           117:      */
        !           118: 
        !           119:     public PropertiesAttribute(String name, String def, int flags) {
        !           120:        super(name, def, flags) ;
        !           121:        this.type = "org.w3c.util.ArrayDictionary";
        !           122:     }
        !           123: 
        !           124: }
        !           125: 

Webmaster