File:  [Public] / java / classes / org / w3c / tools / resources / PropertiesAttribute.java
Revision 1.6: download - view: text, annotated - select for diffs
Fri Oct 18 13:42:26 2013 UTC (12 years, 2 months ago) by ylafon
Branches: MAIN
CVS tags: HEAD
generics + raw types + serializer

// PropertiesAttribute.java
// $Id: PropertiesAttribute.java,v 1.6 2013/10/18 13:42:26 ylafon Exp $
// (c) COPYRIGHT MIT and INRIA, 1996.
// Please first read the full copyright statement in file COPYRIGHT.html

package org.w3c.tools.resources;

import java.util.Enumeration;
import org.w3c.util.ArrayDictionary;

/**
 * The generic description of an PropertiesAttribute.
 * A PropertiesAttribute instance holds a String to String mapping, it
 * should be used only with care, since people may act on a reference to
 * it.
 */

public class PropertiesAttribute extends ArrayAttribute {

    private static final long serialVersionUID = 6772824316326397015L;

    /**
     * Create a description for a generic property list attribute.
     *
     * @param name  The attribute name.
     * @param def   The default value for these attributes.
     * @param flags The associated flags.
     */

    public PropertiesAttribute(String name, String def, int flags) {
        super(name, def, flags);
        this.type = "java.lang.String".intern();
    }

    public PropertiesAttribute() {
        super();
        this.type = "java.lang.String".intern();
    }

    /**
     * Is the given object a valid PropertiesAttribute value ?
     *
     * @param obj The object to test.
     * @return A boolean <strong>true</strong> if value is valid.
     */

    public boolean checkValue(Object obj) {
        return (obj == null) || (obj instanceof ArrayDictionary);
    }

    /**
     * Unpickle an attribute array from a string array.
     *
     * @param array the String array
     * @return a Object array
     */
    public Object unpickle(String array[]) {
        if (array.length < 1)
            return null;
        ArrayDictionary<String, String> a = new ArrayDictionary<String, String>(array.length, 5);
        for (String encoded : array) {
            int idx = encoded.indexOf('=');
            if (idx != -1) {
                String key = encoded.substring(0, idx);
                String value = encoded.substring(idx + 1);
                a.put(key, value);
            }
        }
        return a;
    }

    /**
     * Pickle an attribute array into a String array.
     *
     * @param o the attribute array
     * @return a String array
     */
    public String[] pickle(Object o) {
        ArrayDictionary<String, String> a = (ArrayDictionary<String, String>) o;
        Enumeration<String> e = a.keys();
        int len = a.size();
        String array[] = new String[len];

        for (int i = 0; i < len; i++) {
            String key = e.nextElement();
            array[i] = key + "=" + a.get(key);
        }
        return array;
    }

}



Webmaster