]> 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
bc44d920 5 * @author Apple Inc.
ef416fc2 6 *
7 * Internet Printing Protocol definitions for the Common UNIX Printing
8 * System (CUPS).
9 *
bc44d920 10 * Copyright 2007 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.
266
267 //
268 // Add 1 if first time, or group tag changes.
269 //
270 if (last_group != group_tag)
271 bytes ++;
272
273 bytes ++; // Add 1 for the value tag.
274 bytes += 2; // Add 2 for the name length field.
275 bytes += name.length(); // Add the length of the name.
276
277 for (int i=0; i < values.size(); i++ )
278 {
279 val = (IPPValue)values.get(i);
280
281 if (i > 0)
282 {
283 // If an array of values, add 1 for another value tag, plus
284 // 2 for zero length name.
285 //
286 bytes += 3;
287 }
288
289 switch (value_tag)
290 {
291 case IPPDefs.TAG_INTEGER :
292 case IPPDefs.TAG_ENUM :
293 bytes += 2;
294 bytes += 4;
295 break;
296
297 case IPPDefs.TAG_BOOLEAN :
298 bytes += 2;
299 bytes ++;
300 break;
301
302 case IPPDefs.TAG_TEXT:
303 case IPPDefs.TAG_NAME:
304 case IPPDefs.TAG_KEYWORD:
305 case IPPDefs.TAG_STRING:
306 case IPPDefs.TAG_URI:
307 case IPPDefs.TAG_URISCHEME:
308 case IPPDefs.TAG_CHARSET:
309 case IPPDefs.TAG_LANGUAGE:
310 case IPPDefs.TAG_MIMETYPE:
311 bytes += 2;
312 bytes += val.text.length();
313 break;
314
315 case IPPDefs.TAG_DATE :
316 bytes += 2;
317 bytes += 11;
318 break;
319
320 case IPPDefs.TAG_RESOLUTION :
321 bytes += 2;
322 bytes += 9;
323 break;
324
325 case IPPDefs.TAG_RANGE :
326 bytes += 2;
327 bytes += 8;
328 break;
329
330 case IPPDefs.TAG_TEXTLANG :
331 case IPPDefs.TAG_NAMELANG :
332 bytes += 6; // 2 overall len, 2 charset len, 2 text len
333 bytes += val.charset.length() +
334 val.text.length();
335 break;
336
337 default :
338 bytes += 2;
339 if (val.data != null)
340 bytes += val.data.length;
341 break;
342 }
343 }
344
345 // bytes++; // 1 byte for end of values tag.
346
347 return(bytes);
348
349 } // End of IPPAttribute.sizeInBytes()
350
351
352 //
353 // Get the characters of an attribute
354 //
355 public byte[] getBytes( int sz, int last_group )
356 {
357 IPPValue val;
358
359 int i,j, n;
360 int bi = 0; // Start with one for the group tag.
361 byte[] bytes = new byte[sz];
362
363 if (group_tag != last_group)
364 {
365 bytes[bi++] = (byte)group_tag;
366 last_group = group_tag;
367 }
368 bytes[bi++] = (byte)value_tag;
369
370 bytes[bi++] = (byte)((name.length() & 0xff00) >> 8);
371 bytes[bi++] = (byte)(name.length() & 0xff);
372 for (j=0; j < name.length(); j++)
373 bytes[bi++] = (byte)name.charAt(j);
374
375 for (i=0; i < values.size(); i++ )
376 {
377 if (i > 0)
378 {
379 bytes[bi++] = (byte)value_tag;
380 bytes[bi++] = (byte)0;
381 bytes[bi++] = (byte)0;
382 }
383
384 val = (IPPValue)values.get(i);
385 switch (value_tag)
386 {
387 case IPPDefs.TAG_INTEGER :
388 case IPPDefs.TAG_ENUM :
389 bytes[bi++] = (byte)0;
390 bytes[bi++] = (byte)4;
391 bytes[bi++] = (byte)((val.integer_value & 0xff000000) >> 24);
392 bytes[bi++] = (byte)((val.integer_value & 0xff0000) >> 16);
393 bytes[bi++] = (byte)((val.integer_value & 0xff00) >> 8);
394 bytes[bi++] = (byte)(val.integer_value & 0xff);
395 break;
396
397 case IPPDefs.TAG_BOOLEAN :
398 bytes[bi++] = (byte)0;
399 bytes[bi++] = (byte)1;
400 if (val.boolean_value)
401 bytes[bi++] = (byte)1;
402 else
403 bytes[bi++] = (byte)0;
404 break;
405
406 case IPPDefs.TAG_TEXT :
407 case IPPDefs.TAG_NAME :
408 case IPPDefs.TAG_KEYWORD :
409 case IPPDefs.TAG_STRING :
410 case IPPDefs.TAG_URI :
411 case IPPDefs.TAG_URISCHEME :
412 case IPPDefs.TAG_CHARSET :
413 case IPPDefs.TAG_LANGUAGE :
414 case IPPDefs.TAG_MIMETYPE :
415 bytes[bi++] = (byte)((val.text.length() & 0xff00) >> 8);
416 bytes[bi++] = (byte)(val.text.length() & 0xff);
417 for (j=0; j < val.text.length(); j++)
418 {
419 bytes[bi++] = (byte)val.text.charAt(j);
420 }
421 break;
422
423 case IPPDefs.TAG_DATE:
424 bytes[bi++] = (byte)0;
425 bytes[bi++] = (byte)11;
426 for (j=0; j < 11; j++)
427 bytes[bi++] = (byte)val.date_value[j];
428 break;
429
430 case IPPDefs.TAG_RESOLUTION :
431 bytes[bi++] = (byte)0;
432 bytes[bi++] = (byte)9;
433 bytes[bi++] = (byte)((val.xres & 0xff000000) >> 24);
434 bytes[bi++] = (byte)((val.xres & 0xff0000) >> 16);
435 bytes[bi++] = (byte)((val.xres & 0xff00) >> 8);
436 bytes[bi++] = (byte)(val.xres & 0xff);
437 bytes[bi++] = (byte)((val.yres & 0xff000000) >> 24);
438 bytes[bi++] = (byte)((val.yres & 0xff0000) >> 16);
439 bytes[bi++] = (byte)((val.yres & 0xff00) >> 8);
440 bytes[bi++] = (byte)(val.yres & 0xff);
441 bytes[bi++] = (byte)val.units;
442 break;
443
444 case IPPDefs.TAG_RANGE :
445 bytes[bi++] = (byte)0;
446 bytes[bi++] = (byte)8;
447 bytes[bi++] = (byte)((val.lower & 0xff000000) >> 24);
448 bytes[bi++] = (byte)((val.lower & 0xff0000) >> 16);
449 bytes[bi++] = (byte)((val.lower & 0xff00) >> 8);
450 bytes[bi++] = (byte)(val.lower & 0xff);
451 bytes[bi++] = (byte)((val.upper & 0xff000000) >> 24);
452 bytes[bi++] = (byte)((val.upper & 0xff0000) >> 16);
453 bytes[bi++] = (byte)((val.upper & 0xff00) >> 8);
454 bytes[bi++] = (byte)(val.upper & 0xff);
455 break;
456
457 case IPPDefs.TAG_TEXTLANG :
458 case IPPDefs.TAG_NAMELANG :
459 n = val.charset.length() +
460 val.text.length() + 4;
461 bytes[bi++] = (byte)((n & 0xff00) >> 8);
462 bytes[bi++] = (byte)(n & 0xff);
463
464 n = val.charset.length();
465 bytes[bi++] = (byte)((n & 0xff00) >> 8);
466 bytes[bi++] = (byte)(n & 0xff);
467 for (j=0; j < val.charset.length(); j++)
468 bytes[bi++] = (byte)val.charset.charAt(j);
469
470 n = val.text.length();
471 bytes[bi++] = (byte)((n & 0xff00) >> 8);
472 bytes[bi++] = (byte)(n & 0xff);
473 for (j=0; j < (byte)val.text.length(); j++)
474 bytes[bi++] = (byte)val.text.charAt(j);
475
476 break;
477
478 default :
479 if (val.data != null)
480 {
481 n = val.data.length;
482 bytes[bi++] = (byte)((n & 0xff00) >> 8);
483 bytes[bi++] = (byte)(n & 0xff);
484 for (j=0; j < val.data.length; j++)
485 bytes[bi++] = (byte)val.data[j];
486 }
487 break;
488 }
489 }
490
491 return(bytes);
492
493 } // End of IPPAttribute.getBytes()
494
495
496
497
498 //
499 //
500 //
501 public void dump_values()
502 {
503 IPPValue val;
504
505 if ((values == null) || (values.size() < 1))
506 {
507 System.out.println( " ---- Separator ---- \n");
508 return;
509 }
510
511 for (int i=0; i < values.size(); i++ )
512 {
513 val = (IPPValue)values.get(i);
514
515 System.out.println("ATTR GTAG: " + group_tag );
516 System.out.println("ATTR VTAG: " + value_tag );
517 System.out.println("ATTR NAME: " + name );
518
519 // switch (value_tag & ~IPPDefs.TAG_COPY)
520 switch (value_tag)
521 {
522 case IPPDefs.TAG_INTEGER :
523 case IPPDefs.TAG_ENUM :
524 System.out.println( " INTEGER: " + val.integer_value );
525 break;
526
527 case IPPDefs.TAG_BOOLEAN :
528 System.out.println( " BOOLEAN: " + val.boolean_value );
529 break;
530
531 case IPPDefs.TAG_TEXT :
532 case IPPDefs.TAG_NAME :
533 case IPPDefs.TAG_KEYWORD :
534 case IPPDefs.TAG_STRING :
535 case IPPDefs.TAG_URI :
536 case IPPDefs.TAG_URISCHEME :
537 case IPPDefs.TAG_CHARSET :
538 case IPPDefs.TAG_LANGUAGE :
539 case IPPDefs.TAG_MIMETYPE :
540 System.out.println( " CHARSET: " + val.charset +
541 " TEXT: " + val.text );
542 break;
543
544 case IPPDefs.TAG_DATE :
545 System.out.println( " DATE: " + val.unix_time );
546 break;
547
548 case IPPDefs.TAG_RESOLUTION :
549 System.out.println( " UNITS: " + val.units +
550 " XRES: " + val.xres +
551 " YRES: " + val.yres );
552 break;
553
554 case IPPDefs.TAG_RANGE :
555 System.out.println( " LOWER: " + val.lower +
556 " UPPER: " + val.upper );
557 break;
558
559 case IPPDefs.TAG_TEXTLANG :
560 case IPPDefs.TAG_NAMELANG :
561 System.out.println( " CHARSET: " + val.charset +
562 " TEXT: " + val.text );
563 break;
564
565 case IPPDefs.TAG_ZERO:
566 System.out.println( " ---- Separator ---- \n");
567 break;
568
569 default :
570 break;
571 }
572 }
573 return;
574
575 }
576
577
578
579
580} // End of IPPAttribute class
581
582
583
584