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.

POIXMLPropertiesTextExtractor.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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.ooxml.extractor;
  16. import java.math.BigDecimal;
  17. import java.text.DateFormat;
  18. import java.text.DateFormatSymbols;
  19. import java.text.SimpleDateFormat;
  20. import java.util.Date;
  21. import java.util.Locale;
  22. import java.util.Optional;
  23. import org.apache.poi.extractor.POITextExtractor;
  24. import org.apache.poi.ooxml.POIXMLDocument;
  25. import org.apache.poi.openxml4j.opc.internal.PackagePropertiesPart;
  26. import org.apache.poi.util.LocaleUtil;
  27. import org.openxmlformats.schemas.officeDocument.x2006.customProperties.CTProperty;
  28. /**
  29. * A {@link POITextExtractor} for returning the textual
  30. * content of the OOXML file properties, eg author
  31. * and title.
  32. */
  33. public class POIXMLPropertiesTextExtractor implements POIXMLTextExtractor {
  34. private final POIXMLDocument doc;
  35. private final DateFormat dateFormat;
  36. private boolean doCloseFilesystem = true;
  37. /**
  38. * Creates a new POIXMLPropertiesTextExtractor for the given open document.
  39. *
  40. * @param doc the given open document
  41. */
  42. public POIXMLPropertiesTextExtractor(POIXMLDocument doc) {
  43. this.doc = doc;
  44. DateFormatSymbols dfs = DateFormatSymbols.getInstance(Locale.ROOT);
  45. dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", dfs);
  46. dateFormat.setTimeZone(LocaleUtil.TIMEZONE_UTC);
  47. }
  48. /**
  49. * Creates a new POIXMLPropertiesTextExtractor, for the
  50. * same file that another TextExtractor is already
  51. * working on.
  52. *
  53. * @param otherExtractor the extractor referencing the given file
  54. */
  55. public POIXMLPropertiesTextExtractor(POIXMLTextExtractor otherExtractor) {
  56. this(otherExtractor.getDocument());
  57. }
  58. private void appendIfPresent(StringBuilder text, String thing, boolean value) {
  59. appendIfPresent(text, thing, Boolean.toString(value));
  60. }
  61. private void appendIfPresent(StringBuilder text, String thing, int value) {
  62. appendIfPresent(text, thing, Integer.toString(value));
  63. }
  64. private void appendDateIfPresent(StringBuilder text, String thing, Optional<Date> value) {
  65. if (!value.isPresent()) {
  66. return;
  67. }
  68. appendIfPresent(text, thing, dateFormat.format(value.get()));
  69. }
  70. private void appendIfPresent(StringBuilder text, String thing, Optional<String> value) {
  71. if (!value.isPresent()) {
  72. return;
  73. }
  74. appendIfPresent(text, thing, value.get());
  75. }
  76. private void appendIfPresent(StringBuilder text, String thing, String value) {
  77. if (value == null) {
  78. return;
  79. }
  80. text.append(thing);
  81. text.append(" = ");
  82. text.append(value);
  83. text.append('\n');
  84. }
  85. /**
  86. * Returns the core document properties, eg author
  87. *
  88. * @return the core document properties
  89. */
  90. @SuppressWarnings("resource")
  91. public String getCorePropertiesText() {
  92. POIXMLDocument document = getDocument();
  93. if (document == null) { // event based extractor does not have a document
  94. return "";
  95. }
  96. StringBuilder text = new StringBuilder(64);
  97. PackagePropertiesPart props =
  98. document.getProperties().getCoreProperties().getUnderlyingProperties();
  99. appendIfPresent(text, "Category", props.getCategoryProperty());
  100. appendIfPresent(text, "Category", props.getCategoryProperty());
  101. appendIfPresent(text, "ContentStatus", props.getContentStatusProperty());
  102. appendIfPresent(text, "ContentType", props.getContentTypeProperty());
  103. appendDateIfPresent(text, "Created", props.getCreatedProperty());
  104. appendIfPresent(text, "CreatedString", props.getCreatedPropertyString());
  105. appendIfPresent(text, "Creator", props.getCreatorProperty());
  106. appendIfPresent(text, "Description", props.getDescriptionProperty());
  107. appendIfPresent(text, "Identifier", props.getIdentifierProperty());
  108. appendIfPresent(text, "Keywords", props.getKeywordsProperty());
  109. appendIfPresent(text, "Language", props.getLanguageProperty());
  110. appendIfPresent(text, "LastModifiedBy", props.getLastModifiedByProperty());
  111. appendDateIfPresent(text, "LastPrinted", props.getLastPrintedProperty());
  112. appendIfPresent(text, "LastPrintedString", props.getLastPrintedPropertyString());
  113. appendDateIfPresent(text, "Modified", props.getModifiedProperty());
  114. appendIfPresent(text, "ModifiedString", props.getModifiedPropertyString());
  115. appendIfPresent(text, "Revision", props.getRevisionProperty());
  116. appendIfPresent(text, "Subject", props.getSubjectProperty());
  117. appendIfPresent(text, "Title", props.getTitleProperty());
  118. appendIfPresent(text, "Version", props.getVersionProperty());
  119. return text.toString();
  120. }
  121. /**
  122. * Returns the extended document properties, eg application
  123. *
  124. * @return the extended document properties
  125. */
  126. @SuppressWarnings("resource")
  127. public String getExtendedPropertiesText() {
  128. POIXMLDocument document = getDocument();
  129. if (document == null) { // event based extractor does not have a document
  130. return "";
  131. }
  132. StringBuilder text = new StringBuilder(64);
  133. org.openxmlformats.schemas.officeDocument.x2006.extendedProperties.CTProperties
  134. props = document.getProperties().getExtendedProperties().getUnderlyingProperties();
  135. appendIfPresent(text, "Application", props.getApplication());
  136. appendIfPresent(text, "AppVersion", props.getAppVersion());
  137. appendIfPresent(text, "Characters", props.getCharacters());
  138. appendIfPresent(text, "CharactersWithSpaces", props.getCharactersWithSpaces());
  139. appendIfPresent(text, "Company", props.getCompany());
  140. appendIfPresent(text, "HyperlinkBase", props.getHyperlinkBase());
  141. appendIfPresent(text, "HyperlinksChanged", props.getHyperlinksChanged());
  142. appendIfPresent(text, "Lines", props.getLines());
  143. appendIfPresent(text, "LinksUpToDate", props.getLinksUpToDate());
  144. appendIfPresent(text, "Manager", props.getManager());
  145. appendIfPresent(text, "Pages", props.getPages());
  146. appendIfPresent(text, "Paragraphs", props.getParagraphs());
  147. appendIfPresent(text, "PresentationFormat", props.getPresentationFormat());
  148. appendIfPresent(text, "Template", props.getTemplate());
  149. appendIfPresent(text, "TotalTime", props.getTotalTime());
  150. return text.toString();
  151. }
  152. /**
  153. * Returns the custom document properties, if there are any
  154. *
  155. * @return the custom document properties
  156. */
  157. @SuppressWarnings({"resource"})
  158. public String getCustomPropertiesText() {
  159. POIXMLDocument document = getDocument();
  160. if (document == null) { // event based extractor does not have a document
  161. return "";
  162. }
  163. StringBuilder text = new StringBuilder();
  164. org.openxmlformats.schemas.officeDocument.x2006.customProperties.CTProperties
  165. props = document.getProperties().getCustomProperties().getUnderlyingProperties();
  166. for (CTProperty property : props.getPropertyList()) {
  167. String val = "(not implemented!)";
  168. if (property.isSetLpwstr()) {
  169. val = property.getLpwstr();
  170. } else if (property.isSetLpstr()) {
  171. val = property.getLpstr();
  172. } else if (property.isSetDate()) {
  173. val = property.getDate().toString();
  174. } else if (property.isSetFiletime()) {
  175. val = property.getFiletime().toString();
  176. } else if (property.isSetBool()) {
  177. val = Boolean.toString(property.getBool());
  178. }
  179. // Integers
  180. else if (property.isSetI1()) {
  181. val = Integer.toString(property.getI1());
  182. } else if (property.isSetI2()) {
  183. val = Integer.toString(property.getI2());
  184. } else if (property.isSetI4()) {
  185. val = Integer.toString(property.getI4());
  186. } else if (property.isSetI8()) {
  187. val = Long.toString(property.getI8());
  188. } else if (property.isSetInt()) {
  189. val = Integer.toString(property.getInt());
  190. }
  191. // Unsigned Integers
  192. else if (property.isSetUi1()) {
  193. val = Integer.toString(property.getUi1());
  194. } else if (property.isSetUi2()) {
  195. val = Integer.toString(property.getUi2());
  196. } else if (property.isSetUi4()) {
  197. val = Long.toString(property.getUi4());
  198. } else if (property.isSetUi8()) {
  199. val = property.getUi8().toString();
  200. } else if (property.isSetUint()) {
  201. val = Long.toString(property.getUint());
  202. }
  203. // Reals
  204. else if (property.isSetR4()) {
  205. val = Float.toString(property.getR4());
  206. } else if (property.isSetR8()) {
  207. val = Double.toString(property.getR8());
  208. } else if (property.isSetDecimal()) {
  209. BigDecimal d = property.getDecimal();
  210. if (d == null) {
  211. val = null;
  212. } else {
  213. val = d.toPlainString();
  214. }
  215. }
  216. /*else if (property.isSetArray()) {
  217. // TODO Fetch the array values and output
  218. }
  219. else if (property.isSetVector()) {
  220. // TODO Fetch the vector values and output
  221. }
  222. else if (property.isSetBlob() || property.isSetOblob()) {
  223. // TODO Decode, if possible
  224. }
  225. else if (property.isSetStream() || property.isSetOstream() ||
  226. property.isSetVstream()) {
  227. // TODO Decode, if possible
  228. }
  229. else if (property.isSetStorage() || property.isSetOstorage()) {
  230. // TODO Decode, if possible
  231. }*/
  232. text.append(property.getName()).append(" = ").append(val).append("\n");
  233. }
  234. return text.toString();
  235. }
  236. @Override
  237. public String getText() {
  238. try {
  239. return
  240. getCorePropertiesText() +
  241. getExtendedPropertiesText() +
  242. getCustomPropertiesText();
  243. } catch (Exception e) {
  244. throw new RuntimeException(e);
  245. }
  246. }
  247. @Override
  248. public POIXMLPropertiesTextExtractor getMetadataTextExtractor() {
  249. throw new IllegalStateException("You already have the Metadata Text Extractor, not recursing!");
  250. }
  251. @Override
  252. public POIXMLDocument getDocument() {
  253. return doc;
  254. }
  255. @Override
  256. public void setCloseFilesystem(boolean doCloseFilesystem) {
  257. this.doCloseFilesystem = doCloseFilesystem;
  258. }
  259. @Override
  260. public boolean isCloseFilesystem() {
  261. return doCloseFilesystem;
  262. }
  263. @Override
  264. public POIXMLDocument getFilesystem() {
  265. return null;
  266. }
  267. }