]> git.ipfire.org Git - thirdparty/cups.git/blame - scripting/java/src/com/easysw/cups/IPPAttribute.java
Add missing files for test.
[thirdparty/cups.git] / scripting / java / src / com / easysw / cups / IPPAttribute.java
CommitLineData
ef416fc2 1package com.easysw.cups;
2
3/**
4 * @version 1.00 06-NOV-2002
bc44d920 5 * @author Apple Inc.
ef416fc2 6 *
7 * Internet Printing Protocol definitions for the Common UNIX Printing
8 * System (CUPS).
9 *
080811b1 10 * Copyright 2007-2008 by Apple Inc.
ef416fc2 11 * Copyright 1997-2002 by Easy Software Products.
12 *
13 * These coded instructions, statements, and computer programs are the
bc44d920 14 * property of Apple Inc. and are protected by Federal copyright
15 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
16 * which should have been included with this file. If this file is
17 * file is missing or damaged, see the license at "http://www.cups.org/".
ef416fc2 18 */
19/**
20 * An <code>IPPAttribute</code> object hold attributes for communicating
21 * messages to / from the CUPS server.
22 *
23 * @author TDB
24 * @version 1.0
25 * @since JDK1.3
26 */
27import java.util.*;
28
29public class IPPAttribute
30{
31 int group_tag;
32 int value_tag;
33 String name;
34 List values;
35
36
37
38 public IPPAttribute( int p_group_tag, int p_value_tag,
39 String p_name )
40 {
41 group_tag = p_group_tag;
42 value_tag = p_value_tag;
43 name = p_name;
44 values = new ArrayList();
45
46 } // End of IPPAttribute constructor.
47
48
49 //
50 // Add a boolean value to the objects values list.
51 //
52 public boolean addBoolean( boolean p_bool )
53 {
54 IPPValue val = new IPPValue( p_bool );
55 values.add(val);
56 return(true);
57 }
58
59
60 //
61 // Add a set of boolean values to the objects values list.
62 //
63 public boolean addBooleans( boolean p_bools[] )
64 {
65 if (p_bools.length < 1)
66 return(false);
67
68 for (int i=0; i < p_bools.length; i++ )
69 {
70 IPPValue val = new IPPValue( p_bools[i] );
71 values.add(val);
72 }
73 return(true);
74 }
75
76
77 //
78 // Add an enum value to the objects values list.
79 //
80 public boolean addEnum( int p_int )
81 {
82 IPPValue val = new IPPValue( p_int, true );
83 values.add(val);
84 return(true);
85 }
86
87
88 //
89 // Add an integer value to the objects values list.
90 //
91 public boolean addInteger( int p_int )
92 {
93 IPPValue val = new IPPValue( p_int );
94 values.add(val);
95 return(true);
96 }
97
98
99 //
100 // Add a set of integer values to the objects values list.
101 //
102 public boolean addIntegers( int p_ints[] )
103 {
104 if (p_ints.length < 1)
105 return(false);
106
107 for (int i=0; i < p_ints.length; i++ )
108 {
109 IPPValue val = new IPPValue( p_ints[i] );
110 values.add(val);
111 }
112 return(true);
113 }
114
115
116 //
117 // Add a string value to the objects values list.
118 //
119 public boolean addString( String p_charset, String p_text )
120 {
121 String l_value;
122 String final_value;
123
124 //
125 // Force the value to English for POSIX locale.
126 //
127 if ((value_tag == IPPDefs.TAG_LANGUAGE) && (p_text == "C"))
128 {
129 l_value = "en";
130 }
131 else
132 {
133 l_value = p_text;
134 }
135
136
137 //
138 // Convert language values to lowercase and _ to - as needed.
139 //
140 if ((value_tag == IPPDefs.TAG_LANGUAGE) ||
141 (value_tag == IPPDefs.TAG_CHARSET))
142 {
143 StringBuffer temp = new StringBuffer(l_value.length());
144 char c;
145 for (int i = 0; i < l_value.length(); i++)
146 {
147 c = l_value.charAt(i);
148 if (c == '_')
149 c = '-';
150 else if (Character.isUpperCase(c))
151 c = Character.toLowerCase(c);
152 temp.append(c);
153 }
154 final_value = temp.toString();
155 }
156 else
157 {
158 final_value = l_value;
159 }
160 IPPValue val = new IPPValue( p_charset, final_value );
161 values.add(val);
162 return(true);
163 }
164
165
166 //
167 // Add a set of strings to the objects values list.
168 //
169 public boolean addStrings( String p_charset, String p_texts[] )
170 {
171 if (p_texts.length < 1)
172 return(false);
173
174 //
175 // Just call the singular string method to save on coding.
176 //
177 for (int i=0; i < p_texts.length; i++ )
178 {
179 addString( p_charset, p_texts[i] );
180 }
181 return(true);
182 }
183
184 //
185 // Add an ipp date value to the objects values list.
186 //
187 public boolean addDate( char p_date[] )
188 {
189 IPPValue val = new IPPValue( p_date );
190 values.add(val);
191 return(true);
192 }
193
194 //
195 // Add a range value to the objects values list.
196 //
197 public boolean addRange( int p_lower, int p_upper )
198 {
199 IPPValue val = new IPPValue( p_lower, p_upper );
200 values.add(val);
201 return(true);
202 }
203
204
205 //
206 // Add a set of range values to the objects values list.
207 //
208 public boolean addRanges( int p_lower[], int p_upper[] )
209 {
210 //
211 // Avoid index out of bounds errors.
212 //
213 if (p_lower.length != p_upper.length)
214 return(false);
215
216 for (int i=0; i < p_lower.length; i++ )
217 addRange( p_lower[i], p_upper[i] );
218 return(true);
219 }
220
221
222 //
223 // Add a resolution value to the objects values list.
224 //
225 public boolean addResolution( byte p_units, int p_xres, int p_yres )
226 {
227 IPPValue val = new IPPValue( p_units, p_xres, p_yres );
228 values.add(val);
229 return(true);
230 }
231
232
233 //
234 // Add a set of resolution values to the objects values list.
235 //
236 public boolean addResolutions( byte p_units, int p_xres[], int p_yres[] )
237 {
238 if (p_xres.length != p_yres.length)
239 return(false);
240
241 for (int i=0; i < p_xres.length; i++)
242 addResolution( p_units, p_xres[i], p_yres[i] );
243 return(true);
244 }
245
246
247 //
248 // Set the attribute as a separator.
249 //
250 public boolean addSeparator()
251 {
252 value_tag = IPPDefs.TAG_ZERO;
253 group_tag = IPPDefs.TAG_ZERO;
254 return(true);
255 }
256
257
258
259 //
260 // Calculate the size in bytes for an IPP requests attributes.
261 //
262 public int sizeInBytes(int last_group)
263 {
264 IPPValue val;
265 int bytes = 0; // Start with one for the group tag.
080811b1
MS
266 Charset utf8 = Charset::forName("UTF-8");
267 ByteBuffer temp;
268
ef416fc2 269 //
270 // Add 1 if first time, or group tag changes.
271 //
272 if (last_group != group_tag)
273 bytes ++;
274
275 bytes ++; // Add 1 for the value tag.
276 bytes += 2; // Add 2 for the name length field.
277 bytes += name.length(); // Add the length of the name.
278
279 for (int i=0; i < values.size(); i++ )
280 {
281 val = (IPPValue)values.get(i);
282
283 if (i > 0)
284 {
285 // If an array of values, add 1 for another value tag, plus
286 // 2 for zero length name.
287 //
288 bytes += 3;
289 }
290
291 switch (value_tag)
292 {
293 case IPPDefs.TAG_INTEGER :
294 case IPPDefs.TAG_ENUM :
295 bytes += 2;
296 bytes += 4;
297 break;
298
299 case IPPDefs.TAG_BOOLEAN :
300 bytes += 2;
301 bytes ++;
302 break;
303
304 case IPPDefs.TAG_TEXT:
305 case IPPDefs.TAG_NAME:
306 case IPPDefs.TAG_KEYWORD:
307 case IPPDefs.TAG_STRING:
308 case IPPDefs.TAG_URI:
309 case IPPDefs.TAG_URISCHEME:
310 case IPPDefs.TAG_CHARSET:
311 case IPPDefs.TAG_LANGUAGE:
312 case IPPDefs.TAG_MIMETYPE:
080811b1 313 temp = utf8.encode(val.text);
ef416fc2 314 bytes += 2;
080811b1
MS
315 if (temp.capacity() > 32767)
316 bytes += 32767;
317 else
318 bytes += temp.capacity();
ef416fc2 319 break;
320
321 case IPPDefs.TAG_DATE :
322 bytes += 2;
323 bytes += 11;
324 break;
325
326 case IPPDefs.TAG_RESOLUTION :
327 bytes += 2;
328 bytes += 9;
329 break;
330
331 case IPPDefs.TAG_RANGE :
332 bytes += 2;
333 bytes += 8;
334 break;
335
336 case IPPDefs.TAG_TEXTLANG :
337 case IPPDefs.TAG_NAMELANG :
080811b1 338 temp = utf8.encode(val.text);
ef416fc2 339 bytes += 6; // 2 overall len, 2 charset len, 2 text len
080811b1
MS
340 bytes += val.charset.length();
341 if (temp.capacity() > 32767)
342 bytes += 32767;
343 else
344 bytes += temp.capacity();
ef416fc2 345 break;
346
347 default :
348 bytes += 2;
349 if (val.data != null)
350 bytes += val.data.length;
351 break;
352 }
353 }
354
355 // bytes++; // 1 byte for end of values tag.
356
357 return(bytes);
358
359 } // End of IPPAttribute.sizeInBytes()
360
361
362 //
363 // Get the characters of an attribute
364 //
365 public byte[] getBytes( int sz, int last_group )
366 {
367 IPPValue val;
368
369 int i,j, n;
370 int bi = 0; // Start with one for the group tag.
371 byte[] bytes = new byte[sz];
080811b1
MS
372 Charset utf8 = Charset::forName("UTF-8");
373 ByteBuffer temp;
ef416fc2 374
375 if (group_tag != last_group)
376 {
377 bytes[bi++] = (byte)group_tag;
378 last_group = group_tag;
379 }
380 bytes[bi++] = (byte)value_tag;
381
382 bytes[bi++] = (byte)((name.length() & 0xff00) >> 8);
383 bytes[bi++] = (byte)(name.length() & 0xff);
384 for (j=0; j < name.length(); j++)
385 bytes[bi++] = (byte)name.charAt(j);
386
387 for (i=0; i < values.size(); i++ )
388 {
389 if (i > 0)
390 {
391 bytes[bi++] = (byte)value_tag;
392 bytes[bi++] = (byte)0;
393 bytes[bi++] = (byte)0;
394 }
395
396 val = (IPPValue)values.get(i);
397 switch (value_tag)
398 {
399 case IPPDefs.TAG_INTEGER :
400 case IPPDefs.TAG_ENUM :
401 bytes[bi++] = (byte)0;
402 bytes[bi++] = (byte)4;
403 bytes[bi++] = (byte)((val.integer_value & 0xff000000) >> 24);
404 bytes[bi++] = (byte)((val.integer_value & 0xff0000) >> 16);
405 bytes[bi++] = (byte)((val.integer_value & 0xff00) >> 8);
406 bytes[bi++] = (byte)(val.integer_value & 0xff);
407 break;
408
409 case IPPDefs.TAG_BOOLEAN :
410 bytes[bi++] = (byte)0;
411 bytes[bi++] = (byte)1;
412 if (val.boolean_value)
413 bytes[bi++] = (byte)1;
414 else
415 bytes[bi++] = (byte)0;
416 break;
417
418 case IPPDefs.TAG_TEXT :
419 case IPPDefs.TAG_NAME :
420 case IPPDefs.TAG_KEYWORD :
421 case IPPDefs.TAG_STRING :
422 case IPPDefs.TAG_URI :
423 case IPPDefs.TAG_URISCHEME :
424 case IPPDefs.TAG_CHARSET :
425 case IPPDefs.TAG_LANGUAGE :
426 case IPPDefs.TAG_MIMETYPE :
080811b1
MS
427 temp = utf8.encode(val.text);
428 n = temp.capacity();
429
430 if (n > 32767)
431 n = 32767;
432
433 bytes[bi++] = (byte)((n & 0x7f00) >> 8);
434 bytes[bi++] = (byte)(n & 0xff);
435 temp.get(bytes, bi, n);
436 bi += n;
ef416fc2 437 break;
438
439 case IPPDefs.TAG_DATE:
440 bytes[bi++] = (byte)0;
441 bytes[bi++] = (byte)11;
442 for (j=0; j < 11; j++)
443 bytes[bi++] = (byte)val.date_value[j];
444 break;
445
446 case IPPDefs.TAG_RESOLUTION :
447 bytes[bi++] = (byte)0;
448 bytes[bi++] = (byte)9;
449 bytes[bi++] = (byte)((val.xres & 0xff000000) >> 24);
450 bytes[bi++] = (byte)((val.xres & 0xff0000) >> 16);
451 bytes[bi++] = (byte)((val.xres & 0xff00) >> 8);
452 bytes[bi++] = (byte)(val.xres & 0xff);
453 bytes[bi++] = (byte)((val.yres & 0xff000000) >> 24);
454 bytes[bi++] = (byte)((val.yres & 0xff0000) >> 16);
455 bytes[bi++] = (byte)((val.yres & 0xff00) >> 8);
456 bytes[bi++] = (byte)(val.yres & 0xff);
457 bytes[bi++] = (byte)val.units;
458 break;
459
460 case IPPDefs.TAG_RANGE :
461 bytes[bi++] = (byte)0;
462 bytes[bi++] = (byte)8;
463 bytes[bi++] = (byte)((val.lower & 0xff000000) >> 24);
464 bytes[bi++] = (byte)((val.lower & 0xff0000) >> 16);
465 bytes[bi++] = (byte)((val.lower & 0xff00) >> 8);
466 bytes[bi++] = (byte)(val.lower & 0xff);
467 bytes[bi++] = (byte)((val.upper & 0xff000000) >> 24);
468 bytes[bi++] = (byte)((val.upper & 0xff0000) >> 16);
469 bytes[bi++] = (byte)((val.upper & 0xff00) >> 8);
470 bytes[bi++] = (byte)(val.upper & 0xff);
471 break;
472
473 case IPPDefs.TAG_TEXTLANG :
474 case IPPDefs.TAG_NAMELANG :
080811b1
MS
475 temp = utf8.encode(val.text);
476 n = temp.capacity() + val.charset.length() + 4;
477
478 if (n > 32767)
479 n = 32767;
480
481 bytes[bi++] = (byte)((n & 0x7f00) >> 8);
ef416fc2 482 bytes[bi++] = (byte)(n & 0xff);
483
484 n = val.charset.length();
080811b1 485 bytes[bi++] = (byte)((n & 0x7f00) >> 8);
ef416fc2 486 bytes[bi++] = (byte)(n & 0xff);
487 for (j=0; j < val.charset.length(); j++)
488 bytes[bi++] = (byte)val.charset.charAt(j);
080811b1
MS
489
490 n = temp.capacity();
491
492 if (n > 32767)
493 n = 32767;
494
495 bytes[bi++] = (byte)((n & 0x7f00) >> 8);
ef416fc2 496 bytes[bi++] = (byte)(n & 0xff);
080811b1
MS
497 temp.get(bytes, bi, n);
498 bi += n;
ef416fc2 499 break;
500
501 default :
502 if (val.data != null)
503 {
504 n = val.data.length;
505 bytes[bi++] = (byte)((n & 0xff00) >> 8);
506 bytes[bi++] = (byte)(n & 0xff);
507 for (j=0; j < val.data.length; j++)
508 bytes[bi++] = (byte)val.data[j];
509 }
510 break;
511 }
512 }
513
514 return(bytes);
515
516 } // End of IPPAttribute.getBytes()
517
518
519
520
521 //
522 //
523 //
524 public void dump_values()
525 {
526 IPPValue val;
527
528 if ((values == null) || (values.size() < 1))
529 {
530 System.out.println( " ---- Separator ---- \n");
531 return;
532 }
533
534 for (int i=0; i < values.size(); i++ )
535 {
536 val = (IPPValue)values.get(i);
537
538 System.out.println("ATTR GTAG: " + group_tag );
539 System.out.println("ATTR VTAG: " + value_tag );
540 System.out.println("ATTR NAME: " + name );
541
542 // switch (value_tag & ~IPPDefs.TAG_COPY)
543 switch (value_tag)
544 {
545 case IPPDefs.TAG_INTEGER :
546 case IPPDefs.TAG_ENUM :
547 System.out.println( " INTEGER: " + val.integer_value );
548 break;
549
550 case IPPDefs.TAG_BOOLEAN :
551 System.out.println( " BOOLEAN: " + val.boolean_value );
552 break;
553
554 case IPPDefs.TAG_TEXT :
555 case IPPDefs.TAG_NAME :
556 case IPPDefs.TAG_KEYWORD :
557 case IPPDefs.TAG_STRING :
558 case IPPDefs.TAG_URI :
559 case IPPDefs.TAG_URISCHEME :
560 case IPPDefs.TAG_CHARSET :
561 case IPPDefs.TAG_LANGUAGE :
562 case IPPDefs.TAG_MIMETYPE :
563 System.out.println( " CHARSET: " + val.charset +
564 " TEXT: " + val.text );
565 break;
566
567 case IPPDefs.TAG_DATE :
568 System.out.println( " DATE: " + val.unix_time );
569 break;
570
571 case IPPDefs.TAG_RESOLUTION :
572 System.out.println( " UNITS: " + val.units +
573 " XRES: " + val.xres +
574 " YRES: " + val.yres );
575 break;
576
577 case IPPDefs.TAG_RANGE :
578 System.out.println( " LOWER: " + val.lower +
579 " UPPER: " + val.upper );
580 break;
581
582 case IPPDefs.TAG_TEXTLANG :
583 case IPPDefs.TAG_NAMELANG :
584 System.out.println( " CHARSET: " + val.charset +
585 " TEXT: " + val.text );
586 break;
587
588 case IPPDefs.TAG_ZERO:
589 System.out.println( " ---- Separator ---- \n");
590 break;
591
592 default :
593 break;
594 }
595 }
596 return;
597
598 }
599
600
601
602
603} // End of IPPAttribute class
604
605
606
607