| 1 | package pro.verron.officestamper.asciidoc; | |
| 2 | ||
| 3 | import jakarta.xml.bind.JAXBElement; | |
| 4 | import org.docx4j.TextUtils; | |
| 5 | import org.docx4j.openpackaging.exceptions.Docx4JException; | |
| 6 | import org.docx4j.openpackaging.packages.WordprocessingMLPackage; | |
| 7 | import org.docx4j.openpackaging.parts.WordprocessingML.StyleDefinitionsPart; | |
| 8 | import org.docx4j.wml.*; | |
| 9 | ||
| 10 | import java.io.StringWriter; | |
| 11 | ||
| 12 | /// Minimal DOCX → AsciiDoc text extractor used by tests. This intentionally mirrors a subset of the legacy Stringifier | |
| 13 | /// formatting for: | |
| 14 | /// - Paragraphs | |
| 15 | /// - Tables (|=== fences, each cell prefixed with '|') | |
| 16 | /// - Basic inline text extraction More advanced features (headers/footers, breaks, styles) can be added incrementally | |
| 17 | /// as needed by tests. | |
| 18 | final class DocxToAsciiDoc { | |
| 19 | private DocxToAsciiDoc() {} | |
| 20 | ||
| 21 | static String compile(WordprocessingMLPackage pkg, AsciiDocDialect dialect) { | |
| 22 | var sb = new StringBuilder(); | |
| 23 | var mdp = pkg.getMainDocumentPart(); | |
| 24 | StyleDefinitionsPart styles = mdp.getStyleDefinitionsPart(false); | |
| 25 | for (Object o : mdp.getContent()) { | |
| 26 | Object val = unwrap(o); | |
| 27 |
1
1. compile : negated conditional → NO_COVERAGE |
if (val instanceof P p) { |
| 28 | sb.append(stringifyParagraph(p, styles, dialect)) | |
| 29 | .append("\n\n"); | |
| 30 | } | |
| 31 |
1
1. compile : negated conditional → NO_COVERAGE |
else if (val instanceof Tbl tbl) { |
| 32 | sb.append(stringifyTable(tbl)); | |
| 33 | } | |
| 34 | } | |
| 35 |
1
1. compile : replaced return value with "" for pro/verron/officestamper/asciidoc/DocxToAsciiDoc::compile → NO_COVERAGE |
return sb.toString(); |
| 36 | } | |
| 37 | ||
| 38 | private static Object unwrap(Object o) { | |
| 39 |
2
1. unwrap : replaced return value with null for pro/verron/officestamper/asciidoc/DocxToAsciiDoc::unwrap → NO_COVERAGE 2. unwrap : negated conditional → NO_COVERAGE |
return (o instanceof JAXBElement<?> j) ? j.getValue() : o; |
| 40 | } | |
| 41 | ||
| 42 | private static String stringifyParagraph(P p, StyleDefinitionsPart styles, AsciiDocDialect dialect) { | |
| 43 |
1
1. stringifyParagraph : negated conditional → NO_COVERAGE |
if (dialect == AsciiDocDialect.COMPAT) { |
| 44 | String runs = stringifyRuns(p); | |
| 45 |
1
1. stringifyParagraph : replaced return value with "" for pro/verron/officestamper/asciidoc/DocxToAsciiDoc::stringifyParagraph → NO_COVERAGE |
return applyParagraphStyle(runs, p.getPPr(), styles); |
| 46 | } | |
| 47 | // ADOC (initial simple): just raw text for now | |
| 48 |
1
1. stringifyParagraph : replaced return value with "" for pro/verron/officestamper/asciidoc/DocxToAsciiDoc::stringifyParagraph → NO_COVERAGE |
return extractText(p); |
| 49 | } | |
| 50 | ||
| 51 | private static String stringifyTable(Tbl tbl) { | |
| 52 | var sb = new StringBuilder(); | |
| 53 | sb.append("|===\n"); | |
| 54 | for (Object trO : tbl.getContent()) { | |
| 55 | Object trV = unwrap(trO); | |
| 56 |
1
1. stringifyTable : negated conditional → NO_COVERAGE |
if (!(trV instanceof Tr tr)) continue; |
| 57 | for (Object tcO : tr.getContent()) { | |
| 58 | Object tcV = unwrap(tcO); | |
| 59 |
1
1. stringifyTable : negated conditional → NO_COVERAGE |
if (!(tcV instanceof Tc tc)) continue; |
| 60 | String cellText = extractText(tc).trim(); | |
| 61 | sb.append("|") | |
| 62 | .append(cellText) | |
| 63 | .append("\n\n"); | |
| 64 | } | |
| 65 | } | |
| 66 | sb.append("|===\n"); | |
| 67 |
1
1. stringifyTable : replaced return value with "" for pro/verron/officestamper/asciidoc/DocxToAsciiDoc::stringifyTable → NO_COVERAGE |
return sb.toString(); |
| 68 | } | |
| 69 | ||
| 70 | private static String stringifyRuns(P p) { | |
| 71 | StringBuilder sb = new StringBuilder(); | |
| 72 | for (Object o : p.getContent()) { | |
| 73 | Object v = unwrap(o); | |
| 74 |
1
1. stringifyRuns : negated conditional → NO_COVERAGE |
if (v instanceof R r) { |
| 75 | String inner = stringifyRunContent(r); | |
| 76 |
1
1. stringifyRuns : negated conditional → NO_COVERAGE |
if (inner.isEmpty()) continue; |
| 77 | String rpr = stringifyRPr(r.getRPr()); | |
| 78 |
1
1. stringifyRuns : negated conditional → NO_COVERAGE |
if (rpr != null) { |
| 79 | sb.append("❬") | |
| 80 | .append(inner) | |
| 81 | .append("❘") | |
| 82 | .append(rpr) | |
| 83 | .append("❭"); | |
| 84 | } | |
| 85 | else { | |
| 86 | sb.append(inner); | |
| 87 | } | |
| 88 | } | |
| 89 |
1
1. stringifyRuns : negated conditional → NO_COVERAGE |
else if (v instanceof Br br) { |
| 90 | STBrType type = br.getType(); | |
| 91 |
1
1. stringifyRuns : negated conditional → NO_COVERAGE |
if (type == STBrType.PAGE) sb.append("\n[page-break]\n<<<\n"); |
| 92 |
1
1. stringifyRuns : negated conditional → NO_COVERAGE |
else if (type == STBrType.COLUMN) sb.append("\n[col-break]\n<<<\n"); |
| 93 | else sb.append("<br/>\n"); | |
| 94 | } | |
| 95 |
1
1. stringifyRuns : negated conditional → NO_COVERAGE |
else if (v instanceof JAXBElement<?> j) { |
| 96 | Object x = j.getValue(); | |
| 97 |
1
1. stringifyRuns : negated conditional → NO_COVERAGE |
if (x instanceof R.Tab) { |
| 98 | sb.append("\t"); | |
| 99 | } | |
| 100 | } | |
| 101 | } | |
| 102 |
1
1. stringifyRuns : replaced return value with "" for pro/verron/officestamper/asciidoc/DocxToAsciiDoc::stringifyRuns → NO_COVERAGE |
return sb.toString(); |
| 103 | } | |
| 104 | ||
| 105 | private static String applyParagraphStyle(String text, PPr ppr, StyleDefinitionsPart styles) { | |
| 106 | String result = text; | |
| 107 |
2
1. applyParagraphStyle : negated conditional → NO_COVERAGE 2. applyParagraphStyle : negated conditional → NO_COVERAGE |
if (ppr != null && ppr.getPStyle() != null && ppr.getPStyle() |
| 108 |
2
1. applyParagraphStyle : negated conditional → NO_COVERAGE 2. applyParagraphStyle : negated conditional → NO_COVERAGE |
.getVal() != null && styles != null) { |
| 109 | String styleName = styles.getNameForStyleID(ppr.getPStyle() | |
| 110 | .getVal()); | |
| 111 |
1
1. applyParagraphStyle : negated conditional → NO_COVERAGE |
if (styleName != null) { |
| 112 | String decorated = decorateWithStyle(styleName, text); | |
| 113 |
1
1. applyParagraphStyle : negated conditional → NO_COVERAGE |
if (decorated != null) result = decorated; |
| 114 | } | |
| 115 | } | |
| 116 | // Section break marker after paragraph content | |
| 117 |
2
1. applyParagraphStyle : negated conditional → NO_COVERAGE 2. applyParagraphStyle : negated conditional → NO_COVERAGE |
if (ppr != null && ppr.getSectPr() != null) { |
| 118 | String sect = stringifySectPr(ppr.getSectPr()); | |
| 119 |
1
1. applyParagraphStyle : negated conditional → NO_COVERAGE |
if (!sect.isEmpty()) { |
| 120 | result = result + "\n[section-break, " + sect + "]\n<<<"; | |
| 121 | } | |
| 122 | } | |
| 123 |
1
1. applyParagraphStyle : replaced return value with "" for pro/verron/officestamper/asciidoc/DocxToAsciiDoc::applyParagraphStyle → NO_COVERAGE |
return result; |
| 124 | } | |
| 125 | ||
| 126 | private static String extractText(P p) { | |
| 127 | try { | |
| 128 | var writer = new StringWriter(); | |
| 129 |
1
1. extractText : removed call to org/docx4j/TextUtils::extractText → NO_COVERAGE |
TextUtils.extractText(p, writer); |
| 130 |
1
1. extractText : replaced return value with "" for pro/verron/officestamper/asciidoc/DocxToAsciiDoc::extractText → NO_COVERAGE |
return writer.toString(); |
| 131 | } catch (Docx4JException e) { | |
| 132 | throw new IllegalStateException("Failed to extract text from paragraph", e); | |
| 133 | } catch (Exception e) { | |
| 134 | throw new IllegalStateException("Failed to extract text from paragraph, before docx4j version 1.5.1", e); | |
| 135 | } | |
| 136 | } | |
| 137 | ||
| 138 | private static String extractText(Tc tc) { | |
| 139 | // Concatenate paragraphs text inside the cell | |
| 140 | var sb = new StringBuilder(); | |
| 141 | for (Object o : tc.getContent()) { | |
| 142 | Object v = unwrap(o); | |
| 143 |
1
1. extractText : negated conditional → NO_COVERAGE |
if (v instanceof P p) { |
| 144 | sb.append(extractText(p)) | |
| 145 | .append("\n\n"); | |
| 146 | } | |
| 147 | } | |
| 148 |
1
1. extractText : replaced return value with "" for pro/verron/officestamper/asciidoc/DocxToAsciiDoc::extractText → NO_COVERAGE |
return sb.toString() |
| 149 | .trim(); | |
| 150 | } | |
| 151 | ||
| 152 | private static String stringifyRunContent(R r) { | |
| 153 | StringBuilder sb = new StringBuilder(); | |
| 154 | for (Object rc : r.getContent()) { | |
| 155 | Object rv = unwrap(rc); | |
| 156 |
1
1. stringifyRunContent : negated conditional → NO_COVERAGE |
if (rv instanceof Text t) { |
| 157 | sb.append(t.getValue()); | |
| 158 | } | |
| 159 |
1
1. stringifyRunContent : negated conditional → NO_COVERAGE |
else if (rv instanceof R.Tab) { |
| 160 | sb.append("\t"); | |
| 161 | } | |
| 162 |
1
1. stringifyRunContent : negated conditional → NO_COVERAGE |
else if (rv instanceof Br br) { |
| 163 | STBrType type = br.getType(); | |
| 164 | switch (type) { | |
| 165 | case STBrType.PAGE -> sb.append("\n[page-break]\n<<<\n"); | |
| 166 | case STBrType.COLUMN -> sb.append("\n[col-break]\n<<<\n"); | |
| 167 | default -> sb.append("<br/>\n"); | |
| 168 | } | |
| 169 | } | |
| 170 | } | |
| 171 |
1
1. stringifyRunContent : replaced return value with "" for pro/verron/officestamper/asciidoc/DocxToAsciiDoc::stringifyRunContent → NO_COVERAGE |
return sb.toString(); |
| 172 | } | |
| 173 | ||
| 174 | private static String stringifyRPr(RPr rPr) { | |
| 175 |
2
1. stringifyRPr : replaced return value with "" for pro/verron/officestamper/asciidoc/DocxToAsciiDoc::stringifyRPr → NO_COVERAGE 2. stringifyRPr : negated conditional → NO_COVERAGE |
if (rPr == null) return null; |
| 176 | java.util.TreeMap<String, String> map = new java.util.TreeMap<>(); | |
| 177 |
1
1. stringifyRPr : negated conditional → NO_COVERAGE |
if (rPr.getB() != null && rPr.getB() |
| 178 |
1
1. stringifyRPr : negated conditional → NO_COVERAGE |
.isVal()) { |
| 179 | map.put("b", "true"); | |
| 180 | } | |
| 181 |
1
1. stringifyRPr : negated conditional → NO_COVERAGE |
if (rPr.getI() != null && rPr.getI() |
| 182 |
1
1. stringifyRPr : negated conditional → NO_COVERAGE |
.isVal()) { |
| 183 | map.put("i", "true"); | |
| 184 | } | |
| 185 |
1
1. stringifyRPr : negated conditional → NO_COVERAGE |
if (rPr.getVertAlign() != null && rPr.getVertAlign() |
| 186 |
1
1. stringifyRPr : negated conditional → NO_COVERAGE |
.getVal() != null) { |
| 187 | map.put("vertAlign", | |
| 188 | rPr.getVertAlign() | |
| 189 | .getVal() | |
| 190 | .value()); | |
| 191 | } | |
| 192 |
2
1. stringifyRPr : replaced return value with "" for pro/verron/officestamper/asciidoc/DocxToAsciiDoc::stringifyRPr → NO_COVERAGE 2. stringifyRPr : negated conditional → NO_COVERAGE |
if (map.isEmpty()) return null; |
| 193 |
1
1. stringifyRPr : replaced return value with "" for pro/verron/officestamper/asciidoc/DocxToAsciiDoc::stringifyRPr → NO_COVERAGE |
return map.entrySet() |
| 194 | .stream() | |
| 195 |
1
1. lambda$stringifyRPr$0 : replaced return value with "" for pro/verron/officestamper/asciidoc/DocxToAsciiDoc::lambda$stringifyRPr$0 → NO_COVERAGE |
.map(e -> e.getKey() + "=" + e.getValue()) |
| 196 | .collect(java.util.stream.Collectors.joining(",", "{", "}")); | |
| 197 | } | |
| 198 | ||
| 199 | private static String decorateWithStyle(String styleName, String text) { | |
| 200 |
1
1. decorateWithStyle : negated conditional → NO_COVERAGE |
String name = styleName == null ? "" : styleName.trim(); |
| 201 |
2
1. decorateWithStyle : replaced return value with "" for pro/verron/officestamper/asciidoc/DocxToAsciiDoc::decorateWithStyle → NO_COVERAGE 2. decorateWithStyle : negated conditional → NO_COVERAGE |
if (name.equalsIgnoreCase("Title")) return "= " + text + "\n"; |
| 202 | java.util.regex.Matcher m = java.util.regex.Pattern.compile("(?i)heading\\s*([1-6])") | |
| 203 | .matcher(name); | |
| 204 |
1
1. decorateWithStyle : negated conditional → NO_COVERAGE |
if (m.find()) { |
| 205 | int lvl = Integer.parseInt(m.group(1)); | |
| 206 | String prefix; // Stringifier maps heading 1 -> "== " | |
| 207 | // In Stringifier, "heading 1" => "== ", i.e., level + 1 | |
| 208 | prefix = "=".repeat(Math.clamp(lvl, 1, 6)); | |
| 209 |
1
1. decorateWithStyle : replaced return value with "" for pro/verron/officestamper/asciidoc/DocxToAsciiDoc::decorateWithStyle → NO_COVERAGE |
return prefix + " " + text + "\n"; |
| 210 | } | |
| 211 |
1
1. decorateWithStyle : replaced return value with "" for pro/verron/officestamper/asciidoc/DocxToAsciiDoc::decorateWithStyle → NO_COVERAGE |
return null; |
| 212 | } | |
| 213 | ||
| 214 | private static String stringifySectPr(SectPr sectPr) { | |
| 215 | java.util.TreeMap<String, String> map = new java.util.TreeMap<>(); | |
| 216 | // docGrid | |
| 217 |
1
1. stringifySectPr : negated conditional → NO_COVERAGE |
if (sectPr.getDocGrid() != null && sectPr.getDocGrid() |
| 218 |
1
1. stringifySectPr : negated conditional → NO_COVERAGE |
.getLinePitch() != null) { |
| 219 | map.put("docGrid", | |
| 220 | "{linePitch=" + sectPr.getDocGrid() | |
| 221 | .getLinePitch() + "}"); | |
| 222 | } | |
| 223 | // pgMar | |
| 224 | SectPr.PgMar m = sectPr.getPgMar(); | |
| 225 |
1
1. stringifySectPr : negated conditional → NO_COVERAGE |
if (m != null) { |
| 226 | java.util.TreeMap<String, String> mm = new java.util.TreeMap<>(); | |
| 227 |
1
1. stringifySectPr : negated conditional → NO_COVERAGE |
if (m.getBottom() != null) mm.put("bottom", String.valueOf(m.getBottom())); |
| 228 |
1
1. stringifySectPr : negated conditional → NO_COVERAGE |
if (m.getFooter() != null) mm.put("footer", String.valueOf(m.getFooter())); |
| 229 |
1
1. stringifySectPr : negated conditional → NO_COVERAGE |
if (m.getGutter() != null) mm.put("gutter", String.valueOf(m.getGutter())); |
| 230 |
1
1. stringifySectPr : negated conditional → NO_COVERAGE |
if (m.getHeader() != null) mm.put("header", String.valueOf(m.getHeader())); |
| 231 |
1
1. stringifySectPr : negated conditional → NO_COVERAGE |
if (m.getLeft() != null) mm.put("left", String.valueOf(m.getLeft())); |
| 232 |
1
1. stringifySectPr : negated conditional → NO_COVERAGE |
if (m.getRight() != null) mm.put("right", String.valueOf(m.getRight())); |
| 233 |
1
1. stringifySectPr : negated conditional → NO_COVERAGE |
if (m.getTop() != null) mm.put("top", String.valueOf(m.getTop())); |
| 234 |
1
1. stringifySectPr : negated conditional → NO_COVERAGE |
if (!mm.isEmpty()) { |
| 235 | String v = mm.entrySet() | |
| 236 | .stream() | |
| 237 |
1
1. lambda$stringifySectPr$0 : replaced return value with "" for pro/verron/officestamper/asciidoc/DocxToAsciiDoc::lambda$stringifySectPr$0 → NO_COVERAGE |
.map(e -> e.getKey() + "=" + e.getValue()) |
| 238 | .collect(java.util.stream.Collectors.joining(",", "{", "}")); | |
| 239 | map.put("pgMar", v); | |
| 240 | } | |
| 241 | } | |
| 242 | // pgSz | |
| 243 | SectPr.PgSz s = sectPr.getPgSz(); | |
| 244 |
1
1. stringifySectPr : negated conditional → NO_COVERAGE |
if (s != null) { |
| 245 | java.util.TreeMap<String, String> sm = new java.util.TreeMap<>(); | |
| 246 |
1
1. stringifySectPr : negated conditional → NO_COVERAGE |
if (s.getH() != null) sm.put("h", String.valueOf(s.getH())); |
| 247 |
1
1. stringifySectPr : negated conditional → NO_COVERAGE |
if (s.getOrient() != null) sm.put("orient", String.valueOf(s.getOrient())); |
| 248 |
1
1. stringifySectPr : negated conditional → NO_COVERAGE |
if (s.getW() != null) sm.put("w", String.valueOf(s.getW())); |
| 249 |
1
1. stringifySectPr : negated conditional → NO_COVERAGE |
if (!sm.isEmpty()) { |
| 250 | String v = sm.entrySet() | |
| 251 | .stream() | |
| 252 |
1
1. lambda$stringifySectPr$1 : replaced return value with "" for pro/verron/officestamper/asciidoc/DocxToAsciiDoc::lambda$stringifySectPr$1 → NO_COVERAGE |
.map(e -> e.getKey() + "=" + e.getValue()) |
| 253 | .collect(java.util.stream.Collectors.joining(",", "{", "}")); | |
| 254 | map.put("pgSz", v); | |
| 255 | } | |
| 256 | } | |
| 257 |
1
1. stringifySectPr : negated conditional → NO_COVERAGE |
if (map.isEmpty()) return ""; |
| 258 |
1
1. stringifySectPr : replaced return value with "" for pro/verron/officestamper/asciidoc/DocxToAsciiDoc::stringifySectPr → NO_COVERAGE |
return map.entrySet() |
| 259 | .stream() | |
| 260 |
1
1. lambda$stringifySectPr$2 : replaced return value with "" for pro/verron/officestamper/asciidoc/DocxToAsciiDoc::lambda$stringifySectPr$2 → NO_COVERAGE |
.map(e -> e.getKey() + "=" + e.getValue()) |
| 261 | .collect(java.util.stream.Collectors.joining(",", "{", "}")); | |
| 262 | } | |
| 263 | } | |
Mutations | ||
| 27 |
1.1 |
|
| 31 |
1.1 |
|
| 35 |
1.1 |
|
| 39 |
1.1 2.2 |
|
| 43 |
1.1 |
|
| 45 |
1.1 |
|
| 48 |
1.1 |
|
| 56 |
1.1 |
|
| 59 |
1.1 |
|
| 67 |
1.1 |
|
| 74 |
1.1 |
|
| 76 |
1.1 |
|
| 78 |
1.1 |
|
| 89 |
1.1 |
|
| 91 |
1.1 |
|
| 92 |
1.1 |
|
| 95 |
1.1 |
|
| 97 |
1.1 |
|
| 102 |
1.1 |
|
| 107 |
1.1 2.2 |
|
| 108 |
1.1 2.2 |
|
| 111 |
1.1 |
|
| 113 |
1.1 |
|
| 117 |
1.1 2.2 |
|
| 119 |
1.1 |
|
| 123 |
1.1 |
|
| 129 |
1.1 |
|
| 130 |
1.1 |
|
| 143 |
1.1 |
|
| 148 |
1.1 |
|
| 156 |
1.1 |
|
| 159 |
1.1 |
|
| 162 |
1.1 |
|
| 171 |
1.1 |
|
| 175 |
1.1 2.2 |
|
| 177 |
1.1 |
|
| 178 |
1.1 |
|
| 181 |
1.1 |
|
| 182 |
1.1 |
|
| 185 |
1.1 |
|
| 186 |
1.1 |
|
| 192 |
1.1 2.2 |
|
| 193 |
1.1 |
|
| 195 |
1.1 |
|
| 200 |
1.1 |
|
| 201 |
1.1 2.2 |
|
| 204 |
1.1 |
|
| 209 |
1.1 |
|
| 211 |
1.1 |
|
| 217 |
1.1 |
|
| 218 |
1.1 |
|
| 225 |
1.1 |
|
| 227 |
1.1 |
|
| 228 |
1.1 |
|
| 229 |
1.1 |
|
| 230 |
1.1 |
|
| 231 |
1.1 |
|
| 232 |
1.1 |
|
| 233 |
1.1 |
|
| 234 |
1.1 |
|
| 237 |
1.1 |
|
| 244 |
1.1 |
|
| 246 |
1.1 |
|
| 247 |
1.1 |
|
| 248 |
1.1 |
|
| 249 |
1.1 |
|
| 252 |
1.1 |
|
| 257 |
1.1 |
|
| 258 |
1.1 |
|
| 260 |
1.1 |