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.

DocumentSummaryInformation.java 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  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 static org.apache.poi.hpsf.ClassIDPredefined.DOC_SUMMARY;
  17. import static org.apache.poi.hpsf.ClassIDPredefined.USER_PROPERTIES;
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.util.LinkedList;
  21. import java.util.List;
  22. import java.util.Map;
  23. import org.apache.poi.hpsf.wellknown.PropertyIDMap;
  24. /**
  25. * Convenience class representing a DocumentSummary Information stream in a
  26. * Microsoft Office document.
  27. *
  28. * @see SummaryInformation
  29. */
  30. @SuppressWarnings("unused")
  31. public class DocumentSummaryInformation extends PropertySet {
  32. /**
  33. * The document name a document summary information stream
  34. * usually has in a POIFS filesystem.
  35. */
  36. public static final String DEFAULT_STREAM_NAME =
  37. "\005DocumentSummaryInformation";
  38. /**
  39. * The DocumentSummaryInformation's first and second sections' format ID.
  40. */
  41. public static final ClassID[] FORMAT_ID = {
  42. DOC_SUMMARY.getClassID(), USER_PROPERTIES.getClassID()
  43. };
  44. @Override
  45. public PropertyIDMap getPropertySetIDMap() {
  46. return PropertyIDMap.getDocumentSummaryInformationProperties();
  47. }
  48. /**
  49. * Creates an empty {@link DocumentSummaryInformation}.
  50. */
  51. public DocumentSummaryInformation() {
  52. getFirstSection().setFormatID(DOC_SUMMARY.getClassID());
  53. }
  54. /**
  55. * Creates a {@link DocumentSummaryInformation} from a given
  56. * {@link PropertySet}.
  57. *
  58. * @param ps A property set which should be created from a
  59. * document summary information stream.
  60. * @throws UnexpectedPropertySetTypeException if {@code ps}
  61. * does not contain a document summary information stream.
  62. */
  63. public DocumentSummaryInformation(final PropertySet ps)
  64. throws UnexpectedPropertySetTypeException {
  65. super(ps);
  66. if (!isDocumentSummaryInformation()) {
  67. throw new UnexpectedPropertySetTypeException("Not a " + getClass().getName());
  68. }
  69. }
  70. /**
  71. * Creates a {@link DocumentSummaryInformation} instance from an {@link
  72. * InputStream} in the Horrible Property Set Format.<p>
  73. *
  74. * The constructor reads the first few bytes from the stream
  75. * and determines whether it is really a property set stream. If
  76. * it is, it parses the rest of the stream. If it is not, it
  77. * resets the stream to its beginning in order to let other
  78. * components mess around with the data and throws an
  79. * exception.
  80. *
  81. * @param stream Holds the data making out the property set
  82. * stream.
  83. * @throws IOException
  84. * if the {@link InputStream} cannot be accessed as needed.
  85. * @throws NoPropertySetStreamException
  86. * if the input stream does not contain a property set.
  87. */
  88. public DocumentSummaryInformation(final InputStream stream)
  89. throws NoPropertySetStreamException, IOException {
  90. super(stream);
  91. }
  92. /**
  93. * Returns the category (or {@code null}).
  94. *
  95. * @return The category value
  96. */
  97. public String getCategory() {
  98. return getPropertyStringValue(PropertyIDMap.PID_CATEGORY);
  99. }
  100. /**
  101. * Sets the category.
  102. *
  103. * @param category The category to set.
  104. */
  105. public void setCategory(final String category) {
  106. getFirstSection().setProperty(PropertyIDMap.PID_CATEGORY, category);
  107. }
  108. /**
  109. * Removes the category.
  110. */
  111. public void removeCategory() {
  112. remove1stProperty(PropertyIDMap.PID_CATEGORY);
  113. }
  114. /**
  115. * Returns the presentation format (or
  116. * {@code null}).
  117. *
  118. * @return The presentation format value
  119. */
  120. public String getPresentationFormat() {
  121. return getPropertyStringValue(PropertyIDMap.PID_PRESFORMAT);
  122. }
  123. /**
  124. * Sets the presentation format.
  125. *
  126. * @param presentationFormat The presentation format to set.
  127. */
  128. public void setPresentationFormat(final String presentationFormat) {
  129. getFirstSection().setProperty(PropertyIDMap.PID_PRESFORMAT, presentationFormat);
  130. }
  131. /**
  132. * Removes the presentation format.
  133. */
  134. public void removePresentationFormat() {
  135. remove1stProperty(PropertyIDMap.PID_PRESFORMAT);
  136. }
  137. /**
  138. * Returns the byte count or 0 if the {@link
  139. * DocumentSummaryInformation} does not contain a byte count.
  140. *
  141. * @return The byteCount value
  142. */
  143. public int getByteCount() {
  144. return getPropertyIntValue(PropertyIDMap.PID_BYTECOUNT);
  145. }
  146. /**
  147. * Sets the byte count.
  148. *
  149. * @param byteCount The byte count to set.
  150. */
  151. public void setByteCount(final int byteCount) {
  152. set1stProperty(PropertyIDMap.PID_BYTECOUNT, byteCount);
  153. }
  154. /**
  155. * Removes the byte count.
  156. */
  157. public void removeByteCount() {
  158. remove1stProperty(PropertyIDMap.PID_BYTECOUNT);
  159. }
  160. /**
  161. * Returns the line count or 0 if the {@link
  162. * DocumentSummaryInformation} does not contain a line count.
  163. *
  164. * @return The line count value
  165. */
  166. public int getLineCount() {
  167. return getPropertyIntValue(PropertyIDMap.PID_LINECOUNT);
  168. }
  169. /**
  170. * Sets the line count.
  171. *
  172. * @param lineCount The line count to set.
  173. */
  174. public void setLineCount(final int lineCount) {
  175. set1stProperty(PropertyIDMap.PID_LINECOUNT, lineCount);
  176. }
  177. /**
  178. * Removes the line count.
  179. */
  180. public void removeLineCount() {
  181. remove1stProperty(PropertyIDMap.PID_LINECOUNT);
  182. }
  183. /**
  184. * Returns the par count or 0 if the {@link
  185. * DocumentSummaryInformation} does not contain a par count.
  186. *
  187. * @return The par count value
  188. */
  189. public int getParCount() {
  190. return getPropertyIntValue(PropertyIDMap.PID_PARCOUNT);
  191. }
  192. /**
  193. * Sets the par count.
  194. *
  195. * @param parCount The par count to set.
  196. */
  197. public void setParCount(final int parCount) {
  198. set1stProperty(PropertyIDMap.PID_PARCOUNT, parCount);
  199. }
  200. /**
  201. * Removes the par count.
  202. */
  203. public void removeParCount() {
  204. remove1stProperty(PropertyIDMap.PID_PARCOUNT);
  205. }
  206. /**
  207. * Returns the slide count or 0 if the {@link
  208. * DocumentSummaryInformation} does not contain a slide count.
  209. *
  210. * @return The slide count value
  211. */
  212. public int getSlideCount() {
  213. return getPropertyIntValue(PropertyIDMap.PID_SLIDECOUNT);
  214. }
  215. /**
  216. * Sets the slideCount.
  217. *
  218. * @param slideCount The slide count to set.
  219. */
  220. public void setSlideCount(final int slideCount) {
  221. set1stProperty(PropertyIDMap.PID_SLIDECOUNT, slideCount);
  222. }
  223. /**
  224. * Removes the slide count.
  225. */
  226. public void removeSlideCount() {
  227. remove1stProperty(PropertyIDMap.PID_SLIDECOUNT);
  228. }
  229. /**
  230. * Returns the note count or 0 if the {@link
  231. * DocumentSummaryInformation} does not contain a note count.
  232. *
  233. * @return The note count value
  234. */
  235. public int getNoteCount() {
  236. return getPropertyIntValue(PropertyIDMap.PID_NOTECOUNT);
  237. }
  238. /**
  239. * Sets the note count.
  240. *
  241. * @param noteCount The note count to set.
  242. */
  243. public void setNoteCount(final int noteCount) {
  244. set1stProperty(PropertyIDMap.PID_NOTECOUNT, noteCount);
  245. }
  246. /**
  247. * Removes the noteCount.
  248. */
  249. public void removeNoteCount() {
  250. remove1stProperty(PropertyIDMap.PID_NOTECOUNT);
  251. }
  252. /**
  253. * Returns the hidden count or 0 if the {@link
  254. * DocumentSummaryInformation} does not contain a hidden
  255. * count.
  256. *
  257. * @return The hidden count value
  258. */
  259. public int getHiddenCount() {
  260. return getPropertyIntValue(PropertyIDMap.PID_HIDDENCOUNT);
  261. }
  262. /**
  263. * Sets the hidden count.
  264. *
  265. * @param hiddenCount The hidden count to set.
  266. */
  267. public void setHiddenCount(final int hiddenCount) {
  268. set1stProperty(PropertyIDMap.PID_HIDDENCOUNT, hiddenCount);
  269. }
  270. /**
  271. * Removes the hidden count.
  272. */
  273. public void removeHiddenCount() {
  274. remove1stProperty(PropertyIDMap.PID_HIDDENCOUNT);
  275. }
  276. /**
  277. * Returns the mmclip count or 0 if the {@link
  278. * DocumentSummaryInformation} does not contain a mmclip
  279. * count.
  280. *
  281. * @return The mmclip count value
  282. */
  283. public int getMMClipCount() {
  284. return getPropertyIntValue(PropertyIDMap.PID_MMCLIPCOUNT);
  285. }
  286. /**
  287. * Sets the mmclip count.
  288. *
  289. * @param mmClipCount The mmclip count to set.
  290. */
  291. public void setMMClipCount(final int mmClipCount) {
  292. set1stProperty(PropertyIDMap.PID_MMCLIPCOUNT, mmClipCount);
  293. }
  294. /**
  295. * Removes the mmclip count.
  296. */
  297. public void removeMMClipCount() {
  298. remove1stProperty(PropertyIDMap.PID_MMCLIPCOUNT);
  299. }
  300. /**
  301. * Returns {@code true} when scaling of the thumbnail is
  302. * desired, {@code false} if cropping is desired.
  303. *
  304. * @return The scale value
  305. */
  306. public boolean getScale() {
  307. return getPropertyBooleanValue(PropertyIDMap.PID_SCALE);
  308. }
  309. /**
  310. * Sets the scale.
  311. *
  312. * @param scale The scale to set.
  313. */
  314. public void setScale(final boolean scale) {
  315. set1stProperty(PropertyIDMap.PID_SCALE, scale);
  316. }
  317. /**
  318. * Removes the scale.
  319. */
  320. public void removeScale() {
  321. remove1stProperty(PropertyIDMap.PID_SCALE);
  322. }
  323. /**
  324. * <p>Returns the heading pair (or {@code null})
  325. * <strong>when this method is implemented. Please note that the
  326. * return type is likely to change!</strong>
  327. *
  328. * @return The heading pair value
  329. */
  330. public byte[] getHeadingPair() {
  331. notYetImplemented("Reading byte arrays ");
  332. return (byte[]) getProperty(PropertyIDMap.PID_HEADINGPAIR);
  333. }
  334. /**
  335. * Sets the heading pair.
  336. *
  337. * @param headingPair The heading pair to set.
  338. */
  339. public void setHeadingPair(final byte[] headingPair) {
  340. notYetImplemented("Writing byte arrays ");
  341. }
  342. /**
  343. * Removes the heading pair.
  344. */
  345. public void removeHeadingPair() {
  346. remove1stProperty(PropertyIDMap.PID_HEADINGPAIR);
  347. }
  348. /**
  349. * <p>Returns the doc parts (or {@code null})
  350. * <strong>when this method is implemented. Please note that the
  351. * return type is likely to change!</strong>
  352. *
  353. * @return The doc parts value
  354. */
  355. public byte[] getDocparts() {
  356. notYetImplemented("Reading byte arrays");
  357. return (byte[]) getProperty(PropertyIDMap.PID_DOCPARTS);
  358. }
  359. /**
  360. * Sets the doc parts.
  361. *
  362. * @param docparts The doc parts to set.
  363. */
  364. public void setDocparts(final byte[] docparts) {
  365. notYetImplemented("Writing byte arrays");
  366. }
  367. /**
  368. * Removes the doc parts.
  369. */
  370. public void removeDocparts() {
  371. remove1stProperty(PropertyIDMap.PID_DOCPARTS);
  372. }
  373. /**
  374. * Returns the manager (or {@code null}).
  375. *
  376. * @return The manager value
  377. */
  378. public String getManager() {
  379. return getPropertyStringValue(PropertyIDMap.PID_MANAGER);
  380. }
  381. /**
  382. * Sets the manager.
  383. *
  384. * @param manager The manager to set.
  385. */
  386. public void setManager(final String manager) {
  387. set1stProperty(PropertyIDMap.PID_MANAGER, manager);
  388. }
  389. /**
  390. * Removes the manager.
  391. */
  392. public void removeManager() {
  393. remove1stProperty(PropertyIDMap.PID_MANAGER);
  394. }
  395. /**
  396. * Returns the company (or {@code null}).
  397. *
  398. * @return The company value
  399. */
  400. public String getCompany() {
  401. return getPropertyStringValue(PropertyIDMap.PID_COMPANY);
  402. }
  403. /**
  404. * Sets the company.
  405. *
  406. * @param company The company to set.
  407. */
  408. public void setCompany(final String company) {
  409. set1stProperty(PropertyIDMap.PID_COMPANY, company);
  410. }
  411. /**
  412. * Removes the company.
  413. */
  414. public void removeCompany() {
  415. remove1stProperty(PropertyIDMap.PID_COMPANY);
  416. }
  417. /**
  418. * Returns {@code true} if the custom links are dirty.
  419. *
  420. * @return The links dirty value
  421. */
  422. public boolean getLinksDirty() {
  423. return getPropertyBooleanValue(PropertyIDMap.PID_LINKSDIRTY);
  424. }
  425. /**
  426. * Sets the linksDirty.
  427. *
  428. * @param linksDirty The links dirty value to set.
  429. */
  430. public void setLinksDirty(final boolean linksDirty) {
  431. set1stProperty(PropertyIDMap.PID_LINKSDIRTY, linksDirty);
  432. }
  433. /**
  434. * Removes the links dirty.
  435. */
  436. public void removeLinksDirty() {
  437. remove1stProperty(PropertyIDMap.PID_LINKSDIRTY);
  438. }
  439. /**
  440. * Returns the character count including whitespace, or 0 if the
  441. * {@link DocumentSummaryInformation} does not contain this char count.
  442. * <p>This is the whitespace-including version of {@link SummaryInformation#getCharCount()}
  443. *
  444. * @return The character count or {@code null}
  445. */
  446. public int getCharCountWithSpaces() {
  447. return getPropertyIntValue(PropertyIDMap.PID_CCHWITHSPACES);
  448. }
  449. /**
  450. * Sets the character count including whitespace
  451. *
  452. * @param count The character count to set.
  453. */
  454. public void setCharCountWithSpaces(int count) {
  455. set1stProperty(PropertyIDMap.PID_CCHWITHSPACES, count);
  456. }
  457. /**
  458. * Removes the character count
  459. */
  460. public void removeCharCountWithSpaces() {
  461. remove1stProperty(PropertyIDMap.PID_CCHWITHSPACES);
  462. }
  463. /**
  464. * Get if the User Defined Property Set has been updated outside of the
  465. * Application.<p>
  466. * If it has (true), the hyperlinks should be updated on document load.
  467. *
  468. * @return true, if the hyperlinks should be updated on document load
  469. */
  470. public boolean getHyperlinksChanged() {
  471. return getPropertyBooleanValue(PropertyIDMap.PID_HYPERLINKSCHANGED);
  472. }
  473. /**
  474. * Set the flag for if the User Defined Property Set has been updated outside
  475. * of the Application.
  476. *
  477. * @param changed true, if the User Defined Property Set has been updated
  478. */
  479. public void setHyperlinksChanged(boolean changed) {
  480. set1stProperty(PropertyIDMap.PID_HYPERLINKSCHANGED, changed);
  481. }
  482. /**
  483. * Removes the flag for if the User Defined Property Set has been updated
  484. * outside of the Application.
  485. */
  486. public void removeHyperlinksChanged() {
  487. remove1stProperty(PropertyIDMap.PID_HYPERLINKSCHANGED);
  488. }
  489. /**
  490. * Gets the version of the Application which wrote the
  491. * Property set, stored with the two high order bytes having the major
  492. * version number, and the two low order bytes the minor version number.<p>
  493. * This will be 0 if no version is set.
  494. *
  495. * @return the Application version
  496. */
  497. public int getApplicationVersion() {
  498. return getPropertyIntValue(PropertyIDMap.PID_VERSION);
  499. }
  500. /**
  501. * Sets the Application version, which must be a 4 byte int with
  502. * the two high order bytes having the major version number, and the
  503. * two low order bytes the minor version number.
  504. *
  505. * @param version the Application version
  506. */
  507. public void setApplicationVersion(int version) {
  508. set1stProperty(PropertyIDMap.PID_VERSION, version);
  509. }
  510. /**
  511. * Removes the Application Version
  512. */
  513. public void removeApplicationVersion() {
  514. remove1stProperty(PropertyIDMap.PID_VERSION);
  515. }
  516. /**
  517. * Returns the VBA digital signature for the VBA project
  518. * embedded in the document (or {@code null}).
  519. *
  520. * @return the VBA digital signature
  521. */
  522. public byte[] getVBADigitalSignature() {
  523. Object value = getProperty(PropertyIDMap.PID_DIGSIG);
  524. return (value instanceof byte[]) ? (byte[])value : null;
  525. }
  526. /**
  527. * Sets the VBA digital signature for the VBA project
  528. * embedded in the document.
  529. *
  530. * @param signature VBA Digital Signature for the project
  531. */
  532. public void setVBADigitalSignature(byte[] signature) {
  533. set1stProperty(PropertyIDMap.PID_DIGSIG, signature);
  534. }
  535. /**
  536. * Removes the VBA Digital Signature
  537. */
  538. public void removeVBADigitalSignature() {
  539. remove1stProperty(PropertyIDMap.PID_DIGSIG);
  540. }
  541. /**
  542. * Gets the content type of the file (or {@code null}).
  543. *
  544. * @return the content type of the file
  545. */
  546. public String getContentType() {
  547. return getPropertyStringValue(PropertyIDMap.PID_CONTENTTYPE);
  548. }
  549. /**
  550. * Sets the content type of the file
  551. *
  552. * @param type the content type of the file
  553. */
  554. public void setContentType(String type) {
  555. set1stProperty(PropertyIDMap.PID_CONTENTTYPE, type);
  556. }
  557. /**
  558. * Removes the content type of the file
  559. */
  560. public void removeContentType() {
  561. remove1stProperty(PropertyIDMap.PID_CONTENTTYPE);
  562. }
  563. /**
  564. * Gets the content status of the file (or {@code null}).
  565. *
  566. * @return the content status of the file
  567. */
  568. public String getContentStatus() {
  569. return getPropertyStringValue(PropertyIDMap.PID_CONTENTSTATUS);
  570. }
  571. /**
  572. * Sets the content status of the file
  573. *
  574. * @param status the content status of the file
  575. */
  576. public void setContentStatus(String status) {
  577. set1stProperty(PropertyIDMap.PID_CONTENTSTATUS, status);
  578. }
  579. /**
  580. * Removes the content status of the file
  581. */
  582. public void removeContentStatus() {
  583. remove1stProperty(PropertyIDMap.PID_CONTENTSTATUS);
  584. }
  585. /**
  586. * Gets the document language, which is normally unset and empty (or {@code null}).
  587. *
  588. * @return the document language
  589. */
  590. public String getLanguage() {
  591. return getPropertyStringValue(PropertyIDMap.PID_LANGUAGE);
  592. }
  593. /**
  594. * Set the document language
  595. *
  596. * @param language the document language
  597. */
  598. public void setLanguage(String language) {
  599. set1stProperty(PropertyIDMap.PID_LANGUAGE, language);
  600. }
  601. /**
  602. * Removes the document language
  603. */
  604. public void removeLanguage() {
  605. remove1stProperty(PropertyIDMap.PID_LANGUAGE);
  606. }
  607. /**
  608. * Gets the document version as a string, which is normally unset and empty
  609. * (or {@code null}).
  610. *
  611. * @return the document verion
  612. */
  613. public String getDocumentVersion() {
  614. return getPropertyStringValue(PropertyIDMap.PID_DOCVERSION);
  615. }
  616. /**
  617. * Sets the document version string
  618. *
  619. * @param version the document version string
  620. */
  621. public void setDocumentVersion(String version) {
  622. set1stProperty(PropertyIDMap.PID_DOCVERSION, version);
  623. }
  624. /**
  625. * Removes the document version string
  626. */
  627. public void removeDocumentVersion() {
  628. remove1stProperty(PropertyIDMap.PID_DOCVERSION);
  629. }
  630. /**
  631. * Gets the custom properties.
  632. *
  633. * @return The custom properties.
  634. */
  635. public CustomProperties getCustomProperties() {
  636. CustomProperties cps = null;
  637. if (getSectionCount() >= 2) {
  638. cps = new CustomProperties();
  639. final Section section = getSections().get(1);
  640. final Map<Long,String> dictionary = section.getDictionary();
  641. final Property[] properties = section.getProperties();
  642. int propertyCount = 0;
  643. for (Property p : properties) {
  644. final long id = p.getID();
  645. if (id == PropertyIDMap.PID_CODEPAGE) {
  646. cps.setCodepage((Integer)p.getValue());
  647. } else if (id > PropertyIDMap.PID_CODEPAGE) {
  648. propertyCount++;
  649. final CustomProperty cp = new CustomProperty(p, dictionary.get(id));
  650. cps.put(cp.getName(), cp);
  651. }
  652. }
  653. if (cps.size() != propertyCount) {
  654. cps.setPure(false);
  655. }
  656. }
  657. return cps;
  658. }
  659. /**
  660. * Sets the custom properties.
  661. *
  662. * @param customProperties The custom properties
  663. */
  664. public void setCustomProperties(final CustomProperties customProperties) {
  665. ensureSection2();
  666. final Section section = getSections().get(1);
  667. final Map<Long,String> dictionary = customProperties.getDictionary();
  668. // section.clear();
  669. /* Set the codepage. If both custom properties and section have a
  670. * codepage, the codepage from the custom properties wins, else take the
  671. * one that is defined. If none is defined, take ISO-8859-1. */
  672. int cpCodepage = customProperties.getCodepage();
  673. if (cpCodepage < 0) {
  674. cpCodepage = section.getCodepage();
  675. }
  676. if (cpCodepage < 0) {
  677. cpCodepage = Property.DEFAULT_CODEPAGE;
  678. }
  679. customProperties.setCodepage(cpCodepage);
  680. section.setCodepage(cpCodepage);
  681. section.setDictionary(dictionary);
  682. for (CustomProperty p : customProperties.properties()) {
  683. section.setProperty(p);
  684. }
  685. }
  686. /**
  687. * Creates section 2 if it is not already present.
  688. */
  689. private void ensureSection2() {
  690. if (getSectionCount() < 2) {
  691. Section s2 = new Section();
  692. s2.setFormatID(USER_PROPERTIES.getClassID());
  693. addSection(s2);
  694. }
  695. }
  696. /**
  697. * Removes the custom properties.
  698. */
  699. public void removeCustomProperties() {
  700. if (getSectionCount() < 2) {
  701. throw new HPSFRuntimeException("Illegal internal format of Document SummaryInformation stream: second section is missing.");
  702. }
  703. List<Section> l = new LinkedList<>(getSections());
  704. clearSections();
  705. int idx = 0;
  706. for (Section s : l) {
  707. if (idx++ != 1) {
  708. addSection(s);
  709. }
  710. }
  711. }
  712. /**
  713. * Throws an {@link UnsupportedOperationException} with a message text
  714. * telling which functionality is not yet implemented.
  715. *
  716. * @param msg text telling was leaves to be implemented, e.g.
  717. * "Reading byte arrays".
  718. */
  719. private void notYetImplemented(final String msg) {
  720. throw new UnsupportedOperationException(msg + " is not yet implemented.");
  721. }
  722. }