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