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.Hashtable;
023: import java.util.Iterator;
024: import java.util.Vector;
025:
026: /*
027: * Set of IppAttributeGroup
028: */
029: public class IppAttributeGroupSet extends Hashtable {
030:
031: static final long serialVersionUID = -1273600082773438491L;
032:
033: static protected void sortAttributes(Vector va) {
034: Object v1, v2;
035: IppAttributeGroup g1, g2;
036: boolean the_end = false;
037:
038: while (!the_end) {
039: the_end = true;
040: for (int ii = va.size(), i = 0; i < (ii - 1); i++) {
041: v1 = va.get(i);
042: v2 = va.get(i + 1);
043: if (v1 instanceof Vector) {
044: g1 = (IppAttributeGroup) ((Vector) v1).get(0);
045: } else {
046: g1 = (IppAttributeGroup) v1;
047: }
048: if (v2 instanceof Vector) {
049: g2 = (IppAttributeGroup) ((Vector) v2).get(0);
050: } else {
051: g2 = (IppAttributeGroup) v2;
052: }
053: if (g1 != null && g2 != null
054: && g1.agroupid > g2.agroupid) {
055: the_end = false;
056: va.set(i, v2);
057: va.set(i + 1, v1);
058: }
059: }
060: }
061: }
062:
063: public boolean setAttribute(String aname, Object avalue) {
064: Integer gtag;
065: byte vtag;
066: IppAttributeGroup group;
067:
068: if (avalue == null || aname == null || aname.equals("")) {
069: return false;
070: }
071:
072: gtag = new Integer(IppDefs.getAttributeGtag(aname));
073: vtag = IppDefs.getAttributeVtag(aname);
074: if (avalue instanceof IppAttribute) {
075: if (((IppAttribute) avalue).getTag() != vtag
076: || !aname.equals(new String(((IppAttribute) avalue)
077: .getName()))) {
078: return false;
079: }
080: }
081:
082: group = (IppAttributeGroup) get(gtag);
083: if (group == null) {
084: put(gtag, new IppAttributeGroup(gtag));
085: group = (IppAttributeGroup) get(gtag);
086: }
087: if (group == null) {
088: return false;
089: }
090:
091: int aindex = -1;
092:
093: if (avalue instanceof IppAttribute) {
094: aname = new String(((IppAttribute) avalue).getName());
095: aindex = group.findAttribute(aname);
096: if (aindex >= 0) {
097: group.set(aindex, avalue);
098: } else {
099: group.add(avalue);
100: }
101: return true;
102: }
103:
104: switch (vtag) {
105: case IppAttribute.TAG_INTEGER:
106: case IppAttribute.TAG_BOOLEAN:
107: case IppAttribute.TAG_ENUM:
108: avalue = new IppAttribute(vtag, aname, ((Integer) avalue)
109: .intValue());
110: aindex = group.findAttribute(aname);
111: if (aindex >= 0) {
112: group.set(aindex, avalue);
113: } else {
114: group.add(avalue);
115: }
116: return true;
117: case IppAttribute.TAG_OCTETSTRINGUNSPECIFIEDFORMAT:
118: case IppAttribute.TAG_DATETIME:
119: case IppAttribute.TAG_RESOLUTION:
120: case IppAttribute.TAG_RANGEOFINTEGER:
121: case IppAttribute.TAG_TEXTWITHLANGUAGE:
122: case IppAttribute.TAG_NAMEWITHLANGUAGE:
123: avalue = new IppAttribute(vtag, aname, (byte[]) avalue);
124: aindex = group.findAttribute(aname);
125: if (aindex >= 0) {
126: group.set(aindex, avalue);
127: } else {
128: group.add(avalue);
129: }
130: return true;
131: case IppAttribute.TAG_TEXTWITHOUTLANGUAGE:
132: case IppAttribute.TAG_NAMEWITHOUTLANGUAGE:
133: case IppAttribute.TAG_KEYWORD:
134: case IppAttribute.TAG_URI:
135: case IppAttribute.TAG_URISCHEME:
136: case IppAttribute.TAG_CHARSET:
137: case IppAttribute.TAG_NATURAL_LANGUAGE:
138: case IppAttribute.TAG_MIMEMEDIATYPE:
139: case IppAttribute.TAG_UNSUPPORTED:
140: avalue = new IppAttribute(vtag, aname, (String) avalue);
141: aindex = group.findAttribute(aname);
142: if (aindex >= 0) {
143: group.set(aindex, avalue);
144: } else {
145: group.add(avalue);
146: }
147: return true;
148: case IppAttribute.TAG_UNKNOWN:
149: case IppAttribute.TAG_NO_VALUE:
150: break;
151: default:
152: break;
153: }
154:
155: return false;
156: }
157:
158: public byte[] getBytes() throws IppException {
159: ByteArrayOutputStream ab = new ByteArrayOutputStream();
160: DataOutputStream ba;
161: Iterator ai = values().iterator();
162: Object v;
163: IppAttributeGroup ag;
164: byte[] b;
165: Vector av = new Vector();
166:
167: for (; ai.hasNext();) {
168: av.add(ai.next());
169: }
170: sortAttributes(av);
171:
172: ba = new DataOutputStream(ab);
173:
174: try {
175: for (int ii = av.size(), i = 0; i < ii; i++) {
176: v = av.get(i);
177: if (v != null && v instanceof Vector) {
178: for (int j = 0, jj = ((Vector) v).size(); j < jj; j++) {
179: ag = (IppAttributeGroup) ((Vector) v).get(j);
180: b = ag.getBytes();
181: ba.write(b);
182: }
183: } else {
184: ag = (IppAttributeGroup) v;
185: b = ag.getBytes();
186: ba.write(b);
187: }
188: }
189: ba.write(IppAttributeGroup.TAG_END_OF_ATTRIBUTES);
190: ba.flush();
191: ba.close();
192: } catch (IOException e) {
193: // e.printStackTrace();
194: throw new IppException(e.getMessage());
195: }
196:
197: return ab.toByteArray();
198:
199: }
200: }
|