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

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