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 java.io.IOException;
27 import java.io.DataOutputStream;
28 import java.io.ByteArrayOutputStream;
29 
30 /**
31  *  This class represents a WordSmith document header.
32  *
33  *  @author   David Proulx
34  */
35 class WseHeader extends Wse {
36 
37     private int nParagraphs = 0;
38     private int nAtoms = 0;
39     private int nChars = 0;
40     private int miscSize = 0;
41 
42     /**
43      *  Constructor for use when going from DOM to WordSmith.
44      *
45      *  @param  nPara   The number of paragraphs.
46      *  @param  nAtoms  The number of atoms.
47      *  @param  nChars  The number of characters.
48      *  @param  ft      The font table.
49      *  @param  ct      The color table.
50      */
WseHeader(int nPara, int nAtoms, int nChars, WseFontTable ft, WseColorTable ct)51     public WseHeader(int nPara, int nAtoms, int nChars, WseFontTable ft,
52                      WseColorTable ct) {
53         nParagraphs = nPara;
54         this.nAtoms = nAtoms;
55         this.nChars = nChars;
56         if (ft != null) miscSize += ft.getByteCount();
57         if (ct != null) miscSize += ct.getByteCount();
58     }
59 
60 
61     /**
62      *  Constructor for use when going from WordSmith to DOM.
63      *
64      *  @param  dataArray  <code>byte</code> array.
65      *  @param  i          Index.
66      */
WseHeader(byte dataArray[], int i)67     public WseHeader(byte dataArray[], int i) {
68     // DJP: write this!
69     }
70 
71     /**
72      *  Return true if <code>dataArray[startIndex]</code> is the start
73      *  of a document header.
74      *
75      *  @param dataArray   <code>byte</code> array.
76      *  @param startIndex  The index.
77      *
78      *  @return  true if <code>dataArray[startIndex]</code> is the start
79      *           of a document header, false otherwise.
80      */
isValid(byte dataArray[], int startIndex)81     static boolean isValid(byte dataArray[], int startIndex) {
82         return ((dataArray[startIndex] == 2)
83               && (dataArray[startIndex + 1] == 4));
84     }
85 
86 
87     /**
88      *  Compute and return the index of the first <code>byte</code>
89      *  following this element.  It is assumed that the element
90      *  starting at <code>dataArray[startIndex]</code> is valid.
91      *
92      *  @param  dataArray   <code>byte</code> array.
93      *  @param  startIndex  The start index.
94      *
95      *  @return  The first <code>byte</code> following this element.
96      */
computeNewIndex(byte dataArray[], int startIndex)97     static int computeNewIndex(byte dataArray[], int startIndex) {
98         return startIndex + 18;
99     }
100 
101 
102     /**
103      *  Return the total number of bytes needed to represent this.
104      *
105      *  @return  The total number of bytes needed to represent this.
106      */
getByteCount()107     int getByteCount() {
108         return 18;
109     }
110 
111 
112     /**
113      *  Return a <code>byte</code> array representing this element.
114      *
115      *  @return  A <code>byte</code> array representing this element.
116      */
getBytes()117     byte[] getBytes() {
118         DataOutputStream os;  // Used for storing the data
119         ByteArrayOutputStream bs = null;  // Used for storing the data
120 
121         try {
122             bs = new ByteArrayOutputStream();
123             os = new DataOutputStream(bs);
124             os.write(2);  // binary doc indicator
125             os.write(4);  // binary header indicator
126 
127             os.writeInt(nParagraphs);
128             os.writeInt(nAtoms);
129             os.writeInt(nChars);
130             os.writeInt(miscSize);
131 
132         } catch (IOException e) {
133             e.printStackTrace();
134         }
135 
136         if (bs != null) {
137             return bs.toByteArray();
138         } else return null;
139     }
140 }
141 
142