1    /*
2     *  ====================================================================
3     *  The Apache Software License, Version 1.1
4     *
5     *  Copyright (c) 2000 The Apache Software Foundation.  All rights
6     *  reserved.
7     *
8     *  Redistribution and use in source and binary forms, with or without
9     *  modification, are permitted provided that the following conditions
10    *  are met:
11    *
12    *  1. Redistributions of source code must retain the above copyright
13    *  notice, this list of conditions and the following disclaimer.
14    *
15    *  2. Redistributions in binary form must reproduce the above copyright
16    *  notice, this list of conditions and the following disclaimer in
17    *  the documentation and/or other materials provided with the
18    *  distribution.
19    *
20    *  3. The end-user documentation included with the redistribution,
21    *  if any, must include the following acknowledgment:
22    *  "This product includes software developed by the
23    *  Apache Software Foundation (http://www.apache.org/)."
24    *  Alternately, this acknowledgment may appear in the software itself,
25    *  if and wherever such third-party acknowledgments normally appear.
26    *
27    *  4. The names "Apache" and "Apache Software Foundation" must
28    *  not be used to endorse or promote products derived from this
29    *  software without prior written permission. For written
30    *  permission, please contact apache@apache.org.
31    *
32    *  5. Products derived from this software may not be called "Apache",
33    *  nor may "Apache" appear in their name, without prior written
34    *  permission of the Apache Software Foundation.
35    *
36    *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37    *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38    *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39    *  DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40    *  ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41    *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42    *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43    *  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44    *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45    *  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46    *  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47    *  SUCH DAMAGE.
48    *  ====================================================================
49    *
50    *  This software consists of voluntary contributions made by many
51    *  individuals on behalf of the Apache Software Foundation.  For more
52    *  information on the Apache Software Foundation, please see
53    *  <http://www.apache.org/>.
54    */
55   package org.apache.poi.hpsf.wellknown;
56   
57   import java.util.*;
58   
59   /**
60    * <p>Maps section format IDs to {@link PropertyIDMap}s. It is
61    * initialized with two well-known section format IDs: those of the
62    * <tt>\005SummaryInformation</tt> stream and the
63    * <tt>\005DocumentSummaryInformation</tt> stream.</p>
64    *
65    * <p>If you have a section format ID you can use it as a key to query
66    * this map.  If you get a {@link PropertyIDMap} returned your section
67    * is well-known and you can query the {@link PropertyIDMap} for PID
68    * strings. If you get back <code>null</code> you are on your own.</p>
69    *
70    * <p>This {@link Map} expects the byte arrays of section format IDs
71    * as keys. A key maps to a {@link PropertyIDMap} describing the
72    * property IDs in sections with the specified section format ID.</p>
73    *
74    * @author Rainer Klute (klute@rainer-klute.de)
75    * @version $Id: SectionIDMap.java,v 1.5 2002/12/10 06:15:19 klute Exp $
76    * @since 2002-02-09
77    */
78   public class SectionIDMap extends HashMap
79   {
80   
81       /**
82        * <p>The SummaryInformation's section's format ID.</p>
83        */
84       public final static byte[] SUMMARY_INFORMATION_ID = new byte[]
85       {
86   	(byte) 0xF2, (byte) 0x9F, (byte) 0x85, (byte) 0xE0,
87   	(byte) 0x4F, (byte) 0xF9, (byte) 0x10, (byte) 0x68,
88   	(byte) 0xAB, (byte) 0x91, (byte) 0x08, (byte) 0x00,
89   	(byte) 0x2B, (byte) 0x27, (byte) 0xB3, (byte) 0xD9
90       };
91   
92       /**
93        * <p>The DocumentSummaryInformation's first section's format
94        * ID. The second section has a different format ID which is not
95        * well-known.</p>
96        */
97       public final static byte[] DOCUMENT_SUMMARY_INFORMATION_ID = new byte[]
98       {
99   	(byte) 0xD5, (byte) 0xCD, (byte) 0xD5, (byte) 0x02,
100  	(byte) 0x2E, (byte) 0x9C, (byte) 0x10, (byte) 0x1B,
101  	(byte) 0x93, (byte) 0x97, (byte) 0x08, (byte) 0x00,
102  	(byte) 0x2B, (byte) 0x2C, (byte) 0xF9, (byte) 0xAE
103      };
104  
105      public final static String UNDEFINED = "[undefined]";
106  
107      private static SectionIDMap defaultMap;
108  
109  
110  
111      /**
112       * <p>Returns the singleton instance of the default {@link
113       * SectionIDMap}.</p>
114       *
115       * @return The instance value
116       */
117      public static SectionIDMap getInstance()
118      {
119          if (defaultMap == null)
120  	{
121              final SectionIDMap m = new SectionIDMap();
122              m.put(SUMMARY_INFORMATION_ID,
123  		  PropertyIDMap.getSummaryInformationProperties());
124              m.put(DOCUMENT_SUMMARY_INFORMATION_ID,
125  		  PropertyIDMap.getDocumentSummaryInformationProperties());
126              defaultMap = m;
127          }
128          return defaultMap;
129      }
130  
131  
132  
133      /**
134       * <p>Returns the property ID string that is associated with a
135       * given property ID in a section format ID's namespace.</p>
136       *
137       * @param sectionFormatID Each section format ID has its own name
138       * space of property ID strings and thus must be specified.
139       * @param  pid The property ID
140       * @return The well-known property ID string associated with the
141       * property ID <var>pid</var> in the name space spanned by <var>
142       * sectionFormatID</var> . If the <var>pid</var>
143       * /<var>sectionFormatID </var> combination is not well-known, the
144       * string "[undefined]" is returned.
145       */
146      public static String getPIDString(final byte[] sectionFormatID,
147  				      final int pid)
148      {
149          final PropertyIDMap m =
150  	    (PropertyIDMap) getInstance().get(sectionFormatID);
151          if (m == null)
152              return UNDEFINED;
153          else
154  	{
155              final String s = (String) m.get(pid);
156              if (s == null)
157                  return UNDEFINED;
158              return s;
159          }
160      }
161  
162  
163  
164      /**
165       * <p>Returns the {@link PropertyIDMap} for a given section format
166       * ID.</p>
167       */
168      public PropertyIDMap get(final byte[] sectionFormatID)
169      {
170          return (PropertyIDMap) super.get(new String(sectionFormatID));
171      }
172  
173  
174  
175      /**
176       * <p>Returns the {@link PropertyIDMap} for a given section format
177       * ID.</p>
178       *
179       * @param sectionFormatID A section format ID as a <tt>byte[]</tt> .
180       * @deprecated Use {@link #get(byte[])} instead!
181       */
182      public Object get(final Object sectionFormatID)
183      {
184          return get((byte[]) sectionFormatID);
185      }
186  
187  
188  
189      /**
190       * <p>Associates a section format ID with a {@link
191       * PropertyIDMap}.</p>
192       */
193      public Object put(final byte[] sectionFormatID,
194  		      final PropertyIDMap propertyIDMap)
195      {
196          return super.put(new String(sectionFormatID), propertyIDMap);
197      }
198  
199  
200  
201      /**
202       * @deprecated Use {@link #put(byte[], PropertyIDMap)} instead!
203       */
204      public Object put(final Object key, final Object value)
205      {
206          return put((byte[]) key, (PropertyIDMap) value);
207      }
208  
209  }
210