PageRenderTime 57ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/components/forks/poi/src/loci/poi/hpsf/MutableSection.java

http://github.com/openmicroscopy/bioformats
Java | 739 lines | 335 code | 100 blank | 304 comment | 53 complexity | 4af2341597b1380f5573c853dfcdca4d MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, Apache-2.0, BSD-2-Clause, MPL-2.0-no-copyleft-exception
  1. /*
  2. * #%L
  3. * Fork of Apache Jakarta POI.
  4. * %%
  5. * Copyright (C) 2008 - 2013 Open Microscopy Environment:
  6. * - Board of Regents of the University of Wisconsin-Madison
  7. * - Glencoe Software, Inc.
  8. * - University of Dundee
  9. * %%
  10. * Licensed under the Apache License, Version 2.0 (the "License");
  11. * you may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS,
  18. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. * #L%
  22. */
  23. /* ====================================================================
  24. Licensed to the Apache Software Foundation (ASF) under one or more
  25. contributor license agreements. See the NOTICE file distributed with
  26. this work for additional information regarding copyright ownership.
  27. The ASF licenses this file to You under the Apache License, Version 2.0
  28. (the "License"); you may not use this file except in compliance with
  29. the License. You may obtain a copy of the License at
  30. http://www.apache.org/licenses/LICENSE-2.0
  31. Unless required by applicable law or agreed to in writing, software
  32. distributed under the License is distributed on an "AS IS" BASIS,
  33. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  34. See the License for the specific language governing permissions and
  35. limitations under the License.
  36. ==================================================================== */
  37. package loci.poi.hpsf;
  38. import java.io.ByteArrayOutputStream;
  39. import java.io.IOException;
  40. import java.io.OutputStream;
  41. import java.util.Collections;
  42. import java.util.Comparator;
  43. import java.util.Date;
  44. import java.util.Iterator;
  45. import java.util.LinkedList;
  46. import java.util.List;
  47. import java.util.ListIterator;
  48. import java.util.Map;
  49. import loci.poi.hpsf.wellknown.PropertyIDMap;
  50. import loci.poi.util.LittleEndian;
  51. /**
  52. * <p>Adds writing capability to the {@link Section} class.</p>
  53. *
  54. * <p>Please be aware that this class' functionality will be merged into the
  55. * {@link Section} class at a later time, so the API will change.</p>
  56. *
  57. * @version $Id: MutableSection.java 489730 2006-12-22 19:18:16Z bayard $
  58. * @since 2002-02-20
  59. */
  60. public class MutableSection extends Section
  61. {
  62. /**
  63. * <p>If the "dirty" flag is true, the section's size must be
  64. * (re-)calculated before the section is written.</p>
  65. */
  66. private boolean dirty = true;
  67. /**
  68. * <p>List to assemble the properties. Unfortunately a wrong
  69. * decision has been taken when specifying the "properties" field
  70. * as an Property[]. It should have been a {@link java.util.List}.</p>
  71. */
  72. private List preprops;
  73. /**
  74. * <p>Contains the bytes making out the section. This byte array is
  75. * established when the section's size is calculated and can be reused
  76. * later. It is valid only if the "dirty" flag is false.</p>
  77. */
  78. private byte[] sectionBytes;
  79. /**
  80. * <p>Creates an empty mutable section.</p>
  81. */
  82. public MutableSection()
  83. {
  84. dirty = true;
  85. formatID = null;
  86. offset = -1;
  87. preprops = new LinkedList();
  88. }
  89. /**
  90. * <p>Constructs a <code>MutableSection</code> by doing a deep copy of an
  91. * existing <code>Section</code>. All nested <code>Property</code>
  92. * instances, will be their mutable counterparts in the new
  93. * <code>MutableSection</code>.</p>
  94. *
  95. * @param s The section set to copy
  96. */
  97. public MutableSection(final Section s)
  98. {
  99. setFormatID(s.getFormatID());
  100. final Property[] pa = s.getProperties();
  101. final MutableProperty[] mpa = new MutableProperty[pa.length];
  102. for (int i = 0; i < pa.length; i++)
  103. mpa[i] = new MutableProperty(pa[i]);
  104. setProperties(mpa);
  105. setDictionary(s.getDictionary());
  106. }
  107. /**
  108. * <p>Sets the section's format ID.</p>
  109. *
  110. * @param formatID The section's format ID
  111. *
  112. * @see #setFormatID(byte[])
  113. * @see Section#getFormatID
  114. */
  115. public void setFormatID(final ClassID formatID)
  116. {
  117. this.formatID = formatID;
  118. }
  119. /**
  120. * <p>Sets the section's format ID.</p>
  121. *
  122. * @param formatID The section's format ID as a byte array. It components
  123. * are in big-endian format.
  124. *
  125. * @see #setFormatID(ClassID)
  126. * @see Section#getFormatID
  127. */
  128. public void setFormatID(final byte[] formatID)
  129. {
  130. ClassID fid = getFormatID();
  131. if (fid == null)
  132. {
  133. fid = new ClassID();
  134. setFormatID(fid);
  135. }
  136. fid.setBytes(formatID);
  137. }
  138. /**
  139. * <p>Sets this section's properties. Any former values are overwritten.</p>
  140. *
  141. * @param properties This section's new properties.
  142. */
  143. public void setProperties(final Property[] properties)
  144. {
  145. this.properties = properties;
  146. preprops = new LinkedList();
  147. for (int i = 0; i < properties.length; i++)
  148. preprops.add(properties[i]);
  149. dirty = true;
  150. }
  151. /**
  152. * <p>Sets the string value of the property with the specified ID.</p>
  153. *
  154. * @param id The property's ID
  155. * @param value The property's value. It will be written as a Unicode
  156. * string.
  157. *
  158. * @see #setProperty(int, long, Object)
  159. * @see #getProperty
  160. */
  161. public void setProperty(final int id, final String value)
  162. {
  163. setProperty(id, Variant.VT_LPWSTR, value);
  164. dirty = true;
  165. }
  166. /**
  167. * <p>Sets the int value of the property with the specified ID.</p>
  168. *
  169. * @param id The property's ID
  170. * @param value The property's value.
  171. *
  172. * @see #setProperty(int, long, Object)
  173. * @see #getProperty
  174. */
  175. public void setProperty(final int id, final int value)
  176. {
  177. setProperty(id, Variant.VT_I4, new Integer(value));
  178. dirty = true;
  179. }
  180. /**
  181. * <p>Sets the long value of the property with the specified ID.</p>
  182. *
  183. * @param id The property's ID
  184. * @param value The property's value.
  185. *
  186. * @see #setProperty(int, long, Object)
  187. * @see #getProperty
  188. */
  189. public void setProperty(final int id, final long value)
  190. {
  191. setProperty(id, Variant.VT_I8, new Long(value));
  192. dirty = true;
  193. }
  194. /**
  195. * <p>Sets the boolean value of the property with the specified ID.</p>
  196. *
  197. * @param id The property's ID
  198. * @param value The property's value.
  199. *
  200. * @see #setProperty(int, long, Object)
  201. * @see #getProperty
  202. */
  203. public void setProperty(final int id, final boolean value)
  204. {
  205. setProperty(id, Variant.VT_BOOL, new Boolean(value));
  206. dirty = true;
  207. }
  208. /**
  209. * <p>Sets the value and the variant type of the property with the
  210. * specified ID. If a property with this ID is not yet present in
  211. * the section, it will be added. An already present property with
  212. * the specified ID will be overwritten. A default mapping will be
  213. * used to choose the property's type.</p>
  214. *
  215. * @param id The property's ID.
  216. * @param variantType The property's variant type.
  217. * @param value The property's value.
  218. *
  219. * @see #setProperty(int, String)
  220. * @see #getProperty
  221. * @see Variant
  222. */
  223. public void setProperty(final int id, final long variantType,
  224. final Object value)
  225. {
  226. final MutableProperty p = new MutableProperty();
  227. p.setID(id);
  228. p.setType(variantType);
  229. p.setValue(value);
  230. setProperty(p);
  231. dirty = true;
  232. }
  233. /**
  234. * <p>Sets a property.</p>
  235. *
  236. * @param p The property to be set.
  237. *
  238. * @see #setProperty(int, long, Object)
  239. * @see #getProperty
  240. * @see Variant
  241. */
  242. public void setProperty(final Property p)
  243. {
  244. final long id = p.getID();
  245. removeProperty(id);
  246. preprops.add(p);
  247. dirty = true;
  248. }
  249. /**
  250. * <p>Removes a property.</p>
  251. *
  252. * @param id The ID of the property to be removed
  253. */
  254. public void removeProperty(final long id)
  255. {
  256. for (final Iterator i = preprops.iterator(); i.hasNext();)
  257. if (((Property) i.next()).getID() == id)
  258. {
  259. i.remove();
  260. break;
  261. }
  262. dirty = true;
  263. }
  264. /**
  265. * <p>Sets the value of the boolean property with the specified
  266. * ID.</p>
  267. *
  268. * @param id The property's ID
  269. * @param value The property's value
  270. *
  271. * @see #setProperty(int, long, Object)
  272. * @see #getProperty
  273. * @see Variant
  274. */
  275. protected void setPropertyBooleanValue(final int id, final boolean value)
  276. {
  277. setProperty(id, Variant.VT_BOOL, new Boolean(value));
  278. }
  279. /**
  280. * <p>Returns the section's size.</p>
  281. *
  282. * @return the section's size.
  283. */
  284. public int getSize()
  285. {
  286. if (dirty)
  287. {
  288. try
  289. {
  290. size = calcSize();
  291. dirty = false;
  292. }
  293. catch (HPSFRuntimeException ex)
  294. {
  295. throw ex;
  296. }
  297. catch (Exception ex)
  298. {
  299. throw new HPSFRuntimeException(ex);
  300. }
  301. }
  302. return size;
  303. }
  304. /**
  305. * <p>Calculates the section's size. It is the sum of the lengths of the
  306. * section's header (8), the properties list (16 times the number of
  307. * properties) and the properties themselves.</p>
  308. *
  309. * @return the section's length in bytes.
  310. * @throws WritingNotSupportedException
  311. * @throws IOException
  312. */
  313. private int calcSize() throws WritingNotSupportedException, IOException
  314. {
  315. final ByteArrayOutputStream out = new ByteArrayOutputStream();
  316. write(out);
  317. out.close();
  318. /* Pad to multiple of 4 bytes so that even the Windows shell (explorer)
  319. * shows custom properties. */
  320. sectionBytes = Util.pad4(out.toByteArray());
  321. return sectionBytes.length;
  322. }
  323. /**
  324. * <p>Writes this section into an output stream.</p>
  325. *
  326. * <p>Internally this is done by writing into three byte array output
  327. * streams: one for the properties, one for the property list and one for
  328. * the section as such. The two former are appended to the latter when they
  329. * have received all their data.</p>
  330. *
  331. * @param out The stream to write into.
  332. *
  333. * @return The number of bytes written, i.e. the section's size.
  334. * @exception IOException if an I/O error occurs
  335. * @exception WritingNotSupportedException if HPSF does not yet support
  336. * writing a property's variant type.
  337. */
  338. public int write(final OutputStream out)
  339. throws WritingNotSupportedException, IOException
  340. {
  341. /* Check whether we have already generated the bytes making out the
  342. * section. */
  343. if (!dirty && sectionBytes != null)
  344. {
  345. out.write(sectionBytes);
  346. return sectionBytes.length;
  347. }
  348. /* The properties are written to this stream. */
  349. final ByteArrayOutputStream propertyStream =
  350. new ByteArrayOutputStream();
  351. /* The property list is established here. After each property that has
  352. * been written to "propertyStream", a property list entry is written to
  353. * "propertyListStream". */
  354. final ByteArrayOutputStream propertyListStream =
  355. new ByteArrayOutputStream();
  356. /* Maintain the current position in the list. */
  357. int position = 0;
  358. /* Increase the position variable by the size of the property list so
  359. * that it points behind the property list and to the beginning of the
  360. * properties themselves. */
  361. position += 2 * LittleEndian.INT_SIZE +
  362. getPropertyCount() * 2 * LittleEndian.INT_SIZE;
  363. /* Writing the section's dictionary it tricky. If there is a dictionary
  364. * (property 0) the codepage property (property 1) must be set, too. */
  365. int codepage = -1;
  366. if (getProperty(PropertyIDMap.PID_DICTIONARY) != null)
  367. {
  368. final Object p1 = getProperty(PropertyIDMap.PID_CODEPAGE);
  369. if (p1 != null)
  370. {
  371. if (!(p1 instanceof Integer))
  372. throw new IllegalPropertySetDataException
  373. ("The codepage property (ID = 1) must be an " +
  374. "Integer object.");
  375. }
  376. else
  377. /* Warning: The codepage property is not set although a
  378. * dictionary is present. In order to cope with this problem we
  379. * add the codepage property and set it to Unicode. */
  380. setProperty(PropertyIDMap.PID_CODEPAGE, Variant.VT_I2,
  381. new Integer(Constants.CP_UNICODE));
  382. codepage = getCodepage();
  383. }
  384. /* Sort the property list by their property IDs: */
  385. Collections.sort(preprops, new Comparator()
  386. {
  387. public int compare(final Object o1, final Object o2)
  388. {
  389. final Property p1 = (Property) o1;
  390. final Property p2 = (Property) o2;
  391. if (p1.getID() < p2.getID())
  392. return -1;
  393. else if (p1.getID() == p2.getID())
  394. return 0;
  395. else
  396. return 1;
  397. }
  398. });
  399. /* Write the properties and the property list into their respective
  400. * streams: */
  401. for (final ListIterator i = preprops.listIterator(); i.hasNext();)
  402. {
  403. final MutableProperty p = (MutableProperty) i.next();
  404. final long id = p.getID();
  405. /* Write the property list entry. */
  406. TypeWriter.writeUIntToStream(propertyListStream, p.getID());
  407. TypeWriter.writeUIntToStream(propertyListStream, position);
  408. /* If the property ID is not equal 0 we write the property and all
  409. * is fine. However, if it equals 0 we have to write the section's
  410. * dictionary which has an implicit type only and an explicit
  411. * value. */
  412. if (id != 0)
  413. /* Write the property and update the position to the next
  414. * property. */
  415. position += p.write(propertyStream, getCodepage());
  416. else
  417. {
  418. if (codepage == -1)
  419. throw new IllegalPropertySetDataException
  420. ("Codepage (property 1) is undefined.");
  421. position += writeDictionary(propertyStream, dictionary,
  422. codepage);
  423. }
  424. }
  425. propertyStream.close();
  426. propertyListStream.close();
  427. /* Write the section: */
  428. byte[] pb1 = propertyListStream.toByteArray();
  429. byte[] pb2 = propertyStream.toByteArray();
  430. /* Write the section's length: */
  431. TypeWriter.writeToStream(out, LittleEndian.INT_SIZE * 2 +
  432. pb1.length + pb2.length);
  433. /* Write the section's number of properties: */
  434. TypeWriter.writeToStream(out, getPropertyCount());
  435. /* Write the property list: */
  436. out.write(pb1);
  437. /* Write the properties: */
  438. out.write(pb2);
  439. int streamLength = LittleEndian.INT_SIZE * 2 + pb1.length + pb2.length;
  440. return streamLength;
  441. }
  442. /**
  443. * <p>Writes the section's dictionary.</p>
  444. *
  445. * @param out The output stream to write to.
  446. * @param dictionary The dictionary.
  447. * @param codepage The codepage to be used to write the dictionary items.
  448. * @return The number of bytes written
  449. * @exception IOException if an I/O exception occurs.
  450. */
  451. private static int writeDictionary(final OutputStream out,
  452. final Map dictionary, final int codepage)
  453. throws IOException
  454. {
  455. int length = TypeWriter.writeUIntToStream(out, dictionary.size());
  456. for (final Iterator i = dictionary.keySet().iterator(); i.hasNext();)
  457. {
  458. final Long key = (Long) i.next();
  459. final String value = (String) dictionary.get(key);
  460. if (codepage == Constants.CP_UNICODE)
  461. {
  462. /* Write the dictionary item in Unicode. */
  463. int sLength = value.length() + 1;
  464. if (sLength % 2 == 1)
  465. sLength++;
  466. length += TypeWriter.writeUIntToStream(out, key.longValue());
  467. length += TypeWriter.writeUIntToStream(out, sLength);
  468. final byte[] ca =
  469. value.getBytes(VariantSupport.codepageToEncoding(codepage));
  470. for (int j = 2; j < ca.length; j += 2)
  471. {
  472. out.write(ca[j+1]);
  473. out.write(ca[j]);
  474. length += 2;
  475. }
  476. sLength -= value.length();
  477. while (sLength > 0)
  478. {
  479. out.write(0x00);
  480. out.write(0x00);
  481. length += 2;
  482. sLength--;
  483. }
  484. }
  485. else
  486. {
  487. /* Write the dictionary item in another codepage than
  488. * Unicode. */
  489. length += TypeWriter.writeUIntToStream(out, key.longValue());
  490. length += TypeWriter.writeUIntToStream(out, value.length() + 1);
  491. final byte[] ba =
  492. value.getBytes(VariantSupport.codepageToEncoding(codepage));
  493. for (int j = 0; j < ba.length; j++)
  494. {
  495. out.write(ba[j]);
  496. length++;
  497. }
  498. out.write(0x00);
  499. length++;
  500. }
  501. }
  502. return length;
  503. }
  504. /**
  505. * <p>Overwrites the super class' method to cope with a redundancy:
  506. * the property count is maintained in a separate member variable, but
  507. * shouldn't.</p>
  508. *
  509. * @return The number of properties in this section
  510. */
  511. public int getPropertyCount()
  512. {
  513. return preprops.size();
  514. }
  515. /**
  516. * <p>Gets this section's properties.</p>
  517. *
  518. * @return this section's properties.
  519. */
  520. public Property[] getProperties()
  521. {
  522. properties = (Property[]) preprops.toArray(new Property[0]);
  523. return properties;
  524. }
  525. /**
  526. * <p>Gets a property.</p>
  527. *
  528. * @param id The ID of the property to get
  529. * @return The property or <code>null</code> if there is no such property
  530. */
  531. public Object getProperty(final long id)
  532. {
  533. /* Calling getProperties() ensures that properties and preprops are in
  534. * sync.</p> */
  535. getProperties();
  536. return super.getProperty(id);
  537. }
  538. /**
  539. * <p>Sets the section's dictionary. All keys in the dictionary must be
  540. * {@link java.lang.Long} instances, all values must be
  541. * {@link java.lang.String}s. This method overwrites the properties with IDs
  542. * 0 and 1 since they are reserved for the dictionary and the dictionary's
  543. * codepage. Setting these properties explicitly might have surprising
  544. * effects. An application should never do this but always use this
  545. * method.</p>
  546. *
  547. * @param dictionary The dictionary
  548. *
  549. * @exception IllegalPropertySetDataException if the dictionary's key and
  550. * value types are not correct.
  551. *
  552. * @see Section#getDictionary()
  553. */
  554. public void setDictionary(final Map dictionary)
  555. throws IllegalPropertySetDataException
  556. {
  557. if (dictionary != null)
  558. {
  559. for (final Iterator i = dictionary.keySet().iterator();
  560. i.hasNext();)
  561. if (!(i.next() instanceof Long))
  562. throw new IllegalPropertySetDataException
  563. ("Dictionary keys must be of type Long.");
  564. for (final Iterator i = dictionary.values().iterator();
  565. i.hasNext();)
  566. if (!(i.next() instanceof String))
  567. throw new IllegalPropertySetDataException
  568. ("Dictionary values must be of type String.");
  569. this.dictionary = dictionary;
  570. /* Set the dictionary property (ID 0). Please note that the second
  571. * parameter in the method call below is unused because dictionaries
  572. * don't have a type. */
  573. setProperty(PropertyIDMap.PID_DICTIONARY, -1, dictionary);
  574. /* If the codepage property (ID 1) for the strings (keys and
  575. * values) used in the dictionary is not yet defined, set it to
  576. * Unicode. */
  577. final Integer codepage =
  578. (Integer) getProperty(PropertyIDMap.PID_CODEPAGE);
  579. if (codepage == null)
  580. setProperty(PropertyIDMap.PID_CODEPAGE, Variant.VT_I2,
  581. new Integer(Constants.CP_UNICODE));
  582. }
  583. else
  584. /* Setting the dictionary to null means to remove property 0.
  585. * However, it does not mean to remove property 1 (codepage). */
  586. removeProperty(PropertyIDMap.PID_DICTIONARY);
  587. }
  588. /**
  589. * <p>Sets a property.</p>
  590. *
  591. * @param id The property ID.
  592. * @param value The property's value. The value's class must be one of those
  593. * supported by HPSF.
  594. */
  595. public void setProperty(final int id, final Object value)
  596. {
  597. if (value instanceof String)
  598. setProperty(id, (String) value);
  599. else if (value instanceof Long)
  600. setProperty(id, ((Long) value).longValue());
  601. else if (value instanceof Integer)
  602. setProperty(id, ((Integer) value).intValue());
  603. else if (value instanceof Short)
  604. setProperty(id, ((Short) value).intValue());
  605. else if (value instanceof Boolean)
  606. setProperty(id, ((Boolean) value).booleanValue());
  607. else if (value instanceof Date)
  608. setProperty(id, Variant.VT_FILETIME, value);
  609. else
  610. throw new HPSFRuntimeException(
  611. "HPSF does not support properties of type " +
  612. value.getClass().getName() + ".");
  613. }
  614. /**
  615. * <p>Removes all properties from the section including 0 (dictionary) and
  616. * 1 (codepage).</p>
  617. */
  618. public void clear()
  619. {
  620. final Property[] properties = getProperties();
  621. for (int i = 0; i < properties.length; i++)
  622. {
  623. final Property p = properties[i];
  624. removeProperty(p.getID());
  625. }
  626. }
  627. /**
  628. * <p>Sets the codepage.</p>
  629. *
  630. * @param codepage the codepage
  631. */
  632. public void setCodepage(final int codepage)
  633. {
  634. setProperty(PropertyIDMap.PID_CODEPAGE, Variant.VT_I2,
  635. new Integer(codepage));
  636. }
  637. }