001: // PropertiesAttribute.java
002: // $Id: PropertiesAttribute.java,v 1.4 2000/08/16 21:37:56 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1996.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.tools.resources.upgrade;
007:
008: import java.io.DataInputStream;
009: import java.io.DataOutputStream;
010: import java.io.IOException;
011:
012: import java.util.Enumeration;
013:
014: import org.w3c.util.ArrayDictionary;
015:
016: /**
017: * The generic description of an PropertiesAttribute.
018: * A PropertiesAttribute instance holds a String to String mapping, it
019: * should be used only with care, since people may act on a reference to
020: * it.
021: */
022:
023: public class PropertiesAttribute extends Attribute {
024:
025: /**
026: * Get the pickled length of that string.
027: */
028:
029: private int getPickleLength(String str) {
030: int strlen = str.length();
031: int utflen = 0;
032:
033: for (int i = 0; i < strlen; i++) {
034: int c = str.charAt(i);
035: if ((c >= 0x0001) && (c <= 0x007F)) {
036: utflen++;
037: } else if (c > 0x07FF) {
038: utflen += 3;
039: } else {
040: utflen += 2;
041: }
042: }
043: return utflen + 2;
044: }
045:
046: /**
047: * Is the given object a valid PropertiesAttribute value ?
048: * @param obj The object to test.
049: * @return A boolean <strong>true</strong> if value is valid.
050: */
051:
052: public boolean checkValue(Object obj) {
053: return (obj == null) || (obj instanceof ArrayDictionary);
054: }
055:
056: /**
057: * Get the number of bytes required to save that attribute value.
058: * @param The value about to be pickled.
059: * @return The number of bytes needed to pickle that value.
060: */
061:
062: public final int getPickleLength(Object value) {
063: ArrayDictionary a = (ArrayDictionary) value;
064: Enumeration e = a.keys();
065: int s = 4;
066: while (e.hasMoreElements()) {
067: String key = (String) e.nextElement();
068: s += getPickleLength(key);
069: s += getPickleLength((String) a.get(key));
070: }
071: return s;
072: }
073:
074: /**
075: * Pickle a property list to the given output stream.
076: * @param out The output stream to pickle to.
077: * @param obj The object to pickle.
078: * @exception IOException If some IO error occured.
079: */
080:
081: public void pickle(DataOutputStream out, Object o)
082: throws IOException {
083: ArrayDictionary a = (ArrayDictionary) o;
084: Enumeration e = a.keys();
085: int s = a.size();
086: out.writeInt(s);
087: while (--s >= 0) {
088: String key = (String) e.nextElement();
089: out.writeUTF(key);
090: out.writeUTF((String) a.get(key));
091: }
092: }
093:
094: /**
095: * Unpickle an string from the given input stream.
096: * @param in The input stream to unpickle from.
097: * @return An instance of String.
098: * @exception IOException If some IO error occured.
099: */
100:
101: public Object unpickle(DataInputStream in) throws IOException {
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, Integer flags) {
120: super (name, def, flags);
121: this .type = "java.lang.String";
122: }
123:
124: }
|