You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Section.java 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.hpsf;
  16. import java.io.UnsupportedEncodingException;
  17. import java.util.ArrayList;
  18. import java.util.Collections;
  19. import java.util.Iterator;
  20. import java.util.List;
  21. import java.util.Map;
  22. import org.apache.poi.hpsf.wellknown.PropertyIDMap;
  23. import org.apache.poi.hpsf.wellknown.SectionIDMap;
  24. import org.apache.poi.util.LittleEndian;
  25. /**
  26. * <p>Represents a section in a {@link PropertySet}.</p>
  27. *
  28. * @author Rainer Klute <a
  29. * href="mailto:klute@rainer-klute.de">&lt;klute@rainer-klute.de&gt;</a>
  30. * @author Drew Varner (Drew.Varner allUpIn sc.edu)
  31. */
  32. public class Section
  33. {
  34. /**
  35. * <p>Maps property IDs to section-private PID strings. These
  36. * strings can be found in the property with ID 0.</p>
  37. */
  38. protected Map<Long,String> dictionary;
  39. /**
  40. * <p>The section's format ID, {@link #getFormatID}.</p>
  41. */
  42. protected ClassID formatID;
  43. /**
  44. * <p>Returns the format ID. The format ID is the "type" of the
  45. * section. For example, if the format ID of the first {@link
  46. * Section} contains the bytes specified by
  47. * <code>org.apache.poi.hpsf.wellknown.SectionIDMap.SUMMARY_INFORMATION_ID</code>
  48. * the section (and thus the property set) is a SummaryInformation.</p>
  49. *
  50. * @return The format ID
  51. */
  52. public ClassID getFormatID()
  53. {
  54. return formatID;
  55. }
  56. /**
  57. * @see #getOffset
  58. */
  59. protected long offset;
  60. /**
  61. * <p>Returns the offset of the section in the stream.</p>
  62. *
  63. * @return The offset of the section in the stream.
  64. */
  65. public long getOffset()
  66. {
  67. return offset;
  68. }
  69. /**
  70. * @see #getSize
  71. */
  72. protected int size;
  73. /**
  74. * <p>Returns the section's size in bytes.</p>
  75. *
  76. * @return The section's size in bytes.
  77. */
  78. public int getSize()
  79. {
  80. return size;
  81. }
  82. /**
  83. * <p>Returns the number of properties in this section.</p>
  84. *
  85. * @return The number of properties in this section.
  86. */
  87. public int getPropertyCount()
  88. {
  89. return properties.length;
  90. }
  91. /**
  92. * @see #getProperties
  93. */
  94. protected Property[] properties;
  95. /**
  96. * <p>Returns this section's properties.</p>
  97. *
  98. * @return This section's properties.
  99. */
  100. public Property[] getProperties()
  101. {
  102. return properties;
  103. }
  104. /**
  105. * <p>Creates an empty and uninitialized {@link Section}.
  106. */
  107. protected Section()
  108. { }
  109. /**
  110. * <p>Creates a {@link Section} instance from a byte array.</p>
  111. *
  112. * @param src Contains the complete property set stream.
  113. * @param offset The position in the stream that points to the
  114. * section's format ID.
  115. *
  116. * @exception UnsupportedEncodingException if the section's codepage is not
  117. * supported.
  118. */
  119. public Section(final byte[] src, final int offset)
  120. throws UnsupportedEncodingException
  121. {
  122. int o1 = offset;
  123. /*
  124. * Read the format ID.
  125. */
  126. formatID = new ClassID(src, o1);
  127. o1 += ClassID.LENGTH;
  128. /*
  129. * Read the offset from the stream's start and positions to
  130. * the section header.
  131. */
  132. this.offset = LittleEndian.getUInt(src, o1);
  133. o1 = (int) this.offset;
  134. /*
  135. * Read the section length.
  136. */
  137. size = (int) LittleEndian.getUInt(src, o1);
  138. o1 += LittleEndian.INT_SIZE;
  139. /*
  140. * Read the number of properties.
  141. */
  142. final int propertyCount = (int) LittleEndian.getUInt(src, o1);
  143. o1 += LittleEndian.INT_SIZE;
  144. /*
  145. * Read the properties. The offset is positioned at the first
  146. * entry of the property list. There are two problems:
  147. *
  148. * 1. For each property we have to find out its length. In the
  149. * property list we find each property's ID and its offset relative
  150. * to the section's beginning. Unfortunately the properties in the
  151. * property list need not to be in ascending order, so it is not
  152. * possible to calculate the length as
  153. * (offset of property(i+1) - offset of property(i)). Before we can
  154. * that we first have to sort the property list by ascending offsets.
  155. *
  156. * 2. We have to read the property with ID 1 before we read other
  157. * properties, at least before other properties containing strings.
  158. * The reason is that property 1 specifies the codepage. If it is
  159. * 1200, all strings are in Unicode. In other words: Before we can
  160. * read any strings we have to know whether they are in Unicode or
  161. * not. Unfortunately property 1 is not guaranteed to be the first in
  162. * a section.
  163. *
  164. * The algorithm below reads the properties in two passes: The first
  165. * one looks for property ID 1 and extracts the codepage number. The
  166. * seconds pass reads the other properties.
  167. */
  168. properties = new Property[propertyCount];
  169. /* Pass 1: Read the property list. */
  170. int pass1Offset = o1;
  171. final List<PropertyListEntry> propertyList = new ArrayList<PropertyListEntry>(propertyCount);
  172. PropertyListEntry ple;
  173. for (int i = 0; i < properties.length; i++)
  174. {
  175. ple = new PropertyListEntry();
  176. /* Read the property ID. */
  177. ple.id = (int) LittleEndian.getUInt(src, pass1Offset);
  178. pass1Offset += LittleEndian.INT_SIZE;
  179. /* Offset from the section's start. */
  180. ple.offset = (int) LittleEndian.getUInt(src, pass1Offset);
  181. pass1Offset += LittleEndian.INT_SIZE;
  182. /* Add the entry to the property list. */
  183. propertyList.add(ple);
  184. }
  185. /* Sort the property list by ascending offsets: */
  186. Collections.sort(propertyList);
  187. /* Calculate the properties' lengths. */
  188. for (int i = 0; i < propertyCount - 1; i++)
  189. {
  190. PropertyListEntry ple1 = propertyList.get(i);
  191. PropertyListEntry ple2 = propertyList.get(i + 1);
  192. ple1.length = ple2.offset - ple1.offset;
  193. }
  194. if (propertyCount > 0)
  195. {
  196. ple = propertyList.get(propertyCount - 1);
  197. ple.length = size - ple.offset;
  198. }
  199. /* Look for the codepage. */
  200. int codepage = -1;
  201. for (final Iterator<PropertyListEntry> i = propertyList.iterator();
  202. codepage == -1 && i.hasNext();)
  203. {
  204. ple = i.next();
  205. /* Read the codepage if the property ID is 1. */
  206. if (ple.id == PropertyIDMap.PID_CODEPAGE)
  207. {
  208. /* Read the property's value type. It must be
  209. * VT_I2. */
  210. int o = (int) (this.offset + ple.offset);
  211. final long type = LittleEndian.getUInt(src, o);
  212. o += LittleEndian.INT_SIZE;
  213. if (type != Variant.VT_I2)
  214. throw new HPSFRuntimeException
  215. ("Value type of property ID 1 is not VT_I2 but " +
  216. type + ".");
  217. /* Read the codepage number. */
  218. codepage = LittleEndian.getUShort(src, o);
  219. }
  220. }
  221. /* Pass 2: Read all properties - including the codepage property,
  222. * if available. */
  223. int i1 = 0;
  224. for (final Iterator<PropertyListEntry> i = propertyList.iterator(); i.hasNext();)
  225. {
  226. ple = i.next();
  227. Property p = new Property(ple.id, src,
  228. this.offset + ple.offset,
  229. ple.length, codepage);
  230. if (p.getID() == PropertyIDMap.PID_CODEPAGE)
  231. p = new Property(p.getID(), p.getType(), Integer.valueOf(codepage));
  232. properties[i1++] = p;
  233. }
  234. /*
  235. * Extract the dictionary (if available).
  236. */
  237. dictionary = (Map) getProperty(0);
  238. }
  239. /**
  240. * <p>Represents an entry in the property list and holds a property's ID and
  241. * its offset from the section's beginning.</p>
  242. */
  243. class PropertyListEntry implements Comparable<PropertyListEntry>
  244. {
  245. int id;
  246. int offset;
  247. int length;
  248. /**
  249. * <p>Compares this {@link PropertyListEntry} with another one by their
  250. * offsets. A {@link PropertyListEntry} is "smaller" than another one if
  251. * its offset from the section's begin is smaller.</p>
  252. *
  253. * @see Comparable#compareTo(java.lang.Object)
  254. */
  255. public int compareTo(final PropertyListEntry o)
  256. {
  257. final int otherOffset = o.offset;
  258. if (offset < otherOffset)
  259. return -1;
  260. else if (offset == otherOffset)
  261. return 0;
  262. else
  263. return 1;
  264. }
  265. public String toString()
  266. {
  267. final StringBuffer b = new StringBuffer();
  268. b.append(getClass().getName());
  269. b.append("[id=");
  270. b.append(id);
  271. b.append(", offset=");
  272. b.append(offset);
  273. b.append(", length=");
  274. b.append(length);
  275. b.append(']');
  276. return b.toString();
  277. }
  278. }
  279. /**
  280. * <p>Returns the value of the property with the specified ID. If
  281. * the property is not available, <code>null</code> is returned
  282. * and a subsequent call to {@link #wasNull} will return
  283. * <code>true</code>.</p>
  284. *
  285. * @param id The property's ID
  286. *
  287. * @return The property's value
  288. */
  289. public Object getProperty(final long id)
  290. {
  291. wasNull = false;
  292. for (int i = 0; i < properties.length; i++)
  293. if (id == properties[i].getID())
  294. return properties[i].getValue();
  295. wasNull = true;
  296. return null;
  297. }
  298. /**
  299. * <p>Returns the value of the numeric property with the specified
  300. * ID. If the property is not available, 0 is returned. A
  301. * subsequent call to {@link #wasNull} will return
  302. * <code>true</code> to let the caller distinguish that case from
  303. * a real property value of 0.</p>
  304. *
  305. * @param id The property's ID
  306. *
  307. * @return The property's value
  308. */
  309. protected int getPropertyIntValue(final long id)
  310. {
  311. final Number i;
  312. final Object o = getProperty(id);
  313. if (o == null)
  314. return 0;
  315. if (!(o instanceof Long || o instanceof Integer))
  316. throw new HPSFRuntimeException
  317. ("This property is not an integer type, but " +
  318. o.getClass().getName() + ".");
  319. i = (Number) o;
  320. return i.intValue();
  321. }
  322. /**
  323. * <p>Returns the value of the boolean property with the specified
  324. * ID. If the property is not available, <code>false</code> is
  325. * returned. A subsequent call to {@link #wasNull} will return
  326. * <code>true</code> to let the caller distinguish that case from
  327. * a real property value of <code>false</code>.</p>
  328. *
  329. * @param id The property's ID
  330. *
  331. * @return The property's value
  332. */
  333. protected boolean getPropertyBooleanValue(final int id)
  334. {
  335. final Boolean b = (Boolean) getProperty(id);
  336. if (b == null) {
  337. return false;
  338. }
  339. return b.booleanValue();
  340. }
  341. /**
  342. * <p>This member is <code>true</code> if the last call to {@link
  343. * #getPropertyIntValue} or {@link #getProperty} tried to access a
  344. * property that was not available, else <code>false</code>.</p>
  345. */
  346. private boolean wasNull;
  347. /**
  348. * <p>Checks whether the property which the last call to {@link
  349. * #getPropertyIntValue} or {@link #getProperty} tried to access
  350. * was available or not. This information might be important for
  351. * callers of {@link #getPropertyIntValue} since the latter
  352. * returns 0 if the property does not exist. Using {@link
  353. * #wasNull} the caller can distiguish this case from a property's
  354. * real value of 0.</p>
  355. *
  356. * @return <code>true</code> if the last call to {@link
  357. * #getPropertyIntValue} or {@link #getProperty} tried to access a
  358. * property that was not available, else <code>false</code>.
  359. */
  360. public boolean wasNull()
  361. {
  362. return wasNull;
  363. }
  364. /**
  365. * <p>Returns the PID string associated with a property ID. The ID
  366. * is first looked up in the {@link Section}'s private
  367. * dictionary. If it is not found there, the method calls {@link
  368. * SectionIDMap#getPIDString}.</p>
  369. *
  370. * @param pid The property ID
  371. *
  372. * @return The property ID's string value
  373. */
  374. public String getPIDString(final long pid)
  375. {
  376. String s = null;
  377. if (dictionary != null)
  378. s = (String) dictionary.get(Long.valueOf(pid));
  379. if (s == null)
  380. s = SectionIDMap.getPIDString(getFormatID().getBytes(), pid);
  381. if (s == null)
  382. s = SectionIDMap.UNDEFINED;
  383. return s;
  384. }
  385. /**
  386. * <p>Checks whether this section is equal to another object. The result is
  387. * <code>false</code> if one of the the following conditions holds:</p>
  388. *
  389. * <ul>
  390. *
  391. * <li><p>The other object is not a {@link Section}.</p></li>
  392. *
  393. * <li><p>The format IDs of the two sections are not equal.</p></li>
  394. *
  395. * <li><p>The sections have a different number of properties. However,
  396. * properties with ID 1 (codepage) are not counted.</p></li>
  397. *
  398. * <li><p>The other object is not a {@link Section}.</p></li>
  399. *
  400. * <li><p>The properties have different values. The order of the properties
  401. * is irrelevant.</p></li>
  402. *
  403. * </ul>
  404. *
  405. * @param o The object to compare this section with
  406. * @return <code>true</code> if the objects are equal, <code>false</code> if
  407. * not
  408. */
  409. public boolean equals(final Object o)
  410. {
  411. if (o == null || !(o instanceof Section))
  412. return false;
  413. final Section s = (Section) o;
  414. if (!s.getFormatID().equals(getFormatID()))
  415. return false;
  416. /* Compare all properties except 0 and 1 as they must be handled
  417. * specially. */
  418. Property[] pa1 = new Property[getProperties().length];
  419. Property[] pa2 = new Property[s.getProperties().length];
  420. System.arraycopy(getProperties(), 0, pa1, 0, pa1.length);
  421. System.arraycopy(s.getProperties(), 0, pa2, 0, pa2.length);
  422. /* Extract properties 0 and 1 and remove them from the copy of the
  423. * arrays. */
  424. Property p10 = null;
  425. Property p20 = null;
  426. for (int i = 0; i < pa1.length; i++)
  427. {
  428. final long id = pa1[i].getID();
  429. if (id == 0)
  430. {
  431. p10 = pa1[i];
  432. pa1 = remove(pa1, i);
  433. i--;
  434. }
  435. if (id == 1)
  436. {
  437. // p11 = pa1[i];
  438. pa1 = remove(pa1, i);
  439. i--;
  440. }
  441. }
  442. for (int i = 0; i < pa2.length; i++)
  443. {
  444. final long id = pa2[i].getID();
  445. if (id == 0)
  446. {
  447. p20 = pa2[i];
  448. pa2 = remove(pa2, i);
  449. i--;
  450. }
  451. if (id == 1)
  452. {
  453. // p21 = pa2[i];
  454. pa2 = remove(pa2, i);
  455. i--;
  456. }
  457. }
  458. /* If the number of properties (not counting property 1) is unequal the
  459. * sections are unequal. */
  460. if (pa1.length != pa2.length)
  461. return false;
  462. /* If the dictionaries are unequal the sections are unequal. */
  463. boolean dictionaryEqual = true;
  464. if (p10 != null && p20 != null)
  465. dictionaryEqual = p10.getValue().equals(p20.getValue());
  466. else if (p10 != null || p20 != null)
  467. dictionaryEqual = false;
  468. if (dictionaryEqual) {
  469. return Util.equals(pa1, pa2);
  470. }
  471. return false;
  472. }
  473. /**
  474. * <p>Removes a field from a property array. The resulting array is
  475. * compactified and returned.</p>
  476. *
  477. * @param pa The property array.
  478. * @param i The index of the field to be removed.
  479. * @return the compactified array.
  480. */
  481. private Property[] remove(final Property[] pa, final int i)
  482. {
  483. final Property[] h = new Property[pa.length - 1];
  484. if (i > 0)
  485. System.arraycopy(pa, 0, h, 0, i);
  486. System.arraycopy(pa, i + 1, h, i, h.length - i);
  487. return h;
  488. }
  489. /**
  490. * @see Object#hashCode()
  491. */
  492. public int hashCode()
  493. {
  494. long hashCode = 0;
  495. hashCode += getFormatID().hashCode();
  496. final Property[] pa = getProperties();
  497. for (int i = 0; i < pa.length; i++)
  498. hashCode += pa[i].hashCode();
  499. final int returnHashCode = (int) (hashCode & 0x0ffffffffL);
  500. return returnHashCode;
  501. }
  502. /**
  503. * @see Object#toString()
  504. */
  505. public String toString()
  506. {
  507. final StringBuffer b = new StringBuffer();
  508. final Property[] pa = getProperties();
  509. b.append(getClass().getName());
  510. b.append('[');
  511. b.append("formatID: ");
  512. b.append(getFormatID());
  513. b.append(", offset: ");
  514. b.append(getOffset());
  515. b.append(", propertyCount: ");
  516. b.append(getPropertyCount());
  517. b.append(", size: ");
  518. b.append(getSize());
  519. b.append(", properties: [\n");
  520. for (int i = 0; i < pa.length; i++)
  521. {
  522. b.append(pa[i].toString());
  523. b.append(",\n");
  524. }
  525. b.append(']');
  526. b.append(']');
  527. return b.toString();
  528. }
  529. /**
  530. * <p>Gets the section's dictionary. A dictionary allows an application to
  531. * use human-readable property names instead of numeric property IDs. It
  532. * contains mappings from property IDs to their associated string
  533. * values. The dictionary is stored as the property with ID 0. The codepage
  534. * for the strings in the dictionary is defined by property with ID 1.</p>
  535. *
  536. * @return the dictionary or <code>null</code> if the section does not have
  537. * a dictionary.
  538. */
  539. public Map<Long,String> getDictionary()
  540. {
  541. return dictionary;
  542. }
  543. /**
  544. * <p>Gets the section's codepage, if any.</p>
  545. *
  546. * @return The section's codepage if one is defined, else -1.
  547. */
  548. public int getCodepage()
  549. {
  550. final Integer codepage =
  551. (Integer) getProperty(PropertyIDMap.PID_CODEPAGE);
  552. if (codepage == null)
  553. return -1;
  554. int cp = codepage.intValue();
  555. return cp;
  556. }
  557. }