| 1 | package pro.verron.officestamper.utils.pml; | |
| 2 | ||
| 3 | import org.docx4j.openpackaging.exceptions.Docx4JException; | |
| 4 | import org.docx4j.openpackaging.packages.PresentationMLPackage; | |
| 5 | import org.docx4j.openpackaging.parts.PresentationML.SlidePart; | |
| 6 | import org.docx4j.wml.ContentAccessor; | |
| 7 | import org.docx4j.wml.SdtBlock; | |
| 8 | import org.docx4j.wml.SdtRun; | |
| 9 | import org.jspecify.annotations.Nullable; | |
| 10 | import org.pptx4j.Pptx4jException; | |
| 11 | import org.pptx4j.pml.Shape; | |
| 12 | import org.slf4j.Logger; | |
| 13 | import org.slf4j.LoggerFactory; | |
| 14 | import pro.verron.officestamper.utils.UtilsException; | |
| 15 | import pro.verron.officestamper.utils.iterator.ResetableIterator; | |
| 16 | ||
| 17 | import java.util.*; | |
| 18 | import java.util.function.Supplier; | |
| 19 | ||
| 20 | import static org.docx4j.XmlUtils.unwrap; | |
| 21 | ||
| 22 | /// An iterator implementation for traversing PowerPoint presentation content. This class provides a way to iterate | |
| 23 | /// through all content elements in a PowerPoint file, including slides, shapes, text blocks, and other document | |
| 24 | /// structure elements. | |
| 25 | /// | |
| 26 | /// The iterator handles the complex hierarchical structure of PowerPoint presentations and provides a flat iteration | |
| 27 | /// interface over all content objects. | |
| 28 | /// | |
| 29 | /// @author verron | |
| 30 | /// @since 3.0 | |
| 31 | public class PptxIterator | |
| 32 | implements ResetableIterator<Object> { | |
| 33 | ||
| 34 | private static final Logger log = LoggerFactory.getLogger(PptxIterator.class); | |
| 35 | private final Supplier<Iterator<?>> supplier; | |
| 36 | private Queue<Iterator<?>> iteratorQueue; | |
| 37 | private @Nullable Object next; | |
| 38 | ||
| 39 | /// Constructs a new PptxIterator for the given presentation package. | |
| 40 | /// | |
| 41 | /// @param presentation the PowerPoint presentation package to iterate through | |
| 42 | /// | |
| 43 | /// @throws UtilsException if there is an error accessing the presentation structure | |
| 44 | public PptxIterator(PresentationMLPackage presentation) { | |
| 45 | try { | |
| 46 | var mainPresentationPart = presentation.getMainPresentationPart(); | |
| 47 | var slideParts = mainPresentationPart.getSlideParts(); | |
| 48 | supplier = slideParts::iterator; | |
| 49 | } catch (Pptx4jException e) { | |
| 50 | throw new UtilsException(e); | |
| 51 | } | |
| 52 | var startingIterator = supplier.get(); | |
| 53 | this.iteratorQueue = Collections.asLifoQueue(new ArrayDeque<>()); | |
| 54 | this.iteratorQueue.add(startingIterator); | |
| 55 |
1
1. <init> : negated conditional → NO_COVERAGE |
this.next = startingIterator.hasNext() ? unwrap(startingIterator.next()) : null; |
| 56 | } | |
| 57 | ||
| 58 | @Override | |
| 59 | public void reset() { | |
| 60 | var startingIterator = supplier.get(); | |
| 61 | this.iteratorQueue = Collections.asLifoQueue(new ArrayDeque<>()); | |
| 62 | this.iteratorQueue.add(startingIterator); | |
| 63 |
1
1. reset : negated conditional → NO_COVERAGE |
this.next = startingIterator.hasNext() ? unwrap(startingIterator.next()) : null; |
| 64 | } | |
| 65 | ||
| 66 | @Override | |
| 67 | public boolean hasNext() { | |
| 68 |
2
1. hasNext : replaced boolean return with true for pro/verron/officestamper/utils/pml/PptxIterator::hasNext → NO_COVERAGE 2. hasNext : negated conditional → NO_COVERAGE |
return next != null; |
| 69 | } | |
| 70 | ||
| 71 | @Override | |
| 72 | public Object next() { | |
| 73 |
1
1. next : negated conditional → NO_COVERAGE |
if (next == null) throw new NoSuchElementException("No more elements to iterate"); |
| 74 | ||
| 75 | var result = next; | |
| 76 | next = null; | |
| 77 | switch (result) { | |
| 78 | case ContentAccessor contentAccessor -> { | |
| 79 | var content = contentAccessor.getContent(); | |
| 80 | iteratorQueue.add(content.iterator()); | |
| 81 | } | |
| 82 | case SdtRun sdtRun -> { | |
| 83 | var sdtContent = sdtRun.getSdtContent(); | |
| 84 | var content = sdtContent.getContent(); | |
| 85 | iteratorQueue.add(content.iterator()); | |
| 86 | } | |
| 87 | case SdtBlock sdtBlock -> { | |
| 88 | var sdtContent = sdtBlock.getSdtContent(); | |
| 89 | var content = sdtContent.getContent(); | |
| 90 | iteratorQueue.add(content.iterator()); | |
| 91 | } | |
| 92 | case SlidePart slidePart -> { | |
| 93 | List<Object> content; | |
| 94 | try { | |
| 95 | content = slidePart.getContents() | |
| 96 | .getCSld() | |
| 97 | .getSpTree() | |
| 98 | .getSpOrGrpSpOrGraphicFrame(); | |
| 99 | } catch (Docx4JException e) { | |
| 100 | throw new UtilsException(e); | |
| 101 | } | |
| 102 | iteratorQueue.add(content.iterator()); | |
| 103 | } | |
| 104 | case Shape shape -> { | |
| 105 | var content = shape.getTxBody() | |
| 106 | .getP(); | |
| 107 | iteratorQueue.add(content.iterator()); | |
| 108 | } | |
| 109 | default -> log.debug("Unknown type: {}", result.getClass()); | |
| 110 | } | |
| 111 |
2
1. next : negated conditional → NO_COVERAGE 2. next : negated conditional → NO_COVERAGE |
while (!iteratorQueue.isEmpty() && next == null) { |
| 112 | var nextIterator = iteratorQueue.poll(); | |
| 113 |
1
1. next : negated conditional → NO_COVERAGE |
if (nextIterator.hasNext()) { |
| 114 | next = unwrap(nextIterator.next()); | |
| 115 | iteratorQueue.add(nextIterator); | |
| 116 | } | |
| 117 | } | |
| 118 |
1
1. next : replaced return value with null for pro/verron/officestamper/utils/pml/PptxIterator::next → NO_COVERAGE |
return result; |
| 119 | } | |
| 120 | } | |
Mutations | ||
| 55 |
1.1 |
|
| 63 |
1.1 |
|
| 68 |
1.1 2.2 |
|
| 73 |
1.1 |
|
| 111 |
1.1 2.2 |
|
| 113 |
1.1 |
|
| 118 |
1.1 |