1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package org.openoffice.xmerge.converter.xml.sxw.wordsmith;
25 
26 import org.openoffice.xmerge.util.Debug;
27 import java.io.IOException;
28 import java.io.DataOutputStream;
29 import java.io.ByteArrayOutputStream;
30 
31 /**
32  *  This class represents a single text record in a WordSmith document.
33  *  A record is composed of one or more "WordSmith elements", which
34  *  include: WordSmith header, font table, color table, paragraphs,
35  *  and text runs.
36  *
37  *  @author   David Proulx
38  */
39 
40 class textRecord {
41 
42     java.util.Vector elements;
43 
44 
45     /**
46      *  Default constructor
47      */
textRecord()48     textRecord() {
49         elements = new java.util.Vector(10);
50     }
51 
52 
53     /**
54      *  Add an element
55      *
56      *  @param  elem  The element to add
57      */
addElement(Wse elem)58     void addElement(Wse elem) {
59         elements.add(elem);
60     }
61 
62 
63     /**
64      *  Return the number of bytes needed to represent the current
65      *  contents of this text record.
66      *
67      *  @return  The number of bytes needed to represent the current
68      *           contents of this text record.
69      */
getByteCount()70     int getByteCount() {
71         int totalBytes = 0;
72         int nElements = elements.size();
73         for (int i = 0; i < nElements; i++) {
74             Wse e = (Wse)elements.elementAt(i);
75             totalBytes += e.getByteCount();
76         }
77         return totalBytes;
78     }
79 
80 
81     /**
82      *  Return the contents of this record as a <code>byte</code> array.
83      *
84      *  @return the contents of this record as a <code>byte</code> array.
85      */
getBytes()86     byte[] getBytes() {
87         DataOutputStream os = null;  // Used for storing the data
88         ByteArrayOutputStream bs = null;  // Used for storing the data
89         byte ftBytes[] = null;
90         byte ctBytes[] = null;
91 
92         try {
93             bs = new ByteArrayOutputStream();
94             os = new DataOutputStream(bs);
95             int nElements = elements.size();
96             for (int i = 0; i < nElements; i++) {
97                 Wse e = (Wse)elements.get(i);
98                 os.write(e.getBytes());
99             }
100 
101         } catch (IOException e) {
102             e.printStackTrace();
103         }
104 
105         if (bs != null)
106             return bs.toByteArray();
107         else
108             return null;
109     }
110 }
111 
112