001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Igor A. Pyankov
019: * @version $Revision: 1.2 $
020: */package org.apache.harmony.x.print.ipp;
021:
022: import java.io.ByteArrayInputStream;
023: import java.io.ByteArrayOutputStream;
024: import java.io.DataInputStream;
025: import java.io.DataOutputStream;
026: import java.io.IOException;
027: import java.util.Vector;
028:
029: /*
030: * class IppAttribute stores IPP attribute (http://ietf.org/rfc/rfc2910.txt?number=2910)
031: *
032: * <pre>
033: * The following table specifies the out-of-band values for the
034: * value-tag field.
035: *
036: * Tag Value (Hex) Meaning
037: *
038: * 0x10 unsupported
039: * 0x11 reserved for 'default' for definition in a future
040: * IETF standards track document
041: * 0x12 unknown
042: * 0x13 no-value
043: * 0x14-0x1F reserved for "out-of-band" values in future IETF
044: * standards track documents.
045: *
046: * The following table specifies the integer values for the value-tag
047: * field:
048: *
049: * Tag Value (Hex) Meaning
050: *
051: * 0x20 reserved for definition in a future IETF
052: * standards track document
053: * 0x21 integer
054: * 0x22 boolean
055: * 0x23 enum
056: * 0x24-0x2F reserved for integer types for definition in
057: * future IETF standards track documents
058: *
059: * NOTE: 0x20 is reserved for "generic integer" if it should ever be
060: * needed.
061: *
062: * The following table specifies the octetString values for the value-tag field:
063: *
064: * Tag Value (Hex) Meaning
065: *
066: * 0x30 octetString with an unspecified format
067: * 0x31 dateTime
068: * 0x32 resolution
069: * 0x33 rangeOfInteger
070: * 0x34 reserved for definition in a future IETF * standards track document
071: * 0x35 textWithLanguage
072: * 0x36 nameWithLanguage
073: * 0x37-0x3F reserved for octetString type definitions in
074: * future IETF standards track documents
075: *
076: * The following table specifies the character-string values for the
077: * value-tag field:
078: *
079: * Tag Value (Hex) Meaning
080: *
081: * 0x40 reserved for definition in a future IETF standards track document
082: * 0x41 textWithoutLanguage
083: * 0x42 nameWithoutLanguage
084: * 0x43 reserved for definition in a future IETF standards track document
085: * 0x44 keyword
086: * 0x45 uri
087: * 0x46 uriScheme
088: * 0x47 charset
089: * 0x48 naturalLanguage
090: * 0x49 mimeMediaType
091: * 0x4A-0x5F reserved for character string type definitions
092: * in future IETF standards track documents
093: * </pre>
094: */
095: public class IppAttribute {
096: // "out-of-band" values for the "value-tag" field.
097: public static final byte TAG_UNSUPPORTED = 0x10;
098: public static final byte TAG_UNKNOWN = 0x12;
099: public static final byte TAG_NO_VALUE = 0x13;
100:
101: // integer values for the "value-tag" field.
102: public static final byte TAG_INTEGER = 0x21;
103: public static final byte TAG_BOOLEAN = 0x22;
104: public static final byte TAG_ENUM = 0x23;
105:
106: // octetString values for the "value-tag" field.
107: public static final byte TAG_OCTETSTRINGUNSPECIFIEDFORMAT = 0x30;
108: public static final byte TAG_DATETIME = 0x31;
109: public static final byte TAG_RESOLUTION = 0x32;
110: public static final byte TAG_RANGEOFINTEGER = 0x33;
111: public static final byte TAG_TEXTWITHLANGUAGE = 0x35;
112: public static final byte TAG_NAMEWITHLANGUAGE = 0x36;
113:
114: // character-string values for the "value-tag" field
115: public static final byte TAG_TEXTWITHOUTLANGUAGE = 0x41;
116: public static final byte TAG_NAMEWITHOUTLANGUAGE = 0x42;
117: public static final byte TAG_KEYWORD = 0x44;
118: public static final byte TAG_URI = 0x45;
119: public static final byte TAG_URISCHEME = 0x46;
120: public static final byte TAG_CHARSET = 0x47;
121: public static final byte TAG_NATURAL_LANGUAGE = 0x48;
122: public static final byte TAG_MIMEMEDIATYPE = 0x49;
123:
124: /*
125: * The method just return names of attributes tags
126: */
127: public static String getTagName(byte atag) {
128: String sz = "";
129: switch (atag) {
130: // integer values for the "value-tag" field.
131: case TAG_BOOLEAN:
132: sz = "BOOLEAN";
133: break;
134: case TAG_INTEGER:
135: sz = "INTEGER";
136: break;
137: case TAG_ENUM:
138: sz = "ENUM";
139: break;
140: // octetString values for the "value-tag" field.
141: case TAG_OCTETSTRINGUNSPECIFIEDFORMAT:
142: case TAG_DATETIME:
143: case TAG_RESOLUTION:
144: case TAG_RANGEOFINTEGER:
145: case TAG_TEXTWITHLANGUAGE:
146: case TAG_NAMEWITHLANGUAGE:
147: sz = "OCTETSTRING";
148: break;
149: // character-string values for the "value-tag" field
150: case TAG_TEXTWITHOUTLANGUAGE:
151: case TAG_NAMEWITHOUTLANGUAGE:
152: case TAG_KEYWORD:
153: case TAG_URI:
154: case TAG_URISCHEME:
155: case TAG_CHARSET:
156: case TAG_NATURAL_LANGUAGE:
157: case TAG_MIMEMEDIATYPE:
158: sz = "CHARACTERSTRING";
159: break;
160: default:
161: sz = "UNKNOWN_ATTRIBUTE_TAG";
162: break;
163: }
164: return sz;
165: }
166:
167: protected byte atag;
168: protected byte[] aname;
169: protected Vector avalue;
170:
171: /*
172: * Constructors
173: */
174: public IppAttribute(byte tag, String name, int value) {
175: atag = tag;
176: aname = name.getBytes();
177: avalue = new Vector();
178: avalue.add(new Integer(value));
179: }
180:
181: public IppAttribute(byte tag, String name, String value) {
182: atag = tag;
183: aname = name.getBytes();
184: avalue = new Vector();
185: avalue.add(value.getBytes());
186: }
187:
188: public IppAttribute(byte tag, String name, byte[] value) {
189: atag = tag;
190: aname = name.getBytes();
191: avalue = new Vector();
192: avalue.add(value);
193: }
194:
195: public IppAttribute(byte tag, String name, Vector value) {
196: atag = tag;
197: aname = name.getBytes();
198: avalue = value;
199: }
200:
201: /*
202: * Getters
203: */
204: public byte getTag() {
205: return atag;
206: }
207:
208: public byte[] getName() {
209: return aname;
210: }
211:
212: public Vector getValue() {
213: return avalue;
214: }
215:
216: /*
217: * The method return byte array of attribute
218: * Then these bytes can be wrote to http request
219: */
220: public byte[] getBytes() {
221: ByteArrayOutputStream bbuf = new ByteArrayOutputStream();
222: DataOutputStream dbuf = new DataOutputStream(bbuf);
223: byte[] bv;
224:
225: try {
226: for (int ii = avalue.size(), i = 0; i < ii; i++) {
227: dbuf.writeByte(atag);
228: if (i == 0) {
229: dbuf.writeShort(aname.length);
230: dbuf.write(aname);
231: } else {
232: dbuf.writeShort(0);
233: }
234: switch (atag) {
235: // integer values for the "value-tag" field.
236: case TAG_BOOLEAN:
237: dbuf.writeShort(1);
238: dbuf.write(((Integer) avalue.get(i)).intValue());
239: break;
240: case TAG_INTEGER:
241: case TAG_ENUM:
242: dbuf.writeShort(4);
243: dbuf.writeInt(((Integer) avalue.get(i)).intValue());
244: break;
245: // octetString values for the "value-tag" field.
246: case TAG_OCTETSTRINGUNSPECIFIEDFORMAT:
247: case TAG_DATETIME:
248: case TAG_RESOLUTION:
249: case TAG_RANGEOFINTEGER:
250: case TAG_TEXTWITHLANGUAGE:
251: case TAG_NAMEWITHLANGUAGE:
252: bv = (byte[]) avalue.get(i);
253: dbuf.writeShort(bv.length);
254: dbuf.write(bv);
255: break;
256: // character-string values for the "value-tag" field
257: case TAG_TEXTWITHOUTLANGUAGE:
258: case TAG_NAMEWITHOUTLANGUAGE:
259: case TAG_KEYWORD:
260: case TAG_URI:
261: case TAG_URISCHEME:
262: case TAG_CHARSET:
263: case TAG_NATURAL_LANGUAGE:
264: case TAG_MIMEMEDIATYPE:
265: bv = (byte[]) avalue.get(i);
266: dbuf.writeShort(bv.length);
267: dbuf.write(bv);
268: break;
269: default:
270: break;
271: }
272: }
273: dbuf.flush();
274: dbuf.close();
275: } catch (IOException e) {
276: e.printStackTrace();
277: }
278:
279: return bbuf.toByteArray();
280: }
281:
282: /*
283: * @see java.lang.Object#toString()
284: *
285: * Returns readable form of attribute
286: */
287: public String toString() {
288: ByteArrayOutputStream bbuf = new ByteArrayOutputStream();
289: DataOutputStream dbuf = new DataOutputStream(bbuf);
290:
291: try {
292: dbuf.writeBytes("attribute tag: 0x"
293: + Integer.toHexString(atag) + "("
294: + getTagName(atag) + ")" + "\n");
295: dbuf.writeBytes("attribute name: " + new String(aname)
296: + "\n");
297:
298: switch (atag) {
299: // integer values for the "value-tag" field.
300: case TAG_INTEGER:
301: case TAG_BOOLEAN:
302: case TAG_ENUM:
303: for (int ii = avalue.size(), i = 0; i < ii; i++) {
304: Integer v = (Integer) avalue.get(i);
305: dbuf.writeBytes(v.toString() + "\n");
306: }
307: break;
308: // octetString values for the "value-tag" field.
309: case TAG_OCTETSTRINGUNSPECIFIEDFORMAT:
310: case TAG_RESOLUTION:
311: case TAG_TEXTWITHLANGUAGE:
312: case TAG_NAMEWITHLANGUAGE:
313: for (int ii = avalue.size(), i = 0; i < ii; i++) {
314: byte[] bv = (byte[]) avalue.get(i);
315: dbuf.writeBytes(new String(bv) + "\n");
316: }
317: break;
318: case TAG_DATETIME:
319: /*
320: * <pre> A date-time specification.
321: *
322: * field octets contents range ----- ------ -------- ----- 1 1-2
323: * year 0..65536 2 3 month 1..12 3 4 day 1..31 4 5 hour 0..23 5
324: * 6 minutes 0..59 6 7 seconds 0..60 (use 60 for leap-second) 7
325: * 8 deci-seconds 0..9 8 9 direction from UTC '+' / '-' 9 10
326: * hours from UTC 0..11 10 11 minutes from UTC 0..59 For
327: * example, Tuesday May 26, 1992 at 1:30:15 PM EDT would be
328: * displayed as: 1992-5-26,13:30:15.0,-4:0 </pre>
329: */
330: for (int ii = avalue.size(), i = 0; i < ii; i++) {
331: byte[] bv = (byte[]) avalue.get(i);
332: ByteArrayInputStream bi = new ByteArrayInputStream(
333: bv);
334: DataInputStream di = new DataInputStream(bi);
335:
336: dbuf.writeBytes(Integer.toString(di.readShort())
337: + "-" + Integer.toString(di.readByte())
338: + "-" + Integer.toString(di.readByte())
339: + "," + Integer.toString(di.readByte())
340: + ":" + Integer.toString(di.readByte())
341: + ":" + Integer.toString(di.readByte())
342: + "." + Integer.toString(di.readByte())
343: + "," + (char) di.readByte()
344: + Integer.toString(di.readByte()) + ":"
345: + Integer.toString(di.readByte()) + "\n");
346: }
347: break;
348: case TAG_RANGEOFINTEGER:
349: for (int ii = avalue.size(), i = 0; i < ii; i++) {
350: byte[] bv = (byte[]) avalue.get(i);
351: ByteArrayInputStream bi = new ByteArrayInputStream(
352: bv);
353: DataInputStream di = new DataInputStream(bi);
354: dbuf.writeBytes(Integer.toString(di.readInt())
355: + "..." + Integer.toString(di.readInt())
356: + "\n");
357: }
358: break;
359: // character-string values for the "value-tag" field
360: case TAG_TEXTWITHOUTLANGUAGE:
361: case TAG_NAMEWITHOUTLANGUAGE:
362: case TAG_KEYWORD:
363: case TAG_URI:
364: case TAG_URISCHEME:
365: case TAG_CHARSET:
366: case TAG_NATURAL_LANGUAGE:
367: case TAG_MIMEMEDIATYPE:
368: for (int ii = avalue.size(), i = 0; i < ii; i++) {
369: byte[] bv = (byte[]) avalue.get(i);
370: dbuf.writeBytes(new String(bv) + "\n");
371: }
372: break;
373: default:
374: for (int ii = avalue.size(), i = 0; i < ii; i++) {
375: //byte[] bv = (byte[]) avalue.get(i);
376: //dbuf.writeBytes(new String(bv) + "\n");
377: }
378: break;
379: }
380:
381: dbuf.flush();
382: dbuf.close();
383: } catch (IOException e) {
384: e.printStackTrace();
385: }
386:
387: return bbuf.toString();
388: }
389:
390: }
|