0N/A/*
2362N/A * Copyright (c) 2003, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/Apackage sun.print;
0N/A
0N/Aimport java.io.ByteArrayInputStream;
0N/A
0N/Apublic class AttributeClass {
0N/A private String myName;
0N/A private int myType;
0N/A private int nameLen;
0N/A private Object myValue;
0N/A
286N/A public static final int TAG_UNSUPPORTED_VALUE = 0x10;
0N/A public static final int TAG_INT = 0x21;
0N/A public static final int TAG_BOOL = 0x22;
0N/A public static final int TAG_ENUM = 0x23;
0N/A public static final int TAG_OCTET = 0x30;
0N/A public static final int TAG_DATE = 0x31;
0N/A public static final int TAG_RESOLUTION = 0x32;
0N/A public static final int TAG_RANGE_INTEGER = 0x33;
0N/A
0N/A public static final int TAG_TEXT_LANGUAGE = 0x35;
0N/A public static final int TAG_NAME_LANGUAGE = 0x36;
0N/A
0N/A public static final int TAG_TEXT_WO_LANGUAGE = 0x41;
0N/A public static final int TAG_NAME_WO_LANGUAGE = 0x42;
0N/A public static final int TAG_KEYWORD = 0x44;
0N/A public static final int TAG_URI = 0x45;
0N/A public static final int TAG_CHARSET = 0x47;
0N/A public static final int TAG_NATURALLANGUAGE = 0x48;
0N/A public static final int TAG_MIME_MEDIATYPE = 0x49;
0N/A public static final int TAG_MEMBER_ATTRNAME = 0x4A;
0N/A
0N/A
0N/A public static final AttributeClass ATTRIBUTES_CHARSET =
0N/A new AttributeClass("attributes-charset",
0N/A TAG_CHARSET, "utf-8");
0N/A public static final AttributeClass ATTRIBUTES_NATURAL_LANGUAGE =
0N/A new AttributeClass("attributes-natural-language",
0N/A TAG_NATURALLANGUAGE, "en");
0N/A
0N/A /*
0N/A * value passed in by IPPPrintService.readIPPResponse is a sequence
0N/A * of bytes with this format
0N/A * | length1 | byte1 | byte 2 | ... byten | length2 | byte1 ... byten |
0N/A * :
0N/A * | lengthN | byte1 ... byten | total number of values|
0N/A */
0N/A protected AttributeClass(String name, int type, Object value) {
0N/A myName = name;
0N/A myType = type;
0N/A nameLen = name.length();
0N/A myValue = value;
0N/A }
0N/A
0N/A public byte getType() {
0N/A return (byte)myType;
0N/A }
0N/A
0N/A public char[] getLenChars() {
0N/A char[] chars = new char[2];
0N/A chars[0] = 0;
0N/A chars[1] = (char)nameLen;
0N/A return chars;
0N/A }
0N/A
0N/A /**
0N/A * Returns raw data.
0N/A */
0N/A public Object getObjectValue() {
0N/A return myValue;
0N/A }
0N/A
0N/A /**
0N/A * Returns single int value.
0N/A */
0N/A public int getIntValue() {
0N/A byte[] bufArray = (byte[])myValue;
0N/A
0N/A if (bufArray != null) {
0N/A byte[] buf = new byte[4];
0N/A for (int i=0; i<4; i++) {
0N/A buf[i] = bufArray[i+1];
0N/A }
0N/A
0N/A return convertToInt(buf);
0N/A }
0N/A return 0;
0N/A }
0N/A
0N/A /**
0N/A * Returns array of int values.
0N/A */
0N/A public int[] getArrayOfIntValues() {
0N/A
0N/A byte[] bufArray = (byte[])myValue;
0N/A if (bufArray != null) {
0N/A
0N/A //ArrayList valList = new ArrayList();
0N/A ByteArrayInputStream bufStream =
0N/A new ByteArrayInputStream(bufArray);
0N/A int available = bufStream.available();
0N/A
0N/A // total number of values is at the end of the stream
0N/A bufStream.mark(available);
0N/A bufStream.skip(available-1);
0N/A int length = bufStream.read();
0N/A bufStream.reset();
0N/A
0N/A int[] valueArray = new int[length];
0N/A for (int i = 0; i < length; i++) {
0N/A // read length
0N/A int valLength = bufStream.read();
0N/A if (valLength != 4) {
0N/A // invalid data
0N/A return null;
0N/A }
0N/A
0N/A byte[] bufBytes = new byte[valLength];
0N/A bufStream.read(bufBytes, 0, valLength);
0N/A valueArray[i] = convertToInt(bufBytes);
0N/A
0N/A }
0N/A return valueArray;
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Returns 2 int values.
0N/A */
0N/A public int[] getIntRangeValue() {
0N/A int[] range = {0, 0};
0N/A byte[] bufArray = (byte[])myValue;
0N/A if (bufArray != null) {
0N/A int nBytes = 4; // 32-bit signed integer
0N/A for (int j=0; j<2; j++) { // 2 set of integers
0N/A byte[] intBytes = new byte[nBytes];
0N/A // REMIND: # bytes should be 8
0N/A for (int i=0; i< nBytes; i++) {
0N/A //+ 1 because the 1st byte is length
0N/A intBytes[i] = bufArray[i+(4*j)+1];
0N/A }
0N/A range[j] = convertToInt(intBytes);
0N/A }
0N/A }
0N/A return range;
0N/A
0N/A }
0N/A
0N/A /**
0N/A * Returns String value.
0N/A */
0N/A public String getStringValue() {
0N/A //assumes only 1 attribute value. Will get the first value
0N/A // if > 1.
0N/A String strVal = null;
0N/A byte[] bufArray = (byte[])myValue;
0N/A if (bufArray != null) {
0N/A ByteArrayInputStream bufStream =
0N/A new ByteArrayInputStream(bufArray);
0N/A
0N/A int valLength = bufStream.read();
0N/A
0N/A byte[] strBytes = new byte[valLength];
0N/A bufStream.read(strBytes, 0, valLength);
0N/A try {
0N/A strVal = new String(strBytes, "UTF-8");
0N/A } catch (java.io.UnsupportedEncodingException uee) {
0N/A }
0N/A }
0N/A return strVal;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns array of String values.
0N/A */
0N/A public String[] getArrayOfStringValues() {
0N/A
0N/A byte[] bufArray = (byte[])myValue;
0N/A if (bufArray != null) {
0N/A ByteArrayInputStream bufStream =
0N/A new ByteArrayInputStream(bufArray);
0N/A int available = bufStream.available();
0N/A
0N/A // total number of values is at the end of the stream
0N/A bufStream.mark(available);
0N/A bufStream.skip(available-1);
0N/A int length = bufStream.read();
0N/A bufStream.reset();
0N/A
0N/A String[] valueArray = new String[length];
0N/A for (int i = 0; i < length; i++) {
0N/A // read length
0N/A int valLength = bufStream.read();
0N/A byte[] bufBytes = new byte[valLength];
0N/A bufStream.read(bufBytes, 0, valLength);
0N/A try {
0N/A valueArray[i] = new String(bufBytes, "UTF-8");
0N/A } catch (java.io.UnsupportedEncodingException uee) {
0N/A }
0N/A }
0N/A return valueArray;
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns single byte value.
0N/A */
0N/A public byte getByteValue() {
0N/A byte[] bufArray = (byte[])myValue;
0N/A
0N/A if ((bufArray != null) && (bufArray.length>=2)) {
0N/A return bufArray[1];
0N/A }
0N/A return 0;
0N/A }
0N/A
0N/A /**
0N/A * Returns attribute name.
0N/A */
0N/A public String getName() {
0N/A return myName;
0N/A }
0N/A
0N/A public boolean equals(Object obj) {
0N/A return
0N/A obj != null &&
0N/A obj instanceof AttributeClass &&
0N/A obj.toString().equals (((AttributeClass) obj).toString());
0N/A }
0N/A
0N/A public String toString() {
0N/A return myName;
0N/A }
0N/A
0N/A private int unsignedByteToInt(byte b) {
0N/A return (int) (b & 0xff);
0N/A }
0N/A
0N/A private int convertToInt(byte[] buf) {
0N/A int intVal = 0;
0N/A int pos = 0;
0N/A intVal+= unsignedByteToInt(buf[pos++]) << 24;
0N/A intVal+= unsignedByteToInt(buf[pos++]) << 16;
0N/A intVal+= unsignedByteToInt(buf[pos++]) << 8;
0N/A intVal+= unsignedByteToInt(buf[pos++]) << 0;
0N/A return intVal;
0N/A }
0N/A}