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.

PPTHandler.java 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * ====================================================================
  3. * Licensed to the Apache Software Foundation (ASF) under one or more
  4. * contributor license agreements. See the NOTICE file distributed with
  5. * this work for additional information regarding copyright ownership.
  6. * The ASF licenses this file to You under the Apache License, Version 2.0
  7. * (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. * ====================================================================
  18. */
  19. package org.apache.poi.xslf.util;
  20. import static java.util.Spliterator.NONNULL;
  21. import static java.util.Spliterator.ORDERED;
  22. import java.awt.Graphics2D;
  23. import java.awt.geom.Dimension2D;
  24. import java.io.File;
  25. import java.io.IOException;
  26. import java.io.InputStream;
  27. import java.nio.charset.Charset;
  28. import java.util.Set;
  29. import java.util.Spliterator;
  30. import java.util.Spliterators;
  31. import java.util.TreeSet;
  32. import java.util.function.Consumer;
  33. import java.util.regex.Matcher;
  34. import java.util.regex.Pattern;
  35. import java.util.stream.Collectors;
  36. import java.util.stream.IntStream;
  37. import java.util.stream.Stream;
  38. import java.util.stream.StreamSupport;
  39. import org.apache.poi.common.usermodel.GenericRecord;
  40. import org.apache.poi.sl.draw.EmbeddedExtractor.EmbeddedPart;
  41. import org.apache.poi.sl.usermodel.ObjectData;
  42. import org.apache.poi.sl.usermodel.ObjectShape;
  43. import org.apache.poi.sl.usermodel.Shape;
  44. import org.apache.poi.sl.usermodel.Slide;
  45. import org.apache.poi.sl.usermodel.SlideShow;
  46. import org.apache.poi.sl.usermodel.SlideShowFactory;
  47. import org.apache.poi.util.IOUtils;
  48. import org.apache.poi.util.Internal;
  49. import org.apache.poi.util.LocaleUtil;
  50. /** Handler for ppt and pptx files */
  51. @Internal
  52. class PPTHandler extends MFProxy {
  53. private SlideShow<?,?> ppt;
  54. private Slide<?,?> slide;
  55. private Charset defaultCharset = LocaleUtil.CHARSET_1252;
  56. @Override
  57. public void parse(File file) throws IOException {
  58. try {
  59. ppt = SlideShowFactory.create(file, null, true);
  60. } catch (IOException e) {
  61. if (e.getMessage().contains("scratchpad")) {
  62. throw new PPTX2PNG.NoScratchpadException(e);
  63. } else {
  64. throw e;
  65. }
  66. }
  67. if (ppt == null) {
  68. throw new IOException("Unknown file format or missing poi-scratchpad.jar / poi-ooxml.jar");
  69. }
  70. slide = ppt.getSlides().get(0);
  71. }
  72. @Override
  73. public void parse(InputStream is) throws IOException {
  74. try {
  75. ppt = SlideShowFactory.create(is, null);
  76. } catch (IOException e) {
  77. if (e.getMessage().contains("scratchpad")) {
  78. throw new PPTX2PNG.NoScratchpadException(e);
  79. } else {
  80. throw e;
  81. }
  82. }
  83. if (ppt == null) {
  84. throw new IOException("Unknown file format or missing poi-scratchpad.jar / poi-ooxml.jar");
  85. }
  86. slide = ppt.getSlides().get(0);
  87. }
  88. @Override
  89. public Dimension2D getSize() {
  90. return ppt.getPageSize();
  91. }
  92. @Override
  93. public int getSlideCount() {
  94. return ppt.getSlides().size();
  95. }
  96. @Override
  97. public void setSlideNo(int slideNo) {
  98. slide = ppt.getSlides().get(slideNo-1);
  99. }
  100. @Override
  101. public String getTitle() {
  102. return slide.getTitle();
  103. }
  104. private static final String RANGE_PATTERN = "(^|,)(?<from>\\d+)?(-(?<to>\\d+))?";
  105. @Override
  106. public Set<Integer> slideIndexes(String range) {
  107. final Matcher matcher = Pattern.compile(RANGE_PATTERN).matcher(range);
  108. Spliterator<Matcher> sp = new Spliterators.AbstractSpliterator<Matcher>(range.length(), ORDERED|NONNULL){
  109. @Override
  110. public boolean tryAdvance(Consumer<? super Matcher> action) {
  111. boolean b = matcher.find();
  112. if (b) {
  113. action.accept(matcher);
  114. }
  115. return b;
  116. }
  117. };
  118. return StreamSupport.stream(sp, false).
  119. flatMap(this::range).
  120. collect(Collectors.toCollection(TreeSet::new));
  121. }
  122. @Override
  123. public void draw(Graphics2D ctx) {
  124. slide.draw(ctx);
  125. }
  126. @Override
  127. public void close() throws IOException {
  128. if (ppt != null) {
  129. ppt.close();
  130. }
  131. }
  132. @Override
  133. public GenericRecord getRoot() {
  134. return (ppt instanceof GenericRecord) ? (GenericRecord)ppt : null;
  135. }
  136. private Stream<Integer> range(Matcher m) {
  137. final int slideCount = ppt.getSlides().size();
  138. String fromStr = m.group("from");
  139. String toStr = m.group("to");
  140. int from = (fromStr == null || fromStr.isEmpty() ? 1 : Integer.parseInt(fromStr));
  141. int to = (toStr == null) ? from
  142. : (toStr.isEmpty() || ((fromStr == null || fromStr.isEmpty()) && "1".equals(toStr))) ? slideCount
  143. : Integer.parseInt(toStr);
  144. return IntStream.rangeClosed(from, to).filter(i -> i <= slideCount).boxed();
  145. }
  146. @Override
  147. public Iterable<EmbeddedPart> getEmbeddings(int slideNo) {
  148. return () -> ppt.getSlides().get(slideNo).getShapes().stream().
  149. filter(s -> s instanceof ObjectShape).
  150. map(PPTHandler::fromObjectShape).
  151. iterator()
  152. ;
  153. }
  154. private static EmbeddedPart fromObjectShape(Shape<?,?> s) {
  155. final ObjectShape<?,?> os = (ObjectShape<?,?>)s;
  156. final ObjectData od = os.getObjectData();
  157. EmbeddedPart embed = new EmbeddedPart();
  158. embed.setName(od.getFileName());
  159. embed.setData(() -> {
  160. try (InputStream is = od.getInputStream()) {
  161. return IOUtils.toByteArray(is);
  162. } catch (IOException e) {
  163. // TODO: change to custom runtime exception
  164. throw new RuntimeException(e);
  165. }
  166. });
  167. return embed;
  168. }
  169. @Override
  170. void setDefaultCharset(Charset charset) {
  171. }
  172. }