001: // StringArrayAttribute.java
002: // $Id: StringArrayAttribute.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: /**
013: * The generic description of an StringArrayAttribute.
014: */
015:
016: public class StringArrayAttribute extends Attribute {
017:
018: /**
019: * Turn a StringArray attribute into a String.
020: * We use the <em>normal</em> property convention, which is to separate
021: * each item with a <strong>|</strong>.
022: * @return A String based encoding for that value.
023: */
024:
025: public String stringify(Object value) {
026: if ((value == null) || (!(value instanceof String[])))
027: return null;
028: String as[] = (String[]) value;
029: StringBuffer sb = new StringBuffer();
030: for (int i = 0; i < as.length; i++) {
031: if (i > 0)
032: sb.append('|');
033: sb.append(as[i]);
034: }
035: return sb.toString();
036: }
037:
038: /**
039: * Is the given object a valid StringArrayAttribute value ?
040: * @param obj The object to test.
041: * @return A boolean <strong>true</strong> if okay.
042: */
043:
044: public boolean checkValue(Object obj) {
045: return (obj instanceof String[]);
046: }
047:
048: /**
049: * Get the number of bytes required to save that attribute value.
050: * @param The value about to be pickled.
051: * @return The number of bytes needed to pickle that value.
052: */
053:
054: public int getPickleLength(Object value) {
055: String strs[] = (String[]) value;
056: int sz = 4;
057: for (int n = 0; n < strs.length; n++) {
058: String str = strs[n];
059: int strlen = str.length();
060: int utflen = 0;
061:
062: for (int i = 0; i < strlen; i++) {
063: int c = str.charAt(i);
064: if ((c >= 0x0001) && (c <= 0x007F)) {
065: utflen++;
066: } else if (c > 0x07FF) {
067: utflen += 3;
068: } else {
069: utflen += 2;
070: }
071: }
072: sz += (utflen + 2);
073: }
074: return sz;
075: }
076:
077: /**
078: * Pickle a String array to the given output stream.
079: * @param out The output stream to pickle to.
080: * @param obj The object to pickle.
081: * @exception IOException If some IO error occured.
082: */
083:
084: public void pickle(DataOutputStream out, Object sa)
085: throws IOException {
086: String strs[] = (String[]) sa;
087: out.writeInt(strs.length);
088: for (int i = 0; i < strs.length; i++)
089: out.writeUTF(strs[i]);
090: }
091:
092: /**
093: * Unpickle an String array from the given input stream.
094: * @param in The input stream to unpickle from.
095: * @return An instance of String[].
096: * @exception IOException If some IO error occured.
097: */
098:
099: public Object unpickle(DataInputStream in) throws IOException {
100: int cnt = in.readInt();
101: String strs[] = new String[cnt];
102: for (int i = 0; i < cnt; i++)
103: strs[i] = in.readUTF();
104: return strs;
105: }
106:
107: public StringArrayAttribute(String name, String def[], Integer flags) {
108: super (name, def, flags);
109: this .type = "java.lang.String[]";
110: }
111:
112: }
|