KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > render > afp > modca > MapPageOverlay


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id: MapPageOverlay.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.render.afp.modca;
21
22 import java.io.IOException JavaDoc;
23 import java.io.OutputStream JavaDoc;
24 import java.io.UnsupportedEncodingException JavaDoc;
25 import java.util.ArrayList JavaDoc;
26
27 import org.apache.fop.render.afp.tools.BinaryUtils;
28
29 /**
30  * The Map Page Overlay structured field maps a Resource Local ID to the name of
31  * a Begin Overlay structured field. A Map Page Overlay structured field may
32  * contain from one to 254 repeating groups.
33  *
34  */

35 public class MapPageOverlay extends AbstractAFPObject {
36
37     /**
38      * The collection of overlays (maximum of 254 stored as byte[])
39      */

40     private ArrayList JavaDoc _overLays = new ArrayList JavaDoc();
41
42     /**
43      * Constructor for the Map Page Overlay
44      */

45     public MapPageOverlay() {
46
47     }
48
49     /**
50      * Add an overlay to to the map page overlay object.
51      *
52      * @param name
53      * The name of the overlay.
54      */

55     public void addOverlay(String JavaDoc name) throws MaximumSizeExceededException {
56
57         if (_overLays.size() > 253) {
58             throw new MaximumSizeExceededException();
59         }
60
61         if (name.length() != 8) {
62             throw new IllegalArgumentException JavaDoc("The name of overlay " + name
63                 + " must be 8 characters");
64         }
65
66         if (log.isDebugEnabled()) {
67             log.debug("addOverlay():: adding overlay " + name);
68         }
69
70         byte[] data;
71
72         try {
73
74             data = name.getBytes(AFPConstants.EBCIDIC_ENCODING);
75             _overLays.add(data);
76
77         } catch (UnsupportedEncodingException JavaDoc usee) {
78
79             log
80                 .error("addOverlay():: UnsupportedEncodingException translating the name "
81                 + name);
82
83         }
84
85     }
86
87     /**
88      * Accessor method to write the AFP datastream for the Map Page Overlay
89      * @param os The stream to write to
90      * @throws java.io.IOException
91      */

92     public void writeDataStream(OutputStream JavaDoc os)
93         throws IOException JavaDoc {
94
95
96         int oLayCount = _overLays.size();
97         int recordlength = oLayCount * 18;
98
99         byte[] data = new byte[recordlength + 9];
100
101         data[0] = 0x5A;
102
103         // Set the total record length
104
byte[] rl1 = BinaryUtils.convert(recordlength + 8, 2); //Ignore the
105
// first byte in
106
// the length
107
data[1] = rl1[0];
108         data[2] = rl1[1];
109
110         // Structured field ID for a MPO
111
data[3] = (byte) 0xD3;
112         data[4] = (byte) 0xAB;
113         data[5] = (byte) 0xD8;
114
115         data[6] = 0x00; // Reserved
116
data[7] = 0x00; // Reserved
117
data[8] = 0x00; // Reserved
118

119         int pos = 8;
120
121         //For each overlay
122
byte olayref = 0x00;
123
124         for (int i = 0; i < oLayCount; i++) {
125
126             olayref = (byte) (olayref + 1);
127
128             data[++pos] = 0x00;
129             data[++pos] = 0x12; //the length of repeating group
130

131             data[++pos] = 0x0C; //Fully Qualified Name
132
data[++pos] = 0x02;
133             data[++pos] = (byte) 0x84;
134             data[++pos] = 0x00;
135
136             //now add the name
137
byte[] name = (byte[]) _overLays.get(i);
138
139             for (int j = 0; j < name.length; j++) {
140
141                 data[++pos] = name[j];
142
143             }
144
145             data[++pos] = 0x04; //Resource Local Identifier (RLI)
146
data[++pos] = 0x24;
147             data[++pos] = 0x02;
148
149             //now add the unique id to the RLI
150
data[++pos] = olayref;
151
152         }
153
154         os.write(data);
155
156     }
157
158 }
Popular Tags