PageRenderTime 56ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/tika-parsers/src/main/java/org/apache/tika/parser/microsoft/SummaryExtractor.java

https://github.com/solsson/tika
Java | 155 lines | 118 code | 17 blank | 20 comment | 14 complexity | 64d1deabff94104c27c2076ff362e8df MD5 | raw file
Possible License(s): BSD-3-Clause, MPL-2.0, Apache-2.0
  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. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.tika.parser.microsoft;
  18. import java.io.FileNotFoundException;
  19. import java.io.IOException;
  20. import java.util.Date;
  21. import org.apache.poi.hpsf.CustomProperties;
  22. import org.apache.poi.hpsf.DocumentSummaryInformation;
  23. import org.apache.poi.hpsf.MarkUnsupportedException;
  24. import org.apache.poi.hpsf.NoPropertySetStreamException;
  25. import org.apache.poi.hpsf.PropertySet;
  26. import org.apache.poi.hpsf.SummaryInformation;
  27. import org.apache.poi.hpsf.UnexpectedPropertySetTypeException;
  28. import org.apache.poi.poifs.filesystem.DocumentEntry;
  29. import org.apache.poi.poifs.filesystem.DocumentInputStream;
  30. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  31. import org.apache.tika.exception.TikaException;
  32. import org.apache.tika.metadata.Metadata;
  33. import org.apache.tika.metadata.PagedText;
  34. import org.apache.tika.metadata.Property;
  35. /**
  36. * Outlook Message Parser.
  37. */
  38. class SummaryExtractor {
  39. private static final String SUMMARY_INFORMATION =
  40. SummaryInformation.DEFAULT_STREAM_NAME;
  41. private static final String DOCUMENT_SUMMARY_INFORMATION =
  42. DocumentSummaryInformation.DEFAULT_STREAM_NAME;
  43. private final Metadata metadata;
  44. public SummaryExtractor(Metadata metadata) {
  45. this.metadata = metadata;
  46. }
  47. public void parseSummaries(POIFSFileSystem filesystem)
  48. throws IOException, TikaException {
  49. parseSummaryEntryIfExists(filesystem, SUMMARY_INFORMATION);
  50. parseSummaryEntryIfExists(filesystem, DOCUMENT_SUMMARY_INFORMATION);
  51. }
  52. private void parseSummaryEntryIfExists(
  53. POIFSFileSystem filesystem, String entryName)
  54. throws IOException, TikaException {
  55. try {
  56. DocumentEntry entry =
  57. (DocumentEntry) filesystem.getRoot().getEntry(entryName);
  58. PropertySet properties =
  59. new PropertySet(new DocumentInputStream(entry));
  60. if (properties.isSummaryInformation()) {
  61. parse(new SummaryInformation(properties));
  62. }
  63. if (properties.isDocumentSummaryInformation()) {
  64. parse(new DocumentSummaryInformation(properties));
  65. }
  66. } catch (FileNotFoundException e) {
  67. // entry does not exist, just skip it
  68. } catch (NoPropertySetStreamException e) {
  69. throw new TikaException("Not a HPSF document", e);
  70. } catch (UnexpectedPropertySetTypeException e) {
  71. throw new TikaException("Unexpected HPSF document", e);
  72. } catch (MarkUnsupportedException e) {
  73. throw new TikaException("Invalid DocumentInputStream", e);
  74. }
  75. }
  76. private void parse(SummaryInformation summary) {
  77. set(Metadata.TITLE, summary.getTitle());
  78. set(Metadata.AUTHOR, summary.getAuthor());
  79. set(Metadata.KEYWORDS, summary.getKeywords());
  80. set(Metadata.SUBJECT, summary.getSubject());
  81. set(Metadata.LAST_AUTHOR, summary.getLastAuthor());
  82. set(Metadata.COMMENTS, summary.getComments());
  83. set(Metadata.TEMPLATE, summary.getTemplate());
  84. set(Metadata.APPLICATION_NAME, summary.getApplicationName());
  85. set(Metadata.REVISION_NUMBER, summary.getRevNumber());
  86. set(Metadata.CREATION_DATE, summary.getCreateDateTime());
  87. set(Metadata.CHARACTER_COUNT, summary.getCharCount());
  88. set(Metadata.EDIT_TIME, summary.getEditTime());
  89. set(Metadata.LAST_SAVED, summary.getLastSaveDateTime());
  90. set(Metadata.PAGE_COUNT, summary.getPageCount());
  91. if (summary.getPageCount() > 0) {
  92. metadata.set(PagedText.N_PAGES, summary.getPageCount());
  93. }
  94. set(Metadata.SECURITY, summary.getSecurity());
  95. set(Metadata.WORD_COUNT, summary.getWordCount());
  96. set(Metadata.LAST_PRINTED, summary.getLastPrinted());
  97. }
  98. private void parse(DocumentSummaryInformation summary) {
  99. set(Metadata.COMPANY, summary.getCompany());
  100. set(Metadata.MANAGER, summary.getManager());
  101. set(Metadata.LANGUAGE, getLanguage(summary));
  102. set(Metadata.CATEGORY, summary.getCategory());
  103. set(Metadata.SLIDE_COUNT, summary.getSlideCount());
  104. if (summary.getSlideCount() > 0) {
  105. metadata.set(PagedText.N_PAGES, summary.getSlideCount());
  106. }
  107. }
  108. private String getLanguage(DocumentSummaryInformation summary) {
  109. CustomProperties customProperties = summary.getCustomProperties();
  110. if (customProperties != null) {
  111. Object value = customProperties.get("Language");
  112. if (value instanceof String) {
  113. return (String) value;
  114. }
  115. }
  116. return null;
  117. }
  118. private void set(String name, String value) {
  119. if (value != null) {
  120. metadata.set(name, value);
  121. }
  122. }
  123. private void set(Property property, Date value) {
  124. if (value != null) {
  125. metadata.set(property, value.toString());
  126. }
  127. }
  128. private void set(String name, Date value) {
  129. if (value != null) {
  130. metadata.set(name, value.toString());
  131. }
  132. }
  133. private void set(String name, long value) {
  134. if (value > 0) {
  135. metadata.set(name, Long.toString(value));
  136. }
  137. }
  138. }