KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hwpf > model > PlexOfCps


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    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 package org.apache.poi.hwpf.model;
19
20 import java.util.ArrayList JavaDoc;
21
22 import org.apache.poi.util.LittleEndian;
23
24
25
26 /**
27  * common data structure in a Word file. Contains an array of 4 byte ints in
28  * the front that relate to an array of abitrary data structures in the back.
29  *
30  *
31  * @author Ryan Ackley
32  */

33 public class PlexOfCps
34 {
35   private int _count;
36   private int _offset;
37   private int _sizeOfStruct;
38   private ArrayList JavaDoc _props;
39
40
41   public PlexOfCps(int sizeOfStruct)
42   {
43     _props = new ArrayList JavaDoc();
44     _sizeOfStruct = sizeOfStruct;
45   }
46
47   /**
48    * Constructor
49    *
50    * @param size The size in bytes of this PlexOfCps
51    * @param sizeOfStruct The size of the data structure type stored in
52    * this PlexOfCps.
53    */

54   public PlexOfCps(byte[] buf, int start, int size, int sizeOfStruct)
55   {
56     _count = (size - 4)/(4 + sizeOfStruct);
57     _sizeOfStruct = sizeOfStruct;
58     _props = new ArrayList JavaDoc(_count);
59
60     for (int x = 0; x < _count; x++)
61     {
62       _props.add(getProperty(x, buf, start));
63     }
64   }
65
66   public GenericPropertyNode getProperty(int index)
67   {
68     return (GenericPropertyNode)_props.get(index);
69   }
70
71   public void addProperty(GenericPropertyNode node)
72   {
73     _props.add(node);
74   }
75
76   public byte[] toByteArray()
77   {
78     int size = _props.size();
79     int cpBufSize = ((size + 1) * LittleEndian.INT_SIZE);
80     int structBufSize = + (_sizeOfStruct * size);
81     int bufSize = cpBufSize + structBufSize;
82
83     byte[] buf = new byte[bufSize];
84
85     GenericPropertyNode node = null;
86     for (int x = 0; x < size; x++)
87     {
88       node = (GenericPropertyNode)_props.get(x);
89
90       // put the starting offset of the property into the plcf.
91
LittleEndian.putInt(buf, (LittleEndian.INT_SIZE * x), node.getStart());
92
93       // put the struct into the plcf
94
System.arraycopy(node.getBytes(), 0, buf, cpBufSize + (x * _sizeOfStruct),
95                        _sizeOfStruct);
96     }
97     // put the ending offset of the last property into the plcf.
98
LittleEndian.putInt(buf, LittleEndian.INT_SIZE * size, node.getEnd());
99
100     return buf;
101
102   }
103
104   private GenericPropertyNode getProperty(int index, byte[] buf, int offset)
105   {
106     int start = LittleEndian.getInt(buf, offset + getIntOffset(index));
107     int end = LittleEndian.getInt(buf, offset + getIntOffset(index+1));
108
109     byte[] struct = new byte[_sizeOfStruct];
110     System.arraycopy(buf, offset + getStructOffset(index), struct, 0, _sizeOfStruct);
111
112     return new GenericPropertyNode(start, end, struct);
113   }
114
115   private int getIntOffset(int index)
116   {
117     return index * 4;
118   }
119
120   /**
121    * returns the number of data structures in this PlexOfCps.
122    *
123    * @return The number of data structures in this PlexOfCps
124    */

125   public int length()
126   {
127     return _count;
128   }
129
130   /**
131    * Returns the offset, in bytes, from the beginning if this PlexOfCps to
132    * the data structure at index.
133    *
134    * @param index The index of the data structure.
135    *
136    * @return The offset, in bytes, from the beginning if this PlexOfCps to
137    * the data structure at index.
138    */

139   private int getStructOffset(int index)
140   {
141     return (4 * (_count + 1)) + (_sizeOfStruct * index);
142   }
143 }
144
Popular Tags