1    /* ====================================================================
2     * The Apache Software License, Version 1.1
3     *
4     * Copyright (c) 2000 The Apache Software Foundation.  All rights
5     * reserved.
6     *
7     * Redistribution and use in source and binary forms, with or without
8     * modification, are permitted provided that the following conditions
9     * are met:
10    *
11    * 1. Redistributions of source code must retain the above copyright
12    *    notice, this list of conditions and the following disclaimer.
13    *
14    * 2. Redistributions in binary form must reproduce the above copyright
15    *    notice, this list of conditions and the following disclaimer in
16    *    the documentation and/or other materials provided with the
17    *    distribution.
18    *
19    * 3. The end-user documentation included with the redistribution,
20    *    if any, must include the following acknowledgment:
21    *       "This product includes software developed by the
22    *        Apache Software Foundation (http://www.apache.org/)."
23    *    Alternately, this acknowledgment may appear in the software itself,
24    *    if and wherever such third-party acknowledgments normally appear.
25    *
26    * 4. The names "Apache" and "Apache Software Foundation" must
27    *    not be used to endorse or promote products derived from this
28    *    software without prior written permission. For written
29    *    permission, please contact apache@apache.org.
30    *
31    * 5. Products derived from this software may not be called "Apache",
32    *    nor may "Apache" appear in their name, without prior written
33    *    permission of the Apache Software Foundation.
34    *
35    * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36    * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37    * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38    * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39    * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40    * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41    * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42    * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43    * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44    * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45    * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46    * SUCH DAMAGE.
47    * ====================================================================
48    *
49    * This software consists of voluntary contributions made by many
50    * individuals on behalf of the Apache Software Foundation.  For more
51    * information on the Apache Software Foundation, please see
52    * <http://www.apache.org/>.
53    *
54    * Portions of this software are based upon public domain software
55    * originally written at the National Center for Supercomputing Applications,
56    * University of Illinois, Urbana-Champaign.
57    */
58   
59   package org.apache.poi.hpsf;
60   
61   import java.util.*;
62   import org.apache.poi.hpsf.littleendian.*;
63   import org.apache.poi.hpsf.wellknown.*;
64   
65   /**
66    * <p>Represents a section in a {@link PropertySet}.</p>
67    *
68    * @author Rainer Klute (klute@rainer-klute.de)
69    * @version $Id: Section.java,v 1.1 2002/02/14 04:00:59 mjohnson Exp $
70    * @since 2002-02-09
71    */
72   public class Section
73   {
74   
75       /**
76        * <p>Maps property IDs to section-private PID strings. These
77        * strings can be found in the property with ID 0.</p>
78        */
79       protected Map dictionary;
80   
81   
82   
83       private ClassID formatID;
84   
85       /**
86        * <p>Returns the format ID. The format ID is the "type" of the
87        * section.</p>
88        */
89       public ClassID getFormatID()
90       {
91           return formatID;
92       }
93   
94   
95   
96       private int offset;
97   
98       /**
99        * <p>Returns the offset of the section in the stream.</p>
100       */
101      public int getOffset()
102      {
103          return offset;
104      }
105  
106  
107  
108      private int size;
109  
110      /**
111       * <p>Returns the section's size in bytes.</p>
112       */
113      public int getSize()
114      {
115          return size;
116      }
117  
118  
119  
120      private int propertyCount;
121  
122      /**
123       * <p>Returns the number of properties in this section.</p>
124       */
125      public int getPropertyCount()
126      {
127          return propertyCount;
128      }
129  
130  
131  
132      private Property[] properties;
133  
134      /**
135       * <p>Returns this section's properties.</p>
136       */
137      public Property[] getProperties()
138      {
139          return properties;
140      }
141  
142  
143  
144      /**
145       * <p>Creates a {@link Section} instance from a byte array.</p>
146       *
147       * @param src Contains the complete property set stream.
148       *
149       * @param offset The position in the stream that points to the
150       * section's format ID.
151       */
152      public Section(final byte[] src, int offset)
153      {
154          /* Read the format ID. */
155          formatID = new ClassID(src, offset);
156          offset += ClassID.LENGTH;
157  
158          /* Read the offset from the stream's start and positions to
159           * the section header. */
160          this.offset = new DWord(src, offset).intValue();
161          offset = this.offset;
162  
163          /* Read the section length. */
164          size = new DWord(src, offset).intValue();
165          offset += DWord.LENGTH;
166  
167          /* Read the number of properties. */
168          propertyCount = new DWord(src, offset).intValue();
169          offset += DWord.LENGTH;
170  
171          /* Read the properties. The offset is positioned at the first
172           * entry of the property list. */
173          properties = new Property[propertyCount];
174          for (int i = 0; i < properties.length; i++)
175          {
176              final int id = new DWord(src, offset).intValue();
177              offset += DWord.LENGTH;
178  
179              /* Offset from the section. */
180              final int sOffset = new DWord(src, offset).intValue();
181              offset += DWord.LENGTH;
182  
183              /* Calculate the length of the property. */
184              int length;
185              if (i == properties.length - 1)
186                  length = src.length - this.offset - sOffset;
187              else
188                  length =
189                      new DWord(src, offset + DWord.LENGTH).intValue() - sOffset;
190  
191              /* Create it. */
192              properties[i] =
193                  new Property(id, src, this.offset + sOffset, length);
194          }
195  
196          /* Extract the dictionary (if available). */
197          dictionary = (Map) getProperty(0);
198      }
199  
200  
201  
202      /**
203       * <p>Returns the value of the property with the specified ID. If
204       * the property is not available, <code>null</code> is returned
205       * and a subsequent call to {@link #wasNull} will return
206       * <code>true</code>.</p>
207       */
208      protected Object getProperty(final int id)
209      {
210          wasNull = false;
211          for (int i = 0; i < properties.length; i++)
212              if (id == properties[i].getID())
213                  return properties[i].getValue();
214          wasNull = true;
215          return null;
216      }
217  
218  
219  
220      /**
221       * <p>Returns the value of the numeric property with the specified
222       * ID. If the property is not available, 0 is returned. A
223       * subsequent call to {@link #wasNull} will return
224       * <code>true</code> to let the caller distinguish that case from
225       * a real property value of 0.</p>
226       */
227      protected int getPropertyIntValue(final int id)
228      {
229          final Integer i = (Integer) getProperty(id);
230          if (i != null)
231              return i.intValue();
232          else
233              return 0;
234      }
235  
236  
237  
238      private boolean wasNull;
239  
240      /**
241       * <p>Checks whether the property which the last call to {@link
242       * #getPropertyIntValue} or {@link #getProperty} tried to access
243       * was available or not. This information might be important for
244       * callers of {@link #getPropertyIntValue} since the latter
245       * returns 0 if the property does not exist. Using {@link
246       * #wasNull} the caller can distiguish this case from a property's
247       * real value of 0.</p>
248       *
249       * @return <code>true</code> if the last call to {@link
250       * #getPropertyIntValue} or {@link #getProperty} tried to access a
251       * property that was not available, else <code>false</code>.
252       */
253      public boolean wasNull()
254      {
255          return wasNull;
256      }
257  
258  
259  
260      /**
261       * <p>Returns the PID string associated with a property ID. The ID
262       * is first looked up in the {@link Section}'s private
263       * dictionary. If it is not found there, the method calls {@link
264       * SectionIDMap#getPIDString}.</p>
265       */
266      public String getPIDString(final int pid)
267      {
268          String s = null;
269          if (dictionary != null)
270              s = (String) dictionary.get(new Integer(pid));
271          if (s == null)
272              s =  SectionIDMap.getPIDString(getFormatID().getBytes(), pid);
273          if (s == null)
274              s = SectionIDMap.UNDEFINED;
275          return s;
276      }
277  
278  }
279