blob: 93d19c2947820344f5204cfd02301f9eda0f75d4 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2003 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25package sun.print;
26
27import java.io.ByteArrayInputStream;
28
29public class AttributeClass {
30 private String myName;
31 private int myType;
32 private int nameLen;
33 private Object myValue;
34
35 public static final int TAG_INT = 0x21;
36 public static final int TAG_BOOL = 0x22;
37 public static final int TAG_ENUM = 0x23;
38 public static final int TAG_OCTET = 0x30;
39 public static final int TAG_DATE = 0x31;
40 public static final int TAG_RESOLUTION = 0x32;
41 public static final int TAG_RANGE_INTEGER = 0x33;
42
43 public static final int TAG_TEXT_LANGUAGE = 0x35;
44 public static final int TAG_NAME_LANGUAGE = 0x36;
45
46 public static final int TAG_TEXT_WO_LANGUAGE = 0x41;
47 public static final int TAG_NAME_WO_LANGUAGE = 0x42;
48 public static final int TAG_KEYWORD = 0x44;
49 public static final int TAG_URI = 0x45;
50 public static final int TAG_CHARSET = 0x47;
51 public static final int TAG_NATURALLANGUAGE = 0x48;
52 public static final int TAG_MIME_MEDIATYPE = 0x49;
53 public static final int TAG_MEMBER_ATTRNAME = 0x4A;
54
55
56 public static final AttributeClass ATTRIBUTES_CHARSET =
57 new AttributeClass("attributes-charset",
58 TAG_CHARSET, "utf-8");
59 public static final AttributeClass ATTRIBUTES_NATURAL_LANGUAGE =
60 new AttributeClass("attributes-natural-language",
61 TAG_NATURALLANGUAGE, "en");
62
63 /*
64 * value passed in by IPPPrintService.readIPPResponse is a sequence
65 * of bytes with this format
66 * | length1 | byte1 | byte 2 | ... byten | length2 | byte1 ... byten |
67 * :
68 * | lengthN | byte1 ... byten | total number of values|
69 */
70 protected AttributeClass(String name, int type, Object value) {
71 myName = name;
72 myType = type;
73 nameLen = name.length();
74 myValue = value;
75 }
76
77 public byte getType() {
78 return (byte)myType;
79 }
80
81 public char[] getLenChars() {
82 char[] chars = new char[2];
83 chars[0] = 0;
84 chars[1] = (char)nameLen;
85 return chars;
86 }
87
88 /**
89 * Returns raw data.
90 */
91 public Object getObjectValue() {
92 return myValue;
93 }
94
95 /**
96 * Returns single int value.
97 */
98 public int getIntValue() {
99 byte[] bufArray = (byte[])myValue;
100
101 if (bufArray != null) {
102 byte[] buf = new byte[4];
103 for (int i=0; i<4; i++) {
104 buf[i] = bufArray[i+1];
105 }
106
107 return convertToInt(buf);
108 }
109 return 0;
110 }
111
112 /**
113 * Returns array of int values.
114 */
115 public int[] getArrayOfIntValues() {
116
117 byte[] bufArray = (byte[])myValue;
118 if (bufArray != null) {
119
120 //ArrayList valList = new ArrayList();
121 ByteArrayInputStream bufStream =
122 new ByteArrayInputStream(bufArray);
123 int available = bufStream.available();
124
125 // total number of values is at the end of the stream
126 bufStream.mark(available);
127 bufStream.skip(available-1);
128 int length = bufStream.read();
129 bufStream.reset();
130
131 int[] valueArray = new int[length];
132 for (int i = 0; i < length; i++) {
133 // read length
134 int valLength = bufStream.read();
135 if (valLength != 4) {
136 // invalid data
137 return null;
138 }
139
140 byte[] bufBytes = new byte[valLength];
141 bufStream.read(bufBytes, 0, valLength);
142 valueArray[i] = convertToInt(bufBytes);
143
144 }
145 return valueArray;
146 }
147 return null;
148 }
149
150 /**
151 * Returns 2 int values.
152 */
153 public int[] getIntRangeValue() {
154 int[] range = {0, 0};
155 byte[] bufArray = (byte[])myValue;
156 if (bufArray != null) {
157 int nBytes = 4; // 32-bit signed integer
158 for (int j=0; j<2; j++) { // 2 set of integers
159 byte[] intBytes = new byte[nBytes];
160 // REMIND: # bytes should be 8
161 for (int i=0; i< nBytes; i++) {
162 //+ 1 because the 1st byte is length
163 intBytes[i] = bufArray[i+(4*j)+1];
164 }
165 range[j] = convertToInt(intBytes);
166 }
167 }
168 return range;
169
170 }
171
172 /**
173 * Returns String value.
174 */
175 public String getStringValue() {
176 //assumes only 1 attribute value. Will get the first value
177 // if > 1.
178 String strVal = null;
179 byte[] bufArray = (byte[])myValue;
180 if (bufArray != null) {
181 ByteArrayInputStream bufStream =
182 new ByteArrayInputStream(bufArray);
183
184 int valLength = bufStream.read();
185
186 byte[] strBytes = new byte[valLength];
187 bufStream.read(strBytes, 0, valLength);
188 try {
189 strVal = new String(strBytes, "UTF-8");
190 } catch (java.io.UnsupportedEncodingException uee) {
191 }
192 }
193 return strVal;
194 }
195
196
197 /**
198 * Returns array of String values.
199 */
200 public String[] getArrayOfStringValues() {
201
202 byte[] bufArray = (byte[])myValue;
203 if (bufArray != null) {
204 ByteArrayInputStream bufStream =
205 new ByteArrayInputStream(bufArray);
206 int available = bufStream.available();
207
208 // total number of values is at the end of the stream
209 bufStream.mark(available);
210 bufStream.skip(available-1);
211 int length = bufStream.read();
212 bufStream.reset();
213
214 String[] valueArray = new String[length];
215 for (int i = 0; i < length; i++) {
216 // read length
217 int valLength = bufStream.read();
218 byte[] bufBytes = new byte[valLength];
219 bufStream.read(bufBytes, 0, valLength);
220 try {
221 valueArray[i] = new String(bufBytes, "UTF-8");
222 } catch (java.io.UnsupportedEncodingException uee) {
223 }
224 }
225 return valueArray;
226 }
227 return null;
228 }
229
230
231 /**
232 * Returns single byte value.
233 */
234 public byte getByteValue() {
235 byte[] bufArray = (byte[])myValue;
236
237 if ((bufArray != null) && (bufArray.length>=2)) {
238 return bufArray[1];
239 }
240 return 0;
241 }
242
243 /**
244 * Returns attribute name.
245 */
246 public String getName() {
247 return myName;
248 }
249
250 public boolean equals(Object obj) {
251 return
252 obj != null &&
253 obj instanceof AttributeClass &&
254 obj.toString().equals (((AttributeClass) obj).toString());
255 }
256
257 public String toString() {
258 return myName;
259 }
260
261 private int unsignedByteToInt(byte b) {
262 return (int) (b & 0xff);
263 }
264
265 private int convertToInt(byte[] buf) {
266 int intVal = 0;
267 int pos = 0;
268 intVal+= unsignedByteToInt(buf[pos++]) << 24;
269 intVal+= unsignedByteToInt(buf[pos++]) << 16;
270 intVal+= unsignedByteToInt(buf[pos++]) << 8;
271 intVal+= unsignedByteToInt(buf[pos++]) << 0;
272 return intVal;
273 }
274}