001: /*
002: * Copyright 2003 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025: package sun.print;
026:
027: import java.io.ByteArrayInputStream;
028:
029: public class AttributeClass {
030: private String myName;
031: private int myType;
032: private int nameLen;
033: private Object myValue;
034:
035: public static final int TAG_INT = 0x21;
036: public static final int TAG_BOOL = 0x22;
037: public static final int TAG_ENUM = 0x23;
038: public static final int TAG_OCTET = 0x30;
039: public static final int TAG_DATE = 0x31;
040: public static final int TAG_RESOLUTION = 0x32;
041: public static final int TAG_RANGE_INTEGER = 0x33;
042:
043: public static final int TAG_TEXT_LANGUAGE = 0x35;
044: public static final int TAG_NAME_LANGUAGE = 0x36;
045:
046: public static final int TAG_TEXT_WO_LANGUAGE = 0x41;
047: public static final int TAG_NAME_WO_LANGUAGE = 0x42;
048: public static final int TAG_KEYWORD = 0x44;
049: public static final int TAG_URI = 0x45;
050: public static final int TAG_CHARSET = 0x47;
051: public static final int TAG_NATURALLANGUAGE = 0x48;
052: public static final int TAG_MIME_MEDIATYPE = 0x49;
053: public static final int TAG_MEMBER_ATTRNAME = 0x4A;
054:
055: public static final AttributeClass ATTRIBUTES_CHARSET = new AttributeClass(
056: "attributes-charset", TAG_CHARSET, "utf-8");
057: public static final AttributeClass ATTRIBUTES_NATURAL_LANGUAGE = new AttributeClass(
058: "attributes-natural-language", TAG_NATURALLANGUAGE, "en");
059:
060: /*
061: * value passed in by IPPPrintService.readIPPResponse is a sequence
062: * of bytes with this format
063: * | length1 | byte1 | byte 2 | ... byten | length2 | byte1 ... byten |
064: * :
065: * | lengthN | byte1 ... byten | total number of values|
066: */
067: protected AttributeClass(String name, int type, Object value) {
068: myName = name;
069: myType = type;
070: nameLen = name.length();
071: myValue = value;
072: }
073:
074: public byte getType() {
075: return (byte) myType;
076: }
077:
078: public char[] getLenChars() {
079: char[] chars = new char[2];
080: chars[0] = 0;
081: chars[1] = (char) nameLen;
082: return chars;
083: }
084:
085: /**
086: * Returns raw data.
087: */
088: public Object getObjectValue() {
089: return myValue;
090: }
091:
092: /**
093: * Returns single int value.
094: */
095: public int getIntValue() {
096: byte[] bufArray = (byte[]) myValue;
097:
098: if (bufArray != null) {
099: byte[] buf = new byte[4];
100: for (int i = 0; i < 4; i++) {
101: buf[i] = bufArray[i + 1];
102: }
103:
104: return convertToInt(buf);
105: }
106: return 0;
107: }
108:
109: /**
110: * Returns array of int values.
111: */
112: public int[] getArrayOfIntValues() {
113:
114: byte[] bufArray = (byte[]) myValue;
115: if (bufArray != null) {
116:
117: //ArrayList valList = new ArrayList();
118: ByteArrayInputStream bufStream = new ByteArrayInputStream(
119: bufArray);
120: int available = bufStream.available();
121:
122: // total number of values is at the end of the stream
123: bufStream.mark(available);
124: bufStream.skip(available - 1);
125: int length = bufStream.read();
126: bufStream.reset();
127:
128: int[] valueArray = new int[length];
129: for (int i = 0; i < length; i++) {
130: // read length
131: int valLength = bufStream.read();
132: if (valLength != 4) {
133: // invalid data
134: return null;
135: }
136:
137: byte[] bufBytes = new byte[valLength];
138: bufStream.read(bufBytes, 0, valLength);
139: valueArray[i] = convertToInt(bufBytes);
140:
141: }
142: return valueArray;
143: }
144: return null;
145: }
146:
147: /**
148: * Returns 2 int values.
149: */
150: public int[] getIntRangeValue() {
151: int[] range = { 0, 0 };
152: byte[] bufArray = (byte[]) myValue;
153: if (bufArray != null) {
154: int nBytes = 4; // 32-bit signed integer
155: for (int j = 0; j < 2; j++) { // 2 set of integers
156: byte[] intBytes = new byte[nBytes];
157: // REMIND: # bytes should be 8
158: for (int i = 0; i < nBytes; i++) {
159: //+ 1 because the 1st byte is length
160: intBytes[i] = bufArray[i + (4 * j) + 1];
161: }
162: range[j] = convertToInt(intBytes);
163: }
164: }
165: return range;
166:
167: }
168:
169: /**
170: * Returns String value.
171: */
172: public String getStringValue() {
173: //assumes only 1 attribute value. Will get the first value
174: // if > 1.
175: String strVal = null;
176: byte[] bufArray = (byte[]) myValue;
177: if (bufArray != null) {
178: ByteArrayInputStream bufStream = new ByteArrayInputStream(
179: bufArray);
180:
181: int valLength = bufStream.read();
182:
183: byte[] strBytes = new byte[valLength];
184: bufStream.read(strBytes, 0, valLength);
185: try {
186: strVal = new String(strBytes, "UTF-8");
187: } catch (java.io.UnsupportedEncodingException uee) {
188: }
189: }
190: return strVal;
191: }
192:
193: /**
194: * Returns array of String values.
195: */
196: public String[] getArrayOfStringValues() {
197:
198: byte[] bufArray = (byte[]) myValue;
199: if (bufArray != null) {
200: ByteArrayInputStream bufStream = new ByteArrayInputStream(
201: bufArray);
202: int available = bufStream.available();
203:
204: // total number of values is at the end of the stream
205: bufStream.mark(available);
206: bufStream.skip(available - 1);
207: int length = bufStream.read();
208: bufStream.reset();
209:
210: String[] valueArray = new String[length];
211: for (int i = 0; i < length; i++) {
212: // read length
213: int valLength = bufStream.read();
214: byte[] bufBytes = new byte[valLength];
215: bufStream.read(bufBytes, 0, valLength);
216: try {
217: valueArray[i] = new String(bufBytes, "UTF-8");
218: } catch (java.io.UnsupportedEncodingException uee) {
219: }
220: }
221: return valueArray;
222: }
223: return null;
224: }
225:
226: /**
227: * Returns single byte value.
228: */
229: public byte getByteValue() {
230: byte[] bufArray = (byte[]) myValue;
231:
232: if ((bufArray != null) && (bufArray.length >= 2)) {
233: return bufArray[1];
234: }
235: return 0;
236: }
237:
238: /**
239: * Returns attribute name.
240: */
241: public String getName() {
242: return myName;
243: }
244:
245: public boolean equals(Object obj) {
246: return obj != null
247: && obj instanceof AttributeClass
248: && obj.toString().equals(
249: ((AttributeClass) obj).toString());
250: }
251:
252: public String toString() {
253: return myName;
254: }
255:
256: private int unsignedByteToInt(byte b) {
257: return (int) (b & 0xff);
258: }
259:
260: private int convertToInt(byte[] buf) {
261: int intVal = 0;
262: int pos = 0;
263: intVal += unsignedByteToInt(buf[pos++]) << 24;
264: intVal += unsignedByteToInt(buf[pos++]) << 16;
265: intVal += unsignedByteToInt(buf[pos++]) << 8;
266: intVal += unsignedByteToInt(buf[pos++]) << 0;
267: return intVal;
268: }
269: }
|