1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56: import ;
57: import ;
58: import ;
59: import ;
60: import ;
61: import ;
62: import ;
63: import ;
64: import ;
65: import ;
66: import ;
67: import ;
68: import ;
69: import ;
70: import ;
71: import ;
72: import ;
73: import ;
74: import ;
75: import ;
76: import ;
77: import ;
78:
79: import ;
80: import ;
81: import ;
82: import ;
83: import ;
84:
85: import ;
86: import ;
87: import ;
88: import ;
89: import ;
90: import ;
91: import ;
92: import ;
93: import ;
94: import ;
95: import ;
96: import ;
97: import ;
98: import ;
99: import ;
100: import ;
101: import ;
102: import ;
103: import ;
104: import ;
105: import ;
106: import ;
107: import ;
108: import ;
109: import ;
110: import ;
111: import ;
112: import ;
113: import ;
114: import ;
115: import ;
116: import ;
117: import ;
118: import ;
119: import ;
120: import ;
121: import ;
122: import ;
123: import ;
124: import ;
125: import ;
126: import ;
127: import ;
128: import ;
129: import ;
130: import ;
131: import ;
132: import ;
133: import ;
134: import ;
135:
136:
146: public final class IppUtilities
147: {
148:
149: private static Object[] INTEGER_ATT_VALUE = new Object[1];
150: private static Class<?>[] INTEGER_CLASS_ARRAY = new Class[] {int.class};
151: private static Object[] TEXT_ATT_VALUE = new Object[2];
152: private static Class<?>[] TEXT_CLASS_ARRAY = new Class[] {String.class, Locale.class};
153:
154:
155: private static HashMap<String,Class<? extends Attribute>> classesByName =
156: new HashMap<String,Class<? extends Attribute>>();
157:
158: private static HashMap<Class<? extends Attribute>,SupportedValuesAttribute> instanceByClass =
159: new HashMap<Class<? extends Attribute>,SupportedValuesAttribute>();
160:
161:
164: static
165: {
166:
167: classesByName.put(JobState.ABORTED.getName(), JobState.class);
168: classesByName.put(Sides.DUPLEX.getName(), Sides.class);
169: classesByName.put(SheetCollate.COLLATED.getName(), SheetCollate.class);
170: classesByName.put(Severity.ERROR.getName(), Severity.class);
171: classesByName.put(JobSheets.NONE.getName(), JobSheets.class);
172: classesByName.put(Finishings.BIND.getName(), Finishings.class);
173: classesByName.put(Fidelity.FIDELITY_FALSE.getName(), Fidelity.class);
174: classesByName.put(Compression.GZIP.getName(), Compression.class);
175: classesByName.put(Chromaticity.COLOR.getName(), Chromaticity.class);
176: classesByName.put(PrintQuality.DRAFT.getName(), PrintQuality.class);
177: classesByName.put(PrinterState.IDLE.getName(), PrinterState.class);
178: classesByName.put(SidesDefault.ONE_SIDED.getName(), SidesDefault.class);
179: classesByName.put(ReferenceUriSchemesSupported.FILE.getName(),
180: ReferenceUriSchemesSupported.class);
181: classesByName.put(PrinterStateReason.DOOR_OPEN.getName(),
182: PrinterStateReason.class);
183: classesByName.put(PresentationDirection.TOLEFT_TOTOP.getName(),
184: PresentationDirection.class);
185: classesByName.put(PDLOverrideSupported.ATTEMPTED.getName(),
186: PDLOverrideSupported.class);
187: classesByName.put(OrientationRequested.PORTRAIT.getName(),
188: OrientationRequested.class);
189: classesByName.put(MultipleDocumentHandling.SINGLE_DOCUMENT.getName(),
190: MultipleDocumentHandling.class);
191: classesByName.put(JobStateReason.JOB_QUEUED.getName(),
192: JobStateReason.class);
193: classesByName.put(UriAuthenticationSupported.NONE.getName(),
194: UriAuthenticationSupported.class);
195: classesByName.put(OperationsSupported.GET_JOBS.getName(),
196: OperationsSupported.class);
197: classesByName.put(UriSecuritySupported.NONE.getName(),
198: UriSecuritySupported.class);
199: classesByName.put(FinishingsSupported.NONE.getName(),
200: FinishingsSupported.class);
201: classesByName.put(FinishingsDefault.NONE.getName(),
202: FinishingsDefault.class);
203: classesByName.put(IppVersionsSupported.V_1_0.getName(),
204: IppVersionsSupported.class);
205: classesByName.put(MultipleDocumentHandlingSupported.SINGLE_DOCUMENT.getName(),
206: MultipleDocumentHandlingSupported.class);
207: classesByName.put(MultipleDocumentHandlingDefault.SINGLE_DOCUMENT.getName(),
208: MultipleDocumentHandlingDefault.class);
209: classesByName.put(CompressionSupported.NONE.getName(),
210: CompressionSupported.class);
211: classesByName.put(OrientationRequestedSupported.PORTRAIT.getName(),
212: OrientationRequestedSupported.class);
213: classesByName.put(OrientationRequestedDefault.PORTRAIT.getName(),
214: OrientationRequestedDefault.class);
215: classesByName.put(SidesSupported.ONE_SIDED.getName(),
216: SidesSupported.class);
217: classesByName.put(PrintQualityDefault.DRAFT.getName(),
218: PrintQualityDefault.class);
219: classesByName.put(PrintQualitySupported.DRAFT.getName(),
220: PrintQualitySupported.class);
221: classesByName.put(ReferenceUriSchemesSupported.FTP.getName(),
222: ReferenceUriSchemesSupported.class);
223:
224:
225: classesByName.put(ColorSupported.SUPPORTED.getName(), ColorSupported.class);
226: classesByName.put(PrinterIsAcceptingJobs.ACCEPTING_JOBS.getName(),
227: PrinterIsAcceptingJobs.class);
228: classesByName.put(MultipleDocumentJobsSupported.SUPPORTED.getName(),
229: MultipleDocumentJobsSupported.class);
230: classesByName.put(PageRangesSupported.SUPPORTED.getName(),
231: PageRangesSupported.class);
232:
233:
234: classesByName.put("media-default", MediaDefault.class);
235: classesByName.put("media-supported", MediaSupported.class);
236: classesByName.put("media", MediaSizeName.class);
237: classesByName.put("printer-location", PrinterLocation.class);
238: classesByName.put("printer-info", PrinterInfo.class);
239: classesByName.put("printer-make-and-model", PrinterMakeAndModel.class);
240: classesByName.put("printer-state-message", PrinterStateMessage.class);
241: classesByName.put("job-state-message", JobStateMessage.class);
242: classesByName.put("job-sheets-default", JobSheetsDefault.class);
243: classesByName.put("job-sheets-supported", JobSheetsSupported.class);
244: classesByName.put("job-name", JobName.class);
245: classesByName.put("printer-name", PrinterName.class);
246: classesByName.put("status-message", StatusMessage.class);
247: classesByName.put("detailed-status-message", DetailedStatusMessage.class);
248: classesByName.put("document-access-error", DocumentAccessError.class);
249: classesByName.put("output-device-assigned", OutputDeviceAssigned.class);
250: classesByName.put("job-hold-until-default", JobHoldUntilDefault.class);
251: classesByName.put("job-originating-user-name",
252: JobOriginatingUserName.class);
253: classesByName.put("job-hold-until-supported",
254: JobHoldUntilSupported.class);
255: classesByName.put("job-message-from-operator",
256: JobMessageFromOperator.class);
257: classesByName.put("printer-message-from-operator",
258: PrinterMessageFromOperator.class);
259: classesByName.put("job-detailed-status-messages",
260: JobDetailedStatusMessages.class);
261: classesByName.put("job-document-access-errors",
262: JobDocumentAccessErrors.class);
263:
264:
265: classesByName.put("copies-default", CopiesDefault.class);
266: classesByName.put("job-id", JobId.class);
267: classesByName.put("job-priority-supported", JobPrioritySupported.class);
268: classesByName.put("job-priority-default", JobPriorityDefault.class);
269: classesByName.put("number-up-supported", NumberUpSupported.class);
270: classesByName.put("number-up-default", NumberUpDefault.class);
271: classesByName.put("queued-job-count", QueuedJobCount.class);
272: classesByName.put("printer-up-time", PrinterUpTime.class);
273: classesByName.put("pages-per-minute", PagesPerMinute.class);
274: classesByName.put("pages-per-minute-color", PagesPerMinuteColor.class);
275: classesByName.put("job-k-octets-processed", JobKOctetsProcessed.class);
276: classesByName.put("number-of-intervening-jobs",
277: NumberOfInterveningJobs.class);
278: classesByName.put("job-impressions-completed",
279: JobImpressionsCompleted.class);
280: classesByName.put("job-media-sheets-completed",
281: JobMediaSheetsCompleted.class);
282: classesByName.put("multiple-operation-time-out",
283: MultipleOperationTimeOut.class);
284:
285:
286:
287: instanceByClass.put(JobPriority.class, new JobPrioritySupported(1));
288: instanceByClass.put(JobHoldUntil.class, new JobHoldUntilSupported("", null));
289: instanceByClass.put(JobSheets.class, new JobSheetsSupported("", null));
290: instanceByClass.put(MultipleDocumentHandling.class, MultipleDocumentHandlingSupported.SINGLE_DOCUMENT);
291: instanceByClass.put(Copies.class, new CopiesSupported(1));
292: instanceByClass.put(Finishings.class, FinishingsSupported.BIND);
293: instanceByClass.put(PageRanges.class, PageRangesSupported.SUPPORTED);
294: instanceByClass.put(Sides.class, SidesSupported.DUPLEX);
295: instanceByClass.put(NumberUp.class, new NumberUpSupported(1));
296: instanceByClass.put(OrientationRequested.class, OrientationRequestedSupported.LANDSCAPE);
297: instanceByClass.put(Media.class, new MediaSupported("", null));
298: instanceByClass.put(PrinterResolution.class, new PrinterResolutionSupported(1,1,1));
299: instanceByClass.put(PrintQuality.class, PrintQualitySupported.DRAFT);
300:
301:
302: instanceByClass.put(Compression.class, CompressionSupported.COMPRESS);
303: }
304:
305: private IppUtilities()
306: {
307:
308: }
309:
310:
317: public static Class<? extends Attribute> getClass(String name)
318: {
319: return classesByName.get(name);
320: }
321:
322:
329: public static String getSupportedAttrName(Class<? extends Attribute> clazz)
330: {
331: return instanceByClass.get(clazz).getName();
332: }
333:
334:
341: public static Class<? extends Attribute> getSupportedCategory(Class<? extends Attribute> clazz)
342: {
343: return instanceByClass.get(clazz).getCategory();
344: }
345:
346:
351: public static int convertToInt(byte[] b)
352: {
353: return (((b[0] & 0xff) << 24) | ((b[1] & 0xff) << 16)
354: | ((b[2] & 0xff) << 8) | (b[3] & 0xff));
355: }
356:
357:
365: public static int convertToInt(byte b1, byte b2, byte b3, byte b4)
366: {
367: return (((b1 & 0xff) << 24) | ((b2 & 0xff) << 16)
368: | ((b3 & 0xff) << 8) | (b4 & 0xff));
369: }
370:
371:
377: public static short convertToShort(byte b1, byte b2)
378: {
379: return (short) ((b1 << 8) | (b2 & 0xff));
380: }
381:
382:
390: public static Attribute getEnumAttribute(String name, Object value)
391: {
392: Class<?> attrClass = getClass(name);
393:
394:
395: if (attrClass == null)
396: return null;
397:
398: try
399: {
400: Field[] fields = attrClass.getDeclaredFields();
401: for (int i = 0; i < fields.length; i++)
402: {
403: Field field = fields[i];
404: if (field.getType().equals(attrClass))
405: {
406: EnumSyntax attr = (EnumSyntax) field.get(null);
407: if (value instanceof Integer
408: && attr.getValue() == ((Integer) value).intValue())
409: return (Attribute) attr;
410: else if (value instanceof String
411: && attr.toString().equals(value))
412: return (Attribute) attr;
413: }
414: }
415: }
416: catch (SecurityException e)
417: {
418:
419: }
420: catch (IllegalArgumentException e)
421: {
422:
423: }
424: catch (IllegalAccessException e)
425: {
426:
427: }
428:
429: return null;
430: }
431:
432:
433:
434:
442: public static Attribute getIntegerAttribute(String name, int value)
443: {
444: Class<?> attrClass = getClass(name);
445:
446:
447: if (attrClass == null)
448: return null;
449:
450: try
451: {
452: INTEGER_ATT_VALUE[0] = Integer.valueOf(value);
453: Constructor<?> c = attrClass.getDeclaredConstructor(INTEGER_CLASS_ARRAY);
454: return (Attribute) c.newInstance(INTEGER_ATT_VALUE);
455: }
456: catch (SecurityException e)
457: {
458:
459: }
460: catch (NoSuchMethodException e)
461: {
462:
463: }
464: catch (IllegalAccessException e)
465: {
466:
467: }
468: catch (InstantiationException e)
469: {
470:
471: }
472: catch (InvocationTargetException e)
473: {
474:
475: }
476:
477: return null;
478: }
479:
480:
489: public static Attribute getTextAttribute(String name, byte tag, byte[] value)
490: {
491:
492: if (tag == IppValueTag.NAME_WITHOUT_LANGUAGE
493: || tag == IppValueTag.TEXT_WITHOUT_LANGUAGE)
494: {
495: TEXT_ATT_VALUE[0] = new String(value);
496: TEXT_ATT_VALUE[1] = Locale.getDefault();
497: }
498: else
499: {
500: short langLength = convertToShort(value[0], value[1]);
501: byte[] tmp = new byte[langLength];
502: byte[] tmp2 = new byte[value.length - 4 - langLength];
503: System.arraycopy(value, 2, tmp, 0, langLength);
504:
505:
506: String language = new String(tmp);
507: String text = new String(tmp2);
508: Locale locale = null;
509:
510: if (language.length() > 2)
511: locale = new Locale(language.substring(0, 2), language.substring(3));
512: else
513: locale = new Locale(language);
514:
515: TEXT_ATT_VALUE[0] = text;
516: TEXT_ATT_VALUE[1] = locale;
517: }
518:
519: Class<?> attrClass = getClass(name);
520:
521:
522: if (attrClass == null)
523: return null;
524:
525: try
526: {
527: Constructor<?> c = attrClass.getDeclaredConstructor(TEXT_CLASS_ARRAY);
528: return (Attribute) c.newInstance(TEXT_ATT_VALUE);
529: }
530: catch (SecurityException e)
531: {
532:
533: }
534: catch (NoSuchMethodException e)
535: {
536:
537: }
538: catch (IllegalAccessException e)
539: {
540:
541: }
542: catch (InstantiationException e)
543: {
544:
545: }
546: catch (InvocationTargetException e)
547: {
548:
549: }
550:
551: return null;
552: }
553: }