aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/org/apache/fop/afp/parser/UnparsedStructuredField.java
blob: 0e2eeeb0c4c3f0303f9e06aca17072920d473bda (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/* $Id$ */

package org.apache.fop.afp.parser;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.DecimalFormat;

/**
* Represents an unparsed (generic) AFP structured field.
*/
public final class UnparsedStructuredField {

   private final Introducer introducer;
   private final byte[] extData;
   private final byte[] data;

   /**
    *
    * @param Structured field introducer
    * @param data Structured field data
    * @param extData Structured field extension data
    */
   UnparsedStructuredField(Introducer introducer,
           byte[] data, byte[] extData) {
       this.introducer = introducer;
       this.data = data;
       if (extData != null) {
           this.extData = extData;
       } else {
           this.extData = null;
       }
   }

   @Override
   public String toString() {
       StringBuffer sb = new StringBuffer("Structured Field: ");
       sb.append(Integer.toHexString(getSfTypeID()).toUpperCase());
       sb.append(", len=");
       sb.append(new DecimalFormat("00000").format(getSfLength()));
       sb.append(" ").append(getTypeCodeAsString());
       sb.append(" ").append(getCategoryCodeAsString());
       if (isSfiExtensionPresent()) {
           sb.append(", SFI extension present");
       }
       if (isSfiSegmentedData()) {
           sb.append(", segmented data");
       }
       if (isSfiPaddingPresent()) {
           sb.append(", with padding");
       }
       return sb.toString();
   }


   /**
    * Returns type code function name for this field.
    * @return the type code function name
    */
   private String getTypeCodeAsString() {
       switch (getSfTypeCode() & 0xFF) {
       case 0xA0: return "Attribute";
       case 0xA2: return "CopyCount";
       case 0xA6: return "Descriptor";
       case 0xA7: return "Control";
       case 0xA8: return "Begin";
       case 0xA9: return "End";
       case 0xAB: return "Map";
       case 0xAC: return "Position";
       case 0xAD: return "Process";
       case 0xAF: return "Include";
       case 0xB0: return "Table";
       case 0xB1: return "Migration";
       case 0xB2: return "Variable";
       case 0xB4: return "Link";
       case 0xEE: return "Data";
       default: return "Unknown:" + Integer.toHexString(getSfTypeCode()).toUpperCase();
       }
   }

   /**
    * Returns category code function name for this field.
    * @return the category code function name
    */
   private String getCategoryCodeAsString() {
       switch (getSfCategoryCode() & 0xFF) {
       case 0x5F: return "Page Segment";
       case 0x6B: return "Object Area";
       case 0x77: return "Color Attribute Table";
       case 0x7B: return "IM Image";
       case 0x88: return "Medium";
       case 0x89: return "Font";
       case 0x8A: return "Coded Font";
       case 0x90: return "Process Element";
       case 0x92: return "Object Container";
       case 0x9B: return "Presentation Text";
       case 0xA7: return "Index";
       case 0xA8: return "Document";
       case 0xAD: return "Page Group";
       case 0xAF: return "Page";
       case 0xBB: return "Graphics";
       case 0xC3: return "Data Resource";
       case 0xC4: return "Document Environment Group (DEG)";
       case 0xC6: return "Resource Group";
       case 0xC7: return "Object Environment Group (OEG)";
       case 0xC9: return "Active Environment Group (AEG)";
       case 0xCC: return "Medium Map";
       case 0xCD: return "Form Map";
       case 0xCE: return "Name Resource";
       case 0xD8: return "Page Overlay";
       case 0xD9: return "Resource Environment Group (REG)";
       case 0xDF: return "Overlay";
       case 0xEA: return "Data Supression";
       case 0xEB: return "Bar Code";
       case 0xEE: return "No Operation";
       case 0xFB: return "Image";
       default: return "Unknown:" + Integer.toHexString(getSfTypeCode()).toUpperCase();
       }
   }

   /**
    * Returns the structured field's length.
    * @return the field length
    */
   public short getSfLength() {
       return introducer.length;
   }

   /**
    * Returns the structured field's identifier.
    * @return the field identifier
    */
   public int getSfTypeID() {
       return ((getSfClassCode() & 0xFF) << 16)
       | ((getSfTypeCode() & 0xFF) << 8)
       | (getSfCategoryCode() & 0xFF);
   }

   /**
    * Returns the structured field's class code.
    * @return the field class code
    */
   public byte getSfClassCode() {
       return introducer.classCode;
   }

   /**
    * Returns the structured field's type code.
    * @return the type code
    */
   public byte getSfTypeCode() {
       return introducer.typeCode;
   }

   /**
    * Returns the structured field's category code.
    * @return the sfCategoryCode
    */
   public byte getSfCategoryCode() {
       return introducer.categoryCode;
   }

   /**
    * Indicates whether an field introducer extension is present.
    * @return true if an field introducer extension is present
    */
   public boolean isSfiExtensionPresent() {
       return introducer.extensionPresent && (this.extData != null);
   }

   /**
    * Indicates whether segmented data is present.
    * @return true if the data is segmented
    */
   public boolean isSfiSegmentedData() {
       return introducer.segmentedData;
   }

   /**
    * Indicates whether the data is padded.
    * @return true if the data is padded
    */
   public boolean isSfiPaddingPresent() {
       return introducer.paddingPresent;
   }

   /**
    * Returns the length of the extension if present.
    * @return the length of the extension (or 0 if no extension is present)
    */
   public short getExtLength() {
       return (extData != null) ? (short)(extData.length + 1) : 0;

   }

   /**
    * Returns the extension data if present.
    * @return the extension data (or null if no extension is present)
    */
   byte[] getExtData() {
       if (this.extData == null) {
           return new byte[0];
       }
       byte[] rtn = new byte[this.extData.length];
       System.arraycopy(this.extData, 0, rtn, 0, rtn.length);
       return rtn;
   }

   /**
    * Returns the structured field's payload.
    * @return the field's data
    */
   public byte[] getData() {
       if (this.data == null) {
           return new byte[0];
       }
       byte[] rtn = new byte[this.data.length];
       System.arraycopy(this.data, 0, rtn, 0, rtn.length);
       return rtn;
   }

   //For unit testing
   byte[] getIntroducerData() {
       return introducer.getIntroducerData();
   }

   /**
    * Returns the complete structured field as a byte array.
    * @return the complete field data
    */
   public byte[] getCompleteFieldAsBytes() {

       ByteArrayOutputStream baos = new ByteArrayOutputStream(getSfLength());
       try {
           writeTo(baos);
       } catch (IOException ioe) {
           //nop
       }
       return baos.toByteArray();

   }

   /**
    * Writes this structured field to the given {@link OutputStream}.
    * @param out the output stream
    * @throws IOException if an I/O error occurs
    */
   public void writeTo(OutputStream out) throws IOException {
       out.write(introducer.introducerData);
       if (isSfiExtensionPresent()) {
           out.write(this.extData.length + 1);
           out.write(this.extData);
       }
       out.write(this.data);
   }

   static final class Introducer {

       private final short length;
       private final byte classCode;
       private final byte typeCode;
       private final byte categoryCode;
       private final boolean extensionPresent;
       private final boolean segmentedData;
       private final boolean paddingPresent;
       private final byte[] introducerData;

       Introducer(byte[] introducerData) throws IOException {

           this.introducerData = introducerData;

           // Parse the introducer; the 8 bytes have already been read from the stream just
           // before, so we parse the introducer from the byte array
           DataInputStream iis = new DataInputStream(
                   new ByteArrayInputStream(introducerData));

           length = iis.readShort();

           classCode = iis.readByte();
           typeCode = iis.readByte();
           categoryCode = iis.readByte();

           //Flags
           byte f = iis.readByte();

           extensionPresent = (f & 0x01) != 0;
           segmentedData = (f & 0x04) != 0;
           paddingPresent = (f & 0x10) != 0;

       }


       public short getLength() {
           return length;
       }

       public byte getClassCode() {
           return classCode;
       }

       public byte getTypeCode() {
           return typeCode;
       }

       public byte getCategoryCode() {
           return categoryCode;
       }

       public boolean isExtensionPresent() {
           return extensionPresent;
       }


       public boolean isSegmentedData() {
           return segmentedData;
       }

       public boolean isPaddingPresent() {
           return paddingPresent;
       }

       public byte[] getIntroducerData() {
           byte[] rtn = new byte[introducerData.length];
           System.arraycopy(introducerData, 0, rtn, 0, rtn.length);
           return rtn;
       }
   }

}