KickJava   Java API By Example, From Geeks To Geeks.

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


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: Document.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.render.afp.modca;
21 import java.io.IOException JavaDoc;
22 import java.io.OutputStream JavaDoc;
23 import java.io.UnsupportedEncodingException JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Iterator JavaDoc;
26
27
28 /**
29  * The document is the highest level of the MO:DCA data-stream document
30  * component hierarchy. Documents can be made up of pages, and the pages,
31  * which are at the intermediate level, can be made up of objects. Objects
32  * are at the lowest level, and can be bar codes, graphics, images, and
33  * presentation text.
34  *
35  * At each level of the hierarchy certain sets of MO:DCA data structures,
36  * called structured fields, are permissible. The document, pages and objects
37  * are bounded by structured fields that define their beginnings and their ends.
38  * These structured fields, called begin-end pairs, provide an envelope for the
39  * data-stream components. This feature enables a processor of the data stream
40  * that is not fully compliant with the architecture to bypass those objects
41  * that are beyond its scope, and to process the data stream to the best of its
42  * abilities.
43  *
44  * A presentation document is one that has been formatted and is intended for
45  * presentation, usually on a printer or display device. A data stream containing
46  * a presentation document should produce the same document content in the
47  * same format on different printers or display devices dependent, however,
48  * on the capabilities of each of the printers or display devices. A presentation
49  * document can reference resources that are to be included as part of the
50  * document to be presented.
51  *
52  */

53 public final class Document extends AbstractNamedAFPObject {
54
55     /**
56      * Ststic default name reference
57      */

58     private final static String JavaDoc DEFAULT_NAME = "DOC00001";
59
60     /**
61      * A list of the objects in the document
62      */

63     private ArrayList JavaDoc _objects = new ArrayList JavaDoc();
64
65     /**
66      * The document started state
67      */

68     private boolean _started = false;
69
70     /**
71      * The document completion state
72      */

73     private boolean _complete = false;
74
75     /**
76      * Default constructor for the document object.
77      */

78     public Document() {
79         this(DEFAULT_NAME);
80     }
81
82     /**
83      * Constructor for the document object.
84      * @param name The name of the document
85      */

86     public Document(String JavaDoc name) {
87
88         super(name);
89
90     }
91
92     /**
93      * Adds a page to the document.
94      * @param page - the Page object
95      */

96     public void addPage(PageObject page) {
97         if (!_objects.contains(page)) {
98             _objects.add(page);
99         }
100     }
101
102     /**
103      * Adds a PageGroup to the document.
104      * @param pageGroup the PageGroup object
105      */

106     public void addPageGroup(PageGroup pageGroup) {
107         _objects.add(pageGroup);
108     }
109
110     /**
111      * Method to mark the end of the page group.
112      */

113     public void endDocument() {
114
115         _complete = true;
116
117     }
118
119     /**
120      * Returns an indication if the page group is complete
121      */

122     public boolean isComplete() {
123         return _complete;
124     }
125
126     /**
127      * Accessor method to write the AFP datastream for document.
128      * @param os The stream to write to
129      * @throws java.io.IOException
130      */

131     public void writeDataStream(OutputStream JavaDoc os)
132         throws IOException JavaDoc {
133
134         if (!_started) {
135             writeStart(os);
136             _started = true;
137         }
138
139         for (Iterator JavaDoc it = _objects.iterator(); it.hasNext(); ) {
140             AbstractAFPObject ao = (AbstractAFPObject)it.next();
141             if (ao instanceof PageObject && ((PageObject)ao).isComplete()
142                 || ao instanceof PageGroup && ((PageGroup)ao).isComplete()) {
143                 ao.writeDataStream(os);
144                 it.remove();
145             } else {
146                 break;
147             }
148         }
149
150         if (_complete) {
151             writeEnd(os);
152         }
153
154     }
155
156     /**
157      * Helper method to write the start of the Document
158      * @param os The stream to write to
159      */

160     private void writeStart(OutputStream JavaDoc os)
161         throws IOException JavaDoc {
162
163         byte[] data = new byte[17];
164
165         data[0] = 0x5A; // Structured field identifier
166
data[1] = 0x00; // Length byte 1
167
data[2] = 0x10; // Length byte 2
168
data[3] = (byte) 0xD3; // Structured field id byte 1
169
data[4] = (byte) 0xA8; // Structured field id byte 2
170
data[5] = (byte) 0xA8; // Structured field id byte 3
171
data[6] = 0x00; // Flags
172
data[7] = 0x00; // Reserved
173
data[8] = 0x00; // Reserved
174

175         for (int i = 0; i < _nameBytes.length; i++) {
176
177             data[9 + i] = _nameBytes[i];
178
179         }
180
181         os.write(data);
182
183     }
184
185     /**
186      * Helper method to write the end of the Document.
187      * @param os The stream to write to
188      */

189     private void writeEnd(OutputStream JavaDoc os)
190         throws IOException JavaDoc {
191
192         byte[] data = new byte[17];
193
194         data[0] = 0x5A; // Structured field identifier
195
data[1] = 0x00; // Length byte 1
196
data[2] = 0x10; // Length byte 2
197
data[3] = (byte) 0xD3; // Structured field id byte 1
198
data[4] = (byte) 0xA9; // Structured field id byte 2
199
data[5] = (byte) 0xA8; // Structured field id byte 3
200
data[6] = 0x00; // Flags
201
data[7] = 0x00; // Reserved
202
data[8] = 0x00; // Reserved
203

204         for (int i = 0; i < _nameBytes.length; i++) {
205
206             data[9 + i] = _nameBytes[i];
207
208         }
209
210         os.write(data);
211
212     }
213
214 }
Popular Tags