📄 ipputilities.java
字号:
JobImpressionsCompleted.class); classesByName.put("job-media-sheets-completed", JobMediaSheetsCompleted.class); classesByName.put("multiple-operation-time-out", MultipleOperationTimeOut.class); // 4.2 job template attributes instanceByClass.put(JobPriority.class, new JobPrioritySupported(1)); instanceByClass.put(JobHoldUntil.class, new JobHoldUntilSupported("", null)); instanceByClass.put(JobSheets.class, new JobSheetsSupported("", null)); instanceByClass.put(MultipleDocumentHandling.class, MultipleDocumentHandlingSupported.SINGLE_DOCUMENT); instanceByClass.put(Copies.class, new CopiesSupported(1)); instanceByClass.put(Finishings.class, FinishingsSupported.BIND); instanceByClass.put(PageRanges.class, PageRangesSupported.SUPPORTED); instanceByClass.put(Sides.class, SidesSupported.DUPLEX); instanceByClass.put(NumberUp.class, new NumberUpSupported(1)); instanceByClass.put(OrientationRequested.class, OrientationRequestedSupported.LANDSCAPE); instanceByClass.put(Media.class, new MediaSupported("", null)); instanceByClass.put(PrinterResolution.class, new PrinterResolutionSupported(1,1,1)); instanceByClass.put(PrintQuality.class, PrintQualitySupported.DRAFT); // 4.4 printer attributes instanceByClass.put(Compression.class, CompressionSupported.COMPRESS); } private IppUtilities() { // not to be instantiated } /** * Returns the implementing class object for given * attribute name objects. * * @param name the attribute name * @return The <code>Class</code> object. */ public static Class getClass(String name) { return (Class) classesByName.get(name); } /** * Returns the name of the supported attribute * based on the given standard attribute category. * * @param clazz the standard attribute category * @return The name of the supported attribute category. */ public static String getSupportedAttrName(Class clazz) { return ((SupportedValuesAttribute) instanceByClass.get(clazz)).getName(); } /** * Returns the category of the supported attribute * based on the given standard attribute category. * * @param clazz the standard attribute category * @return The supported attribute category. */ public static Class getSupportedCategory(Class clazz) { return ((SupportedValuesAttribute) instanceByClass.get(clazz)).getCategory(); } /** * Helper method to convert to an int. * @param b the byte array * @return The converted int. */ public static int convertToInt(byte[] b) { return (((b[0] & 0xff) << 24) | ((b[1] & 0xff) << 16) | ((b[2] & 0xff) << 8) | (b[3] & 0xff)); } /** * Helper method to convert to an int. * @param b1 the 1th byte * @param b2 the 2th byte * @param b3 the 3th byte * @param b4 the 4th byte * @return The converted int. */ public static int convertToInt(byte b1, byte b2, byte b3, byte b4) { return (((b1 & 0xff) << 24) | ((b2 & 0xff) << 16) | ((b3 & 0xff) << 8) | (b4 & 0xff)); } /** * Helper method to convert to a short. * @param b1 the 1th byte * @param b2 the 2th byte * @return The converted short. */ public static short convertToShort(byte b1, byte b2) { return (short) ((b1 << 8) | (b2 & 0xff)); } /** * Instantiates an <code>EnumSyntax</code> based attribute with the given IPP * name and the given value (Enums maybe int or String based). * * @param name the attribute name of the subclass. * @param value the integer value of the specific enum. * @return The Attribute (a subclass of EnumSyntax) */ public static Attribute getEnumAttribute(String name, Object value) { Class attrClass = getClass(name); // There might be unknown enums we have no mapped class for if (attrClass == null) return null; try { Field[] fields = attrClass.getDeclaredFields(); for (int i = 0; i < fields.length; i++) { Field field = fields[i]; if (field.getType().equals(attrClass)) { EnumSyntax attr = (EnumSyntax) field.get(null); if (value instanceof Integer && attr.getValue() == ((Integer) value).intValue()) return (Attribute) attr; else if (value instanceof String && attr.toString().equals(value)) return (Attribute) attr; } } } catch (SecurityException e) { // should not happen } catch (IllegalArgumentException e) { // should not happen } catch (IllegalAccessException e) { // should not happen, all fields are public } return null; } /** * Instantiates an <code>IntegerSyntax</code> based attribute with the * given IPP name for the given int value. * * @param name the attribute name of the subclass. * @param value the integer value * @return The Attribute (a subclass of IntegerSyntax) */ public static Attribute getIntegerAttribute(String name, int value) { Class attrClass = getClass(name); // There might be unknown attributes we have no mapped class for if (attrClass == null) return null; try { INTEGER_ATT_VALUE[0] = new Integer(value); Constructor c = attrClass.getDeclaredConstructor(INTEGER_CLASS_ARRAY); return (Attribute) c.newInstance(INTEGER_ATT_VALUE); } catch (SecurityException e) { // should not happen } catch (NoSuchMethodException e) { // should not happen } catch (IllegalAccessException e) { // should not happen, all fields are public } catch (InstantiationException e) { // should not happen, all fields are public } catch (InvocationTargetException e) { // should not happen, all fields are public } return null; } /** * Instantiates an <code>TextSyntax</code> based attribute with the given * IPP name for the given text value (will be decoded). * * @param name the attribute name of the subclass. * @param tag the tag defined in {@link IppValueTag} * @param value the byte[] value to be decoded based on the tag value. * @return The Attribute (a subclass of TextSyntax) */ public static Attribute getTextAttribute(String name, byte tag, byte[] value) { // without language tag is rather easy - default locale if (tag == IppValueTag.NAME_WITHOUT_LANGUAGE || tag == IppValueTag.TEXT_WITHOUT_LANGUAGE) { TEXT_ATT_VALUE[0] = new String(value); TEXT_ATT_VALUE[1] = Locale.getDefault(); } else { short langLength = convertToShort(value[0], value[1]); byte[] tmp = new byte[langLength]; byte[] tmp2 = new byte[value.length - 4 - langLength]; System.arraycopy(value, 2, tmp, 0, langLength); // parse into language/region String language = new String(tmp); String text = new String(tmp2); Locale locale = null; if (language.length() > 2) locale = new Locale(language.substring(0, 2), language.substring(3)); else locale = new Locale(language); TEXT_ATT_VALUE[0] = text; TEXT_ATT_VALUE[1] = locale; } Class attrClass = getClass(name); // There might be unknown attributes we have no mapped class for if (attrClass == null) return null; try { Constructor c = attrClass.getDeclaredConstructor(TEXT_CLASS_ARRAY); return (Attribute) c.newInstance(TEXT_ATT_VALUE); } catch (SecurityException e) { // should not happen } catch (NoSuchMethodException e) { // should not happen } catch (IllegalAccessException e) { // should not happen, all fields are public } catch (InstantiationException e) { // should not happen, all fields are public } catch (InvocationTargetException e) { // should not happen, all fields are public } return null; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -