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.

HwmfEmbeddedIterator.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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.hwmf.usermodel;
  16. import java.io.IOException;
  17. import java.util.ArrayDeque;
  18. import java.util.Deque;
  19. import java.util.Iterator;
  20. import java.util.NoSuchElementException;
  21. import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream;
  22. import org.apache.poi.hwmf.record.HwmfEscape;
  23. import org.apache.poi.hwmf.record.HwmfEscape.EscapeFunction;
  24. import org.apache.poi.hwmf.record.HwmfEscape.WmfEscapeEMF;
  25. import org.apache.poi.hwmf.record.HwmfFill.HwmfImageRecord;
  26. import org.apache.poi.hwmf.record.HwmfRecord;
  27. public class HwmfEmbeddedIterator implements Iterator<HwmfEmbedded> {
  28. private final Deque<Iterator<?>> iterStack = new ArrayDeque<>();
  29. private Object current;
  30. public HwmfEmbeddedIterator(HwmfPicture wmf) {
  31. this(wmf.getRecords().iterator());
  32. }
  33. public HwmfEmbeddedIterator(Iterator<HwmfRecord> recordIterator) {
  34. iterStack.add(recordIterator);
  35. }
  36. @Override
  37. public boolean hasNext() {
  38. if (iterStack.isEmpty()) {
  39. return false;
  40. }
  41. if (current != null) {
  42. // don't search twice and potentially skip items
  43. return true;
  44. }
  45. Iterator<?> iter;
  46. do {
  47. iter = iterStack.peek();
  48. while (iter.hasNext()) {
  49. Object obj = iter.next();
  50. if (obj instanceof HwmfImageRecord) {
  51. current = obj;
  52. return true;
  53. }
  54. if (obj instanceof HwmfEscape && ((HwmfEscape)obj).getEscapeFunction() == EscapeFunction.META_ESCAPE_ENHANCED_METAFILE) {
  55. WmfEscapeEMF emfData = ((HwmfEscape)obj).getEscapeData();
  56. if (emfData.isValid()) {
  57. current = obj;
  58. return true;
  59. }
  60. }
  61. }
  62. iterStack.pop();
  63. } while (!iterStack.isEmpty());
  64. return false;
  65. }
  66. @Override
  67. public HwmfEmbedded next() {
  68. HwmfEmbedded emb;
  69. if ((emb = checkHwmfImageRecord()) != null) {
  70. return emb;
  71. }
  72. if ((emb = checkHwmfEscapeRecord()) != null) {
  73. return emb;
  74. }
  75. throw new NoSuchElementException("no further embedded emf records found.");
  76. }
  77. private HwmfEmbedded checkHwmfImageRecord() {
  78. if (!(current instanceof HwmfImageRecord)) {
  79. return null;
  80. }
  81. HwmfImageRecord hir = (HwmfImageRecord)current;
  82. current = null;
  83. HwmfEmbedded emb = new HwmfEmbedded();
  84. emb.setEmbeddedType(HwmfEmbeddedType.BMP);
  85. emb.setData(hir.getBMPData());
  86. return emb;
  87. }
  88. private HwmfEmbedded checkHwmfEscapeRecord() {
  89. if (!(current instanceof HwmfEscape)) {
  90. return null;
  91. }
  92. final HwmfEmbedded emb = new HwmfEmbedded();
  93. emb.setEmbeddedType(HwmfEmbeddedType.EMF);
  94. try (UnsynchronizedByteArrayOutputStream bos = new UnsynchronizedByteArrayOutputStream()) {
  95. WmfEscapeEMF img;
  96. do {
  97. final HwmfEscape esc = (HwmfEscape)current;
  98. assert(esc.getEscapeFunction() == EscapeFunction.META_ESCAPE_ENHANCED_METAFILE);
  99. img = esc.getEscapeData();
  100. assert(img.isValid());
  101. bos.write(img.getEmfData());
  102. current = null;
  103. } while (img.getRemainingBytes() > 0 && hasNext() && (current instanceof HwmfEscape));
  104. emb.setData(bos.toByteArray());
  105. return emb;
  106. } catch (IOException ignored) {
  107. // UnsynchronizedByteArrayOutputStream doesn't throw IOException
  108. return null;
  109. }
  110. }
  111. }