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: package org.apache.harmony.x.print.ipp;
018:
019: import java.io.ByteArrayOutputStream;
020: import java.io.DataOutputStream;
021: import java.io.IOException;
022: import java.util.Vector;
023:
024: /*
025: * class IppAttributeGroup stores IPP attributes group
026: */
027: public class IppAttributeGroup extends Vector {
028:
029: static final long serialVersionUID = -2197671478629444252L;
030:
031: /* According to RFC2910 (http://ietf.org/rfc/rfc2910.txt?number=2910):
032: *
033: * Each "attribute-group" field MUST be encoded with the "begin-
034: * attribute-group-tag" field followed by zero or more "attribute" sub-
035: * fields.
036: *
037: * The table below maps the model document group name to value of the
038: * "begin-attribute-group-tag" field:
039: *
040: * Model Document Group "begin-attribute-group-tag" field values
041: *
042: * Operation Attributes "operations-attributes-tag"
043: * Job Template Attributes "job-attributes-tag"
044: * Job Object Attributes "job-attributes-tag"
045: * Requested Attributes "job-attributes-tag" (Get-Job-Attributes)
046: * Requested Attributes "printer-attributes-tag" (Get-Printer-Attributes)
047: * Unsupported Attributes * "unsupported-attributes-tag"
048: * Document Content in a special position as described above
049: *
050: * 0x00 reserved for definition in a future IETF standards track document
051: * 0x01 "operation-attributes-tag"
052: * 0x02 "job-attributes-tag"
053: * 0x03 "end-of-attributes-tag"
054: * 0x04 "printer-attributes-tag"
055: * 0x05 "unsupported-attributes-tag"
056: * 0x06-0x0f reserved for future delimiters in
057: * IETF standards track documents
058: */
059: public static final byte TAG_RESERVED = 0x00;
060: public static final byte TAG_OPERATION_ATTRIBUTES = 0x01;
061: public static final byte TAG_JOB_ATTRIBUTES = 0x02;
062: public static final byte TAG_JOB_TEMPLATE_ATTRIBUTES = TAG_JOB_ATTRIBUTES;
063: public static final byte TAG_JOB_OBJECT_ATTRIBUTES = TAG_JOB_ATTRIBUTES;
064: public static final byte TAG_GET_JOB_ATTRIBUTES = TAG_JOB_ATTRIBUTES;
065: public static final byte TAG_END_OF_ATTRIBUTES = 0x03;
066: public static final byte TAG_PRINTER_ATTRIBUTES = 0x04;
067: public static final byte TAG_GET_PRINTER_ATTRIBUTES = TAG_PRINTER_ATTRIBUTES;
068: public static final byte TAG_UNSUPPORTED_ATTRIBUTES = 0x05;
069:
070: /*
071: * @return Returns the name of the group.
072: */
073: public static String getGname(int agid) {
074: switch (agid) {
075: case TAG_RESERVED:
076: return IppResources
077: .getString("IppAttributesGroup.RESERVED");
078: case TAG_OPERATION_ATTRIBUTES:
079: return IppResources
080: .getString("IppAttributesGroup.OPERATION_ATTRIBUTES");
081: case TAG_JOB_ATTRIBUTES:
082: return IppResources
083: .getString("IppAttributesGroup.JOB_TEMPLATE_ATTRIBUTES");
084: case TAG_PRINTER_ATTRIBUTES:
085: return IppResources
086: .getString("IppAttributesGroup.GET_PRINTER_ATTRIBUTES");
087: case TAG_END_OF_ATTRIBUTES:
088: return IppResources
089: .getString("IppAttributesGroup.END_OF_ATTRIBUTES");
090: case TAG_UNSUPPORTED_ATTRIBUTES:
091: return IppResources
092: .getString("IppAttributesGroup.UNSUPPORTED_ATTRIBUTES");
093: }
094: return "UNKNOWN_ATTRIBUTES";
095: }
096:
097: protected int agroupid;
098:
099: public IppAttributeGroup(int agid) {
100: super ();
101: this .agroupid = agid;
102: }
103:
104: public IppAttributeGroup(Integer agid) {
105: super ();
106: this .agroupid = agid.byteValue();
107: }
108:
109: /*
110: * change/add attributes in this group by attributes from parameter
111: */
112: public int set(IppAttributeGroup ag) {
113: if (ag != null && agroupid == ag.agroupid && ag.size() > 0) {
114: int i1;
115: IppAttribute a;
116:
117: for (int ii = ag.size(), i = 0; i < ii; i++) {
118: a = (IppAttribute) ag.get(i);
119: i1 = findAttribute(new String(a.aname));
120: if (i1 >= 0) {
121: set(i1, a);
122: } else {
123: add(a);
124: }
125: }
126: }
127:
128: return size();
129: }
130:
131: public int findAttribute(String name) {
132: for (int ii = size(), i = 0; i < ii; i++) {
133: if (name.equals(new String(((IppAttribute) get(i)).aname))) {
134: return i;
135: }
136: }
137:
138: return -1;
139: }
140:
141: /*
142: * return array of byte presentation of group
143: */
144: public byte[] getBytes() {
145: ByteArrayOutputStream ba = new ByteArrayOutputStream();
146: IppAttribute a;
147: byte[] b;
148:
149: ba.write((byte) agroupid);
150: for (int ii = size(), i = 0; i < ii; i++) {
151: a = (IppAttribute) get(i);
152: b = a.getBytes();
153: ba.write(b, 0, b.length);
154: }
155: return ba.toByteArray();
156: }
157:
158: /*
159: * human readable form of group
160: *
161: * @see java.lang.Object#toString()
162: */
163: public String toString() {
164: ByteArrayOutputStream ab = new ByteArrayOutputStream();
165: DataOutputStream ba = new DataOutputStream(ab);
166: IppAttribute a;
167: String s;
168:
169: try {
170: ba.writeBytes("---- Attributes group: 0x"
171: + Integer.toHexString(agroupid) + "("
172: + getGname(agroupid) + ")" + "\n");
173: for (int ii = size(), i = 0; i < ii; i++) {
174: a = (IppAttribute) get(i);
175: s = a.toString();
176: ba.writeBytes(s + "\n");
177: }
178: } catch (IOException e) {
179: // IGNORE exception
180: e.printStackTrace();
181: }
182:
183: return ab.toString();
184: }
185: }
|