]> git.ipfire.org Git - thirdparty/cups.git/blob - scripting/java/src/com/easysw/cups/IPPValue.java
Add missing files for test.
[thirdparty/cups.git] / scripting / java / src / com / easysw / cups / IPPValue.java
1 package com.easysw.cups;
2
3 /**
4 * @version 1.00 06-NOV-2002
5 * @author Apple Inc.
6 *
7 * Internet Printing Protocol definitions for the Common UNIX Printing
8 * System (CUPS).
9 *
10 * Copyright 2007 by Apple Inc.
11 * Copyright 1997-2002 by Easy Software Products.
12 *
13 * These coded instructions, statements, and computer programs are the
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/".
18 */
19
20 /**
21 * An <code>IPPValue</code> object is used to hold the
22 * different kinds of values in a generic object.
23 *
24 * @author TDB
25 * @version 1.0
26 * @since JDK1.3
27 */
28 import java.util.*;
29
30 public class IPPValue
31 {
32
33 int value_type; // Type of value for this object.
34
35 int integer_value; // Integer value
36 boolean boolean_value; // Boolean value
37
38
39 char date_value[]; // Date/time value
40 long unix_time; // Unix time ....
41
42 //
43 // Resolution Type
44 //
45 int xres;
46 int yres;
47 byte units;
48
49 //
50 // Range Type
51 //
52 int lower;
53 int upper;
54
55 //
56 // String Type
57 //
58 String charset;
59 String text;
60
61 //
62 // Unknown Type
63 //
64 int length;
65 char data[];
66
67
68 /**
69 * Byte constructor.
70 *
71 * @param <code>p_byte</code> Byte value.
72 */
73 public IPPValue( byte p_byte )
74 {
75 value_type = IPPDefs.TAG_INTEGER;
76 integer_value = (int)p_byte;
77 }
78
79 /**
80 * Short constructor.
81 *
82 * @param <code>p_short</code> Short value.
83 */
84 public IPPValue( short p_short )
85 {
86 value_type = IPPDefs.TAG_INTEGER;
87 integer_value = (int)p_short;
88 }
89
90 /**
91 * Integer constructor.
92 *
93 * @param <code>p_int</code> Integer value.
94 */
95 public IPPValue( int p_int )
96 {
97 value_type = IPPDefs.TAG_INTEGER;
98 integer_value = p_int;
99 }
100
101 /**
102 * Enum constructor.
103 *
104 * @param <code>p_int</code> Integer value - force to IPP enum.
105 */
106 public IPPValue( int p_int, boolean anything )
107 {
108 value_type = IPPDefs.TAG_ENUM;
109 integer_value = p_int;
110 }
111
112 /**
113 * Boolean constructor.
114 *
115 * @param <code>p_boolean</code> Boolean value.
116 */
117 public IPPValue( boolean p_boolean )
118 {
119 value_type = IPPDefs.TAG_BOOLEAN;
120 boolean_value = p_boolean;
121 }
122
123
124 /**
125 * Date constructor. Also set the <code>unix_time</code> member.
126 *
127 * @param <code>p_date[]</code> Character array with date value.
128 */
129 public IPPValue( char p_date[] )
130 {
131 value_type = IPPDefs.TAG_DATE;
132 date_value = p_date;
133 unix_time = IPPDateToTime();
134 }
135
136
137
138 /**
139 * String constructor. Set the <code>string</code> and
140 * <code>charset</code> values.
141 *
142 * @param <code>p_charset</code> Charset for string.
143 * @param <code>p_text</code> Text for string.
144 */
145 public IPPValue( String p_charset, String p_text )
146 {
147 value_type = IPPDefs.TAG_STRING;
148 charset = p_charset;
149 text = p_text;
150 }
151
152
153 /**
154 * Range constructor. Automatically swap as needed.
155 *
156 * @param <code>p_lower</code> Integer lower value.
157 * @param <code>p_upper</code> Integer upper value.
158 */
159 public IPPValue( int p_lower, int p_upper )
160 {
161 value_type = IPPDefs.TAG_RANGE;
162 if (p_lower < p_upper)
163 {
164 lower = p_lower;
165 upper = p_upper;
166 }
167 else
168 {
169 lower = p_upper;
170 upper = p_lower;
171 }
172 }
173
174
175 /**
176 * Resolution constructor.
177 *
178 * @param <code>p_units</code> Unit of measure.
179 * @param <code>p_xres</code> X resolution.
180 * @param <code>p_yres</code> Y resolution.
181 */
182 public IPPValue( byte p_units, int p_xres, int p_yres )
183 {
184 value_type = IPPDefs.TAG_RESOLUTION;
185 units = p_units;
186 xres = p_xres;
187 yres = p_yres;
188 }
189
190
191 /**
192 * Raw data constructor.
193 *
194 * @param <code>p_length</code> Size of array.
195 * @param <code>p_data[]</code> Data.
196 */
197 public IPPValue( int p_length, char p_data[] )
198 {
199 value_type = IPPDefs.TAG_UNKNOWN;
200 length = p_length;
201 data = p_data;
202 }
203
204
205
206 /**
207 * Convert an IPP Date value to Unix Time.
208 *
209 * @return <code>long</code> Unix time in seconds.
210 * @see <code>IPPCalender</code>
211 */
212 public long IPPDateToTime()
213 {
214
215 //
216 // Compute the offset from GMT in milliseconds.
217 //
218 int raw_offset = (((int)date_value[9] * 3600) + ((int)date_value[10] * 60)) * 1000;
219 if (date_value[8] == '-')
220 raw_offset = 0 - raw_offset;
221
222 //
223 // Get the timezone for that offset.
224 //
225 TimeZone tz = new SimpleTimeZone(raw_offset,"GMT");
226
227 //
228 // Create a subclassed gregorian calendar (sub classed so we have
229 // access to the getTimeInMillis() method).
230 //
231 IPPCalendar cl = new IPPCalendar();
232
233 int year = ((((int)date_value[0]) << 8) | (((int)date_value[1]) - 1900));
234 int month = ((int)date_value[2]) - 1;
235 int day = (int)date_value[3];
236 int hour = (int)date_value[4];
237 int minute = (int)date_value[5];
238 int second = (int)date_value[6];
239
240 //
241 // Now set the calendar to the matching time.
242 //
243 cl.setTimeZone( tz );
244 cl.set( year, month, day, hour, minute, second );
245
246 //
247 // And finally get the unix time.
248 //
249 long the_time = cl.getTimeInMillis();
250 the_time /= 1000;
251
252 return(the_time);
253 }
254
255 } // End of class