Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

POIXMLTextExtractor.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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.io.IOException;
  17. import org.apache.poi.extractor.POITextExtractor;
  18. import org.apache.poi.ooxml.POIXMLDocument;
  19. import org.apache.poi.ooxml.POIXMLProperties.CoreProperties;
  20. import org.apache.poi.ooxml.POIXMLProperties.CustomProperties;
  21. import org.apache.poi.ooxml.POIXMLProperties.ExtendedProperties;
  22. import org.apache.poi.openxml4j.opc.OPCPackage;
  23. import org.apache.poi.openxml4j.util.ZipSecureFile;
  24. public interface POIXMLTextExtractor extends POITextExtractor {
  25. /**
  26. * Returns the core document properties
  27. *
  28. * @return the core document properties
  29. */
  30. default CoreProperties getCoreProperties() {
  31. return getDocument().getProperties().getCoreProperties();
  32. }
  33. /**
  34. * Returns the extended document properties
  35. *
  36. * @return the extended document properties
  37. */
  38. default ExtendedProperties getExtendedProperties() {
  39. return getDocument().getProperties().getExtendedProperties();
  40. }
  41. /**
  42. * Returns the custom document properties
  43. *
  44. * @return the custom document properties
  45. */
  46. default CustomProperties getCustomProperties() {
  47. return getDocument().getProperties().getCustomProperties();
  48. }
  49. /**
  50. * Returns opened document
  51. *
  52. * @return the opened document
  53. */
  54. @Override
  55. POIXMLDocument getDocument();
  56. /**
  57. * Returns the opened OPCPackage that contains the document
  58. *
  59. * @return the opened OPCPackage
  60. */
  61. default OPCPackage getPackage() {
  62. POIXMLDocument doc = getDocument();
  63. return doc != null ? doc.getPackage() : null;
  64. }
  65. /**
  66. * Returns an OOXML properties text extractor for the
  67. * document properties metadata, such as title and author.
  68. */
  69. @Override
  70. default POIXMLPropertiesTextExtractor getMetadataTextExtractor() {
  71. return new POIXMLPropertiesTextExtractor(getDocument());
  72. }
  73. @Override
  74. default void close() throws IOException {
  75. // e.g. XSSFEventBaseExcelExtractor passes a null-document
  76. if (isCloseFilesystem()) {
  77. @SuppressWarnings("resource")
  78. OPCPackage pkg = getPackage();
  79. if (pkg != null) {
  80. // revert the package to not re-write the file, which is very likely not wanted for a TextExtractor!
  81. pkg.revert();
  82. }
  83. }
  84. }
  85. default void checkMaxTextSize(CharSequence text, String string) {
  86. if(string == null) {
  87. return;
  88. }
  89. int size = text.length() + string.length();
  90. if(size > ZipSecureFile.getMaxTextSize()) {
  91. throw new IllegalStateException("The text would exceed the max allowed overall size of extracted text. "
  92. + "By default this is prevented as some documents may exhaust available memory and it may indicate that the file is used to inflate memory usage and thus could pose a security risk. "
  93. + "You can adjust this limit via ZipSecureFile.setMaxTextSize() if you need to work with files which have a lot of text. "
  94. + "Size: " + size + ", limit: MAX_TEXT_SIZE: " + ZipSecureFile.getMaxTextSize());
  95. }
  96. }
  97. }