Слияние docx4j 17.3.5 в wordpdf-core и форк FO-рендерера 2.11-docx4j.8 в реактор
- wordpdf-core/src/main/java впитаны исходники docx4j-core, -export-fo, -chart-render, -JAXB-ReferenceImpl, -copy, -generated-objects (17.3.5) + сгенерированная XJC JAXB-модель; ресурсы — тем же путём; артефакты группы org.docx4j из внешних репозиториев больше не подключаются - vendor/fop: форк docx4j-fo-renderer 2.11-docx4j.8 (util/events/core/fop) собирается в реакторе вместо jar из Nexus - docx2fo.xslt перевендорен со стилем 17.3.5, оба AUDIT-FIX-патча перенесены; дрейф-гейт vendoredXsltTracksJarCopy зелёный - upstream-тесты docx4j перенесены в src/test/java/org (658 классов, вне гейта); execution upstream-docx4j-tests в профиле upstream-tests (./mise.sh run upstream-tests, в pr.yml non-blocking шаг): 3470 тестов, 3457 зелёных, 13 красных — категории окружения/калибровки хоста и следствий слияния classpath задокументированы в docs/agent/testing.md - surefire: провайдер SLF4J фиксируется коротким slf4j.provider по execution; перенесён upstream logback.xml (root=DEBUG без конфига ломает visitor-экспорт VML); upstream-форки пинят JDK JAXP-фабрики; PMD/JaCoCo исключают org/** - wordpdf-app: без изменений кода (зависит от слитого ядра); тесты 44/44 - документация синхронизирована (AGENTS.md, README, docs/agent/*), mise task upstream-tests
This commit is contained in:
+368
-14
@@ -12,24 +12,153 @@
|
||||
|
||||
<artifactId>wordpdf-core</artifactId>
|
||||
<name>wordpdf-core</name>
|
||||
<description>Библиотека: конвертация docx в PDF (docx4j + FOP, шрифты внутри jar)</description>
|
||||
<description>Библиотека: конвертация docx в PDF (docx4j 17.3.5 слит в модуль, FOP-форк из vendor/fop, шрифты внутри jar)</description>
|
||||
|
||||
<dependencies>
|
||||
<!-- docx4j: чтение/запись docx, маппинг шрифтов -->
|
||||
<!-- ============ зависимости слитого docx4j 17.3.5 (org.docx4j.* в src/main/java) ============
|
||||
Единственные артефакты группы org.docx4j на classpath — форк FO-рендерера
|
||||
(reactor-модуль vendor/fop); docx4j-core/export-fo/chart-render из Central НЕ
|
||||
используются. Версии — в корневом dependencyManagement (upstream 17.3.5). -->
|
||||
|
||||
<!-- JAXB: API + реализация ReferenceImpl (service-файл jaxb-runtime), txw2 -->
|
||||
<dependency>
|
||||
<groupId>jakarta.xml.bind</groupId>
|
||||
<artifactId>jakarta.xml.bind-api</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.glassfish.jaxb</groupId>
|
||||
<artifactId>jaxb-runtime</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.glassfish.jaxb</groupId>
|
||||
<artifactId>jaxb-core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.glassfish.jaxb</groupId>
|
||||
<artifactId>txw2</artifactId>
|
||||
</dependency>
|
||||
<!-- рантайм XJC-плагинов модели (-Xcopy: org.docx4j.copy.* слиты сюда же) -->
|
||||
<dependency>
|
||||
<groupId>org.jvnet.jaxb</groupId>
|
||||
<artifactId>jaxb-plugins-runtime</artifactId>
|
||||
</dependency>
|
||||
<!-- перепакованные схемы SVG/XSL-FO -->
|
||||
<dependency>
|
||||
<groupId>org.plutext</groupId>
|
||||
<artifactId>jaxb-svg11</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.plutext</groupId>
|
||||
<artifactId>jaxb-xslfo</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.engio</groupId>
|
||||
<artifactId>mbassador</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-collections4</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.zaxxer</groupId>
|
||||
<artifactId>SparseBitSet</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.xmlgraphics</groupId>
|
||||
<artifactId>xmlgraphics-commons</artifactId>
|
||||
</dependency>
|
||||
<!-- перепакованный Xalan для XPathEnhancer -->
|
||||
<dependency>
|
||||
<groupId>org.docx4j.org.apache</groupId>
|
||||
<artifactId>xalan-interpretive</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.docx4j.org.apache</groupId>
|
||||
<artifactId>xalan-serializer</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.antlr</groupId>
|
||||
<artifactId>antlr-runtime</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.antlr</groupId>
|
||||
<artifactId>stringtemplate</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.checkerframework</groupId>
|
||||
<artifactId>checker-qual</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.errorprone</groupId>
|
||||
<artifactId>error_prone_annotations</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jsoup</groupId>
|
||||
<artifactId>jsoup</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.thoughtworks.qdox</groupId>
|
||||
<artifactId>qdox</artifactId>
|
||||
</dependency>
|
||||
<!-- Batik: SVG/OMML (jeuclid) и chart-render -->
|
||||
<dependency>
|
||||
<groupId>org.apache.xmlgraphics</groupId>
|
||||
<artifactId>batik-anim</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.xmlgraphics</groupId>
|
||||
<artifactId>batik-awt-util</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.xmlgraphics</groupId>
|
||||
<artifactId>batik-bridge</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.xmlgraphics</groupId>
|
||||
<artifactId>batik-codec</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.xmlgraphics</groupId>
|
||||
<artifactId>batik-dom</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.xmlgraphics</groupId>
|
||||
<artifactId>batik-extension</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.xmlgraphics</groupId>
|
||||
<artifactId>batik-gvt</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.xmlgraphics</groupId>
|
||||
<artifactId>batik-svggen</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.xmlgraphics</groupId>
|
||||
<artifactId>batik-transcoder</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>de.rototor.jeuclid</groupId>
|
||||
<artifactId>jeuclid-fop</artifactId>
|
||||
</dependency>
|
||||
<!-- форк FO-рендерера: собирается reactor-модулем vendor/fop (2.11-docx4j.8) -->
|
||||
<dependency>
|
||||
<groupId>org.docx4j</groupId>
|
||||
<artifactId>docx4j-core</artifactId>
|
||||
<artifactId>docx4j-fo-renderer</artifactId>
|
||||
</dependency>
|
||||
<!-- Реализация JAXB для docx4j -->
|
||||
<dependency>
|
||||
<groupId>org.docx4j</groupId>
|
||||
<artifactId>docx4j-JAXB-ReferenceImpl</artifactId>
|
||||
</dependency>
|
||||
<!-- Пайплайн экспорта: docx -> XSL-FO -> Apache FOP -> PDF -->
|
||||
<dependency>
|
||||
<groupId>org.docx4j</groupId>
|
||||
<artifactId>docx4j-export-fo</artifactId>
|
||||
<artifactId>docx4j-fo-renderer-events</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Простой логгер в консоль (slf4j). Обязателен: без него docx4j
|
||||
пишет предупреждение прямо в выходной PDF. -->
|
||||
<dependency>
|
||||
@@ -50,8 +179,8 @@
|
||||
<artifactId>jcl-over-slf4j</artifactId>
|
||||
</dependency>
|
||||
<!-- Используется DependencyGuard (проверка совместимости classpath);
|
||||
транзитивно приходит от docx4j-core, объявлена явно как контракт
|
||||
интеграции — потребитель видит её в своём дереве зависимостей -->
|
||||
объявлена явно как контракт интеграции — потребитель видит её в
|
||||
своём дереве зависимостей -->
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-compress</artifactId>
|
||||
@@ -76,12 +205,112 @@
|
||||
<artifactId>poi-ooxml</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<!-- ===== upstream-набор тестов docx4j (JUnit 4 через vintage) ===== -->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.vintage</groupId>
|
||||
<artifactId>junit-vintage-engine</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<!-- тесты upstream работают против logback-бэкенда (LoggerContext/ListAppender);
|
||||
в гейт по умолчанию не попадает: default-test фиксирует slf4j-simple через
|
||||
-Dslf4j.provider -->
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-classic</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<!-- Saxon XPath (тесты mc-preprocessor/XSLT), tika (детект MIME),
|
||||
xerces (DOM-каноникализация), fop-hyph (флекс-расстановка) -->
|
||||
<dependency>
|
||||
<groupId>net.sf.saxon</groupId>
|
||||
<artifactId>Saxon-HE</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.tika</groupId>
|
||||
<artifactId>tika-core</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>xerces</groupId>
|
||||
<artifactId>xercesImpl</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.sf.offo</groupId>
|
||||
<artifactId>fop-hyph</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<!-- main-код docx4j-markdown перенесён в test-дерево (нужен тестам core-tests) -->
|
||||
<dependency>
|
||||
<groupId>org.commonmark</groupId>
|
||||
<artifactId>commonmark</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.commonmark</groupId>
|
||||
<artifactId>commonmark-ext-gfm-tables</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.commonmark</groupId>
|
||||
<artifactId>commonmark-ext-gfm-strikethrough</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.commonmark</groupId>
|
||||
<artifactId>commonmark-ext-task-list-items</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.commonmark</groupId>
|
||||
<artifactId>commonmark-ext-footnotes</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.commonmark</groupId>
|
||||
<artifactId>commonmark-ext-yaml-front-matter</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<!-- XSLT-части src/main/java: стилист export-fo (org/docx4j/convert/out/fo/docx2fo.xslt)
|
||||
и xml-части лежат в дереве исходников — без этой записи они не попадут в jar,
|
||||
и гейт vendoredXsltTracksJarCopy упадёт. -->
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<excludes>
|
||||
<!-- фильтруется вторым ресурсом: ${docx4j.version} = 17.3.5 -->
|
||||
<exclude>docx4j_version.properties</exclude>
|
||||
</excludes>
|
||||
</resource>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<includes>
|
||||
<include>docx4j_version.properties</include>
|
||||
</includes>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
<resource>
|
||||
<directory>src/main/java</directory>
|
||||
<includes>
|
||||
<include>**/*.xslt</include>
|
||||
<include>**/*.xml</include>
|
||||
</includes>
|
||||
</resource>
|
||||
</resources>
|
||||
<plugins>
|
||||
<!-- Тестовый jar с готовыми .docx из src/test/resources/samples:
|
||||
используется тестами wordpdf-app. -->
|
||||
используется тестами wordpdf-app. В jar попадают только фикстуры
|
||||
проекта (samples/, geom/) и тестовые классы ru.pjandro — перенесённые
|
||||
upstream-тесты (org/**) и их 60 МБ ресурсов в app не нужны. -->
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
@@ -91,15 +320,75 @@
|
||||
<goals>
|
||||
<goal>test-jar</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<includes>
|
||||
<include>ru/pjandro/**</include>
|
||||
<include>samples/**</include>
|
||||
<include>geom/**</include>
|
||||
</includes>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<!-- Разделение наборов: гейт по умолчанию — только тесты проекта;
|
||||
upstream-набор — в профиле upstream-tests (см. ниже). -->
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>default-test</id>
|
||||
<configuration>
|
||||
<!-- slf4j-бэкенд тестов проекта — slf4j-simple (as prod);
|
||||
logback-classic на test-classpath нужен только
|
||||
upstream-тестам, провайдер фиксируем явно -->
|
||||
<argLine>@{argLine} -Djava.awt.headless=true -Dslf4j.provider=org.slf4j.simple.SimpleServiceProvider</argLine>
|
||||
<includes>
|
||||
<!-- plexus-паттерн: `**` НЕ матчит ноль каталогов —
|
||||
классы корневого пакета добавлены отдельным паттерном -->
|
||||
<include>ru/pjandro/wordpdf/*Test.java</include>
|
||||
<include>ru/pjandro/wordpdf/**/*Test.java</include>
|
||||
</includes>
|
||||
<excludes>
|
||||
<exclude>org/**</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<!-- Чужой код (слитый docx4j) не линтеруется и не измеряется:
|
||||
PMD и JaCoCo — только по ru.pjandro.*. -->
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-pmd-plugin</artifactId>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>**/org/docx4j/**</exclude>
|
||||
<exclude>**/org/pptx4j/**</exclude>
|
||||
<exclude>**/org/xlsx4j/**</exclude>
|
||||
<exclude>**/org/glox4j/**</exclude>
|
||||
<exclude>**/org/opendope/**</exclude>
|
||||
<exclude>**/org/merlin/**</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<!-- Гейт покрытия ядра: к общему LINE 0.90 (корневой pluginManagement)
|
||||
добавлен BRANCH 0.85 — только для core; у wordpdf-app бранчи ниже
|
||||
порога, общий rule в корне остаётся LINE-only. -->
|
||||
порога, общий rule в корне остаётся LINE-only. Исключения — слитый
|
||||
docx4j: иначе 4 тыс. чужих файлов уронят порог. -->
|
||||
<plugin>
|
||||
<groupId>org.jacoco</groupId>
|
||||
<artifactId>jacoco-maven-plugin</artifactId>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>org/docx4j/**</exclude>
|
||||
<exclude>org/pptx4j/**</exclude>
|
||||
<exclude>org/xlsx4j/**</exclude>
|
||||
<exclude>org/glox4j/**</exclude>
|
||||
<exclude>org/opendope/**</exclude>
|
||||
<exclude>org/merlin/**</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>check</id>
|
||||
@@ -127,4 +416,69 @@
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<profiles>
|
||||
<!-- Перенесённый upstream-набор тестов docx4j (409 core + 182 export-fo-tests +
|
||||
37 in-module + 7 chart-render + 1 JAXB-RI). Не в гейте по умолчанию:
|
||||
~660 классов × отдельный JVM (требование upstream: Saxon XPath и тесты,
|
||||
мутирующие статические карты) = десятки минут. Пуск:
|
||||
mvn -pl wordpdf-core -am test -Pupstream-tests -->
|
||||
<profile>
|
||||
<id>upstream-tests</id>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>upstream-docx4j-tests</id>
|
||||
<phase>test</phase>
|
||||
<goals>
|
||||
<goal>test</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<!-- upstream-набор пишет через logback (несколько
|
||||
классов достают LoggerContext напрямую); JAXP-фабрики —
|
||||
JDK-реализации, как в upstream docx4j.properties
|
||||
(xercesImpl на test-classpath иначе перехватывает
|
||||
DocumentBuilderFactory.newInstance() через
|
||||
META-INF/services, а standalone-Xerces 2.12.2 не
|
||||
знает factory-свойство accessExternalDTD, на котором
|
||||
падает ExtractionConformanceTest) -->
|
||||
<argLine>@{argLine} -Djava.awt.headless=true -Dslf4j.provider=ch.qos.logback.classic.spi.LogbackServiceProvider -Djavax.xml.parsers.DocumentBuilderFactory=com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl -Djavax.xml.parsers.SAXParserFactory=com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl</argLine>
|
||||
<includes>
|
||||
<!-- `**` не матчит ноль каталогов — root-пакеты отдельными паттернами -->
|
||||
<include>org/docx4j/*Test.java</include>
|
||||
<include>org/docx4j/*Tests.java</include>
|
||||
<include>org/docx4j/**/*Test.java</include>
|
||||
<include>org/docx4j/**/*Tests.java</include>
|
||||
<include>org/pptx4j/*Test.java</include>
|
||||
<include>org/pptx4j/**/*Test.java</include>
|
||||
<include>org/pptx4j/**/*Tests.java</include>
|
||||
<include>org/xlsx4j/*Test.java</include>
|
||||
<include>org/xlsx4j/**/*Test.java</include>
|
||||
<include>org/xlsx4j/**/*Tests.java</include>
|
||||
</includes>
|
||||
<excludes>
|
||||
<exclude>**/samples/**</exclude>
|
||||
<exclude>**/AbstractNumberingTest.java</exclude>
|
||||
<!-- читает docx4j.properties из корня classpath — он
|
||||
намеренно не перенесён (менял бы поведение всем
|
||||
тестам модуля, см. план слияния); остальные
|
||||
зависимости от того properties — известно-красные
|
||||
методы, триаж в docs/agent/testing.md (surefire
|
||||
запрещает #method в excludes POM'а) -->
|
||||
<exclude>**/Docx4jPropertiesFoFileTest.java</exclude>
|
||||
</excludes>
|
||||
<forkCount>1</forkCount>
|
||||
<reuseForks>false</reuseForks>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
</profiles>
|
||||
</project>
|
||||
|
||||
@@ -0,0 +1,451 @@
|
||||
grammar XPathEnhancer;
|
||||
|
||||
options {
|
||||
output = template;
|
||||
rewrite = true;
|
||||
}
|
||||
|
||||
tokens {
|
||||
PATHSEP = '/';
|
||||
ABRPATH = '//';
|
||||
LPAR = '(';
|
||||
RPAR = ')';
|
||||
LBRAC = '[';
|
||||
RBRAC = ']';
|
||||
MINUS = '-';
|
||||
PLUS = '+';
|
||||
DOT = '.';
|
||||
MUL = '*';
|
||||
DOTDOT = '..';
|
||||
AT = '@';
|
||||
COMMA = ',';
|
||||
PIPE = '|';
|
||||
LESS = '<';
|
||||
MORE = '>';
|
||||
LE = '<=';
|
||||
GE = '>=';
|
||||
COLON = ':';
|
||||
CC = '::';
|
||||
APOS = '\'';
|
||||
QUOT = '\"';
|
||||
}
|
||||
|
||||
@parser::header {
|
||||
/*
|
||||
Based on the XPath 1.0 (http://www.w3.org/TR/1999/REC-xpath-19991116) grammar by Jan-Willem van den Broek,
|
||||
version 1.0, downloaded on 2011-06-18 from http://blog.jwbroek.com/2010/07/antlr-grammar-for-parsing-xpath-10.html
|
||||
|
||||
That page said "Do with this code as you will."
|
||||
|
||||
*/
|
||||
package org.docx4j.model.datastorage;
|
||||
|
||||
}
|
||||
|
||||
@lexer::header {
|
||||
package org.docx4j.model.datastorage;
|
||||
}
|
||||
|
||||
@members {
|
||||
private String index;
|
||||
private String prefix;
|
||||
private int prefixLength;
|
||||
|
||||
public static String enhanceXPath(final String prefix, final int index, final String xpath) {
|
||||
|
||||
final ANTLRStringStream stringStream = new ANTLRStringStream(xpath);
|
||||
final XPathEnhancerLexer xl = new XPathEnhancerLexer(stringStream);
|
||||
final TokenRewriteStream trs = new TokenRewriteStream(xl);
|
||||
final XPathEnhancerParser xp = new XPathEnhancerParser(trs, index, prefix);
|
||||
|
||||
try {
|
||||
xp.main();
|
||||
} catch (RecognitionException e) {
|
||||
throw new IllegalArgumentException(xpath + " is no valid XPath expression", e);
|
||||
}
|
||||
|
||||
final String enhanced = trs.toString();
|
||||
return enhanced;
|
||||
}
|
||||
|
||||
public XPathEnhancerParser(TokenStream input, final int index, final String prefix) {
|
||||
this(input);
|
||||
this.index = "" + index;
|
||||
this.prefix = prefix;
|
||||
this.prefixLength = prefix.length();
|
||||
}
|
||||
|
||||
private boolean sharesPrefix(final String path) {
|
||||
return path != null && path.startsWith(prefix)
|
||||
&& !positionFunctionPredicateFollows(path.substring(prefixLength));
|
||||
}
|
||||
|
||||
/* True if the part of the path following the repeat prefix begins with
|
||||
predicates of which one uses position() or last(): the author is
|
||||
deliberately addressing a specific item of the repeated collection
|
||||
(eg items[last()]), so the path is left as-is rather than being
|
||||
contextualized to the current repeat instance.
|
||||
A numeric predicate (eg [1]) still means the current instance, per the
|
||||
sample-based authoring convention.
|
||||
See https://github.com/plutext/docx4j/discussions/691 */
|
||||
private static boolean positionFunctionPredicateFollows(final String suffix) {
|
||||
int i = 0;
|
||||
while (suffix != null && i < suffix.length() && suffix.charAt(i) == '[') {
|
||||
final int start = i;
|
||||
int depth = 0;
|
||||
for (; i < suffix.length(); i++) {
|
||||
final char c = suffix.charAt(i);
|
||||
if (c == '[') {
|
||||
depth++;
|
||||
} else if (c == ']') {
|
||||
depth--;
|
||||
if (depth == 0) {
|
||||
i++;
|
||||
break;
|
||||
}
|
||||
} else if (c == '\'' || c == '"') {
|
||||
final int close = suffix.indexOf(c, i + 1);
|
||||
if (close < 0) return false; // malformed
|
||||
i = close;
|
||||
}
|
||||
}
|
||||
if (depth != 0) return false; // malformed
|
||||
final String predicate = suffix.substring(start + 1, i - 1);
|
||||
if (predicate.contains("position(") || predicate.contains("last(")) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private String remainingSuffix(final String path) {
|
||||
if (path == null || path.length() < prefixLength) {
|
||||
return null;
|
||||
} else {
|
||||
return path.substring(prefixLength);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
main
|
||||
:
|
||||
expr
|
||||
;
|
||||
|
||||
locationPath
|
||||
:
|
||||
relativeLocationPath
|
||||
| absoluteLocationPathNoroot
|
||||
;
|
||||
|
||||
absoluteLocationPathNoroot
|
||||
:
|
||||
originalAbsoluteLocationPathNoroot
|
||||
->
|
||||
template ( sharesPrefix = { sharesPrefix($text) },
|
||||
commonPrefix = { prefix },
|
||||
index = { index },
|
||||
remainingSuffix = { remainingSuffix($text) },
|
||||
originalPath = { $text } )
|
||||
|
||||
"<if(sharesPrefix)><commonPrefix>[<index>]<remainingSuffix><else><originalPath><endif>"
|
||||
;
|
||||
|
||||
originalAbsoluteLocationPathNoroot
|
||||
:
|
||||
'/' relativeLocationPath
|
||||
| '//' relativeLocationPath
|
||||
;
|
||||
|
||||
relativeLocationPath
|
||||
:
|
||||
step
|
||||
(
|
||||
(
|
||||
'/'
|
||||
| '//'
|
||||
)
|
||||
step
|
||||
)*
|
||||
;
|
||||
|
||||
step
|
||||
:
|
||||
axisSpecifier nodeTest predicate*
|
||||
| abbreviatedStep
|
||||
;
|
||||
|
||||
axisSpecifier
|
||||
:
|
||||
AxisName '::'
|
||||
| '@'?
|
||||
;
|
||||
|
||||
nodeTest
|
||||
:
|
||||
nameTest
|
||||
| NodeType '(' ')'
|
||||
| 'processing-instruction' '(' Literal ')'
|
||||
;
|
||||
|
||||
predicate
|
||||
:
|
||||
'[' expr ']'
|
||||
;
|
||||
|
||||
abbreviatedStep
|
||||
:
|
||||
'.'
|
||||
| '..'
|
||||
;
|
||||
|
||||
expr
|
||||
:
|
||||
orExpr
|
||||
;
|
||||
|
||||
primaryExpr
|
||||
:
|
||||
variableReference
|
||||
| '(' expr ')'
|
||||
| Literal
|
||||
| Number
|
||||
| functionCall
|
||||
;
|
||||
|
||||
functionCall
|
||||
:
|
||||
functionName '(' (expr (',' expr)*)? ')'
|
||||
;
|
||||
|
||||
unionExprNoRoot
|
||||
:
|
||||
pathExprNoRoot ('|' unionExprNoRoot)?
|
||||
| '/' '|' unionExprNoRoot
|
||||
;
|
||||
|
||||
pathExprNoRoot
|
||||
:
|
||||
locationPath
|
||||
| filterExpr absoluteLocationPathNoroot ?
|
||||
;
|
||||
|
||||
filterExpr
|
||||
:
|
||||
primaryExpr predicate*
|
||||
;
|
||||
|
||||
orExpr
|
||||
:
|
||||
andExpr ('or' andExpr)*
|
||||
;
|
||||
|
||||
andExpr
|
||||
:
|
||||
equalityExpr ('and' equalityExpr)*
|
||||
;
|
||||
|
||||
equalityExpr
|
||||
:
|
||||
relationalExpr
|
||||
(
|
||||
(
|
||||
'='
|
||||
| '!='
|
||||
)
|
||||
relationalExpr
|
||||
)*
|
||||
;
|
||||
|
||||
relationalExpr
|
||||
:
|
||||
additiveExpr
|
||||
(
|
||||
(
|
||||
'<'
|
||||
| '>'
|
||||
| '<='
|
||||
| '>='
|
||||
)
|
||||
additiveExpr
|
||||
)*
|
||||
;
|
||||
|
||||
additiveExpr
|
||||
:
|
||||
multiplicativeExpr
|
||||
(
|
||||
(
|
||||
'+'
|
||||
| '-'
|
||||
)
|
||||
multiplicativeExpr
|
||||
)*
|
||||
;
|
||||
|
||||
multiplicativeExpr
|
||||
:
|
||||
unaryExprNoRoot
|
||||
(
|
||||
(
|
||||
'*'
|
||||
| 'div'
|
||||
| 'mod'
|
||||
)
|
||||
multiplicativeExpr
|
||||
)?
|
||||
| '/'
|
||||
(
|
||||
(
|
||||
'div'
|
||||
| 'mod'
|
||||
)
|
||||
multiplicativeExpr
|
||||
)?
|
||||
;
|
||||
|
||||
unaryExprNoRoot
|
||||
:
|
||||
'-'* unionExprNoRoot
|
||||
;
|
||||
|
||||
qName
|
||||
:
|
||||
nCName (':' nCName)?
|
||||
;
|
||||
|
||||
functionName
|
||||
:
|
||||
qName // Does not match nodeType, as per spec.
|
||||
;
|
||||
|
||||
variableReference
|
||||
:
|
||||
'$' qName
|
||||
;
|
||||
|
||||
nameTest
|
||||
:
|
||||
'*'
|
||||
| nCName ':' '*'
|
||||
| qName
|
||||
;
|
||||
|
||||
nCName
|
||||
:
|
||||
NCName
|
||||
| AxisName
|
||||
;
|
||||
|
||||
NodeType
|
||||
:
|
||||
'comment'
|
||||
| 'text'
|
||||
| 'processing-instruction'
|
||||
| 'node'
|
||||
;
|
||||
|
||||
Number
|
||||
:
|
||||
Digits ('.' Digits?)?
|
||||
| '.' Digits
|
||||
;
|
||||
|
||||
fragment
|
||||
Digits
|
||||
:
|
||||
('0'..'9')+
|
||||
;
|
||||
|
||||
AxisName
|
||||
:
|
||||
'ancestor'
|
||||
| 'ancestor-or-self'
|
||||
| 'attribute'
|
||||
| 'child'
|
||||
| 'descendant'
|
||||
| 'descendant-or-self'
|
||||
| 'following'
|
||||
| 'following-sibling'
|
||||
| 'namespace'
|
||||
| 'parent'
|
||||
| 'preceding'
|
||||
| 'preceding-sibling'
|
||||
| 'self'
|
||||
;
|
||||
|
||||
Literal
|
||||
:
|
||||
'"' ~'"'* '"'
|
||||
| '\'' ~'\''* '\''
|
||||
;
|
||||
|
||||
Whitespace
|
||||
:
|
||||
(
|
||||
' '
|
||||
| '\t'
|
||||
| '\n'
|
||||
| '\r'
|
||||
)+
|
||||
|
||||
{
|
||||
$channel = HIDDEN;
|
||||
}
|
||||
;
|
||||
|
||||
NCName
|
||||
:
|
||||
NCNameStartChar NCNameChar*
|
||||
;
|
||||
|
||||
fragment
|
||||
NCNameStartChar
|
||||
:
|
||||
'A'..'Z'
|
||||
| '_'
|
||||
| 'a'..'z'
|
||||
| '\u00C0'..'\u00D6'
|
||||
| '\u00D8'..'\u00F6'
|
||||
| '\u00F8'..'\u02FF'
|
||||
| '\u0370'..'\u037D'
|
||||
| '\u037F'..'\u1FFF'
|
||||
| '\u200C'..'\u200D'
|
||||
| '\u2070'..'\u218F'
|
||||
| '\u2C00'..'\u2FEF'
|
||||
| '\u3001'..'\uD7FF'
|
||||
| '\uF900'..'\uFDCF'
|
||||
| '\uFDF0'..'\uFFFD'
|
||||
| PermittedHighSurrogateChar LowSurrogateChar
|
||||
;
|
||||
|
||||
// UniCode supplementary character support added
|
||||
// 2011-06-18 Karsten Tinnefeld
|
||||
// cf. http://java.sun.com/javase/technologies/core/basic/intl/faq.jsp
|
||||
// [#x10000-#xEFFFF] is [#xD800 0xDC00-#xDB7F 0xDFFF] in UTF-16
|
||||
// cf. http://en.wikipedia.org/wiki/UTF-16/UCS-2
|
||||
// cf. http://www.w3.org/TR/xml11/#sec-common-syn
|
||||
|
||||
fragment
|
||||
PermittedHighSurrogateChar
|
||||
:
|
||||
'\uD800'..'\uDB7F'
|
||||
;
|
||||
|
||||
fragment
|
||||
LowSurrogateChar
|
||||
:
|
||||
'\uDC00'..'\uDFFF'
|
||||
;
|
||||
|
||||
fragment
|
||||
NCNameChar
|
||||
:
|
||||
NCNameStartChar
|
||||
| '-'
|
||||
| '.'
|
||||
| '0'..'9'
|
||||
| '\u00B7'
|
||||
| '\u0300'..'\u036F'
|
||||
| '\u203F'..'\u2040'
|
||||
;
|
||||
@@ -0,0 +1,71 @@
|
||||
T__37=37
|
||||
T__38=38
|
||||
T__39=39
|
||||
T__40=40
|
||||
T__41=41
|
||||
T__42=42
|
||||
T__43=43
|
||||
T__44=44
|
||||
PATHSEP=4
|
||||
ABRPATH=5
|
||||
LPAR=6
|
||||
RPAR=7
|
||||
LBRAC=8
|
||||
RBRAC=9
|
||||
MINUS=10
|
||||
PLUS=11
|
||||
DOT=12
|
||||
MUL=13
|
||||
DOTDOT=14
|
||||
AT=15
|
||||
COMMA=16
|
||||
PIPE=17
|
||||
LESS=18
|
||||
MORE=19
|
||||
LE=20
|
||||
GE=21
|
||||
COLON=22
|
||||
CC=23
|
||||
APOS=24
|
||||
QUOT=25
|
||||
AxisName=26
|
||||
NodeType=27
|
||||
Literal=28
|
||||
Number=29
|
||||
NCName=30
|
||||
Digits=31
|
||||
Whitespace=32
|
||||
NCNameStartChar=33
|
||||
NCNameChar=34
|
||||
PermittedHighSurrogateChar=35
|
||||
LowSurrogateChar=36
|
||||
'/'=4
|
||||
'//'=5
|
||||
'('=6
|
||||
')'=7
|
||||
'['=8
|
||||
']'=9
|
||||
'-'=10
|
||||
'+'=11
|
||||
'.'=12
|
||||
'*'=13
|
||||
'..'=14
|
||||
'@'=15
|
||||
','=16
|
||||
'|'=17
|
||||
'<'=18
|
||||
'>'=19
|
||||
'<='=20
|
||||
'>='=21
|
||||
':'=22
|
||||
'::'=23
|
||||
'\''=24
|
||||
'\"'=25
|
||||
'processing-instruction'=37
|
||||
'or'=38
|
||||
'and'=39
|
||||
'='=40
|
||||
'!='=41
|
||||
'div'=42
|
||||
'mod'=43
|
||||
'$'=44
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,313 @@
|
||||
package org.docx4j;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.docx4j.utils.ResourceUtils;
|
||||
|
||||
/**
|
||||
* docx4j's configuration: {@code docx4j.properties}, loaded once from the classpath, plus
|
||||
* whatever a caller {@link #setProperty(String, String) sets}.
|
||||
*
|
||||
* <p>Since 17.2.0 a second, optional file, {@code docx4j-fo.properties}, supplies
|
||||
* <b>fallback</b> values: it is loaded from the classpath after {@code docx4j.properties}
|
||||
* and only its keys that {@code docx4j.properties} does not set are taken. It is where
|
||||
* the PDF-via-XSL-FO properties ({@code docx4j.convert.out.fo.*} - the Word layout rules'
|
||||
* opt-outs and tunings, which a deployment rarely changes) live in the reference
|
||||
* configuration ({@code docx4j-samples-resources}), so that {@code docx4j.properties}
|
||||
* stays short. A key set in {@code docx4j.properties} wins over the same key in
|
||||
* {@code docx4j-fo.properties}; a key set programmatically wins over both. Any key may
|
||||
* appear in either file; a key in {@code docx4j-fo.properties} outside
|
||||
* {@code docx4j.convert.out.fo} is honoured but logged, since it is probably in the wrong
|
||||
* file. Absent, the file is not missed (a deployment without docx4j-export-fo has no
|
||||
* use for it); an absent {@code docx4j.properties} is still warned about.</p>
|
||||
*/
|
||||
public class Docx4jProperties {
|
||||
|
||||
protected static Logger log = LoggerFactory.getLogger(Docx4jProperties.class);
|
||||
|
||||
/** The optional second file; see the class comment. @since 17.2.0 */
|
||||
public static final String FO_PROPERTIES = "docx4j-fo.properties";
|
||||
|
||||
/** Keys expected in {@link #FO_PROPERTIES}; others there are honoured but logged. @since 17.2.0 */
|
||||
public static final String FO_PREFIX = "docx4j.convert.out.fo";
|
||||
|
||||
private static Properties properties;
|
||||
|
||||
private static void init() {
|
||||
|
||||
defaultTheme = null;
|
||||
InputStream main = null;
|
||||
try {
|
||||
main = ResourceUtils.getResource("docx4j.properties");
|
||||
} catch (Exception e) {
|
||||
log.warn("Couldn't find/read docx4j.properties; " + e.getMessage());
|
||||
}
|
||||
InputStream fo = null;
|
||||
try {
|
||||
fo = ResourceUtils.getResourceIfPresent(FO_PROPERTIES);
|
||||
} catch (Exception e) {
|
||||
log.warn("Couldn't read " + FO_PROPERTIES + "; " + e.getMessage());
|
||||
}
|
||||
try {
|
||||
properties = load(main, fo);
|
||||
} finally {
|
||||
close(main);
|
||||
close(fo);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The table {@link #init()} builds: {@code docx4j.properties}' entries, then those of
|
||||
* {@code docx4j-fo.properties} the first did not set. Either stream may be null (the
|
||||
* file is absent); neither is closed here.
|
||||
*
|
||||
* @since 17.2.0
|
||||
*/
|
||||
public static Properties load(InputStream docx4jProperties, InputStream foProperties) {
|
||||
Properties p = new Properties();
|
||||
if (docx4jProperties != null) {
|
||||
try {
|
||||
p.load(docx4jProperties);
|
||||
} catch (Exception e) {
|
||||
log.warn("Couldn't read docx4j.properties; " + e.getMessage());
|
||||
}
|
||||
}
|
||||
if (foProperties != null) {
|
||||
Properties fo = new Properties();
|
||||
try {
|
||||
fo.load(foProperties);
|
||||
} catch (Exception e) {
|
||||
log.warn("Couldn't read " + FO_PROPERTIES + "; " + e.getMessage());
|
||||
}
|
||||
int taken = 0;
|
||||
for (String key : fo.stringPropertyNames()) {
|
||||
if (!key.startsWith(FO_PREFIX)) {
|
||||
log.info(FO_PROPERTIES + " sets " + key + ", which is not a " + FO_PREFIX
|
||||
+ " property; honoured, but docx4j.properties is its usual place");
|
||||
}
|
||||
if (p.containsKey(key)) {
|
||||
log.debug(key + ": docx4j.properties' value kept over " + FO_PROPERTIES + "'s");
|
||||
} else {
|
||||
p.setProperty(key, fo.getProperty(key));
|
||||
taken++;
|
||||
}
|
||||
}
|
||||
log.debug(FO_PROPERTIES + ": " + taken + " of " + fo.size() + " properties taken");
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
private static void close(InputStream is) {
|
||||
if (is != null) {
|
||||
try {
|
||||
is.close();
|
||||
} catch (Exception e) {
|
||||
log.debug("closing: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Which Office theme docx4j supplies where a document asks for a theme font and its
|
||||
* package has <b>no theme part</b>, and which theme part
|
||||
* {@code WordprocessingMLPackage.createPackage} puts in a new package.
|
||||
*
|
||||
* <p>Word supplies its own default theme to such a document, and which faces that names
|
||||
* has changed twice: measured on a Word 365 rendering of a package with no theme part
|
||||
* (CR-001 batch 49), the body face is <b>Aptos</b> and the heading face
|
||||
* <b>Aptos Display</b>, where Office 2013 to 2022 answered Calibri and Calibri Light and
|
||||
* Office 2007 to 2010 answered Calibri and Cambria. So this is the version of Word a
|
||||
* themeless document should be read as having come from - not a docx4j preference - and
|
||||
* the default is the current one.</p>
|
||||
*
|
||||
* <p>Values: {@code 2023} (the default), {@code 2013}, {@code 2007};
|
||||
* see {@link DefaultTheme}. Anything else logs a warning and falls back to the default.</p>
|
||||
*
|
||||
* @since 17.2.0
|
||||
*/
|
||||
public static final String DEFAULT_THEME = "docx4j.fonts.defaultTheme";
|
||||
|
||||
/**
|
||||
* The three answers {@link Docx4jProperties#DEFAULT_THEME} takes, each with the Latin
|
||||
* faces its font scheme names and the theme part docx4j bundles for it.
|
||||
*
|
||||
* @since 17.2.0
|
||||
*/
|
||||
public enum DefaultTheme {
|
||||
|
||||
/** Word 365's Office theme: Aptos Display / Aptos. The default. */
|
||||
THEME_2023("2023", "Aptos Display", "Aptos"),
|
||||
/** Office 2013 to 2022: Calibri Light / Calibri. */
|
||||
THEME_2013("2013", "Calibri Light", "Calibri"),
|
||||
/** Office 2007 to 2010: Cambria / Calibri; docx4j's answer up to 17.1.0. */
|
||||
THEME_2007("2007", "Cambria", "Calibri");
|
||||
|
||||
private final String value;
|
||||
private final String majorLatin;
|
||||
private final String minorLatin;
|
||||
|
||||
private DefaultTheme(String value, String majorLatin, String minorLatin) {
|
||||
this.value = value;
|
||||
this.majorLatin = majorLatin;
|
||||
this.minorLatin = minorLatin;
|
||||
}
|
||||
|
||||
/** The property value which selects this theme. */
|
||||
public String value() {
|
||||
return value;
|
||||
}
|
||||
|
||||
/** The face its font scheme names for the major (heading) Latin slot. */
|
||||
public String majorLatin() {
|
||||
return majorLatin;
|
||||
}
|
||||
|
||||
/** The face its font scheme names for the minor (body) Latin slot. */
|
||||
public String minorLatin() {
|
||||
return minorLatin;
|
||||
}
|
||||
|
||||
/** The classpath resource holding this theme's own theme1.xml. */
|
||||
public String resource() {
|
||||
return "org/docx4j/openpackaging/parts/WordprocessingML/theme-" + value + ".xml";
|
||||
}
|
||||
|
||||
/** The theme this property value names, or null where it names none. */
|
||||
public static DefaultTheme of(String value) {
|
||||
if (value == null) return null;
|
||||
String v = value.trim();
|
||||
for (DefaultTheme t : values()) {
|
||||
if (t.value.equalsIgnoreCase(v)) return t;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/** Read once (see {@link #getDefaultTheme()}); cleared by {@link #setProperty}. */
|
||||
private static DefaultTheme defaultTheme;
|
||||
|
||||
/**
|
||||
* {@link #DEFAULT_THEME}, parsed; never null.
|
||||
*
|
||||
* <p>Read once and remembered, because it is asked for per run of a themeless document.
|
||||
* {@code setProperty} clears it, so a unit test can change it.</p>
|
||||
*
|
||||
* @since 17.2.0
|
||||
*/
|
||||
public static DefaultTheme getDefaultTheme() {
|
||||
|
||||
DefaultTheme t = defaultTheme;
|
||||
if (t != null) return t;
|
||||
|
||||
String value = getProperty(DEFAULT_THEME);
|
||||
t = DefaultTheme.of(value);
|
||||
if (t == null) {
|
||||
if (value != null && value.trim().length() > 0) {
|
||||
log.warn(DEFAULT_THEME + "=" + value + " is not one of 2023, 2013 or 2007; using "
|
||||
+ DefaultTheme.THEME_2023.value());
|
||||
}
|
||||
t = DefaultTheme.THEME_2023;
|
||||
}
|
||||
defaultTheme = t;
|
||||
return t;
|
||||
}
|
||||
|
||||
public static String getProperty(String key) {
|
||||
|
||||
if (properties==null) {init();}
|
||||
|
||||
return properties.getProperty(key);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @since 2.7.2
|
||||
*/
|
||||
public static String getProperty(String key, String defaultValue) {
|
||||
|
||||
if (properties==null) {init();}
|
||||
|
||||
return properties.getProperty(key, defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 3.3.0
|
||||
*/
|
||||
public static boolean getProperty(String key, boolean defaultValue) {
|
||||
|
||||
if (properties==null) {init();}
|
||||
String result = properties.getProperty(key, Boolean.toString(defaultValue));
|
||||
return Boolean.parseBoolean(result);
|
||||
}
|
||||
|
||||
public static int getProperty(String key, int defaultValue) {
|
||||
|
||||
if (properties==null) {init();}
|
||||
String val = properties.getProperty(key);
|
||||
if (val==null) return defaultValue;
|
||||
|
||||
try {
|
||||
return (Integer.parseInt(val));
|
||||
} catch (NumberFormatException e) {
|
||||
log.info(e.getMessage(),e);
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @since 6.0.0
|
||||
*/
|
||||
public static long getPropertyLong(String key, long defaultValue) {
|
||||
|
||||
if (properties==null) {init();}
|
||||
String val = properties.getProperty(key);
|
||||
if (val==null) return defaultValue;
|
||||
|
||||
try {
|
||||
return (Long.parseLong(val));
|
||||
} catch (NumberFormatException e) {
|
||||
log.info(e.getMessage(),e);
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static Properties getProperties() {
|
||||
|
||||
if (properties==null) {init();}
|
||||
return properties;
|
||||
}
|
||||
|
||||
/**
|
||||
* Useful if a unit test requires a certain property value.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public static void setProperty(String key, Boolean value) {
|
||||
if (properties==null) {init();}
|
||||
properties.setProperty(key, value.toString());
|
||||
if (DEFAULT_THEME.equals(key)) defaultTheme = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Useful if a unit test requires a certain property value.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public static void setProperty(String key, String value) {
|
||||
if (properties==null) {init();}
|
||||
properties.setProperty(key, value);
|
||||
if (DEFAULT_THEME.equals(key)) defaultTheme = null;
|
||||
}
|
||||
/**
|
||||
* @since 6.0.0
|
||||
*/
|
||||
public static void setPropertyLong(String key, long value) {
|
||||
|
||||
if (properties==null) {init();}
|
||||
properties.setProperty(key, Long.toString(value));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,295 @@
|
||||
/*
|
||||
* Copyright 2007-2008, Plutext Pty Ltd.
|
||||
*
|
||||
* This file is part of docx4j.
|
||||
|
||||
docx4j is licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
||||
*/
|
||||
|
||||
|
||||
package org.docx4j;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.io.Writer;
|
||||
|
||||
import jakarta.xml.bind.JAXBContext;
|
||||
import jakarta.xml.bind.JAXBElement;
|
||||
import jakarta.xml.bind.JAXBException;
|
||||
import jakarta.xml.bind.Marshaller;
|
||||
import javax.xml.namespace.QName;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.docx4j.jaxb.Context;
|
||||
import org.docx4j.jaxb.NamespacePrefixMapperUtils;
|
||||
import org.docx4j.openpackaging.exceptions.Docx4JException;
|
||||
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
|
||||
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
|
||||
import org.xml.sax.SAXException;
|
||||
import org.xml.sax.helpers.DefaultHandler;
|
||||
|
||||
/**
|
||||
* The character content of an object's XML, as its marshalled form gives it.
|
||||
*
|
||||
* @deprecated since 17.3.4: this is not docx4j's text model, and no reader docx4j ships uses it
|
||||
* (CR-046 phase 3, WD REQ-202). It returns the text of whatever it is given, in whatever view the
|
||||
* object is (deleted text included), with no stories, no hidden text left out, no separators
|
||||
* between paragraphs, and field codes for some objects. Use {@link org.docx4j.model.text.Texts}
|
||||
* ({@code Texts.plainText(pkg, options)}, or {@code Texts.of(part or content, options)} for the
|
||||
* block model), which reads a document as the text model specifies. Its output is unchanged.
|
||||
*/
|
||||
@Deprecated
|
||||
public class TextUtils {
|
||||
|
||||
private static Logger log = LoggerFactory.getLogger(TextUtils.class);
|
||||
|
||||
/**
|
||||
* Extract contents of descendant <w:t> elements.
|
||||
*
|
||||
* @param o
|
||||
* @return String
|
||||
* @since 6.0.0
|
||||
* @deprecated since 17.3.4: not the text model; see {@link org.docx4j.model.text.Texts}
|
||||
*/
|
||||
@Deprecated
|
||||
public static String getText(Object o) {
|
||||
|
||||
StringWriter w = new StringWriter();
|
||||
try {
|
||||
extractText(o, w, Context.jc);
|
||||
} catch (Exception e) {
|
||||
log.warn(e.getMessage());
|
||||
return null;
|
||||
}
|
||||
return w.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract contents of descendant <w:t> elements.
|
||||
*
|
||||
* @param o
|
||||
* @param jcSVG JAXBContext
|
||||
* @return
|
||||
* @deprecated since 17.3.4: not the text model; see {@link org.docx4j.model.text.Texts}
|
||||
*/
|
||||
@Deprecated
|
||||
public static void extractText(Object o, Writer w) throws Docx4JException {
|
||||
|
||||
extractText(o, w, Context.jc);
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract contents of descendant <w:t> elements.
|
||||
*
|
||||
* @param o
|
||||
* @param jc JAXBContext
|
||||
* @return
|
||||
* @throws Docx4JException
|
||||
* @deprecated since 17.3.4: not the text model; see {@link org.docx4j.model.text.Texts}
|
||||
*/
|
||||
@Deprecated
|
||||
public static void extractText(Object o, Writer w, JAXBContext jc) throws Docx4JException {
|
||||
|
||||
if (o==null) {
|
||||
throw new Docx4JException("Can't extractText from null object");
|
||||
}
|
||||
try {
|
||||
Marshaller marshaller=jc.createMarshaller();
|
||||
NamespacePrefixMapperUtils.setProperty(marshaller,
|
||||
NamespacePrefixMapperUtils.getPrefixMapper());
|
||||
marshaller.marshal(o, new TextExtractor(w));
|
||||
} catch (JAXBException e) {
|
||||
throw new Docx4JException("JAXB error marshalling to extractText", e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract contents of descendant <w:t> elements.
|
||||
* Use this for objects which don't have @XmlRootElement
|
||||
*
|
||||
* @param o
|
||||
* @param w
|
||||
* @param jc
|
||||
* @param uri
|
||||
* @param local
|
||||
* @param declaredType
|
||||
* @throws Exception
|
||||
* @deprecated since 17.3.4: not the text model; see {@link org.docx4j.model.text.Texts}
|
||||
*/
|
||||
@Deprecated
|
||||
public static void extractText(Object o, Writer w, JAXBContext jc,
|
||||
String uri, String local, Class declaredType) throws Docx4JException {
|
||||
|
||||
try {
|
||||
Marshaller marshaller=jc.createMarshaller();
|
||||
NamespacePrefixMapperUtils.setProperty(marshaller,
|
||||
NamespacePrefixMapperUtils.getPrefixMapper());
|
||||
marshaller.marshal(
|
||||
new JAXBElement(new QName(uri,local), declaredType, o ),
|
||||
new TextExtractor(w));
|
||||
} catch (JAXBException e) {
|
||||
throw new Docx4JException("JAXB error marshalling to extractText", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* A SAX ContentHandler that writes all #PCDATA onto a java.io.Writer
|
||||
*
|
||||
* From http://www.cafeconleche.org/books/xmljava/chapters/ch06s03.html
|
||||
*
|
||||
*/
|
||||
/**
|
||||
* Writes the character content it is handed.
|
||||
*
|
||||
* <p>mc:AlternateContent (since 17.2.0, CR-021): the text of ONE branch - the one
|
||||
* {@link org.docx4j.jaxb.McSelection} draws - is written; the other branches are
|
||||
* skipped, so a text box's text appears once. The rule is McSelection's exactly:
|
||||
* the first Choice whose Requires prefixes are all preferred, else the Fallback,
|
||||
* else (Choices but no Fallback) the first Choice. Being a stream, this cannot
|
||||
* know a Fallback is absent until the element ends, so the first unpreferred
|
||||
* Choice's text is held back and written only then, if nothing else was taken.</p>
|
||||
*/
|
||||
public static class TextExtractor extends DefaultHandler {
|
||||
|
||||
private static final String MC_NS = "http://schemas.openxmlformats.org/markup-compatibility/2006";
|
||||
|
||||
private Writer out;
|
||||
|
||||
/** One open mc:AlternateContent. */
|
||||
private static final class Alternate {
|
||||
boolean taken;
|
||||
StringBuilder firstChoice; // held-back text of the first unpreferred Choice
|
||||
int firstChoiceDepth; // the depth of that Choice element
|
||||
}
|
||||
|
||||
private final java.util.ArrayDeque<Alternate> alternates = new java.util.ArrayDeque<Alternate>();
|
||||
/** Where characters go: the innermost held-back Choice's buffer, else the writer. */
|
||||
private final java.util.ArrayDeque<StringBuilder> captures = new java.util.ArrayDeque<StringBuilder>();
|
||||
/** Depth inside a skipped branch; 0 when writing. */
|
||||
private int skipping = 0;
|
||||
private int depth = 0;
|
||||
|
||||
public TextExtractor(Writer out) {
|
||||
this.out = out;
|
||||
}
|
||||
|
||||
private void emit(CharSequence text) throws SAXException {
|
||||
if (captures.isEmpty()) {
|
||||
try {
|
||||
out.write(text.toString());
|
||||
} catch (IOException e) {
|
||||
throw new SAXException(e);
|
||||
}
|
||||
} else {
|
||||
captures.peek().append(text);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void startElement(String uri, String localName, String qName, org.xml.sax.Attributes atts)
|
||||
throws SAXException {
|
||||
depth++;
|
||||
if (skipping > 0) {
|
||||
skipping++;
|
||||
return;
|
||||
}
|
||||
if (!MC_NS.equals(uri)) return;
|
||||
if ("AlternateContent".equals(localName)) {
|
||||
alternates.push(new Alternate());
|
||||
} else if ("Choice".equals(localName)) {
|
||||
Alternate alt = alternates.peek();
|
||||
String requires = atts.getValue("Requires");
|
||||
if (requires == null) requires = atts.getValue("", "Requires");
|
||||
if (alt == null) return; // a stray Choice: written through
|
||||
if (!alt.taken && org.docx4j.jaxb.McSelection.prefersChoice(requires)) {
|
||||
alt.taken = true;
|
||||
alt.firstChoice = null;
|
||||
} else if (!alt.taken && alt.firstChoice == null) {
|
||||
// the last resort, held back until the element ends
|
||||
alt.firstChoice = new StringBuilder();
|
||||
alt.firstChoiceDepth = depth;
|
||||
captures.push(alt.firstChoice);
|
||||
} else {
|
||||
skipping = 1;
|
||||
}
|
||||
} else if ("Fallback".equals(localName)) {
|
||||
Alternate alt = alternates.peek();
|
||||
if (alt == null) return;
|
||||
if (!alt.taken) {
|
||||
alt.taken = true;
|
||||
alt.firstChoice = null; // the held-back Choice is not needed
|
||||
} else {
|
||||
skipping = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endElement(String uri, String localName, String qName) throws SAXException {
|
||||
if (skipping > 0) {
|
||||
skipping--;
|
||||
depth--;
|
||||
return;
|
||||
}
|
||||
if (MC_NS.equals(uri)) {
|
||||
if ("Choice".equals(localName)) {
|
||||
Alternate alt = alternates.peek();
|
||||
if (alt != null && alt.firstChoiceDepth == depth && !captures.isEmpty()
|
||||
&& (alt.firstChoice == null || captures.peek() == alt.firstChoice)) {
|
||||
captures.pop();
|
||||
}
|
||||
} else if ("AlternateContent".equals(localName) && !alternates.isEmpty()) {
|
||||
Alternate alt = alternates.pop();
|
||||
if (!alt.taken && alt.firstChoice != null) {
|
||||
emit(alt.firstChoice);
|
||||
}
|
||||
}
|
||||
}
|
||||
depth--;
|
||||
}
|
||||
|
||||
public void characters(char[] text, int start, int length) throws SAXException {
|
||||
if (skipping > 0) return;
|
||||
emit(new String(text, start, length));
|
||||
}
|
||||
} // end TextExtractor
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
String inputfilepath = System.getProperty("user.dir") + "/sample-docs/Table.docx";
|
||||
//String inputfilepath = System.getProperty("user.dir") + "/sample-docs/Word2007-fonts.docx";
|
||||
|
||||
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath));
|
||||
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
|
||||
|
||||
org.docx4j.wml.Document wmlDocumentEl = (org.docx4j.wml.Document)documentPart.getJaxbElement();
|
||||
|
||||
// UTF-8 whatever the platform's default, so that no character is lost to a code page (WD REQ-711)
|
||||
Writer out = new OutputStreamWriter(System.out, java.nio.charset.StandardCharsets.UTF_8);
|
||||
|
||||
extractText(wmlDocumentEl, out);
|
||||
|
||||
//out.flush();
|
||||
out.close();
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,968 @@
|
||||
/*
|
||||
* Copyright 2010-2011, Plutext Pty Ltd.
|
||||
*
|
||||
* This file is part of docx4j.
|
||||
|
||||
docx4j is licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
||||
*/
|
||||
package org.docx4j;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import jakarta.xml.bind.JAXBElement;
|
||||
|
||||
import org.docx4j.com.microsoft.schemas.office.word.x2010.wordprocessingShape.CTWordprocessingShape;
|
||||
import org.docx4j.dml.CTHyperlink;
|
||||
import org.docx4j.dml.CTNonVisualDrawingProps;
|
||||
import org.docx4j.dml.diagram.CTDataModel;
|
||||
import org.docx4j.jaxb.Context;
|
||||
import org.docx4j.jaxb.McMode;
|
||||
import org.docx4j.jaxb.McSelection;
|
||||
import org.docx4j.mce.AlternateContent;
|
||||
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
|
||||
import org.docx4j.openpackaging.parts.WordprocessingML.CommentsPart;
|
||||
import org.docx4j.openpackaging.parts.WordprocessingML.EndnotesPart;
|
||||
import org.docx4j.openpackaging.parts.WordprocessingML.FooterPart;
|
||||
import org.docx4j.openpackaging.parts.WordprocessingML.FootnotesPart;
|
||||
import org.docx4j.openpackaging.parts.WordprocessingML.HeaderPart;
|
||||
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
|
||||
import org.docx4j.openpackaging.parts.relationships.Namespaces;
|
||||
import org.docx4j.openpackaging.parts.relationships.RelationshipsPart;
|
||||
import org.docx4j.relationships.Relationship;
|
||||
import org.docx4j.utils.CompoundTraversalUtilVisitorCallback;
|
||||
import org.docx4j.utils.SingleTraversalUtilVisitorCallback;
|
||||
import org.docx4j.utils.TraversalUtilVisitor;
|
||||
import org.docx4j.wml.Br;
|
||||
import org.docx4j.wml.CTBookmark;
|
||||
import org.docx4j.wml.CTMarkupRange;
|
||||
import org.docx4j.wml.CTObject;
|
||||
import org.docx4j.wml.Comments.Comment;
|
||||
import org.docx4j.wml.FldChar;
|
||||
import org.docx4j.wml.Pict;
|
||||
import org.docx4j.wml.ProofErr;
|
||||
import org.docx4j.wml.R;
|
||||
import org.jvnet.jaxb.lang.Child;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
|
||||
/**
|
||||
* Traverse a list of JAXB objects (eg document.xml's document/body
|
||||
* children), and do something to them.
|
||||
*
|
||||
* This is similar to what one could do via XSLT,
|
||||
* but avoids marshalling/unmarshalling. The downside is that
|
||||
* not everything will necessarily get traversed here
|
||||
* since visitChildren is not (yet) comprehensive.
|
||||
*
|
||||
* This utility might be redundant if we used
|
||||
* http://code.google.com/p/jaxb-visitor/ or
|
||||
* https://github.com/ops4j/org.ops4j.xvisitor
|
||||
* at XJC time. Haven't tried them though.
|
||||
*
|
||||
* See also org.docx4j.utils.SingleTraversalUtilVisitorCallback
|
||||
* and CompoundTraversalUtilVisitorCallback
|
||||
*
|
||||
* <p><b>mc:AlternateContent</b> (since 17.2.0, CR-021): a walk visits ONE branch of
|
||||
* each mc:AlternateContent by default, which is {@link McMode#READ}: since 17.3.4 the
|
||||
* branch docx4j reads for text ({@link org.docx4j.jaxb.McPurpose#TEXT}, CR-046 phase 3),
|
||||
* before it the branch docx4j draws, which a walk serving an output's layout asks for
|
||||
* with {@link McMode#DRAW} (see {@link McSelection}). A walk that edits the tree
|
||||
* (binding, field update, merge, anonymisation, find and replace) must ask for
|
||||
* {@link McMode#ALL} through the constructor or visit overload that takes a mode,
|
||||
* or by {@link CallbackImpl#setMcMode}, so its edit reaches every branch. Before
|
||||
* 17.2.0 every walk saw every branch, and a text box's paragraphs twice.</p>
|
||||
*
|
||||
* @author jharrop, alberto
|
||||
*
|
||||
*/
|
||||
public class TraversalUtil {
|
||||
|
||||
private static Logger log = LoggerFactory.getLogger(TraversalUtil.class);
|
||||
|
||||
public interface Callback {
|
||||
|
||||
void walkJAXBElements(Object parent);
|
||||
|
||||
List<Object> getChildren(Object o);
|
||||
|
||||
/**
|
||||
* Visits a node in pre order (before its children have been visited).
|
||||
*
|
||||
* A node is visited only if all its parents have been traversed (
|
||||
* {@link #shouldTraverse(Object)}).</p>
|
||||
*
|
||||
* <p>
|
||||
* Implementations can have side effects.
|
||||
* </p>
|
||||
*/
|
||||
List<Object> apply(Object o);
|
||||
|
||||
/**
|
||||
* Decide whether this node's children should be traversed.
|
||||
*
|
||||
* @return whether the children of this node should be visited
|
||||
*/
|
||||
boolean shouldTraverse(Object o);
|
||||
|
||||
// TODO for next gen interface, add
|
||||
// List<Object> applyAfterWalkChildren(Object o);
|
||||
|
||||
}
|
||||
|
||||
public static abstract class CallbackImpl implements Callback {
|
||||
|
||||
// Depth first
|
||||
public void walkJAXBElements(Object parent) {
|
||||
|
||||
List children = getChildren(parent);
|
||||
if (children != null) {
|
||||
|
||||
Object o2;
|
||||
for (Object o : children) {
|
||||
|
||||
// In 3.3.4, we don't expect to do any parent fixes here.
|
||||
if (o instanceof jakarta.xml.bind.JAXBElement) {
|
||||
// get its value; this is ok,
|
||||
// provided the results of the Callback
|
||||
// won't be marshalled
|
||||
o2 = ((JAXBElement)o).getValue();
|
||||
|
||||
if (o2 instanceof Child) {
|
||||
|
||||
if (parent instanceof List){
|
||||
// It shouldn't be (as ArrayListWml usually handles), but do nothing
|
||||
if (log.isDebugEnabled()) {
|
||||
if ( ((Child)o2).getParent()==null) {
|
||||
log.debug("Unknown parent for " + o2.getClass().getName());
|
||||
} else {
|
||||
log.debug("Parent of " + o2.getClass().getName()
|
||||
+ " is currently " + ((Child)o2).getParent().getClass().getName());
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
||||
if (parent==((Child)o2).getParent()) {
|
||||
// all good
|
||||
|
||||
} else {
|
||||
|
||||
// workaround for broken getParent in cases where ArrayListWml doesn't help
|
||||
// (ie we're not in a content list)
|
||||
if ( ((Child)o2).getParent()==null) {
|
||||
|
||||
// 3.3.4: to setParent, or not?
|
||||
// Yes, let's, since something is better than null.
|
||||
// But note, we could be setting it to some other ancestor because of how getChildren works
|
||||
// When does this ever happen? Log at warn level to find out..
|
||||
// I think it would only ever happen if there was foo.setBar(JAXBElement z)
|
||||
if (log.isWarnEnabled()) {
|
||||
log.warn("Unknown parent for " + o2.getClass().getName()
|
||||
+ "; setting to " + parent.getClass().getName()
|
||||
);
|
||||
}
|
||||
((Child)o2).setParent(parent);
|
||||
|
||||
} else if (((Child)o2).getParent() instanceof org.docx4j.dml.GraphicData) {
|
||||
// by design: handleGraphicData flattens a:graphicData's content (a chart's
|
||||
// c:chart or cx:chart) into the anchor's children, so the child's own parent
|
||||
// is the GraphicData while the walk's is the anchor; not a defect (a chartex
|
||||
// chart's CTRelId warned here once per chart, Enterprise's finding 2026-10-28)
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Parent of " + o2.getClass().getName() + " is its GraphicData; the walk's is "
|
||||
+ parent.getClass().getName());
|
||||
}
|
||||
} else {
|
||||
// If this happens, we need to understand why
|
||||
// eg Parent of org.docx4j.wml.Tc is currently org.docx4j.wml.CTSdtContentCell
|
||||
if (log.isWarnEnabled()) {
|
||||
log.warn("Parent of " + o2.getClass().getName()
|
||||
+ " is currently " + ((Child)o2).getParent().getClass().getName()
|
||||
+ " not " + parent.getClass().getName()
|
||||
);
|
||||
}
|
||||
// We don't things in this case
|
||||
//((Child)o2).setParent(parent);
|
||||
}
|
||||
if (log.isInfoEnabled()) {
|
||||
log.info("setting to " + parent.getClass().getName() );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
} else /* not wrapped in JAXBElement, so getParent should be OK. We never change in this case. */ {
|
||||
o2 = o;
|
||||
if (log.isDebugEnabled()) {
|
||||
|
||||
if (o2 instanceof Child) {
|
||||
if (((Child) o2).getParent() == null) {
|
||||
|
||||
log.debug("Unknown parent for "
|
||||
+ o2.getClass().getName());
|
||||
|
||||
} else if (parent != ((Child) o2).getParent()) {
|
||||
|
||||
// This can happen because getChildren() skips layers,
|
||||
// so the 'parent' passed in here might actually be the grandparent
|
||||
log.debug("Parent of "
|
||||
+ o2.getClass().getName()
|
||||
+ " is currently "
|
||||
+ ((Child) o2).getParent().getClass()
|
||||
.getName()
|
||||
+ " not " + parent.getClass().getName() );
|
||||
// Parent of org.docx4j.wml.P is org.docx4j.wml.Body
|
||||
// (org.docx4j.wml.ArrayListWml should never be the parent; that's an implementation detail)
|
||||
}
|
||||
} else {
|
||||
log.info(o2.getClass().getName() + " not an instanceof Child!");
|
||||
// eg org.docx4j.wml.STFldCharType
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
this.apply(o2);
|
||||
|
||||
if (this.shouldTraverse(o2)) {
|
||||
walkJAXBElements(o2);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private McMode mcMode = McMode.READ;
|
||||
|
||||
/** How this walk treats mc:AlternateContent; {@link McMode#READ} unless set.
|
||||
* @since 17.2.0 */
|
||||
public McMode getMcMode() {
|
||||
return mcMode;
|
||||
}
|
||||
|
||||
/** A walk that edits the tree sets {@link McMode#ALL} before walking, so its
|
||||
* edit lands in every branch of each mc:AlternateContent. @since 17.2.0 */
|
||||
public void setMcMode(McMode mcMode) {
|
||||
this.mcMode = mcMode == null ? McMode.READ : mcMode;
|
||||
}
|
||||
|
||||
public List<Object> getChildren(Object o) {
|
||||
return TraversalUtil.getChildrenImpl(o, mcMode);
|
||||
}
|
||||
|
||||
/**
|
||||
* Visits a node in pre order (before its children have been visited).
|
||||
|
||||
*
|
||||
* A node is visited only if all its parents have been traversed (
|
||||
* {@link #shouldTraverse(Object)}).</p>
|
||||
*
|
||||
* <p>
|
||||
* Implementations can have side effects.
|
||||
* </p>
|
||||
*/
|
||||
public abstract List<Object> apply(Object o);
|
||||
|
||||
/**
|
||||
* Decide whether this node's children should be traversed.
|
||||
*
|
||||
* @return whether the children of this node should be visited
|
||||
*/
|
||||
public boolean shouldTraverse(Object o) {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Callback cb;
|
||||
|
||||
/**
|
||||
* Traverse the object using your callback. Invoking this constructor starts the
|
||||
* traverse, by invoking the callback's walkJAXBElements method.
|
||||
*
|
||||
* @param parent
|
||||
* @param cb
|
||||
*/
|
||||
public TraversalUtil(Object parent, Callback cb) {
|
||||
|
||||
this.cb = cb;
|
||||
cb.walkJAXBElements(parent);
|
||||
}
|
||||
|
||||
/**
|
||||
* As {@link #TraversalUtil(Object, Callback)}, with the walk's treatment of
|
||||
* mc:AlternateContent: {@link McMode#ALL} for a callback that edits the tree.
|
||||
* The mode is set on a {@link CallbackImpl}; a callback implementing
|
||||
* {@link Callback} directly applies it in its own getChildren, by calling
|
||||
* {@link #getChildrenImpl(Object, McMode)}.
|
||||
*
|
||||
* @since 17.2.0 (CR-021)
|
||||
*/
|
||||
public TraversalUtil(Object parent, Callback cb, McMode mcMode) {
|
||||
this.cb = cb;
|
||||
if (cb instanceof CallbackImpl) {
|
||||
((CallbackImpl) cb).setMcMode(mcMode);
|
||||
} else if (mcMode == McMode.ALL) {
|
||||
log.debug(cb.getClass().getName() + " does not extend CallbackImpl; it applies McMode.ALL itself or not at all");
|
||||
}
|
||||
cb.walkJAXBElements(parent);
|
||||
}
|
||||
|
||||
static void visitChildrenImpl(Object o) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
private static List<Object> handleGraphicData(org.docx4j.dml.GraphicData graphicData) {
|
||||
// Added by Peter Buil
|
||||
List<Object> tmpArtificialList = new ArrayList<Object>();
|
||||
if (graphicData.getPic() != null) {
|
||||
//GraphicData can have a hyperlink reference, which can be found this way
|
||||
CTNonVisualDrawingProps picNonVisual = graphicData.getPic().getNvPicPr().getCNvPr();
|
||||
if (picNonVisual != null) {
|
||||
handleCTNonVisualDrawingProps(picNonVisual, tmpArtificialList);
|
||||
}
|
||||
}
|
||||
// Its not graphicData.getAny() we're typically interested in
|
||||
if (graphicData.getPic() != null && graphicData.getPic().getBlipFill() != null
|
||||
&& graphicData.getPic().getBlipFill().getBlip() != null) {
|
||||
log.debug("found CTBlip");
|
||||
List<Object> artificialList = new ArrayList<Object>();
|
||||
if (!tmpArtificialList.isEmpty())
|
||||
artificialList.addAll(tmpArtificialList);
|
||||
artificialList.add(graphicData.getPic().getBlipFill().getBlip());
|
||||
return artificialList;
|
||||
} else if (graphicData.getWordprocessingShape() != null) {
|
||||
CTWordprocessingShape wsp = graphicData.getWordprocessingShape();
|
||||
if (wsp.getTxbx()==null) {
|
||||
return null;
|
||||
} else {
|
||||
return wsp.getTxbx().getTxbxContent().getContent();
|
||||
// grandchildren, like we do for vml textbox
|
||||
}
|
||||
} else {
|
||||
// Chart is in here
|
||||
return graphicData.getAny();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* There can be hyperlinks references in CTNonVisualDrawingProps.
|
||||
* @param drawingProps
|
||||
* @param artificialList
|
||||
*/
|
||||
private static void handleCTNonVisualDrawingProps(CTNonVisualDrawingProps drawingProps, List<Object> artificialList){
|
||||
if (drawingProps != null) {
|
||||
CTHyperlink docPrHyperLink = drawingProps.getHlinkClick();
|
||||
if (docPrHyperLink != null)
|
||||
artificialList.add(docPrHyperLink);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the children of some docx content object
|
||||
* (as opposed to pptx, xlsx content).
|
||||
*
|
||||
* @param o
|
||||
* @return
|
||||
*/
|
||||
public static List<Object> getChildrenImpl(Object o) {
|
||||
return getChildrenImpl(o, McMode.READ);
|
||||
}
|
||||
|
||||
/**
|
||||
* As {@link #getChildrenImpl(Object)}, with the treatment of mc:AlternateContent:
|
||||
* in {@link McMode#READ} its children are the one branch docx4j reads for text (since
|
||||
* 17.3.4; {@link McSelection#branches(AlternateContent, McMode)}), in {@link McMode#DRAW}
|
||||
* the one it draws ({@link McSelection#selectedBranch}); in {@link McMode#ALL} every
|
||||
* Choice and the Fallback. Everything else is the same in every mode.
|
||||
*
|
||||
* @since 17.2.0 (CR-021)
|
||||
*/
|
||||
public static List<Object> getChildrenImpl(Object o, McMode mcMode) {
|
||||
|
||||
if (o==null) {
|
||||
log.warn("null passed to getChildrenImpl");
|
||||
return null;
|
||||
}
|
||||
|
||||
if (o instanceof JAXBElement) {
|
||||
log.debug(".. unwrapping");
|
||||
o = XmlUtils.unwrap(o);
|
||||
}
|
||||
|
||||
if (log.isDebugEnabled()) log.debug("getting children of " + o.getClass().getName() );
|
||||
if (o instanceof org.docx4j.wml.Text) return null;
|
||||
|
||||
// Short circuit for common elements
|
||||
if (o instanceof List) {
|
||||
// Handy if you have your own list of objects you wish to process
|
||||
return (List<Object>) o;
|
||||
|
||||
} else if (o instanceof org.docx4j.wml.Document) {
|
||||
// since 3.3.4, so traverse visits Body as well. This is more correct, and potentially useful.
|
||||
List<Object> artificialList = new ArrayList<Object>();
|
||||
artificialList.add( ((org.docx4j.wml.Document)o).getBody() );
|
||||
return artificialList;
|
||||
|
||||
} else if (o instanceof Br
|
||||
|| o instanceof CTBookmark
|
||||
|| o instanceof CTMarkupRange) {
|
||||
return null;
|
||||
} else if (o instanceof R.Tab
|
||||
|| o instanceof R.LastRenderedPageBreak) {
|
||||
return null;
|
||||
} else if (o instanceof org.docx4j.wml.ContentAccessor) {
|
||||
return ((org.docx4j.wml.ContentAccessor) o).getContent();
|
||||
|
||||
} else if (o instanceof org.docx4j.wml.SdtElement) {
|
||||
if (((org.docx4j.wml.SdtElement) o).getSdtContent()!=null) {
|
||||
return ((org.docx4j.wml.SdtElement) o).getSdtContent().getContent();
|
||||
} else {
|
||||
log.warn("SdtElement is missing content element");
|
||||
return null;
|
||||
}
|
||||
|
||||
// For 3.3.4, I'd prefer to do the following, but it breaks OpenDoPE repeat handling!!
|
||||
|
||||
// } else if (o instanceof org.docx4j.wml.SdtElement) {
|
||||
// // 3.3.4: don't just return ((org.docx4j.wml.SdtElement) o).getSdtContent().getContent()
|
||||
// // since then we're passing the grandparent as the parent,
|
||||
// // and we don't want to use that in any parent fix...
|
||||
// List<Object> artificialList = new ArrayList<Object>();
|
||||
// artificialList.add(
|
||||
// ((org.docx4j.wml.SdtElement) o).getSdtContent());
|
||||
// return artificialList;
|
||||
|
||||
} else if (o instanceof org.docx4j.dml.wordprocessingDrawing.Anchor) {
|
||||
org.docx4j.dml.wordprocessingDrawing.Anchor anchor = (org.docx4j.dml.wordprocessingDrawing.Anchor) o;
|
||||
List<Object> artificialList = new ArrayList<Object>();
|
||||
CTNonVisualDrawingProps drawingProps = anchor.getDocPr();
|
||||
if (drawingProps != null) {
|
||||
handleCTNonVisualDrawingProps(drawingProps, artificialList);
|
||||
}
|
||||
if (anchor.getGraphic() != null) {
|
||||
log.debug("found a:graphic");
|
||||
org.docx4j.dml.Graphic graphic = anchor.getGraphic();
|
||||
if (graphic.getGraphicData() != null) {
|
||||
List<Object> handledGraphicData = handleGraphicData(graphic.getGraphicData());
|
||||
if (handledGraphicData==null) {
|
||||
// log.info("handleGraphicData returned an empty list");
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug(XmlUtils.marshaltoString(graphic, true, true, Context.jc,
|
||||
"http://schemas.openxmlformats.org/drawingml/2006/main", "graphic", org.docx4j.dml.Graphic.class));
|
||||
}
|
||||
} else {
|
||||
artificialList.addAll(handledGraphicData);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!artificialList.isEmpty())
|
||||
return artificialList;
|
||||
|
||||
} else if (o instanceof org.docx4j.dml.wordprocessingDrawing.Inline) {
|
||||
org.docx4j.dml.wordprocessingDrawing.Inline inline = (org.docx4j.dml.wordprocessingDrawing.Inline) o;
|
||||
List<Object> artificialList = new ArrayList<Object>();
|
||||
CTNonVisualDrawingProps drawingProps = inline.getDocPr();
|
||||
if (drawingProps != null) {
|
||||
handleCTNonVisualDrawingProps(drawingProps, artificialList);
|
||||
}
|
||||
if (inline.getGraphic() != null) {
|
||||
log.debug("found a:graphic");
|
||||
org.docx4j.dml.Graphic graphic = inline.getGraphic();
|
||||
if (graphic.getGraphicData() != null) {
|
||||
List<Object> handledGraphicData = handleGraphicData(graphic.getGraphicData());
|
||||
if (handledGraphicData==null) {
|
||||
// log.info("handleGraphicData returned an empty list");
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug(XmlUtils.marshaltoString(graphic, true, true, Context.jc,
|
||||
"http://schemas.openxmlformats.org/drawingml/2006/main", "graphic", org.docx4j.dml.Graphic.class));
|
||||
}
|
||||
} else {
|
||||
artificialList.addAll(handledGraphicData);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!artificialList.isEmpty())
|
||||
return artificialList;
|
||||
|
||||
} else if (o instanceof Pict) {
|
||||
return ((Pict)o).getAnyAndAny(); // (why didn't the reflection below find this?)
|
||||
|
||||
} else if (o instanceof ProofErr) {
|
||||
return null;
|
||||
|
||||
} else if (o instanceof org.docx4j.dml.picture.Pic) { // Post 2.7.1; untested
|
||||
|
||||
org.docx4j.dml.picture.Pic dmlPic = ((org.docx4j.dml.picture.Pic)o);
|
||||
if (dmlPic.getBlipFill()!=null
|
||||
&& dmlPic.getBlipFill().getBlip()!=null) {
|
||||
log.debug("found DML Blip");
|
||||
List<Object> artificialList = new ArrayList<Object>();
|
||||
artificialList.add(dmlPic.getBlipFill().getBlip());
|
||||
return artificialList;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
} else if (o instanceof org.docx4j.dml.CTGvmlPicture) { // Post 2.7.1
|
||||
|
||||
org.docx4j.dml.CTGvmlPicture dmlPic = ((org.docx4j.dml.CTGvmlPicture)o);
|
||||
if (dmlPic.getBlipFill()!=null
|
||||
&& dmlPic.getBlipFill().getBlip()!=null) {
|
||||
log.debug("found DML Blip");
|
||||
List<Object> artificialList = new ArrayList<Object>();
|
||||
artificialList.add(dmlPic.getBlipFill().getBlip());
|
||||
return artificialList;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
||||
} else if (o instanceof org.docx4j.vml.CTShape) {
|
||||
// return ((org.docx4j.vml.CTShape)o).getAny();
|
||||
List<Object> artificialList = new ArrayList<Object>();
|
||||
for (JAXBElement<?> j : ((org.docx4j.vml.CTShape)o).getPathOrFormulasOrHandles() ) {
|
||||
// System.out.println(XmlUtils.unwrap(j).getClass().getName() );
|
||||
artificialList.add(j);
|
||||
}
|
||||
return artificialList;
|
||||
} else if (o instanceof CTDataModel) {
|
||||
CTDataModel dataModel = (CTDataModel)o;
|
||||
List<Object> artificialList = new ArrayList<Object>();
|
||||
// We're going to create a list merging two children ..
|
||||
artificialList.addAll(dataModel.getPtLst().getPt());
|
||||
artificialList.addAll(dataModel.getCxnLst().getCxn());
|
||||
return artificialList;
|
||||
} else if (o instanceof org.docx4j.dml.diagram2008.CTDrawing) {
|
||||
return ((org.docx4j.dml.diagram2008.CTDrawing)o).getSpTree().getSpOrGrpSp();
|
||||
} else if (o instanceof org.docx4j.vml.CTTextbox) {
|
||||
// return ((org.docx4j.vml.CTTextbox)o).getAny();
|
||||
org.docx4j.vml.CTTextbox textBox = ((org.docx4j.vml.CTTextbox)o);
|
||||
if (textBox.getTxbxContent()==null) {
|
||||
return null;
|
||||
} else {
|
||||
return textBox.getTxbxContent().getContent();
|
||||
// grandchildren
|
||||
}
|
||||
// } else if (o instanceof org.docx4j.wml.CTTxbxContent) {
|
||||
// return ((org.docx4j.wml.CTTxbxContent)o).getEGBlockLevelElts();
|
||||
} else if (o instanceof CTObject) {
|
||||
|
||||
CTObject ctObject = (CTObject)o;
|
||||
List<Object> artificialList = new ArrayList<Object>();
|
||||
artificialList.addAll(ctObject.getAnyAndAny());
|
||||
if (ctObject.getControl()!=null) {
|
||||
artificialList.add(ctObject.getControl() ); // CTControl
|
||||
}
|
||||
return artificialList;
|
||||
|
||||
} else if (o instanceof org.docx4j.dml.CTGvmlGroupShape) {
|
||||
return ((org.docx4j.dml.CTGvmlGroupShape)o).getTxSpOrSpOrCxnSp();
|
||||
|
||||
} else if (o instanceof org.docx4j.dml.CTGvmlShape) {
|
||||
|
||||
org.docx4j.dml.CTGvmlShape sp = (org.docx4j.dml.CTGvmlShape)o;
|
||||
if (sp!=null
|
||||
&& sp.getTxSp()!=null
|
||||
&& sp.getTxSp().getTxBody()!=null) {
|
||||
|
||||
List<Object> artificialList = new ArrayList<Object>();
|
||||
artificialList.addAll(sp.getTxSp().getTxBody().getP());
|
||||
|
||||
return artificialList;
|
||||
}
|
||||
return null;
|
||||
|
||||
} else if(o instanceof FldChar) {
|
||||
FldChar fldChar = ((FldChar)o);
|
||||
List<Object> artificialList = new ArrayList<Object>();
|
||||
artificialList.add(fldChar.getFldCharType());
|
||||
if(fldChar.getFfData() != null) {
|
||||
artificialList.add(fldChar.getFfData());
|
||||
}
|
||||
if(fldChar.getFldData() != null) {
|
||||
artificialList.add(fldChar.getFldData());
|
||||
}
|
||||
if(fldChar.getNumberingChange() != null) {
|
||||
artificialList.add(fldChar.getNumberingChange());
|
||||
}
|
||||
return artificialList;
|
||||
} else if (o instanceof org.docx4j.mce.AlternateContent) {
|
||||
// CR-021: one branch (the one docx4j draws) unless the walk asked for ALL
|
||||
return McSelection.branches((AlternateContent)o, mcMode);
|
||||
} else if (o instanceof org.docx4j.com.microsoft.schemas.office.word.x2010.wordprocessingShape.CTWordprocessingShape) {
|
||||
org.docx4j.com.microsoft.schemas.office.word.x2010.wordprocessingShape.CTWordprocessingShape sp = (org.docx4j.com.microsoft.schemas.office.word.x2010.wordprocessingShape.CTWordprocessingShape)o;
|
||||
if (sp!=null
|
||||
&& sp.getTxbx()!=null
|
||||
&& sp.getTxbx().getTxbxContent()!=null) {
|
||||
|
||||
return sp.getTxbx().getTxbxContent().getContent();
|
||||
}
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
// OK, what is this? Use reflection ..
|
||||
// This should work for things including w:drawing
|
||||
if (log.isDebugEnabled()) log.debug(o.getClass().getName() + " .. looking for method which returns list " );
|
||||
try {
|
||||
// the first List-returning method of the class, looked up once per class (before
|
||||
// 17.3.4 on every call, which made a walk's cost mostly reflection: CR-052 phase 1)
|
||||
Method listGetter = LIST_GETTERS.get(o.getClass());
|
||||
if (listGetter == null) {
|
||||
listGetter = NO_LIST_GETTER;
|
||||
Method[] methods = o.getClass().getDeclaredMethods();
|
||||
for (int i = 0; i<methods.length; i++) {
|
||||
Method m = methods[i];
|
||||
if (m.getReturnType().getName().equals("java.util.List") && m.getParameterCount() == 0) {
|
||||
listGetter = m;
|
||||
break;
|
||||
}
|
||||
}
|
||||
LIST_GETTERS.put(o.getClass(), listGetter);
|
||||
}
|
||||
if (listGetter != NO_LIST_GETTER) {
|
||||
return (List<Object>)listGetter.invoke(o);
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
}
|
||||
log.debug(".. no list member");
|
||||
return null;
|
||||
}
|
||||
|
||||
private static final java.util.concurrent.ConcurrentHashMap<Class<?>, Method> LIST_GETTERS
|
||||
= new java.util.concurrent.ConcurrentHashMap<Class<?>, Method>();
|
||||
private static final Method NO_LIST_GETTER;
|
||||
static {
|
||||
try {
|
||||
NO_LIST_GETTER = Object.class.getMethod("hashCode");
|
||||
} catch (NoSuchMethodException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static void replaceChildren(Object o, List<Object> newChildren) {
|
||||
|
||||
// Available if you need something like this. Would be used with
|
||||
// something like:
|
||||
|
||||
/*
|
||||
* public void walkJAXBElements(Object parent){
|
||||
// Breadth first
|
||||
|
||||
List<Object> newChildren = new ArrayList<Object>();
|
||||
|
||||
List children = getChildren(parent);
|
||||
if (children==null) {
|
||||
log.warn("no children: " + parent.getClass().getName() );
|
||||
} else {
|
||||
for (Object o : getChildren(parent) ) {
|
||||
|
||||
// if its wrapped in jakarta.xml.bind.JAXBElement, get its value
|
||||
o = XmlUtils.unwrap(o);
|
||||
|
||||
newChildren.addAll(this.apply(o));
|
||||
}
|
||||
}
|
||||
// Replace list, so we'll traverse all the new sdts we've just created
|
||||
replaceChildren(parent, newChildren);
|
||||
|
||||
|
||||
for (Object o : getChildren(parent) ) {
|
||||
|
||||
this.apply(o);
|
||||
|
||||
if ( this.shouldTraverse(o) ) {
|
||||
walkJAXBElements(o);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
//log.debug("Clearing " + o.getClass().getName() );
|
||||
|
||||
if (o instanceof org.docx4j.wml.ContentAccessor) {
|
||||
|
||||
((org.docx4j.wml.ContentAccessor) o).getContent().clear();
|
||||
((org.docx4j.wml.ContentAccessor) o).getContent().addAll(newChildren);
|
||||
|
||||
} else if (o instanceof List /* eg org.docx4j.wml.ArrayListWml */) {
|
||||
// a little unexpected? Would happen if you pass walkJAXBElements a list,
|
||||
// and your walkJAXBElements invokes replaceChildren
|
||||
// That is best avoided, since this was not added until after 6.1.0
|
||||
log.debug("replaceChildren invoked on list object");
|
||||
|
||||
((List) o).clear();
|
||||
((List) o).addAll(newChildren);
|
||||
|
||||
} else if (o instanceof org.docx4j.wml.SdtElement) {
|
||||
|
||||
((org.docx4j.wml.SdtElement) o).getSdtContent().getContent().clear();
|
||||
((org.docx4j.wml.SdtElement) o).getSdtContent().getContent().addAll(newChildren);
|
||||
|
||||
} else if (o instanceof org.docx4j.wml.CTTxbxContent) {
|
||||
|
||||
((org.docx4j.wml.CTTxbxContent) o).getContent().clear();
|
||||
((org.docx4j.wml.CTTxbxContent) o).getContent().addAll(newChildren);
|
||||
|
||||
} else if (o instanceof AlternateContent.Choice) {
|
||||
// a walk in McMode.ALL (OpenDoPE's) edits a branch's content too (CR-051 section 10.5)
|
||||
((AlternateContent.Choice) o).getAny().clear();
|
||||
((AlternateContent.Choice) o).getAny().addAll(newChildren);
|
||||
|
||||
} else if (o instanceof AlternateContent.Fallback) {
|
||||
((AlternateContent.Fallback) o).getAny().clear();
|
||||
((AlternateContent.Fallback) o).getAny().addAll(newChildren);
|
||||
|
||||
} else {
|
||||
|
||||
log.info("Don't know how to replaceChildren in " + o.getClass().getName() );
|
||||
|
||||
if (o instanceof org.w3c.dom.Node) {
|
||||
log.warn(" IGNORED " + ((org.w3c.dom.Node) o).getNodeName());
|
||||
} else {
|
||||
// log.warn(" IGNORED " + o.getClass().getName());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Use this if there is only a single object type (eg just P's)
|
||||
* you are interested in doing something with.
|
||||
*
|
||||
* This method allows you to traverse just the main document
|
||||
* part, or also headers/footers, footnotes/endnotes, and comments
|
||||
* as well.
|
||||
*
|
||||
* @param wmlPackage
|
||||
* @param bodyOnly
|
||||
* @param visitor
|
||||
*/
|
||||
public static void visit(WordprocessingMLPackage wmlPackage,
|
||||
boolean bodyOnly, TraversalUtilVisitor visitor) {
|
||||
|
||||
if (visitor != null) {
|
||||
visit(wmlPackage, bodyOnly, new SingleTraversalUtilVisitorCallback(visitor));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Use this if there is only a single object type (eg just P's)
|
||||
* you are interested in doing something with.
|
||||
*
|
||||
* This method is for traversing an arbitrary WML object (eg a table), as opposed to
|
||||
* eg the main document part, or a header.
|
||||
*
|
||||
* @param parent
|
||||
* @param visitor
|
||||
*/
|
||||
public static void visit(Object parent, TraversalUtilVisitor visitor) {
|
||||
|
||||
if (visitor != null) {
|
||||
visit(parent, new SingleTraversalUtilVisitorCallback(visitor));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Use this if there is more than one object type (eg Tables and Paragraphs)
|
||||
* you are interested in doing something with during the traversal.
|
||||
*
|
||||
* This method allows you to traverse just the main document
|
||||
* part, or also headers/footers, footnotes/endnotes, and comments
|
||||
* as well.
|
||||
*
|
||||
* @param wmlPackage
|
||||
* @param bodyOnly
|
||||
* @param visitorList
|
||||
*/
|
||||
public static void visit(WordprocessingMLPackage wmlPackage,
|
||||
boolean bodyOnly, List<TraversalUtilVisitor> visitorList) {
|
||||
|
||||
CompoundTraversalUtilVisitorCallback callback = null;
|
||||
if ((visitorList != null) && (!visitorList.isEmpty())) {
|
||||
if (visitorList.size() > 1) {
|
||||
visit(wmlPackage, bodyOnly,
|
||||
new CompoundTraversalUtilVisitorCallback(visitorList));
|
||||
} else {
|
||||
visit(wmlPackage, bodyOnly, visitorList.get(0));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Use this if there is more than one object type (eg Tables and Paragraphs)
|
||||
* you are interested in doing something with during the traversal.
|
||||
*
|
||||
* This method is for traversing an arbitrary WML object (eg a table), as opposed to
|
||||
* eg the main document part, or a header.
|
||||
*
|
||||
* @param parent
|
||||
* @param visitorList
|
||||
*/
|
||||
public static void visit(Object parent,
|
||||
List<TraversalUtilVisitor> visitorList) {
|
||||
|
||||
CompoundTraversalUtilVisitorCallback callback = null;
|
||||
if ((visitorList != null) && (!visitorList.isEmpty())) {
|
||||
if (visitorList.size() > 1) {
|
||||
visit(parent, new CompoundTraversalUtilVisitorCallback(
|
||||
visitorList));
|
||||
} else {
|
||||
visit(parent, visitorList.get(0));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void visit(Object parent, Callback callback) {
|
||||
|
||||
if ((parent != null) && (callback != null)) {
|
||||
callback.walkJAXBElements(parent);
|
||||
}
|
||||
}
|
||||
|
||||
/** As {@link #visit(Object, Callback)}, in this {@link McMode} (see
|
||||
* {@link #TraversalUtil(Object, Callback, McMode)}). @since 17.2.0 */
|
||||
public static void visit(Object parent, Callback callback, McMode mcMode) {
|
||||
if (callback instanceof CallbackImpl) ((CallbackImpl) callback).setMcMode(mcMode);
|
||||
visit(parent, callback);
|
||||
}
|
||||
|
||||
/** As {@link #visit(Object, TraversalUtilVisitor)}, in this {@link McMode}. @since 17.2.0 */
|
||||
public static void visit(Object parent, TraversalUtilVisitor visitor, McMode mcMode) {
|
||||
if (visitor != null) {
|
||||
visit(parent, new SingleTraversalUtilVisitorCallback(visitor), mcMode);
|
||||
}
|
||||
}
|
||||
|
||||
/** As {@link #visit(WordprocessingMLPackage, boolean, Callback)}, in this {@link McMode}. @since 17.2.0 */
|
||||
public static void visit(WordprocessingMLPackage wmlPackage, boolean bodyOnly, Callback callback, McMode mcMode) {
|
||||
if (callback instanceof CallbackImpl) ((CallbackImpl) callback).setMcMode(mcMode);
|
||||
visit(wmlPackage, bodyOnly, callback);
|
||||
}
|
||||
|
||||
/** As {@link #visit(WordprocessingMLPackage, boolean, TraversalUtilVisitor)}, in this {@link McMode}. @since 17.2.0 */
|
||||
public static void visit(WordprocessingMLPackage wmlPackage, boolean bodyOnly, TraversalUtilVisitor visitor, McMode mcMode) {
|
||||
if (visitor != null) {
|
||||
visit(wmlPackage, bodyOnly, new SingleTraversalUtilVisitorCallback(visitor), mcMode);
|
||||
}
|
||||
}
|
||||
|
||||
/** As {@link #visit(WordprocessingMLPackage, boolean, List)}, in this {@link McMode}. @since 17.2.0 */
|
||||
public static void visit(WordprocessingMLPackage wmlPackage, boolean bodyOnly, List<TraversalUtilVisitor> visitorList, McMode mcMode) {
|
||||
if ((visitorList != null) && (!visitorList.isEmpty())) {
|
||||
if (visitorList.size() > 1) {
|
||||
visit(wmlPackage, bodyOnly, new CompoundTraversalUtilVisitorCallback(visitorList), mcMode);
|
||||
} else {
|
||||
visit(wmlPackage, bodyOnly, visitorList.get(0), mcMode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void visit(WordprocessingMLPackage wmlPackage,
|
||||
boolean bodyOnly, Callback callback) {
|
||||
|
||||
MainDocumentPart mainDocument = null;
|
||||
RelationshipsPart relPart = null;
|
||||
List<Relationship> relList = null;
|
||||
List<Object> elementList = null;
|
||||
|
||||
if ((wmlPackage != null) && (callback != null)) {
|
||||
mainDocument = wmlPackage.getMainDocumentPart();
|
||||
callback.walkJAXBElements(mainDocument.getJaxbElement().getBody());
|
||||
if (!bodyOnly) {
|
||||
relPart = mainDocument.getRelationshipsPart();
|
||||
relList = relPart.getRelationships().getRelationship();
|
||||
for (Relationship rs : relList) {
|
||||
elementList = null;
|
||||
if (Namespaces.HEADER.equals(rs.getType())) {
|
||||
elementList = ((HeaderPart) relPart.getPart(rs))
|
||||
.getJaxbElement().getContent();
|
||||
} else if (Namespaces.FOOTER.equals(rs.getType())) {
|
||||
elementList = ((FooterPart) relPart.getPart(rs))
|
||||
.getJaxbElement().getContent();
|
||||
} else if (Namespaces.ENDNOTES.equals(rs.getType())) {
|
||||
//elementList = ((EndnotesPart) relPart.getPart(rs)).getContent();
|
||||
elementList = new ArrayList();
|
||||
elementList.addAll(
|
||||
((EndnotesPart) relPart.getPart(rs)).getJaxbElement().getEndnote() );
|
||||
} else if (Namespaces.FOOTNOTES.equals(rs.getType())) {
|
||||
//elementList = ((FootnotesPart) relPart.getPart(rs)).getContent();
|
||||
elementList = new ArrayList();
|
||||
elementList.addAll(
|
||||
((FootnotesPart) relPart.getPart(rs)).getJaxbElement().getFootnote() );
|
||||
} else if (Namespaces.COMMENTS.equals(rs.getType())) {
|
||||
elementList = new ArrayList();
|
||||
for (Comment comment : ((CommentsPart) relPart
|
||||
.getPart(rs)).getJaxbElement().getComment()) {
|
||||
elementList.addAll(comment.getContent());
|
||||
}
|
||||
}
|
||||
if ((elementList != null) && (!elementList.isEmpty())) {
|
||||
log.debug("Processing target: "
|
||||
+ rs.getTarget() + ", type: " + rs.getType());
|
||||
callback.walkJAXBElements(elementList);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// private void describeDrawing( org.docx4j.wml.Drawing d) {
|
||||
//
|
||||
// log.info("In wml.Drawing" );
|
||||
//
|
||||
// if ( d.getAnchorOrInline().get(0) instanceof Anchor ) {
|
||||
//
|
||||
// log.info(" ENCOUNTERED w:drawing/wp:anchor " );
|
||||
// // That's all for now...
|
||||
//
|
||||
// } else if ( d.getAnchorOrInline().get(0) instanceof Inline ) {
|
||||
//
|
||||
// // Extract
|
||||
// w:drawing/wp:inline/a:graphic/a:graphicData/pic:pic/pic:blipFill/a:blip/@r:embed
|
||||
//
|
||||
// Inline inline = (Inline )d.getAnchorOrInline().get(0);
|
||||
//
|
||||
// Pic pic = inline.getGraphic().getGraphicData().getPic();
|
||||
//
|
||||
// if (pic!=null) {
|
||||
// log.info("image relationship: " + pic.getBlipFill().getBlip().getEmbed()
|
||||
// );
|
||||
// }
|
||||
//
|
||||
//
|
||||
// } else {
|
||||
//
|
||||
// log.info(" Didn't get Inline :( How to handle " +
|
||||
// d.getAnchorOrInline().get(0).getClass().getName() );
|
||||
// }
|
||||
//
|
||||
// }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,246 @@
|
||||
/*
|
||||
* Copyright 2009, Plutext Pty Ltd.
|
||||
*
|
||||
* This file is part of docx4j.
|
||||
|
||||
docx4j is licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
||||
*/
|
||||
|
||||
package org.docx4j;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
import java.text.DecimalFormatSymbols;
|
||||
import java.util.Locale;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* @author jharrop, azerolo
|
||||
*
|
||||
*/
|
||||
public class UnitsOfMeasurement {
|
||||
|
||||
private final static Logger log = LoggerFactory.getLogger(UnitsOfMeasurement.class);
|
||||
|
||||
public final static DecimalFormat format2DP;
|
||||
public final static int DPI;
|
||||
static {
|
||||
format2DP = new DecimalFormat("##.##",
|
||||
new DecimalFormatSymbols(Locale.ENGLISH));
|
||||
|
||||
|
||||
DPI = Integer.parseInt(Docx4jProperties.getProperty("docx4j.DPI", "96"));
|
||||
|
||||
}
|
||||
|
||||
public static long twipToEMU(double twips) {
|
||||
return Math.round(635 * twips);
|
||||
}
|
||||
|
||||
public static int inchToTwip(float inch ) {
|
||||
// 1440 twip = 1 inch;
|
||||
return Math.round(inch*1440);
|
||||
}
|
||||
|
||||
public static float twipToInch(int twip) {
|
||||
return twip/1440.00f;
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public static float twipToMm(int twip) {
|
||||
return twip / 56.6928f;
|
||||
}
|
||||
|
||||
public static int mmToTwip(float mm ) {
|
||||
float inch = mm*0.0394f;
|
||||
return inchToTwip(inch);
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public static float twipToPoint(int twip) {
|
||||
return twip / 20f;
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public static int pointToTwip(float point ) {
|
||||
return Math.round(20 * point);
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public static int pxToTwip(float px) {
|
||||
|
||||
float inch = px/DPI;
|
||||
return inchToTwip(inch);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert an inches value to twips and return as a double.
|
||||
*/
|
||||
public static double inchToTwipDouble(double inch) {
|
||||
// 1440 twip = 1 inch;
|
||||
return inch * 1440;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a pixel value to twips and return as a double. This may be useful
|
||||
* where the returned value will then be converted to another unit, and we
|
||||
* don't want to lose precision due to rounding.
|
||||
*/
|
||||
public static double pxToTwipDouble(double px) {
|
||||
double inch = px / DPI;
|
||||
return inchToTwipDouble(inch);
|
||||
}
|
||||
|
||||
/**
|
||||
* 1440 twip = 1 inch;Try to guess whether inches or mm looks nicer
|
||||
* @param left
|
||||
* @return
|
||||
*/
|
||||
/**
|
||||
* A twips value as a CSS/XSL-FO length, losslessly: whole inches as "Nin",
|
||||
* otherwise points ("12pt", "35.4pt"; a twip is 0.05pt, so two decimals are exact).
|
||||
*
|
||||
* Before 17.0.5 this rounded to whole millimetres whenever the value was not a
|
||||
* multiple of 1/80 inch, which lost up to 0.5mm (1.4pt) per value: a 708-twip
|
||||
* header distance became "12mm" (12.49), 12pt paragraph spacing became "4mm"
|
||||
* (11.34pt). Measured against Word (CR-001 harness), those losses were a
|
||||
* visible part of the page layout error.
|
||||
*/
|
||||
public static String twipToBest(int leftL ) {
|
||||
if (leftL % 1440 == 0) {
|
||||
return (leftL / 1440) + "in";
|
||||
}
|
||||
if (leftL % 20 == 0) {
|
||||
return (leftL / 20) + "pt";
|
||||
}
|
||||
String s = String.format(java.util.Locale.ROOT, "%.2f", leftL / 20f);
|
||||
s = s.replaceAll("0+$", "").replaceAll("\\.$", "");
|
||||
return s + "pt";
|
||||
}
|
||||
|
||||
public static String rgbTripleToHex(float red, float green, float blue) {
|
||||
return getHex(red) + getHex(green) + getHex(blue);
|
||||
}
|
||||
|
||||
private static String getHex(float f) {
|
||||
|
||||
int i = Math.round(f);
|
||||
String hexValue = Integer.toHexString(i);
|
||||
|
||||
if (i < 16) {
|
||||
// Pad so we have 2 digits
|
||||
return "0" + hexValue;
|
||||
}
|
||||
return hexValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public static String toHexColor(int color) {
|
||||
String ret = Integer.toHexString(color).toUpperCase();
|
||||
return (ret.length() < 6 ?
|
||||
"000000".substring(0, 6 - ret.length()) + ret :
|
||||
ret);
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public static int combineColors(int fgColor, int bgColor, int pctFg) {
|
||||
int resColor = 0;
|
||||
if (pctFg < 1) {
|
||||
resColor = bgColor;
|
||||
}
|
||||
else if (pctFg == 100) {
|
||||
resColor = fgColor;
|
||||
}
|
||||
else {
|
||||
int pctBg = 100 - pctFg;
|
||||
resColor =
|
||||
//Red
|
||||
(((((((fgColor >> 16) & 0xff) * pctFg) +
|
||||
(((bgColor >> 16) & 0xff) * pctBg))) / 100) << 16) |
|
||||
//Green
|
||||
(((((((fgColor >> 8) & 0xff) * pctFg) +
|
||||
(((bgColor >> 8) & 0xff) * pctBg))) / 100) << 8) |
|
||||
//Blue
|
||||
(((((fgColor & 0xff) * pctFg) +
|
||||
((bgColor & 0xff) * pctBg))) / 100);
|
||||
}
|
||||
|
||||
return resColor;
|
||||
}
|
||||
|
||||
private String calcHexColor(int value) {
|
||||
String ret = Integer.toHexString(value).toUpperCase();
|
||||
return (ret.length() < 6 ?
|
||||
"000000".substring(0, 6 - ret.length()) + ret :
|
||||
ret);
|
||||
}
|
||||
|
||||
// public static void main(String[] args) throws Exception {
|
||||
// System.out.println(format2DP.format(twipToInch(2235)));
|
||||
// System.out.println(twipToBest(2235) );
|
||||
// }
|
||||
|
||||
/** Word's 1/300 inch grid, in points: 0.24pt a cell, the unit its layout works in. */
|
||||
private static final double BORDER_GRID_PT = 72.0 / 300.0;
|
||||
|
||||
/**
|
||||
* The width Word inks a {@code w:sz} border: the eighths of a point it names, laid on
|
||||
* Word's 1/300-inch grid and <b>truncated</b> to whole cells.
|
||||
*
|
||||
* <p>Measured on four Word goldens, five distinct widths, every one the floor:</p>
|
||||
*
|
||||
* <pre>
|
||||
* w:sz named cells Word inks golden
|
||||
* 4 0.50pt 2.083 0.480pt table-cell-measure, table-first-row-border
|
||||
* 8 1.00pt 4.167 0.960pt page-top-space-before, table-first-row-border
|
||||
* 12 1.50pt 6.250 1.440pt table-cell-measure
|
||||
* 18 2.25pt 9.375 2.160pt border-hanging
|
||||
* 24 3.00pt 12.500 2.880pt table-cell-measure
|
||||
* </pre>
|
||||
*
|
||||
* <p>{@code w:sz} 24 is the one that settles the rounding: 3.00pt is <b>exactly</b> 12.5
|
||||
* cells and Word inks 12, not 13, so it truncates rather than rounding half up. (Half to
|
||||
* even would agree on all five; truncation is the simpler statement of the same readings.)</p>
|
||||
*
|
||||
* <p>Used by the XSL FO paths only. The paragraph border wrote millimetres to two
|
||||
* decimals until 17.2.0 - {@code w:sz} 18 came out as 0.79mm = 2.2394pt against Word's
|
||||
* 2.160 - and the table border wrote the named points, 0.50 where Word inks 0.480; both
|
||||
* were part of the residue measured on every bar of the {@code border-hanging} golden
|
||||
* (CR-001 batch 48 item 9 §2, batch 49 item 5). The CSS paths still write millimetres:
|
||||
* a browser has no 1/300-inch grid to honour.</p>
|
||||
*
|
||||
* @param eighths the {@code w:sz}, in eighths of a point
|
||||
* @since 17.2.0 (CR-001 batch 49 item 5)
|
||||
*/
|
||||
public static double eighthsToGridPt(int eighths) {
|
||||
if (eighths <= 0) return 0;
|
||||
return Math.floor((eighths / 8.0) / BORDER_GRID_PT) * BORDER_GRID_PT;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package org.docx4j;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import org.docx4j.jaxb.Context;
|
||||
import org.docx4j.utils.ResourceUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class Version {
|
||||
|
||||
protected static Logger log = LoggerFactory.getLogger(Version.class);
|
||||
|
||||
private static Properties properties;
|
||||
|
||||
private static void init() {
|
||||
|
||||
properties = new Properties();
|
||||
try {
|
||||
properties.load(
|
||||
ResourceUtils.getResource("docx4j_version.properties"));
|
||||
} catch (Exception e) {
|
||||
log.warn("Couldn't find/read docx4j_version.properties; " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private static String version;
|
||||
|
||||
public static String getDocx4jVersion() {
|
||||
|
||||
if (version==null) {
|
||||
|
||||
if (properties==null) {init();}
|
||||
|
||||
version = properties.getProperty("version");
|
||||
}
|
||||
|
||||
return version;
|
||||
}
|
||||
|
||||
private static String poweredBy;
|
||||
|
||||
public static String getPoweredBy(boolean isNewPkg) {
|
||||
if (poweredBy==null) {
|
||||
String vendor = System.getProperty("java.vendor");
|
||||
if (vendor !=null
|
||||
&& vendor.equals("Oracle Corporation") ) {
|
||||
vendor = "Oracle";
|
||||
}
|
||||
String verb = isNewPkg ? "Created" : "Modified";
|
||||
poweredBy = verb + " by docx4j " + Version.getDocx4jVersion()
|
||||
+ " (Apache licensed) using " + Context.getJaxbImplementation() + " JAXB"
|
||||
+ " in " + vendor + " Java " + System.getProperty("java.version")
|
||||
+ " on " + System.getProperty("os.name") ;
|
||||
}
|
||||
return poweredBy;
|
||||
}
|
||||
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,461 @@
|
||||
package org.docx4j.anon;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import jakarta.xml.bind.JAXBElement;
|
||||
|
||||
import org.docx4j.TraversalUtil;
|
||||
import org.docx4j.anon.AnonymizeResult.Action;
|
||||
import org.docx4j.anon.JaxbGraphWalker.Visitor;
|
||||
import org.docx4j.anon.PartsAnalyzer.Treatment;
|
||||
import org.docx4j.openpackaging.exceptions.Docx4JException;
|
||||
import org.docx4j.openpackaging.exceptions.InvalidFormatException;
|
||||
import org.docx4j.openpackaging.packages.OpcPackage;
|
||||
import org.docx4j.openpackaging.packages.PresentationMLPackage;
|
||||
import org.docx4j.openpackaging.packages.SpreadsheetMLPackage;
|
||||
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
|
||||
import org.docx4j.openpackaging.parts.JaxbXmlPart;
|
||||
import org.docx4j.openpackaging.parts.Part;
|
||||
import org.docx4j.openpackaging.parts.PartName;
|
||||
import org.docx4j.openpackaging.parts.XmlPart;
|
||||
import org.docx4j.openpackaging.parts.WordprocessingML.FooterPart;
|
||||
import org.docx4j.openpackaging.parts.WordprocessingML.HeaderPart;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* Anonymises a docx (or, since 17.2.1, a pptx or xlsx) in place: the text is scrambled,
|
||||
* identities, references and metadata scrubbed, media replaced, and whatever
|
||||
* cannot be made clean removed (STRICT, the default) or kept and reported (KEEP).
|
||||
* <p>
|
||||
* The guarantee (CR-019): a document this returns with {@code result.isClean()}
|
||||
* contains no text from the original in any text-bearing part (scrambled per
|
||||
* Unicode range); no author, initials, user id or presence information; no
|
||||
* external target or field-instruction argument; no metadata beyond what the
|
||||
* package needs to open; no media but placeholder pixels; no embedded object,
|
||||
* chart data, diagram text, macro, custom XML or altChunk. What it keeps, by
|
||||
* design, is the document's structure - paragraphs, runs, tables, sections,
|
||||
* styles, numbering, fonts, sizes, the shape of fields and content controls -
|
||||
* because that is what a layout, a corruption or a converter bug lives in.
|
||||
* <p>
|
||||
* One thing the structure does not keep is the conformance class: docx4j reads a
|
||||
* strict (ISO/IEC 29500 Strict) package by converting each part to the
|
||||
* transitional namespaces, and never writes strict, so an anonymised strict
|
||||
* document is a transitional one. Nothing else about it changes, but a bug which
|
||||
* is the strict dialect's own will not reproduce from the anonymised copy - ask
|
||||
* for the original in that case.
|
||||
* <p>
|
||||
* Usage:
|
||||
* <pre>
|
||||
* WordprocessingMLPackage pkg = WordprocessingMLPackage.load(in, McLoadMode.PRESERVE);
|
||||
* AnonymizeResult result = new Anonymize(pkg).go();
|
||||
* if (result.isClean()) pkg.save(out);
|
||||
* System.out.println(result.toJson());
|
||||
* </pre>
|
||||
* Set a font mapper on the package first if the non-Latin replacement characters
|
||||
* should be chosen from the glyphs the document's fonts have (the CLI does).
|
||||
* <p>
|
||||
* For a pptx (CR-019 phase 2) the same walk covers the slides, layouts, masters,
|
||||
* notes and handout master, the presentation (section names, custom shows, the
|
||||
* modify verifier, the embedded-font list), legacy and 2018 comments with their
|
||||
* authors, tags, table styles, charts and diagrams; media, OLE objects, ActiveX,
|
||||
* ink, embedded fonts, VBA and the thumbnail go as in a docx. A pptx has no
|
||||
* styles part and no font mapper, so non-Latin replacement characters stay in the
|
||||
* character's Unicode block with no glyph check.
|
||||
* <p>
|
||||
* Load in {@link org.docx4j.jaxb.McLoadMode#PRESERVE} mode (as the CLI does): the output is a
|
||||
* reproduction of the document, so a block-level {@code mc:AlternateContent} must come through
|
||||
* whole rather than resolved to the branch Word reads (CR-051 section 3.2).
|
||||
* <p>
|
||||
* For an xlsx (CR-019 phase 3) a third visitor, {@link SpreadsheetScrubber},
|
||||
* covers the workbook, sheets, shared strings, styles, tables, comments (legacy
|
||||
* and threaded), connections, query tables and external links: strings are
|
||||
* scrambled consistently (a table column still matches its header), numbers have
|
||||
* their digits randomised ({@link #setKeepNumbers(boolean)} keeps them, decision
|
||||
* 3), formulas are rewritten to compute over the result ({@link SmlFormulas}),
|
||||
* sheets become {@code Sheet<n>} and defined names {@code n_<hash>}; pivot tables
|
||||
* and caches, slicers, timelines and the data model are removed (their cells stay
|
||||
* as values) whatever the mode; OLE, ActiveX, printer settings and the rich-value
|
||||
* parts go in STRICT.
|
||||
*/
|
||||
public class Anonymize {
|
||||
|
||||
private static Logger log = LoggerFactory.getLogger(Anonymize.class);
|
||||
|
||||
/** What to do with a part the tool cannot make clean. */
|
||||
public enum Mode {
|
||||
/** remove it (with its relationships and the markup that pointed at it); the default */
|
||||
STRICT,
|
||||
/** keep it, report it, and never call the result clean */
|
||||
KEEP
|
||||
}
|
||||
|
||||
private final OpcPackage pkg;
|
||||
private final Mode mode;
|
||||
private boolean verify = true;
|
||||
private boolean keepNumbers = false;
|
||||
|
||||
ScrambleText latinizer = null;
|
||||
MarkupScrubber markupScrubber = null;
|
||||
SpreadsheetScrubber spreadsheetScrubber = null;
|
||||
DmlVmlAnalyzer dmlVmlAnalyzer = null;
|
||||
Names names = null;
|
||||
|
||||
AnonymizeResult result;
|
||||
|
||||
public Anonymize(WordprocessingMLPackage wordMLPackage) {
|
||||
this((OpcPackage) wordMLPackage, Mode.STRICT);
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 17.2.0
|
||||
*/
|
||||
public Anonymize(WordprocessingMLPackage wordMLPackage, Mode mode) {
|
||||
this((OpcPackage) wordMLPackage, mode);
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 17.2.1
|
||||
*/
|
||||
public Anonymize(PresentationMLPackage pmlPackage, Mode mode) {
|
||||
this((OpcPackage) pmlPackage, mode);
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 17.2.1
|
||||
*/
|
||||
public Anonymize(SpreadsheetMLPackage smlPackage, Mode mode) {
|
||||
this((OpcPackage) smlPackage, mode);
|
||||
}
|
||||
|
||||
/**
|
||||
* A docx, pptx or xlsx (as {@link OpcPackage#load(java.io.File)} returns it).
|
||||
*
|
||||
* @throws IllegalArgumentException for a package of another kind
|
||||
* @since 17.2.1
|
||||
*/
|
||||
public Anonymize(OpcPackage pkg, Mode mode) {
|
||||
if (!(pkg instanceof WordprocessingMLPackage) && !(pkg instanceof PresentationMLPackage)
|
||||
&& !(pkg instanceof SpreadsheetMLPackage)) {
|
||||
throw new IllegalArgumentException("only docx, pptx and xlsx are supported: "
|
||||
+ (pkg == null ? "null" : pkg.getClass().getSimpleName()));
|
||||
}
|
||||
this.pkg = pkg;
|
||||
this.mode = mode;
|
||||
result = new AnonymizeResult();
|
||||
}
|
||||
|
||||
/**
|
||||
* STRICT.
|
||||
*
|
||||
* @since 17.2.1
|
||||
*/
|
||||
public Anonymize(OpcPackage pkg) {
|
||||
this(pkg, Mode.STRICT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether {@link #go()} extracts the tokens of the document before and after
|
||||
* and checks that none survived ({@link Verify}). On by default; it costs a
|
||||
* marshal of every part twice.
|
||||
*
|
||||
* @since 17.2.0
|
||||
*/
|
||||
public void setVerify(boolean verify) {
|
||||
this.verify = verify;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a workbook's numbers (cell values, formula constants, filter and
|
||||
* input-cell values) keep their digits. Off by default: numbers are usually
|
||||
* the confidential part of a spreadsheet, and a layout or corruption bug rarely
|
||||
* depends on their values (CR-019 decision 3); on for the bug that does (a
|
||||
* number format, a formula result). No effect on a docx or pptx.
|
||||
*
|
||||
* @since 17.2.1
|
||||
*/
|
||||
public void setKeepNumbers(boolean keepNumbers) {
|
||||
this.keepNumbers = keepNumbers;
|
||||
}
|
||||
|
||||
public AnonymizeResult go() throws Docx4JException {
|
||||
|
||||
result.mode = mode;
|
||||
|
||||
if (pkg.isWasStrict()) {
|
||||
// docx4j reads a strict package by converting each part to the transitional
|
||||
// namespaces, and never writes strict; the report says so, because a bug which is
|
||||
// the strict dialect's own will not reproduce from the anonymised copy
|
||||
result.notes.add("the input was ISO/IEC 29500 Strict; docx4j converts on read and writes transitional, "
|
||||
+ "so this output is a transitional package");
|
||||
}
|
||||
|
||||
Verify.Extraction before = verify ? Verify.extract(pkg) : null;
|
||||
|
||||
names = new Names(pkg);
|
||||
|
||||
// the visitors first: the text scrambler's font selection reads the font table and
|
||||
// its embedded fonts, which STRICT is about to remove
|
||||
latinizer = new ScrambleText(pkg, names);
|
||||
markupScrubber = new MarkupScrubber(names, mode == Mode.STRICT, result.notes::add);
|
||||
if (pkg instanceof SpreadsheetMLPackage) {
|
||||
latinizer.setKeepNumbers(keepNumbers);
|
||||
spreadsheetScrubber = new SpreadsheetScrubber(latinizer, names, mode == Mode.STRICT, result.notes::add,
|
||||
markupScrubber.objectPreviews);
|
||||
// a chart's c:f in a workbook keeps pointing at its (scrambled) cells
|
||||
latinizer.setFormulas(spreadsheetScrubber.formulas()::scrub);
|
||||
}
|
||||
|
||||
// the inventory (fields present, VML, objects of interest), read before the scramble;
|
||||
// it walks the WordprocessingML story parts, so a pptx has none
|
||||
if (pkg instanceof WordprocessingMLPackage) {
|
||||
detectDmlVmlContent();
|
||||
}
|
||||
|
||||
// what each part is
|
||||
Map<Part, Treatment> treatments = new LinkedHashMap<Part, Treatment>();
|
||||
for (Entry<PartName, Part> entry : pkg.getParts().getParts().entrySet()) {
|
||||
treatments.put(entry.getValue(), PartsAnalyzer.classify(entry.getValue()));
|
||||
}
|
||||
|
||||
// remove what goes: always-removed parts, and in STRICT mode what cannot be made clean
|
||||
removeParts(treatments);
|
||||
|
||||
// metadata
|
||||
MetadataScrubber metadata = new MetadataScrubber(pkg);
|
||||
metadata.scrubDocProps();
|
||||
for (Entry<Part, Treatment> e : treatments.entrySet()) {
|
||||
if (e.getValue() == Treatment.METADATA && pkg.getParts().get(e.getKey().getPartName()) != null) {
|
||||
result.record(e.getKey(), Action.CLEARED, "descriptive properties, identities and dates cleared");
|
||||
}
|
||||
}
|
||||
|
||||
// text, identities, references, embedded-object markup: one walk over every JAXB part
|
||||
walkParts(treatments);
|
||||
|
||||
// relationships to outside the package
|
||||
result.externalTargetsReplaced = metadata.scrubExternalTargets();
|
||||
|
||||
// media
|
||||
replaceMedia(treatments);
|
||||
|
||||
result.authorsRenamed = names.authorCount();
|
||||
result.hasGreek = latinizer.hasGreek;
|
||||
result.hasCyrillic = latinizer.hasCyrillic;
|
||||
result.hasHebrew = latinizer.hasHebrew;
|
||||
result.hasArabic = latinizer.hasArabic;
|
||||
result.hasHiragana = latinizer.hasHiragana;
|
||||
result.hasKatakana = latinizer.hasKatakana;
|
||||
result.hasCJK = latinizer.hasCJK;
|
||||
|
||||
if (before != null) {
|
||||
Verify.Extraction after = Verify.extract(pkg);
|
||||
result.leaks.addAll(Verify.compare(before, after));
|
||||
result.verified = result.leaks.isEmpty();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private void removeParts(Map<Part, Treatment> treatments) {
|
||||
|
||||
for (Entry<Part, Treatment> e : treatments.entrySet()) {
|
||||
Part p = e.getKey();
|
||||
if (pkg.getParts().get(p.getPartName()) == null) continue; // went with an earlier removal
|
||||
|
||||
if (e.getValue() == Treatment.REMOVE) {
|
||||
List<PartName> removed = MediaReplacer.removePart(pkg, p);
|
||||
result.record(p, Action.REMOVED, "always removed: " + p.getClass().getSimpleName());
|
||||
recordCascade(removed, p);
|
||||
|
||||
} else if (e.getValue() == Treatment.UNSCRUBBABLE) {
|
||||
result.unsafeParts.add(p);
|
||||
if (mode == Mode.STRICT) {
|
||||
List<PartName> removed = MediaReplacer.removePart(pkg, p);
|
||||
result.record(p, Action.REMOVED, "cannot be made clean: " + p.getClass().getSimpleName());
|
||||
recordCascade(removed, p);
|
||||
} else {
|
||||
result.record(p, Action.KEPT_UNSAFE, "cannot be made clean: " + p.getClass().getSimpleName());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void recordCascade(List<PartName> removed, Part p) {
|
||||
for (PartName name : removed) {
|
||||
if (!name.equals(p.getPartName())) {
|
||||
result.record(name.getName(), "", Action.REMOVED, "target of " + p.getPartName().getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void walkParts(Map<Part, Treatment> treatments) throws Docx4JException {
|
||||
|
||||
final Visitor composite = new Visitor() {
|
||||
@Override
|
||||
public JaxbGraphWalker.Action visit(Object o, JAXBElement<?> wrapper) {
|
||||
JaxbGraphWalker.Action a = latinizer.visit(o, wrapper);
|
||||
JaxbGraphWalker.Action b = markupScrubber.visit(o, wrapper);
|
||||
JaxbGraphWalker.Action c = spreadsheetScrubber == null ? JaxbGraphWalker.Action.CONTINUE : spreadsheetScrubber.visit(o, wrapper);
|
||||
if (a == JaxbGraphWalker.Action.REMOVE || b == JaxbGraphWalker.Action.REMOVE || c == JaxbGraphWalker.Action.REMOVE) return JaxbGraphWalker.Action.REMOVE;
|
||||
if (a == JaxbGraphWalker.Action.REPLACE || b == JaxbGraphWalker.Action.REPLACE) return JaxbGraphWalker.Action.REPLACE;
|
||||
if (a == JaxbGraphWalker.Action.SKIP_CHILDREN || b == JaxbGraphWalker.Action.SKIP_CHILDREN || c == JaxbGraphWalker.Action.SKIP_CHILDREN) return JaxbGraphWalker.Action.SKIP_CHILDREN;
|
||||
return JaxbGraphWalker.Action.CONTINUE;
|
||||
}
|
||||
@Override
|
||||
public Object replacement() {
|
||||
return markupScrubber.replacement();
|
||||
}
|
||||
};
|
||||
|
||||
for (Entry<Part, Treatment> e : treatments.entrySet()) {
|
||||
Part p = e.getKey();
|
||||
Treatment t = e.getValue();
|
||||
if (pkg.getParts().get(p.getPartName()) == null) continue; // removed
|
||||
if (t == Treatment.METADATA || t == Treatment.REPLACE_IMAGE) continue;
|
||||
if (!(p instanceof JaxbXmlPart)) {
|
||||
if (t == Treatment.SCRUB && p instanceof XmlPart) {
|
||||
// a DOM part the table names: the 2018 PowerPoint comments and authors
|
||||
scrubDom((XmlPart) p);
|
||||
} else if (t == Treatment.SAFE) {
|
||||
result.record(p, Action.KEPT, "structure");
|
||||
}
|
||||
continue;
|
||||
}
|
||||
JaxbXmlPart<?> jp = (JaxbXmlPart<?>) p;
|
||||
Object contents;
|
||||
try {
|
||||
contents = jp.getContents();
|
||||
} catch (Exception ex) {
|
||||
log.warn(p.getPartName().getName() + " could not be read: " + ex);
|
||||
contents = null;
|
||||
}
|
||||
if (contents == null) {
|
||||
result.unsafeParts.add(p);
|
||||
if (mode == Mode.STRICT) {
|
||||
MediaReplacer.removePart(pkg, p);
|
||||
result.record(p, Action.REMOVED, "could not be read, so could not be scrubbed");
|
||||
} else {
|
||||
result.record(p, Action.KEPT_UNSAFE, "could not be read, so could not be scrubbed");
|
||||
}
|
||||
continue;
|
||||
}
|
||||
log.debug("Scrubbing " + p.getPartName().getName());
|
||||
latinizer.latinText = null;
|
||||
markupScrubber.setCurrentPart(p);
|
||||
if (spreadsheetScrubber != null) spreadsheetScrubber.setCurrentPart(p);
|
||||
new JaxbGraphWalker(composite).walk(contents);
|
||||
if (contents instanceof org.xlsx4j.sml.CTConnections && ((org.xlsx4j.sml.CTConnections) contents).getConnection().isEmpty()) {
|
||||
// every connection was a data-model or Power Query one: an empty part is no use
|
||||
MediaReplacer.removePart(pkg, p);
|
||||
result.record(p, Action.REMOVED, "no connection left after the data-model and Power Query ones");
|
||||
continue;
|
||||
}
|
||||
if (t == Treatment.SCRUB) {
|
||||
result.record(p, Action.SCRUBBED, null);
|
||||
} else if (t == Treatment.SAFE) {
|
||||
result.record(p, Action.KEPT, "structure (walked)");
|
||||
}
|
||||
// an UNSCRUBBABLE JAXB part kept in KEEP mode was recorded as KEPT_UNSAFE already;
|
||||
// the walk scrubbed what it recognised in it, but it is not vouched for
|
||||
}
|
||||
}
|
||||
|
||||
private void scrubDom(XmlPart p) {
|
||||
org.w3c.dom.Document doc;
|
||||
try {
|
||||
doc = p.getDocument();
|
||||
} catch (Exception ex) {
|
||||
log.warn(p.getPartName().getName() + " could not be read: " + ex);
|
||||
doc = null;
|
||||
}
|
||||
if (doc == null) {
|
||||
result.unsafeParts.add(p);
|
||||
if (mode == Mode.STRICT) {
|
||||
MediaReplacer.removePart(pkg, p);
|
||||
result.record(p, Action.REMOVED, "could not be read, so could not be scrubbed");
|
||||
} else {
|
||||
result.record(p, Action.KEPT_UNSAFE, "could not be read, so could not be scrubbed");
|
||||
}
|
||||
return;
|
||||
}
|
||||
log.debug("Scrubbing (DOM) " + p.getPartName().getName());
|
||||
new ModernCommentsScrubber(names, latinizer).scrub(doc.getDocumentElement());
|
||||
result.record(p, Action.SCRUBBED, "2018 comments: authors and persons renamed, dates fixed, text scrambled");
|
||||
}
|
||||
|
||||
private void replaceMedia(Map<Part, Treatment> treatments) throws Docx4JException {
|
||||
MediaReplacer media = new MediaReplacer(pkg, markupScrubber.objectPreviews);
|
||||
for (Entry<Part, Treatment> e : treatments.entrySet()) {
|
||||
Part p = e.getKey();
|
||||
if (e.getValue() != Treatment.REPLACE_IMAGE) continue;
|
||||
if (pkg.getParts().get(p.getPartName()) == null) continue; // went with a removed part
|
||||
result.record(p, Action.REPLACED, media.replace(p));
|
||||
}
|
||||
}
|
||||
|
||||
// ---- the inventory walk (pre-17.2.0), kept for its report
|
||||
|
||||
private void detectDmlVmlContent() throws InvalidFormatException {
|
||||
|
||||
dmlVmlAnalyzer = new DmlVmlAnalyzer();
|
||||
|
||||
WordprocessingMLPackage pkg = (WordprocessingMLPackage) this.pkg;
|
||||
detectDmlVml(pkg.getMainDocumentPart());
|
||||
|
||||
for (Entry<PartName, Part> entry : new ArrayList<Entry<PartName, Part>>(pkg.getParts().getParts().entrySet())) {
|
||||
Part p = entry.getValue();
|
||||
if (p instanceof HeaderPart) {
|
||||
detectDmlVml((HeaderPart) p);
|
||||
}
|
||||
if (p instanceof FooterPart) {
|
||||
detectDmlVml((FooterPart) p);
|
||||
}
|
||||
}
|
||||
|
||||
if (pkg.getMainDocumentPart().getFootnotesPart() != null) {
|
||||
detectDmlVml(pkg.getMainDocumentPart().getFootnotesPart());
|
||||
}
|
||||
if (pkg.getMainDocumentPart().getEndNotesPart() != null) {
|
||||
detectDmlVml(pkg.getMainDocumentPart().getEndNotesPart());
|
||||
}
|
||||
if (pkg.getMainDocumentPart().getCommentsPart() != null) {
|
||||
detectDmlVml(pkg.getMainDocumentPart().getCommentsPart());
|
||||
}
|
||||
}
|
||||
|
||||
public void detectDmlVml(JaxbXmlPart p) {
|
||||
|
||||
log.debug("Inspecting " + p.getPartName().getName());
|
||||
|
||||
Object contents;
|
||||
try {
|
||||
contents = p.getContents();
|
||||
} catch (Docx4JException e) {
|
||||
log.warn(p.getPartName().getName() + " could not be read: " + e);
|
||||
return;
|
||||
}
|
||||
|
||||
dmlVmlAnalyzer.reinit();
|
||||
dmlVmlAnalyzer.setPart(p);
|
||||
// CR-021: ALL - a shape in any branch must be inspected
|
||||
new TraversalUtil(contents, dmlVmlAnalyzer, org.docx4j.jaxb.McMode.ALL);
|
||||
|
||||
result.unsafeObjectsByPart.put(p, dmlVmlAnalyzer.unsafeObjects);
|
||||
if (dmlVmlAnalyzer.unsafeObjects.size() > 0) {
|
||||
result.anyUnsafeObjects = true;
|
||||
}
|
||||
result.inventoryObjectsByPart.put(p, dmlVmlAnalyzer.inventoryObjects);
|
||||
|
||||
if (!result.containsVML) {
|
||||
result.containsVML = dmlVmlAnalyzer.containsVML;
|
||||
}
|
||||
|
||||
result.fieldsPresent.addAll(dmlVmlAnalyzer.fieldsPresent);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
/*
|
||||
* Copyright 2026, Plutext Pty Ltd.
|
||||
*
|
||||
* This file is part of docx4j.
|
||||
|
||||
docx4j is licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
||||
*/
|
||||
package org.docx4j.anon;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
|
||||
import org.docx4j.fonts.IdentityPlusMapper;
|
||||
import org.docx4j.jaxb.McLoadMode;
|
||||
import org.docx4j.openpackaging.packages.OpcPackage;
|
||||
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
|
||||
|
||||
/**
|
||||
* The command line:
|
||||
* <pre>
|
||||
* java -cp "docx4j-core.jar:lib/*" org.docx4j.anon.AnonymizeCli in.docx out.docx [--keep] [--keep-numbers] [--json report.json] [--no-verify] [--no-fonts]
|
||||
* </pre>
|
||||
* (docx4j-core and a JAXB runtime such as docx4j-JAXB-ReferenceImpl on the
|
||||
* classpath; the class was the docx4j-docx-anon jar's Main-Class until the
|
||||
* anonymiser moved into core in 17.2.1). The input may be a docx or, since
|
||||
* 17.2.1, a pptx or xlsx (the package decides, not the extension: docm, dotx,
|
||||
* pptm, ppsx, potx, xlsm and xltx are the same).
|
||||
* <ul>
|
||||
* <li>{@code --keep-numbers}: a workbook's numbers keep their digits (by default
|
||||
* they are randomised; CR-019 decision 3).</li>
|
||||
* <li>{@code --keep}: KEEP mode - parts the tool cannot make clean stay, and are
|
||||
* reported; the default is STRICT, which removes them.</li>
|
||||
* <li>{@code --json file}: write the report there (it is always printed).</li>
|
||||
* <li>{@code --no-verify}: skip the before/after token check.</li>
|
||||
* <li>{@code --no-fonts}: do not discover the system's fonts (faster; the
|
||||
* non-Latin replacement characters are then not checked against the document's
|
||||
* fonts' glyphs). A pptx has no font mapper, so this makes no difference there.</li>
|
||||
* </ul>
|
||||
* A strict (ISO/IEC 29500 Strict) input is written out transitional, because that
|
||||
* is how docx4j reads one; see {@link Anonymize}.
|
||||
* <p>
|
||||
* The output is written whether or not the result is clean (in KEEP mode it
|
||||
* never is); the exit code is 0 only when {@code result.isClean()}, 1 when not,
|
||||
* 2 for a usage error, 3 when the document could not be processed.
|
||||
*
|
||||
* @since 17.2.0
|
||||
*/
|
||||
public class AnonymizeCli {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
System.exit(run(args));
|
||||
}
|
||||
|
||||
static int run(String[] args) {
|
||||
|
||||
String in = null, out = null, json = null;
|
||||
Anonymize.Mode mode = Anonymize.Mode.STRICT;
|
||||
boolean verify = true, fonts = true, keepNumbers = false;
|
||||
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
String a = args[i];
|
||||
if (a.equals("--keep")) {
|
||||
mode = Anonymize.Mode.KEEP;
|
||||
} else if (a.equals("--keep-numbers")) {
|
||||
keepNumbers = true;
|
||||
} else if (a.equals("--json")) {
|
||||
if (i + 1 >= args.length) return usage("--json needs a file");
|
||||
json = args[++i];
|
||||
} else if (a.equals("--no-verify")) {
|
||||
verify = false;
|
||||
} else if (a.equals("--no-fonts")) {
|
||||
fonts = false;
|
||||
} else if (a.startsWith("--")) {
|
||||
return usage("unknown option " + a);
|
||||
} else if (in == null) {
|
||||
in = a;
|
||||
} else if (out == null) {
|
||||
out = a;
|
||||
} else {
|
||||
return usage("too many arguments");
|
||||
}
|
||||
}
|
||||
if (in == null || out == null) return usage(null);
|
||||
|
||||
AnonymizeResult result;
|
||||
try {
|
||||
// preserve: the output reproduces the document that caused a problem, every
|
||||
// mc:AlternateContent branch included (CR-051 section 3.2, D5)
|
||||
OpcPackage pkg = OpcPackage.load(new File(in), McLoadMode.PRESERVE);
|
||||
if (fonts && pkg instanceof WordprocessingMLPackage) {
|
||||
((WordprocessingMLPackage) pkg).setFontMapper(new IdentityPlusMapper());
|
||||
}
|
||||
Anonymize anon = new Anonymize(pkg, mode);
|
||||
anon.setVerify(verify);
|
||||
anon.setKeepNumbers(keepNumbers);
|
||||
result = anon.go();
|
||||
|
||||
try (OutputStream os = new FileOutputStream(out)) {
|
||||
pkg.save(os);
|
||||
}
|
||||
String report = result.toJson();
|
||||
if (json != null) {
|
||||
Files.write(new File(json).toPath(), report.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
System.out.println(report);
|
||||
} catch (Exception e) {
|
||||
System.err.println("docx4j anon: " + e);
|
||||
e.printStackTrace(System.err);
|
||||
return 3;
|
||||
}
|
||||
|
||||
System.err.println(result.isClean() ? "clean" : "NOT clean - see the report");
|
||||
return result.isClean() ? 0 : 1;
|
||||
}
|
||||
|
||||
private static int usage(String problem) {
|
||||
if (problem != null) System.err.println(problem);
|
||||
System.err.println("usage: AnonymizeCli in.docx|in.pptx|in.xlsx out [--keep] [--keep-numbers] [--json report.json] [--no-verify] [--no-fonts]");
|
||||
return 2;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,322 @@
|
||||
package org.docx4j.anon;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
|
||||
import org.docx4j.XmlUtils;
|
||||
import org.docx4j.openpackaging.parts.Part;
|
||||
|
||||
/**
|
||||
* What {@link Anonymize#go()} did, and whether the result is clean.
|
||||
* <p>
|
||||
* Since 17.2.0 (CR-019) the single answer is {@link #isClean()}: true only when
|
||||
* every part was scrubbed, replaced or removed, nothing was kept unsafe, and the
|
||||
* verification (if it ran) found no token of the original surviving. The
|
||||
* per-part record is {@link #getActions()}, and {@link #toJson()} is the
|
||||
* machine-readable report the CLI writes.
|
||||
*/
|
||||
public class AnonymizeResult {
|
||||
|
||||
/** What was done with a part. */
|
||||
public enum Action {
|
||||
/** text scrambled, identities and references scrubbed */
|
||||
SCRUBBED,
|
||||
/** metadata fields cleared */
|
||||
CLEARED,
|
||||
/** image replaced by placeholder pixels */
|
||||
REPLACED,
|
||||
/** part removed, with its relationships */
|
||||
REMOVED,
|
||||
/** structure only: left as it was */
|
||||
KEPT,
|
||||
/** could not be made clean and was kept (KEEP mode, or a part which failed to load): the result is not clean */
|
||||
KEPT_UNSAFE
|
||||
}
|
||||
|
||||
/** One part's record. */
|
||||
public static class PartAction {
|
||||
public final String partName;
|
||||
public final String partClass;
|
||||
public final Action action;
|
||||
public final String reason;
|
||||
|
||||
PartAction(String partName, String partClass, Action action, String reason) {
|
||||
this.partName = partName;
|
||||
this.partClass = partClass;
|
||||
this.action = action;
|
||||
this.reason = reason;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return partName + ": " + action + (reason == null ? "" : " (" + reason + ")");
|
||||
}
|
||||
}
|
||||
|
||||
Anonymize.Mode mode;
|
||||
final List<PartAction> actions = new ArrayList<PartAction>();
|
||||
final List<String> notes = new ArrayList<String>();
|
||||
int externalTargetsReplaced = 0;
|
||||
int authorsRenamed = 0;
|
||||
|
||||
Boolean verified = null; // null: verification did not run
|
||||
final List<Verify.Leak> leaks = new ArrayList<Verify.Leak>();
|
||||
|
||||
public boolean hasGreek = false;
|
||||
public boolean hasCyrillic = false;
|
||||
public boolean hasHebrew = false;
|
||||
public boolean hasArabic = false;
|
||||
public boolean hasHiragana = false;
|
||||
public boolean hasKatakana = false;
|
||||
public boolean hasCJK = false;
|
||||
|
||||
void record(Part p, Action action, String reason) {
|
||||
actions.add(new PartAction(p.getPartName().getName(), p.getClass().getSimpleName(), action, reason));
|
||||
}
|
||||
|
||||
void record(String partName, String partClass, Action action, String reason) {
|
||||
actions.add(new PartAction(partName, partClass, action, reason));
|
||||
}
|
||||
|
||||
public Anonymize.Mode getMode() {
|
||||
return mode;
|
||||
}
|
||||
|
||||
/** Per part: what was done and why. */
|
||||
public List<PartAction> getActions() {
|
||||
return actions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Element-level removals (w:altChunk, o:OLEObject, w:mailMerge ...), one line
|
||||
* each, and anything else the report should say about the whole document - the
|
||||
* conversion of a strict package, for one.
|
||||
*/
|
||||
public List<String> getNotes() {
|
||||
return notes;
|
||||
}
|
||||
|
||||
public int getExternalTargetsReplaced() {
|
||||
return externalTargetsReplaced;
|
||||
}
|
||||
|
||||
public int getAuthorsRenamed() {
|
||||
return authorsRenamed;
|
||||
}
|
||||
|
||||
/** The parts kept although the tool could not make them clean. */
|
||||
public List<PartAction> getKeptUnsafe() {
|
||||
List<PartAction> result = new ArrayList<PartAction>();
|
||||
for (PartAction a : actions) {
|
||||
if (a.action == Action.KEPT_UNSAFE) result.add(a);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/** null if verification did not run; else whether it found no surviving token. */
|
||||
public Boolean getVerified() {
|
||||
return verified;
|
||||
}
|
||||
|
||||
/** The tokens of the original which survived, where. Empty when clean. */
|
||||
public List<Verify.Leak> getLeaks() {
|
||||
return leaks;
|
||||
}
|
||||
|
||||
/**
|
||||
* The guarantee: no part kept unsafe, no unsafe object, and - when
|
||||
* verification ran - no token of the original found in the output.
|
||||
*/
|
||||
public boolean isClean() {
|
||||
return getKeptUnsafe().isEmpty()
|
||||
&& !anyUnsafeObjects
|
||||
&& (verified == null || verified.booleanValue());
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated since 17.2.0, use {@link #isClean()}
|
||||
*/
|
||||
@Deprecated
|
||||
public boolean isOK() {
|
||||
return isClean();
|
||||
}
|
||||
|
||||
/** A JSON document: mode, clean, verified, the actions, the notes, the leaks. */
|
||||
public String toJson() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("{\n");
|
||||
sb.append(" \"mode\": ").append(json(mode == null ? null : mode.name())).append(",\n");
|
||||
sb.append(" \"clean\": ").append(isClean()).append(",\n");
|
||||
sb.append(" \"verified\": ").append(verified == null ? "null" : verified.toString()).append(",\n");
|
||||
sb.append(" \"externalTargetsReplaced\": ").append(externalTargetsReplaced).append(",\n");
|
||||
sb.append(" \"authorsRenamed\": ").append(authorsRenamed).append(",\n");
|
||||
sb.append(" \"scripts\": {");
|
||||
sb.append("\"greek\": ").append(hasGreek).append(", \"cyrillic\": ").append(hasCyrillic)
|
||||
.append(", \"hebrew\": ").append(hasHebrew).append(", \"arabic\": ").append(hasArabic)
|
||||
.append(", \"hiragana\": ").append(hasHiragana).append(", \"katakana\": ").append(hasKatakana)
|
||||
.append(", \"cjk\": ").append(hasCJK).append("},\n");
|
||||
sb.append(" \"parts\": [\n");
|
||||
for (int i = 0; i < actions.size(); i++) {
|
||||
PartAction a = actions.get(i);
|
||||
sb.append(" {\"part\": ").append(json(a.partName))
|
||||
.append(", \"class\": ").append(json(a.partClass))
|
||||
.append(", \"action\": ").append(json(a.action.name()))
|
||||
.append(", \"reason\": ").append(json(a.reason)).append("}");
|
||||
sb.append(i + 1 < actions.size() ? ",\n" : "\n");
|
||||
}
|
||||
sb.append(" ],\n");
|
||||
sb.append(" \"notes\": [");
|
||||
for (int i = 0; i < notes.size(); i++) {
|
||||
sb.append(i == 0 ? "\n " : ",\n ").append(json(notes.get(i)));
|
||||
}
|
||||
sb.append(notes.isEmpty() ? "],\n" : "\n ],\n");
|
||||
sb.append(" \"leaks\": [");
|
||||
for (int i = 0; i < leaks.size(); i++) {
|
||||
Verify.Leak l = leaks.get(i);
|
||||
sb.append(i == 0 ? "\n " : ",\n ")
|
||||
.append("{\"part\": ").append(json(l.partName))
|
||||
.append(", \"where\": ").append(json(l.where))
|
||||
.append(", \"token\": ").append(json(l.token)).append("}");
|
||||
}
|
||||
sb.append(leaks.isEmpty() ? "]\n" : "\n ]\n");
|
||||
sb.append("}\n");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
static String json(String s) {
|
||||
if (s == null) return "null";
|
||||
StringBuilder sb = new StringBuilder("\"");
|
||||
for (int i = 0; i < s.length(); i++) {
|
||||
char c = s.charAt(i);
|
||||
switch (c) {
|
||||
case '"': sb.append("\\\""); break;
|
||||
case '\\': sb.append("\\\\"); break;
|
||||
case '\n': sb.append("\\n"); break;
|
||||
case '\r': sb.append("\\r"); break;
|
||||
case '\t': sb.append("\\t"); break;
|
||||
default:
|
||||
if (c < 0x20) {
|
||||
sb.append(String.format("\\u%04x", (int) c));
|
||||
} else {
|
||||
sb.append(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
return sb.append('"').toString();
|
||||
}
|
||||
|
||||
// ---- the pre-17.2.0 inventory, still populated
|
||||
|
||||
/**
|
||||
* The parts the tool could not make clean (in STRICT mode they were removed,
|
||||
* so this is the record of what went; in KEEP mode they are still in the package).
|
||||
*/
|
||||
public HashSet<Part> getUnsafeParts() {
|
||||
return unsafeParts;
|
||||
}
|
||||
HashSet<Part> unsafeParts = new HashSet<Part>();
|
||||
|
||||
/**
|
||||
* Objects flagged by the inventory walk, by story part.
|
||||
*/
|
||||
public HashMap<Part, Set<Object>> getUnsafeObjectsByPart() {
|
||||
return unsafeObjectsByPart;
|
||||
}
|
||||
HashMap<Part, Set<Object>> unsafeObjectsByPart = new HashMap<Part, Set<Object>>();
|
||||
|
||||
boolean anyUnsafeObjects = false;
|
||||
|
||||
public boolean hasAnyUnsafeObjects() {
|
||||
return anyUnsafeObjects;
|
||||
}
|
||||
|
||||
/**
|
||||
* Misc interesting objects, by story part, for further consideration
|
||||
*/
|
||||
public HashMap<Part, Set<Object>> getInventoryObjectsByPart() {
|
||||
return inventoryObjectsByPart;
|
||||
}
|
||||
HashMap<Part, Set<Object>> inventoryObjectsByPart = new HashMap<Part, Set<Object>>();
|
||||
|
||||
/**
|
||||
* Whether this docx contains VML
|
||||
*/
|
||||
public boolean containsVML() {
|
||||
return containsVML;
|
||||
}
|
||||
boolean containsVML = false;
|
||||
|
||||
HashSet<String> fieldsPresent = new HashSet<String>();
|
||||
|
||||
/** The field instructions found (before scrubbing). */
|
||||
public HashSet<String> getFieldsPresent() {
|
||||
return fieldsPresent;
|
||||
}
|
||||
|
||||
public String reportUnsafeObjects() {
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
if (hasAnyUnsafeObjects()) {
|
||||
sb.append("The following objects may leak info: \n");
|
||||
for (Entry<Part, Set<Object>> entry : getUnsafeObjectsByPart().entrySet()) {
|
||||
|
||||
Part p = entry.getKey();
|
||||
|
||||
if (!entry.getValue().isEmpty()) {
|
||||
sb.append(p.getPartName().getName() + ", of type " + p.getClass().getName() + "\n");
|
||||
|
||||
for (Object o : entry.getValue()) {
|
||||
|
||||
if (o instanceof String) {
|
||||
sb.append(o + "\n");
|
||||
} else {
|
||||
sb.append(o.getClass().getName());
|
||||
try {
|
||||
sb.append(XmlUtils.marshaltoString(o) + "\n");
|
||||
} catch (Exception e) {
|
||||
sb.append("\n");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
|
||||
}
|
||||
|
||||
/** A human-readable summary: clean or not, and why. */
|
||||
public String summary() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(isClean() ? "CLEAN" : "NOT CLEAN").append(" (mode ").append(mode).append(")\n");
|
||||
for (PartAction a : actions) {
|
||||
sb.append(" ").append(a).append("\n");
|
||||
}
|
||||
for (String n : notes) {
|
||||
sb.append(" - ").append(n).append("\n");
|
||||
}
|
||||
if (externalTargetsReplaced > 0) {
|
||||
sb.append(" external targets replaced: ").append(externalTargetsReplaced).append("\n");
|
||||
}
|
||||
if (authorsRenamed > 0) {
|
||||
sb.append(" authors renamed: ").append(authorsRenamed).append("\n");
|
||||
}
|
||||
if (verified != null) {
|
||||
sb.append(" verified: ").append(verified).append(leaks.isEmpty() ? "" : ", leaks: " + leaks.size()).append("\n");
|
||||
for (Verify.Leak l : leaks) {
|
||||
sb.append(" ").append(l).append("\n");
|
||||
}
|
||||
}
|
||||
sb.append(reportUnsafeObjects());
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,516 @@
|
||||
package org.docx4j.anon;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
|
||||
import jakarta.xml.bind.JAXBContext;
|
||||
import jakarta.xml.bind.JAXBElement;
|
||||
|
||||
import org.docx4j.TraversalUtil.CallbackImpl;
|
||||
import org.docx4j.XmlUtils;
|
||||
import org.docx4j.dml.CTBlip;
|
||||
import org.docx4j.dml.CTHyperlink;
|
||||
import org.docx4j.dml.CTNonVisualDrawingProps;
|
||||
import org.docx4j.dml.diagram.CTDataModel;
|
||||
import org.docx4j.jaxb.Context;
|
||||
import org.docx4j.openpackaging.parts.JaxbXmlPart;
|
||||
import org.docx4j.openpackaging.parts.Part;
|
||||
import org.docx4j.openpackaging.parts.WordprocessingML.ImageBmpPart;
|
||||
import org.docx4j.openpackaging.parts.WordprocessingML.ImageGifPart;
|
||||
import org.docx4j.openpackaging.parts.WordprocessingML.ImageJpegPart;
|
||||
import org.docx4j.openpackaging.parts.WordprocessingML.ImagePngPart;
|
||||
import org.docx4j.openpackaging.parts.WordprocessingML.ImageTiffPart;
|
||||
import org.docx4j.openpackaging.parts.relationships.Namespaces;
|
||||
import org.docx4j.vml.CTImageData;
|
||||
import org.docx4j.wml.CTObject;
|
||||
import org.docx4j.wml.FldChar;
|
||||
import org.docx4j.wml.Pict;
|
||||
import org.docx4j.wml.SdtBlock;
|
||||
import org.docx4j.wml.Text;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* This will detect DrawingML or VML which does anything more than
|
||||
* link to a safe image (ie one we've replaced)
|
||||
*
|
||||
* @author jharrop
|
||||
*
|
||||
*/
|
||||
public class DmlVmlAnalyzer extends CallbackImpl {
|
||||
|
||||
private static Logger log = LoggerFactory.getLogger(DmlVmlAnalyzer.class);
|
||||
|
||||
private JaxbXmlPart sourcePart;
|
||||
public void setPart(JaxbXmlPart p) {
|
||||
this.sourcePart = p;
|
||||
}
|
||||
|
||||
/**
|
||||
* Objects we might not anonymise
|
||||
*/
|
||||
HashSet<Object> unsafeObjects = null;
|
||||
|
||||
/**
|
||||
* Objects it is interesting to note are present
|
||||
*/
|
||||
HashSet<Object> inventoryObjects = null;
|
||||
|
||||
HashSet<String> fieldsPresent = null;
|
||||
|
||||
|
||||
boolean containsVML;
|
||||
|
||||
public void reinit() {
|
||||
unsafeObjects = new HashSet<Object>();
|
||||
inventoryObjects = new HashSet<Object>();
|
||||
|
||||
containsVML = false;
|
||||
|
||||
fieldsPresent = new HashSet<String>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean shouldTraverse(Object o) {
|
||||
|
||||
if (o instanceof org.docx4j.math.CTOMathPara) {
|
||||
// since 17.2.0 (CR-019) the m:t text is scrambled like any other, so math is
|
||||
// inventory, not unsafe
|
||||
inventoryObjects.add(o);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public List<Object> apply(Object o2) {
|
||||
|
||||
// System.out.println(o.getClass().getName());
|
||||
|
||||
if (o2 instanceof JAXBElement) {
|
||||
|
||||
// record field instruction
|
||||
if (((JAXBElement)o2).getName().getLocalPart().equals("instrText")) {
|
||||
|
||||
Text instr = (Text)XmlUtils.unwrap(o2);
|
||||
fieldsPresent.add(instr.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
Object o = XmlUtils.unwrap(o2);
|
||||
|
||||
if (o instanceof org.docx4j.vml.CTImageData) {
|
||||
// remove its title
|
||||
((CTImageData)o).setTitle("foo");
|
||||
|
||||
if ( ((CTImageData)o).getRelid()!=null ) {
|
||||
|
||||
String rId = ((CTImageData)o).getRelid();
|
||||
Part embeddedPart = sourcePart.getRelationshipsPart().getPart(rId);
|
||||
if (embeddedPart instanceof ImagePngPart
|
||||
|| embeddedPart instanceof ImageGifPart
|
||||
|| embeddedPart instanceof ImageJpegPart
|
||||
|| embeddedPart instanceof ImageBmpPart
|
||||
|| embeddedPart instanceof ImageTiffPart
|
||||
// Others treated as unsafe
|
||||
) {
|
||||
// We've handled this
|
||||
|
||||
} else {
|
||||
// Unsafe, but noted elsewhere
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public List<Object> getChildren(Object o2) {
|
||||
|
||||
if (o2==null) {
|
||||
log.warn("null passed to getChildrenImpl");
|
||||
return null;
|
||||
}
|
||||
|
||||
Object o = XmlUtils.unwrap(o2);
|
||||
|
||||
log.debug("getting children of " + o.getClass().getName() );
|
||||
if (o instanceof org.docx4j.wml.Text) return null;
|
||||
|
||||
// Short circuit for common elements
|
||||
if (o instanceof List) {
|
||||
// Handy if you have your own list of objects you wish to process
|
||||
return (List<Object>) o;
|
||||
} else if (o instanceof org.docx4j.wml.ContentAccessor) {
|
||||
return ((org.docx4j.wml.ContentAccessor) o).getContent();
|
||||
} else if (o instanceof org.docx4j.wml.SdtElement) {
|
||||
return ((org.docx4j.wml.SdtElement) o).getSdtContent().getContent();
|
||||
|
||||
} else if (o instanceof org.docx4j.mce.AlternateContent) {
|
||||
// the walk's mode (ALL, from Anonymize): reflection would find getChoice() alone,
|
||||
// and a VML Fallback's o:title would never be scrubbed (CR-051 section 10.5)
|
||||
return org.docx4j.jaxb.McSelection.branches((org.docx4j.mce.AlternateContent) o, getMcMode());
|
||||
|
||||
} else if (o instanceof org.docx4j.dml.wordprocessingDrawing.Anchor) {
|
||||
|
||||
// Similar to wordprocessingDrawing.Inline below
|
||||
|
||||
log.debug( sourcePart.getPartName().getName() + "\n"
|
||||
+ XmlUtils.marshaltoString(o, true, true, Context.jc,
|
||||
"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing", "anchor", o.getClass()));
|
||||
|
||||
org.docx4j.dml.wordprocessingDrawing.Anchor anchor = (org.docx4j.dml.wordprocessingDrawing.Anchor) o;
|
||||
List<Object> artificialList = new ArrayList<Object>();
|
||||
CTNonVisualDrawingProps drawingProps = anchor.getDocPr();
|
||||
if (drawingProps != null) {
|
||||
handleCTNonVisualDrawingProps(drawingProps, artificialList);
|
||||
}
|
||||
if (anchor.getGraphic() == null) {
|
||||
log.warn("TODO: Handle case of no a:graphic: " + XmlUtils.marshaltoString(o, true, true, Context.jc,
|
||||
"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing", "anchor", o.getClass()));
|
||||
} else {
|
||||
log.debug("found a:graphic");
|
||||
org.docx4j.dml.Graphic graphic = anchor.getGraphic();
|
||||
if (graphic.getGraphicData() == null) {
|
||||
|
||||
log.warn("TODO: Handle case of no a:graphicData: " + XmlUtils.marshaltoString(o, true, true, Context.jc, "foo", "Inline", o.getClass()));
|
||||
|
||||
} else {
|
||||
List l = handleGraphicData(graphic.getGraphicData());
|
||||
if (l!=null) {
|
||||
artificialList.addAll(l);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!artificialList.isEmpty())
|
||||
return artificialList;
|
||||
|
||||
} else if (o instanceof org.docx4j.dml.wordprocessingDrawing.Inline) {
|
||||
|
||||
// Done
|
||||
|
||||
/*
|
||||
<ns34:Inline distR="0" distL="0" distB="0" distT="0" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:ns21="urn:schemas-microsoft-com:office:powerpoint" xmlns:ns23="http://schemas.microsoft.com/office/2006/coverPageProps" xmlns:dsp="http://schemas.microsoft.com/office/drawing/2008/diagram" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:odx="http://opendope.org/xpaths" xmlns:odgm="http://opendope.org/SmartArt/DataHierarchy" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:dgm="http://schemas.openxmlformats.org/drawingml/2006/diagram" xmlns:ns17="urn:schemas-microsoft-com:office:excel" xmlns:b="http://schemas.openxmlformats.org/officeDocument/2006/bibliography" xmlns:c="http://schemas.openxmlformats.org/drawingml/2006/chart" xmlns:odi="http://opendope.org/components" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:ns34="foo" xmlns:ns9="http://schemas.openxmlformats.org/schemaLibrary/2006/main" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:ns32="http://schemas.openxmlformats.org/drawingml/2006/compatibility" xmlns:ns33="http://schemas.openxmlformats.org/drawingml/2006/lockedCanvas" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture" xmlns:ns12="http://schemas.openxmlformats.org/drawingml/2006/chartDrawing" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:odq="http://opendope.org/questions" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:xdr="http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing" xmlns:odc="http://opendope.org/conditions" xmlns:oda="http://opendope.org/answers">
|
||||
<wp:extent cy="2362200" cx="3238500"/>
|
||||
<wp:effectExtent b="0" r="0" t="0" l="19050"/>
|
||||
<wp:docPr descr="C:\Documents and Settings\Jason Harrop\My Documents\tmp-test-docs\pangolin.jpeg" name="Picture 1" id="1"/>
|
||||
<wp:cNvGraphicFramePr>
|
||||
<a:graphicFrameLocks noChangeAspect="true"/>
|
||||
</wp:cNvGraphicFramePr>
|
||||
<a:graphic>
|
||||
<a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">
|
||||
*/
|
||||
|
||||
org.docx4j.dml.wordprocessingDrawing.Inline inline = (org.docx4j.dml.wordprocessingDrawing.Inline) o;
|
||||
|
||||
List<Object> artificialList = new ArrayList<Object>();
|
||||
CTNonVisualDrawingProps drawingProps = inline.getDocPr();
|
||||
if (drawingProps != null) {
|
||||
// handle <wp:docPr descr="
|
||||
handleCTNonVisualDrawingProps(drawingProps, artificialList);
|
||||
}
|
||||
if (inline.getGraphic() == null) {
|
||||
log.warn("TODO: Handle case of no a:graphic: " + XmlUtils.marshaltoString(o, true, true, Context.jc, "foo", "Inline", o.getClass()));
|
||||
} else {
|
||||
log.debug("found a:graphic");
|
||||
org.docx4j.dml.Graphic graphic = inline.getGraphic();
|
||||
if (graphic.getGraphicData() == null) {
|
||||
|
||||
log.warn("TODO: Handle case of no a:graphicData: " + XmlUtils.marshaltoString(o, true, true, Context.jc, "foo", "Inline", o.getClass()));
|
||||
|
||||
} else {
|
||||
List l = handleGraphicData(graphic.getGraphicData());
|
||||
if (l!=null) {
|
||||
artificialList.addAll(l);
|
||||
}
|
||||
}
|
||||
return artificialList;
|
||||
}
|
||||
if (!artificialList.isEmpty())
|
||||
return artificialList;
|
||||
|
||||
} else if (o instanceof Pict) {
|
||||
|
||||
/*
|
||||
<w:pict>
|
||||
<v:shapetype >
|
||||
:
|
||||
<v:shape
|
||||
*/
|
||||
|
||||
log.debug(XmlUtils.marshaltoString(o));
|
||||
|
||||
return ((Pict)o).getAnyAndAny();
|
||||
|
||||
} else if (o instanceof org.docx4j.dml.picture.Pic) { // Post 2.7.1; untested
|
||||
|
||||
log.warn("TODO: " + XmlUtils.marshaltoString(o));
|
||||
|
||||
org.docx4j.dml.picture.Pic dmlPic = ((org.docx4j.dml.picture.Pic)o);
|
||||
if (dmlPic.getBlipFill()!=null
|
||||
&& dmlPic.getBlipFill().getBlip()!=null) {
|
||||
log.debug("found DML Blip");
|
||||
List<Object> artificialList = new ArrayList<Object>();
|
||||
artificialList.add(dmlPic.getBlipFill().getBlip());
|
||||
return artificialList;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
} else if (o instanceof org.docx4j.dml.CTGvmlPicture) { // Post 2.7.1
|
||||
|
||||
log.warn("TODO: " + XmlUtils.marshaltoString(o));
|
||||
|
||||
org.docx4j.dml.CTGvmlPicture dmlPic = ((org.docx4j.dml.CTGvmlPicture)o);
|
||||
if (dmlPic.getBlipFill()!=null
|
||||
&& dmlPic.getBlipFill().getBlip()!=null) {
|
||||
log.debug("found DML Blip");
|
||||
List<Object> artificialList = new ArrayList<Object>();
|
||||
artificialList.add(dmlPic.getBlipFill().getBlip());
|
||||
return artificialList;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
||||
} else if (o instanceof org.docx4j.vml.CTShapetype ) {
|
||||
|
||||
// NB, may not be triggered, depending on parent.
|
||||
|
||||
/* eg
|
||||
<v:shapetype id="_x0000_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t"
|
||||
path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f">
|
||||
<v:stroke joinstyle="miter"/>
|
||||
<v:formulas>
|
||||
<v:f eqn="if lineDrawn pixelLineWidth 0"/>
|
||||
<v:f eqn="sum @0 1 0"/>
|
||||
*/
|
||||
|
||||
containsVML = true;
|
||||
|
||||
// Generally nothing sensitive here
|
||||
log.debug( XmlUtils.marshaltoString(o, true, true, Context.jc,
|
||||
Namespaces.VML, "shapetype", o.getClass()));
|
||||
inventoryObjects.add(o);
|
||||
|
||||
return null;
|
||||
|
||||
} else if (o instanceof org.docx4j.vml.CTShape) {
|
||||
|
||||
containsVML = true;
|
||||
|
||||
log.debug(XmlUtils.marshaltoString(o));
|
||||
|
||||
// return ((org.docx4j.vml.CTShape)o).getAny();
|
||||
List<Object> artificialList = new ArrayList<Object>();
|
||||
for (JAXBElement<?> j : ((org.docx4j.vml.CTShape)o).getPathOrFormulasOrHandles() ) {
|
||||
// System.out.println(XmlUtils.unwrap(j).getClass().getName() );
|
||||
artificialList.add(j);
|
||||
}
|
||||
return artificialList;
|
||||
|
||||
} else if (o instanceof CTDataModel) {
|
||||
|
||||
log.warn("TODO: " + XmlUtils.marshaltoString(o));
|
||||
|
||||
CTDataModel dataModel = (CTDataModel)o;
|
||||
List<Object> artificialList = new ArrayList<Object>();
|
||||
// We're going to create a list merging two children ..
|
||||
artificialList.addAll(dataModel.getPtLst().getPt());
|
||||
artificialList.addAll(dataModel.getCxnLst().getCxn());
|
||||
return artificialList;
|
||||
|
||||
} else if (o instanceof org.docx4j.dml.diagram2008.CTDrawing) {
|
||||
|
||||
log.warn("TODO: " + XmlUtils.marshaltoString(o));
|
||||
|
||||
return ((org.docx4j.dml.diagram2008.CTDrawing)o).getSpTree().getSpOrGrpSp();
|
||||
|
||||
} else if (o instanceof org.docx4j.vml.CTTextbox) {
|
||||
|
||||
// We anon inside
|
||||
/*
|
||||
<v:textbox style="mso-fit-shape-to-text:t">
|
||||
<w:txbxContent>
|
||||
<w:p */
|
||||
|
||||
log.debug(XmlUtils.marshaltoString(o, true, true, Context.jc,
|
||||
Namespaces.VML, "textbox", o.getClass()));
|
||||
|
||||
org.docx4j.vml.CTTextbox textBox = ((org.docx4j.vml.CTTextbox)o);
|
||||
if (textBox.getTxbxContent()==null) {
|
||||
return null;
|
||||
} else {
|
||||
return textBox.getTxbxContent().getContent();
|
||||
// grandchildren
|
||||
}
|
||||
|
||||
} else if (o instanceof CTObject) {
|
||||
|
||||
// w:object is not of itself interesting here
|
||||
log.debug(XmlUtils.marshaltoString(o, true, true, Context.jc, Namespaces.NS_WORD12, "object", o.getClass()));
|
||||
|
||||
CTObject ctObject = (CTObject)o;
|
||||
List<Object> artificialList = new ArrayList<Object>();
|
||||
artificialList.addAll(ctObject.getAnyAndAny());
|
||||
if (ctObject.getControl()!=null) {
|
||||
artificialList.add(ctObject.getControl() ); // CTControl
|
||||
}
|
||||
return artificialList;
|
||||
|
||||
} else if (o instanceof org.docx4j.dml.CTGvmlGroupShape) {
|
||||
|
||||
log.warn("TODO: " + XmlUtils.marshaltoString(o));
|
||||
|
||||
return ((org.docx4j.dml.CTGvmlGroupShape)o).getTxSpOrSpOrCxnSp();
|
||||
|
||||
} else if(o instanceof FldChar) {
|
||||
|
||||
// Interesting to analyse fields; we record instrText above
|
||||
|
||||
FldChar fldChar = ((FldChar)o);
|
||||
List<Object> artificialList = new ArrayList<Object>();
|
||||
artificialList.add(fldChar.getFldCharType());
|
||||
if(fldChar.getFfData() != null) {
|
||||
artificialList.add(fldChar.getFfData());
|
||||
}
|
||||
if(fldChar.getFldData() != null) {
|
||||
artificialList.add(fldChar.getFldData());
|
||||
}
|
||||
if(fldChar.getNumberingChange() != null) {
|
||||
artificialList.add(fldChar.getNumberingChange());
|
||||
}
|
||||
return artificialList;
|
||||
}
|
||||
|
||||
// OK, what is this? Use reflection ..
|
||||
// This should work for things including w:drawing
|
||||
log.debug(".. looking for method which returns list " );
|
||||
try {
|
||||
Method[] methods = o.getClass().getDeclaredMethods();
|
||||
for (int i = 0; i<methods.length; i++) {
|
||||
Method m = methods[i];
|
||||
if (m.getReturnType().getName().equals("java.util.List") ) {
|
||||
return (List<Object>)m.invoke(o);
|
||||
}
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
log.debug(".. no list member");
|
||||
return null;
|
||||
}
|
||||
|
||||
private List<Object> handleGraphicData(org.docx4j.dml.GraphicData graphicData) {
|
||||
|
||||
/*
|
||||
<a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">
|
||||
<pic:pic>
|
||||
<pic:nvPicPr>
|
||||
<pic:cNvPr descr="C:\Documents and Settings\Jason Harrop\My Documents\tmp-test-docs\pangolin.jpeg" name="Picture 1" id="0"/>
|
||||
<pic:cNvPicPr>
|
||||
<a:picLocks noChangeArrowheads="true" noChangeAspect="true"/>
|
||||
</pic:cNvPicPr>
|
||||
</pic:nvPicPr>
|
||||
<pic:blipFill>
|
||||
<a:blip r:embed="rId8" cstate="print"/>
|
||||
<a:srcRect/>
|
||||
<a:stretch>
|
||||
<a:fillRect/>
|
||||
</a:stretch>
|
||||
</pic:blipFill> */
|
||||
|
||||
List<Object> tmpArtificialList = new ArrayList<Object>();
|
||||
if (graphicData.getPic() != null) {
|
||||
//GraphicData can have a hyperlink reference, which can be found this way
|
||||
CTNonVisualDrawingProps picNonVisual = graphicData.getPic().getNvPicPr().getCNvPr();
|
||||
if (picNonVisual != null) {
|
||||
handleCTNonVisualDrawingProps(picNonVisual, tmpArtificialList);
|
||||
}
|
||||
}
|
||||
// Its not graphicData.getAny() we're typically interested in
|
||||
if (graphicData.getPic() != null && graphicData.getPic().getBlipFill() != null
|
||||
&& graphicData.getPic().getBlipFill().getBlip() != null) {
|
||||
|
||||
CTBlip blip = graphicData.getPic().getBlipFill().getBlip();
|
||||
if (blip.getLink()!=null) {
|
||||
// Assume OK. Either its on the public internet, or its inaccessible.
|
||||
log.debug("blip contained a link .. assumed ok");
|
||||
} else if (blip.getEmbed()!=null ) {
|
||||
|
||||
String rId = blip.getEmbed();
|
||||
Part embeddedPart = sourcePart.getRelationshipsPart().getPart(rId);
|
||||
if (embeddedPart instanceof ImagePngPart
|
||||
|| embeddedPart instanceof ImageGifPart
|
||||
|| embeddedPart instanceof ImageJpegPart
|
||||
|| embeddedPart instanceof ImageBmpPart
|
||||
|| embeddedPart instanceof ImageTiffPart
|
||||
// Others treated as unsafe
|
||||
) {
|
||||
|
||||
// We've handled this
|
||||
|
||||
} else {
|
||||
|
||||
// Unsafe, but noted elsewhere
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return null;
|
||||
} else {
|
||||
|
||||
// Charts, shapes, diagrams: since 17.2.0 (CR-019) the anonymiser's walk reaches
|
||||
// their text and caches, so this is inventory, not unsafe
|
||||
inventoryObjects.add(graphicData);
|
||||
|
||||
return graphicData.getAny();
|
||||
}
|
||||
}
|
||||
|
||||
private void addUnsafe(Object o,
|
||||
String uri, String local, Class declaredType) {
|
||||
|
||||
// For now, we'll use marshalled content
|
||||
unsafeObjects.add(XmlUtils.marshaltoString(o, true, true, Context.jc,
|
||||
uri, local, declaredType));
|
||||
}
|
||||
|
||||
// private void addUnsafe(Object o) {
|
||||
//
|
||||
// // For now, we'll use marshalled content
|
||||
// unsafeObjects.add(XmlUtils.marshaltoString(o));
|
||||
// }
|
||||
|
||||
|
||||
/**
|
||||
* There can be hyperlinks references in CTNonVisualDrawingProps.
|
||||
* @param drawingProps
|
||||
* @param artificialList
|
||||
*/
|
||||
private void handleCTNonVisualDrawingProps(CTNonVisualDrawingProps drawingProps, List<Object> artificialList){
|
||||
if (drawingProps != null) {
|
||||
|
||||
// <wp:docPr descr="C:\Documents and Settings\Jason Harrop\My Documents\tmp-test-docs\pangolin.jpeg"
|
||||
// name="Picture 1" id="1"/>
|
||||
|
||||
if (drawingProps.getDescr()!=null) {
|
||||
drawingProps.setDescr(null);
|
||||
}
|
||||
// Name is probably ok
|
||||
|
||||
|
||||
CTHyperlink docPrHyperLink = drawingProps.getHlinkClick();
|
||||
if (docPrHyperLink != null)
|
||||
artificialList.add(docPrHyperLink);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,244 @@
|
||||
/*
|
||||
* Copyright 2026, Plutext Pty Ltd.
|
||||
*
|
||||
* This file is part of docx4j.
|
||||
|
||||
docx4j is licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
||||
*/
|
||||
package org.docx4j.anon;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/**
|
||||
* Scrubs a field instruction (w:instrText, w:delInstrText, w:fldSimple/@w:instr):
|
||||
* the field keyword and its switches survive, every argument loses its text.
|
||||
* <p>
|
||||
* Rules (CR-019 phase 1, item 2, "the MERGEFIELD pattern, generalised"):
|
||||
* <ul>
|
||||
* <li>the keyword is kept;</li>
|
||||
* <li>a switch ({@code \x}) is kept; the argument of a format switch
|
||||
* ({@code \*}, {@code \#}, {@code \@}) is a picture, not content, and is kept;</li>
|
||||
* <li>an argument which names a bookmark (REF, PAGEREF, NOTEREF, ASK, SET and
|
||||
* SEQ's identifier; HYPERLINK {@code \l}; TOC {@code \b} and {@code \c})
|
||||
* is renamed through the identifier map, so it still matches the renamed
|
||||
* bookmark;</li>
|
||||
* <li>STYLEREF's argument is kept when it is a built-in style name (the
|
||||
* predicate decides), else scrambled like any other;</li>
|
||||
* <li>a formula field ({@code = ...}) keeps its operators and functions;
|
||||
* only tokens the identifier map knows (bookmark names) are renamed;</li>
|
||||
* <li>form-field instructions (FORMTEXT, FORMCHECKBOX, FORMDROPDOWN) have no
|
||||
* argument and are untouched;</li>
|
||||
* <li>every other argument, quoted or bare, has its letters scrambled (digits
|
||||
* and punctuation stay, so a path keeps its shape and a switch value like
|
||||
* {@code "1-3"} its meaning).</li>
|
||||
* </ul>
|
||||
*
|
||||
* @since 17.2.0
|
||||
*/
|
||||
public class FieldInstructions {
|
||||
|
||||
/** Word's field keywords: kept by the scrub, allowed by {@link Verify}. */
|
||||
public static final Set<String> KEYWORDS = Collections.unmodifiableSet(new HashSet<String>(Arrays.asList(
|
||||
"ADDRESSBLOCK", "ADVANCE", "ASK", "AUTHOR", "AUTONUM", "AUTONUMLGL", "AUTONUMOUT", "AUTOTEXT",
|
||||
"AUTOTEXTLIST", "BARCODE", "BIBLIOGRAPHY", "BIDIOUTLINE", "CITATION", "COMMENTS", "COMPARE",
|
||||
"CREATEDATE", "DATABASE", "DATE", "DDE", "DDEAUTO", "DISPLAYBARCODE", "DOCPROPERTY", "DOCVARIABLE",
|
||||
"EDITTIME", "EMBED", "EQ", "FILENAME", "FILESIZE", "FILLIN", "FORMCHECKBOX", "FORMDROPDOWN",
|
||||
"FORMTEXT", "GLOSSARY", "GOTOBUTTON", "GREETINGLINE", "HYPERLINK", "IF", "IMPORT", "INCLUDE",
|
||||
"INCLUDEPICTURE", "INCLUDETEXT", "INDEX", "INFO", "KEYWORDS", "LASTSAVEDBY", "LINK", "LISTNUM",
|
||||
"MACROBUTTON", "MERGEBARCODE", "MERGEFIELD", "MERGEREC", "MERGESEQ", "NEXT", "NEXTIF", "NOTEREF",
|
||||
"NUMCHARS", "NUMPAGES", "NUMWORDS", "PAGE", "PAGEREF", "PRINT", "PRINTDATE", "PRIVATE", "QUOTE",
|
||||
"RD", "REF", "REVNUM", "SAVEDATE", "SECTION", "SECTIONPAGES", "SEQ", "SET", "SHAPE", "SKIPIF",
|
||||
"STYLEREF", "SUBJECT", "SYMBOL", "TA", "TC", "TEMPLATE", "TIME", "TITLE", "TOA", "TOC",
|
||||
"USERADDRESS", "USERINITIALS", "USERNAME", "XE",
|
||||
// format-switch words and formula functions, kept with the switch / the formula
|
||||
"MERGEFORMAT", "CHARFORMAT", "UPPER", "LOWER", "FIRSTCAP", "CAPS", "ALPHABETIC", "ARABIC",
|
||||
"ARABICDASH", "CARDTEXT", "DOLLARTEXT", "HEX", "ORDINAL", "ORDTEXT", "ROMAN", "SUM", "AVERAGE",
|
||||
"ABOVE", "BELOW", "LEFT", "RIGHT", "COUNT", "MAX", "MIN", "PRODUCT", "ROUND", "ABS", "AND",
|
||||
"OR", "NOT", "DEFINED", "FALSE", "TRUE", "INT", "MOD", "SIGN")));
|
||||
|
||||
private static final Set<String> FORMAT_SWITCHES = new HashSet<String>(Arrays.asList("\\*", "\\#", "\\@"));
|
||||
|
||||
/** keywords whose first argument is a bookmark or sequence identifier */
|
||||
private static final Set<String> FIRST_ARG_IS_IDENTIFIER = new HashSet<String>(Arrays.asList(
|
||||
"REF", "PAGEREF", "NOTEREF", "ASK", "SET", "SEQ", "GOTOBUTTON"));
|
||||
|
||||
/** switches whose argument is a bookmark or sequence identifier */
|
||||
private static final Set<String> SWITCH_ARG_IS_IDENTIFIER = new HashSet<String>(Arrays.asList("\\l", "\\b", "\\c", "\\s"));
|
||||
|
||||
private static final Set<String> NO_ARGUMENT = new HashSet<String>(Arrays.asList(
|
||||
"FORMTEXT", "FORMCHECKBOX", "FORMDROPDOWN"));
|
||||
|
||||
private final Function<String, String> scrambler;
|
||||
private final Function<String, String> identifiers;
|
||||
private final Predicate<String> safeName;
|
||||
|
||||
/**
|
||||
* @param scrambler replaces the letters of an argument (digits and punctuation kept)
|
||||
* @param identifiers maps a bookmark or sequence name to its anonymised name
|
||||
* (returns null for a name it does not know)
|
||||
* @param safeName true for a name which may survive (a built-in style name for STYLEREF)
|
||||
*/
|
||||
public FieldInstructions(Function<String, String> scrambler, Function<String, String> identifiers,
|
||||
Predicate<String> safeName) {
|
||||
this.scrambler = scrambler;
|
||||
this.identifiers = identifiers;
|
||||
this.safeName = safeName;
|
||||
}
|
||||
|
||||
/** The keyword of an instruction ("=" for a formula), upper-cased, or null. */
|
||||
public static String keyword(String instr) {
|
||||
if (instr == null) return null;
|
||||
List<Token> tokens = tokenize(instr);
|
||||
return tokens.isEmpty() ? null : tokens.get(0).text.toUpperCase(Locale.ROOT);
|
||||
}
|
||||
|
||||
public String scrub(String instr) {
|
||||
if (instr == null || instr.trim().isEmpty()) return instr;
|
||||
|
||||
List<Token> tokens = tokenize(instr);
|
||||
if (tokens.isEmpty()) return instr;
|
||||
|
||||
String keyword = tokens.get(0).text.toUpperCase(Locale.ROOT);
|
||||
if (NO_ARGUMENT.contains(keyword)) return instr;
|
||||
|
||||
StringBuilder out = new StringBuilder();
|
||||
int pos = 0;
|
||||
String pendingSwitch = null; // the switch whose argument the next token is
|
||||
boolean firstArgSeen = false;
|
||||
|
||||
for (int i = 0; i < tokens.size(); i++) {
|
||||
Token t = tokens.get(i);
|
||||
out.append(instr, pos, t.start);
|
||||
pos = t.end;
|
||||
|
||||
String replacement;
|
||||
if (i == 0) {
|
||||
replacement = t.text; // the keyword
|
||||
} else if (t.kind == Kind.SWITCH) {
|
||||
out.append(t.text);
|
||||
pendingSwitch = t.text;
|
||||
continue;
|
||||
} else if (pendingSwitch != null && FORMAT_SWITCHES.contains(pendingSwitch)) {
|
||||
replacement = t.text; // a picture
|
||||
} else if (pendingSwitch != null && SWITCH_ARG_IS_IDENTIFIER.contains(pendingSwitch)
|
||||
&& (keyword.equals("HYPERLINK") || keyword.equals("TOC") || keyword.equals("SEQ"))) {
|
||||
replacement = identifier(t);
|
||||
} else if (keyword.equals("=")) {
|
||||
replacement = formulaToken(t);
|
||||
} else if (!firstArgSeen && pendingSwitch == null && FIRST_ARG_IS_IDENTIFIER.contains(keyword)) {
|
||||
replacement = identifier(t);
|
||||
} else if (!firstArgSeen && pendingSwitch == null && keyword.equals("STYLEREF")
|
||||
&& safeName.test(unquote(t.text))) {
|
||||
replacement = t.text;
|
||||
} else {
|
||||
replacement = scramble(t);
|
||||
}
|
||||
if (i > 0 && pendingSwitch == null) firstArgSeen = true;
|
||||
pendingSwitch = null;
|
||||
out.append(replacement);
|
||||
}
|
||||
out.append(instr, pos, instr.length());
|
||||
return out.toString();
|
||||
}
|
||||
|
||||
private String identifier(Token t) {
|
||||
String name = unquote(t.text);
|
||||
String mapped = identifiers.apply(name);
|
||||
if (mapped == null) return scramble(t);
|
||||
return requote(t.text, mapped);
|
||||
}
|
||||
|
||||
private static final java.util.regex.Pattern PLAIN_IDENTIFIER = java.util.regex.Pattern.compile("[\\p{L}_][\\p{L}\\p{N}_.]*");
|
||||
|
||||
/**
|
||||
* In a formula only bookmark names change: a plain identifier which is not a
|
||||
* function or keyword; operators, numbers, cell references and functions stay.
|
||||
*/
|
||||
private String formulaToken(Token t) {
|
||||
if (t.kind == Kind.QUOTED) return scramble(t);
|
||||
if (!PLAIN_IDENTIFIER.matcher(t.text).matches()) return t.text;
|
||||
if (KEYWORDS.contains(t.text.toUpperCase(Locale.ROOT))) return t.text;
|
||||
if (t.text.matches("[A-Za-z]{1,3}[0-9]+")) return t.text; // a cell reference
|
||||
String mapped = identifiers.apply(t.text);
|
||||
return mapped == null ? t.text : mapped;
|
||||
}
|
||||
|
||||
private String scramble(Token t) {
|
||||
String inner = unquote(t.text);
|
||||
return requote(t.text, scrambler.apply(inner));
|
||||
}
|
||||
|
||||
private static String unquote(String s) {
|
||||
if (s.length() >= 2 && s.charAt(0) == '"' && s.charAt(s.length() - 1) == '"') {
|
||||
return s.substring(1, s.length() - 1);
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
private static String requote(String original, String inner) {
|
||||
if (original.length() >= 2 && original.charAt(0) == '"' && original.charAt(original.length() - 1) == '"') {
|
||||
return "\"" + inner + "\"";
|
||||
}
|
||||
return inner;
|
||||
}
|
||||
|
||||
enum Kind { WORD, QUOTED, SWITCH }
|
||||
|
||||
static class Token {
|
||||
final int start, end;
|
||||
final String text;
|
||||
final Kind kind;
|
||||
Token(int start, int end, String text, Kind kind) {
|
||||
this.start = start; this.end = end; this.text = text; this.kind = kind;
|
||||
}
|
||||
}
|
||||
|
||||
/** Splits on whitespace, keeping quoted strings (with \" escapes) whole; a switch is a backslash plus one character. */
|
||||
static List<Token> tokenize(String s) {
|
||||
List<Token> tokens = new ArrayList<Token>();
|
||||
int i = 0, n = s.length();
|
||||
while (i < n) {
|
||||
char c = s.charAt(i);
|
||||
if (Character.isWhitespace(c)) { i++; continue; }
|
||||
int start = i;
|
||||
if (c == '"') {
|
||||
i++;
|
||||
while (i < n) {
|
||||
char d = s.charAt(i);
|
||||
if (d == '\\' && i + 1 < n) { i += 2; continue; }
|
||||
i++;
|
||||
if (d == '"') break;
|
||||
}
|
||||
tokens.add(new Token(start, i, s.substring(start, i), Kind.QUOTED));
|
||||
} else if (c == '\\' && i + 1 < n && !Character.isWhitespace(s.charAt(i + 1))) {
|
||||
i += 2;
|
||||
tokens.add(new Token(start, i, s.substring(start, i), Kind.SWITCH));
|
||||
} else {
|
||||
while (i < n && !Character.isWhitespace(s.charAt(i)) && s.charAt(i) != '"') i++;
|
||||
if (i == start) i++;
|
||||
tokens.add(new Token(start, i, s.substring(start, i), Kind.WORD));
|
||||
}
|
||||
}
|
||||
return tokens;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,304 @@
|
||||
/*
|
||||
* Copyright 2026, Plutext Pty Ltd.
|
||||
*
|
||||
* This file is part of docx4j.
|
||||
|
||||
docx4j is licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
||||
*/
|
||||
package org.docx4j.anon;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.IdentityHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import jakarta.xml.bind.JAXBElement;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* Visits every object reachable from a JAXB root through its public getters:
|
||||
* lists, JAXBElement wrappers, and every generated class (org.docx4j.*,
|
||||
* org.pptx4j.*, org.xlsx4j.*, org.glox4j.*, org.opendope.*).
|
||||
* <p>
|
||||
* The anonymiser uses this rather than {@link org.docx4j.TraversalUtil} because
|
||||
* the guarantee (CR-019) is "no text from the original in any text-bearing
|
||||
* part", and TraversalUtil's children rules are written for the content tree
|
||||
* (paragraphs, runs, tables, the drawings it knows): they do not reach
|
||||
* paragraph and run properties (where w:rPrChange and w:pPrChange carry
|
||||
* authors), settings, chart caches, diagram data, or any DrawingML text
|
||||
* TraversalUtil's handleGraphicData does not list. A reflective walk reaches
|
||||
* all of it, at the cost of some speed, which the anonymiser can afford.
|
||||
* <p>
|
||||
* Pre-order: a visitor sees a parent before its children, and sees each object
|
||||
* once (an identity set guards against cycles and against the convenience
|
||||
* getters which return an object also held in a list, such as
|
||||
* {@code SdtPr.getTag()}). DOM nodes ({@code xs:any} content) are handed to the
|
||||
* visitor and not descended into; the visitor decides what to do with them.
|
||||
*
|
||||
* @since 17.2.0
|
||||
*/
|
||||
public class JaxbGraphWalker {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(JaxbGraphWalker.class);
|
||||
|
||||
/** What the visitor wants done with the object it was just shown. */
|
||||
public enum Action {
|
||||
/** visit its children */
|
||||
CONTINUE,
|
||||
/** do not visit its children */
|
||||
SKIP_CHILDREN,
|
||||
/**
|
||||
* remove it from its parent: from the list it sits in, or by invoking the
|
||||
* setter matching the getter it came from with null; its children are not visited
|
||||
*/
|
||||
REMOVE,
|
||||
/**
|
||||
* replace it in the list it sits in with {@link Visitor#replacement()} (when it
|
||||
* came from a getter, it is set to null instead); its children are not visited
|
||||
*/
|
||||
REPLACE
|
||||
}
|
||||
|
||||
public interface Visitor {
|
||||
/**
|
||||
* @param o the object, unwrapped from any JAXBElement
|
||||
* @param wrapper the JAXBElement it came wrapped in, or null; its name tells
|
||||
* w:instrText from w:t (both are wml Text)
|
||||
*/
|
||||
Action visit(Object o, JAXBElement<?> wrapper);
|
||||
|
||||
/** The object to put in place of the one just answered with {@link Action#REPLACE}. */
|
||||
default Object replacement() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private final Visitor visitor;
|
||||
private final Set<Object> seen = Collections.newSetFromMap(new IdentityHashMap<Object, Boolean>());
|
||||
|
||||
public JaxbGraphWalker(Visitor visitor) {
|
||||
this.visitor = visitor;
|
||||
}
|
||||
|
||||
/** Walks from root (the root itself is visited too). */
|
||||
public void walk(Object root) {
|
||||
if (root == null) return;
|
||||
Object unwrapped = root;
|
||||
JAXBElement<?> wrapper = null;
|
||||
if (root instanceof JAXBElement) {
|
||||
wrapper = (JAXBElement<?>) root;
|
||||
unwrapped = wrapper.getValue();
|
||||
if (unwrapped == null) return;
|
||||
}
|
||||
Action a = visitUnseen(unwrapped, wrapper);
|
||||
if (a == Action.CONTINUE) {
|
||||
walkChildren(unwrapped);
|
||||
} else if (a == Action.REMOVE || a == Action.REPLACE) {
|
||||
log.warn(a + " of a root object is ignored: " + unwrapped.getClass().getName());
|
||||
}
|
||||
}
|
||||
|
||||
private Action visitUnseen(Object o, JAXBElement<?> wrapper) {
|
||||
if (!seen.add(o)) return Action.SKIP_CHILDREN;
|
||||
return visitor.visit(o, wrapper);
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
private void walkChildren(Object parent) {
|
||||
|
||||
if (parent instanceof List) {
|
||||
List list = (List) parent;
|
||||
// iterate a snapshot: the visitor may ask for removals
|
||||
for (Object child : new ArrayList<Object>(list)) {
|
||||
walkListChild(list, child);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isModelObject(parent)) return;
|
||||
|
||||
for (Method getter : accessors(parent.getClass())) {
|
||||
Object value;
|
||||
try {
|
||||
value = getter.invoke(parent);
|
||||
} catch (Exception e) {
|
||||
log.debug(getter + " threw " + e);
|
||||
continue;
|
||||
}
|
||||
if (value == null) continue;
|
||||
|
||||
if (value instanceof List) {
|
||||
List list = (List) value;
|
||||
for (Object child : new ArrayList<Object>(list)) {
|
||||
walkListChild(list, child);
|
||||
}
|
||||
} else {
|
||||
Object unwrapped = value;
|
||||
JAXBElement<?> wrapper = null;
|
||||
if (value instanceof JAXBElement) {
|
||||
wrapper = (JAXBElement<?>) value;
|
||||
unwrapped = wrapper.getValue();
|
||||
if (unwrapped == null) continue;
|
||||
}
|
||||
if (!isVisitable(unwrapped)) continue;
|
||||
Action a = visitUnseen(unwrapped, wrapper);
|
||||
if (a == Action.CONTINUE) {
|
||||
walkChildren(unwrapped);
|
||||
} else if (a == Action.REMOVE || a == Action.REPLACE) {
|
||||
setNull(parent, getter);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "rawtypes" })
|
||||
private void walkListChild(List list, Object child) {
|
||||
if (child == null) return;
|
||||
Object unwrapped = child;
|
||||
JAXBElement<?> wrapper = null;
|
||||
if (child instanceof JAXBElement) {
|
||||
wrapper = (JAXBElement<?>) child;
|
||||
unwrapped = wrapper.getValue();
|
||||
if (unwrapped == null) return;
|
||||
}
|
||||
if (!isVisitable(unwrapped)) return;
|
||||
Action a = visitUnseen(unwrapped, wrapper);
|
||||
if (a == Action.CONTINUE) {
|
||||
walkChildren(unwrapped);
|
||||
} else if (a == Action.REMOVE) {
|
||||
removeByIdentity(list, child);
|
||||
} else if (a == Action.REPLACE) {
|
||||
Object replacement = visitor.replacement();
|
||||
if (replacement == null) {
|
||||
removeByIdentity(list, child);
|
||||
} else {
|
||||
replaceByIdentity(list, child, replacement);
|
||||
seen.add(replacement instanceof JAXBElement ? ((JAXBElement<?>) replacement).getValue() : replacement);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
private static void replaceByIdentity(List list, Object child, Object replacement) {
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
if (list.get(i) == child) {
|
||||
list.set(i, replacement);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
private static void removeByIdentity(List list, Object child) {
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
if (list.get(i) == child) {
|
||||
list.remove(i);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void setNull(Object parent, Method getter) {
|
||||
String setterName = "set" + getter.getName().substring(3);
|
||||
try {
|
||||
Method setter = parent.getClass().getMethod(setterName, getter.getReturnType());
|
||||
setter.invoke(parent, new Object[] { null });
|
||||
} catch (Exception e) {
|
||||
log.warn("cannot null " + getter.getName() + " on " + parent.getClass().getName() + ": " + e);
|
||||
}
|
||||
}
|
||||
|
||||
/** Objects the visitor is shown: model objects, lists, DOM nodes, strings in mixed content. */
|
||||
private static boolean isVisitable(Object o) {
|
||||
return isModelObject(o)
|
||||
|| o instanceof List
|
||||
|| o instanceof org.w3c.dom.Node
|
||||
|| o instanceof String;
|
||||
}
|
||||
|
||||
/** A generated (or hand-written) content-model class: recurse into its getters. */
|
||||
static boolean isModelObject(Object o) {
|
||||
if (o == null) return false;
|
||||
String p = o.getClass().getName();
|
||||
return (p.startsWith("org.docx4j.")
|
||||
|| p.startsWith("org.pptx4j.")
|
||||
|| p.startsWith("org.xlsx4j.")
|
||||
|| p.startsWith("org.glox4j.")
|
||||
|| p.startsWith("org.opendope."))
|
||||
&& !p.startsWith("org.docx4j.openpackaging.")
|
||||
&& !p.startsWith("org.docx4j.anon.")
|
||||
&& !p.endsWith("ObjectFactory")
|
||||
&& !(o instanceof Enum);
|
||||
}
|
||||
|
||||
private static final Map<Class<?>, List<Method>> ACCESSORS = new ConcurrentHashMap<Class<?>, List<Method>>();
|
||||
|
||||
/**
|
||||
* The public no-arg getters of a class which can lead to further model objects
|
||||
* (anything but scalars), computed once per class.
|
||||
*/
|
||||
private static List<Method> accessors(Class<?> c) {
|
||||
List<Method> cached = ACCESSORS.get(c);
|
||||
if (cached != null) return cached;
|
||||
|
||||
// lists first: a convenience getter (SdtPr.getTag()) returns an object which also
|
||||
// sits in a list, and a removal must come off the list, not through a setter the
|
||||
// convenience getter may not have
|
||||
List<Method> lists = new ArrayList<Method>();
|
||||
List<Method> singles = new ArrayList<Method>();
|
||||
for (Method m : c.getMethods()) {
|
||||
if (m.getParameterCount() != 0) continue;
|
||||
if (Modifier.isStatic(m.getModifiers())) continue;
|
||||
if (m.isBridge() || m.isSynthetic()) continue;
|
||||
if (m.getDeclaringClass() == Object.class) continue;
|
||||
String name = m.getName();
|
||||
if (!name.startsWith("get") || name.length() < 4) continue;
|
||||
if (name.equals("getParent") || name.equals("getClass")) continue;
|
||||
Class<?> rt = m.getReturnType();
|
||||
if (isScalar(rt)) continue;
|
||||
if (List.class.isAssignableFrom(rt)) {
|
||||
lists.add(m);
|
||||
} else {
|
||||
singles.add(m);
|
||||
}
|
||||
}
|
||||
List<Method> result = new ArrayList<Method>(lists);
|
||||
result.addAll(singles);
|
||||
ACCESSORS.put(c, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
private static boolean isScalar(Class<?> rt) {
|
||||
return rt.isPrimitive()
|
||||
|| rt == String.class
|
||||
|| rt.isEnum()
|
||||
|| Number.class.isAssignableFrom(rt)
|
||||
|| rt == Boolean.class
|
||||
|| rt == Character.class
|
||||
|| rt == byte[].class
|
||||
|| rt.isArray()
|
||||
|| javax.xml.datatype.XMLGregorianCalendar.class.isAssignableFrom(rt)
|
||||
|| javax.xml.datatype.Duration.class.isAssignableFrom(rt)
|
||||
|| rt == javax.xml.namespace.QName.class
|
||||
|| rt == java.util.Map.class;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
* Copyright 2026, Plutext Pty Ltd.
|
||||
*
|
||||
* This file is part of docx4j.
|
||||
|
||||
docx4j is licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
||||
*/
|
||||
package org.docx4j.anon;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* The replacement vocabulary: the words of the classic <i>lorem ipsum</i> passage
|
||||
* (Cicero, <i>De finibus bonorum et malorum</i>, as the printers' filler has carried it
|
||||
* since the 1500s - public domain), from which {@link ScrambleText} draws the Latin
|
||||
* text it writes in place of a document's, and against which {@link Verify} tests
|
||||
* whether a surviving token could be the scrambler's own.
|
||||
* <p>
|
||||
* Until 17.2.1 this was com.thedeanda:lorem, the one dependency that kept the
|
||||
* anonymiser in a module of its own (and a filename-based automatic module under
|
||||
* JPMS). The scrambler only ever needed a run of pseudo-words with a natural
|
||||
* length distribution, and the check only ever needed the same list; one array
|
||||
* serves both, so the list cannot drift from the check.
|
||||
*
|
||||
* @since 17.2.1
|
||||
*/
|
||||
public final class Lorem {
|
||||
|
||||
private Lorem() {}
|
||||
|
||||
/** The distinct words of the passage (the filler and its Cicero source), lower case; the same list {@link Verify} allows. */
|
||||
public static final String[] WORDS = {
|
||||
"lorem", "ipsum", "dolor", "sit", "amet", "consectetur", "adipiscing", "elit", "sed", "do",
|
||||
"eiusmod", "tempor", "incididunt", "ut", "labore", "et", "dolore", "magna", "aliqua", "enim",
|
||||
"ad", "minim", "veniam", "quis", "nostrud", "exercitation", "ullamco", "laboris", "nisi",
|
||||
"aliquip", "ex", "ea", "commodo", "consequat", "duis", "aute", "irure", "in", "reprehenderit",
|
||||
"voluptate", "velit", "esse", "cillum", "eu", "fugiat", "nulla", "pariatur", "excepteur",
|
||||
"sint", "occaecat", "cupidatat", "non", "proident", "sunt", "culpa", "qui", "officia",
|
||||
"deserunt", "mollit", "anim", "id", "est", "laborum", "neque", "porro", "quisquam", "quia",
|
||||
"dolorem", "quaerat", "voluptatem", "aspernatur", "aut", "odit", "fugit", "consequuntur",
|
||||
"magni", "dolores", "eos", "ratione", "sequi", "nesciunt", "ipsam", "ipsa", "voluptas",
|
||||
"aliquam", "numquam", "eius", "modi", "tempora", "incidunt", "magnam", "quam", "nostrum",
|
||||
"exercitationem", "ullam", "corporis", "suscipit", "laboriosam", "aliquid", "commodi",
|
||||
"consequatur", "autem", "vel", "eum", "iure", "nihil", "molestiae", "illum", "quo", "at",
|
||||
"vero", "accusamus", "iusto", "odio", "dignissimos", "ducimus", "blanditiis", "praesentium",
|
||||
"voluptatum", "deleniti", "atque", "corrupti", "quos", "quas", "molestias", "excepturi",
|
||||
"obcaecati", "cupiditate", "provident", "similique", "mollitia", "animi", "dolorum", "fuga",
|
||||
"harum", "quidem", "rerum", "facilis", "expedita", "distinctio", "nam", "libero", "tempore",
|
||||
"cum", "soluta", "nobis", "eligendi", "optio", "cumque", "impedit", "minus", "quod", "maxime",
|
||||
"placeat", "facere", "possimus", "omnis", "assumenda", "temporibus", "quibusdam", "officiis",
|
||||
"debitis", "necessitatibus", "saepe", "eveniet", "voluptates", "repudiandae", "recusandae",
|
||||
"itaque", "earum", "hic", "tenetur", "sapiente", "delectus", "reiciendis", "maiores", "alias",
|
||||
"perferendis", "doloribus", "asperiores", "repellat" };
|
||||
|
||||
/**
|
||||
* @param count how many words
|
||||
* @return that many words drawn at random, separated by single spaces
|
||||
*/
|
||||
public static String words(int count, Random random) {
|
||||
StringBuilder sb = new StringBuilder(count * 7);
|
||||
for (int i = 0; i < count; i++) {
|
||||
if (i > 0) sb.append(' ');
|
||||
sb.append(WORDS[random.nextInt(WORDS.length)]);
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private static String joined;
|
||||
|
||||
/** true if the token (lower case) is a substring of some word of the list */
|
||||
public static synchronized boolean isFragment(String token) {
|
||||
if (joined == null) {
|
||||
StringBuilder sb = new StringBuilder(" ");
|
||||
for (String w : WORDS) sb.append(w).append(' ');
|
||||
joined = sb.toString();
|
||||
}
|
||||
return joined.contains(token);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,549 @@
|
||||
/*
|
||||
* Copyright 2026, Plutext Pty Ltd.
|
||||
*
|
||||
* This file is part of docx4j.
|
||||
|
||||
docx4j is licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
||||
*/
|
||||
package org.docx4j.anon;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import jakarta.xml.bind.JAXBElement;
|
||||
|
||||
import org.docx4j.XmlUtils;
|
||||
import org.docx4j.anon.JaxbGraphWalker.Action;
|
||||
import org.docx4j.openpackaging.parts.Part;
|
||||
import org.docx4j.w15.CTPerson;
|
||||
import org.docx4j.w15.CTPresenceInfo;
|
||||
import org.docx4j.w16cex.CTCommentExtensible;
|
||||
import org.docx4j.wml.CTAltChunk;
|
||||
import org.docx4j.wml.CTBookmark;
|
||||
import org.docx4j.wml.CTControl;
|
||||
import org.docx4j.wml.CTDataBinding;
|
||||
import org.docx4j.wml.CTDocProtect;
|
||||
import org.docx4j.wml.CTDocVars;
|
||||
import org.docx4j.wml.CTFFData;
|
||||
import org.docx4j.wml.CTFFName;
|
||||
import org.docx4j.wml.CTMailMerge;
|
||||
import org.docx4j.wml.CTMoveBookmark;
|
||||
import org.docx4j.wml.CTObject;
|
||||
import org.docx4j.wml.CTPictureBase;
|
||||
import org.docx4j.wml.CTRel;
|
||||
import org.docx4j.wml.CTSdtDate;
|
||||
import org.docx4j.wml.CTSimpleField;
|
||||
import org.docx4j.wml.CTTblPrBase;
|
||||
import org.docx4j.wml.CTTrackChange;
|
||||
import org.docx4j.wml.CTWriteProtection;
|
||||
import org.docx4j.wml.Comments;
|
||||
import org.docx4j.wml.FldChar;
|
||||
import org.docx4j.wml.FontRel;
|
||||
import org.docx4j.wml.Lvl;
|
||||
import org.docx4j.wml.Numbering;
|
||||
import org.docx4j.wml.P;
|
||||
import org.docx4j.wml.PPrBase;
|
||||
import org.docx4j.wml.Pict;
|
||||
import org.docx4j.wml.RStyle;
|
||||
import org.docx4j.wml.Style;
|
||||
import org.docx4j.wml.Tag;
|
||||
|
||||
/**
|
||||
* The second visitor of the anonymiser's walk: identities, references and the
|
||||
* markup that points at embedded objects.
|
||||
* <ul>
|
||||
* <li>Identity: every {@link CTTrackChange} (w:ins, w:del, w:moveFrom,
|
||||
* w:moveTo, every *Change, w:cellIns/cellDel/cellMerge, w:numberingChange, and
|
||||
* w:comment) has its author renamed and its date fixed; move bookmarks the same;
|
||||
* comment initials renamed; people.xml authors and presence; commentsExtensible
|
||||
* dates.</li>
|
||||
* <li>References: bookmark and form-field names renamed (consistently with
|
||||
* the field instructions {@link ScrambleText} scrubs); hyperlink tooltips,
|
||||
* anchors and document locations; content-control tags and data bindings
|
||||
* removed, date-picker values fixed; field data blobs removed; custom style ids
|
||||
* renamed at the definition and at every reference.</li>
|
||||
* <li>Settings: mail merge, document variables, attached template and printer
|
||||
* settings relationships removed; protection hashes and salts cleared (the
|
||||
* protection itself stays, with no password).</li>
|
||||
* <li>Embedded objects, in STRICT mode: w:altChunk, o:OLEObject, w:control,
|
||||
* c:externalData, cx:externalData and the font-table embed elements are removed
|
||||
* with the parts they point at ({@link Anonymize} removes the parts); in KEEP
|
||||
* mode they stay, and the part is reported as kept unsafe.</li>
|
||||
* <li>PresentationML (CR-019 phase 2): legacy comment dates fixed and comment
|
||||
* authors renamed (with their p15 presence info); the modify-verifier (password
|
||||
* hash) removed; in STRICT mode the embedded-font list, smart tags, ink content
|
||||
* parts, every media reference (a:videoFile, a:audioFile, a:quickTimeFile,
|
||||
* a:audioCd, a:wavAudioFile / a:snd, p14:media, the audio and video timing
|
||||
* nodes, transition sounds), ActiveX controls (p:control) and OLE objects are
|
||||
* removed - an OLE graphic frame becomes its own preview picture, which
|
||||
* {@link MediaReplacer} then labels "OLE object removed".</li>
|
||||
* </ul>
|
||||
*
|
||||
* @since 17.2.0
|
||||
*/
|
||||
public class MarkupScrubber implements JaxbGraphWalker.Visitor {
|
||||
|
||||
private final Names names;
|
||||
private final boolean strict;
|
||||
private final Consumer<String> notes;
|
||||
private String lastPersonAuthor;
|
||||
private String currentPartName = "";
|
||||
private Object replacement;
|
||||
|
||||
/**
|
||||
* "partName#relId" of every picture which stood in for a removed OLE object or
|
||||
* ActiveX control: {@link MediaReplacer} gives those the labelled placeholder
|
||||
* rather than the plain pixels.
|
||||
*/
|
||||
final Set<String> objectPreviews = new HashSet<String>();
|
||||
|
||||
/** The part being walked: relationship ids are per part. */
|
||||
public void setCurrentPart(Part part) {
|
||||
currentPartName = part == null ? "" : part.getPartName().getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object replacement() {
|
||||
return replacement;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param names the shared renamings
|
||||
* @param strict remove the markup of embedded objects (their parts go too)
|
||||
* @param notes receives one line per removal, for the report
|
||||
*/
|
||||
public MarkupScrubber(Names names, boolean strict, Consumer<String> notes) {
|
||||
this.names = names;
|
||||
this.strict = strict;
|
||||
this.notes = notes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Action visit(Object o, JAXBElement<?> wrapper) {
|
||||
|
||||
// ---- identity
|
||||
if (o instanceof CTTrackChange) {
|
||||
CTTrackChange tc = (CTTrackChange) o;
|
||||
tc.setAuthor(names.author(tc.getAuthor()));
|
||||
if (tc.getDate() != null) tc.setDate(Names.fixedDate());
|
||||
if (tc.getDateUtc() != null) tc.setDateUtc(null);
|
||||
if (o instanceof Comments.Comment) {
|
||||
Comments.Comment c = (Comments.Comment) o;
|
||||
c.setInitials(names.initials(c.getInitials()));
|
||||
}
|
||||
return Action.CONTINUE;
|
||||
}
|
||||
if (o instanceof CTMoveBookmark) {
|
||||
CTMoveBookmark mb = (CTMoveBookmark) o;
|
||||
mb.setAuthor(names.author(mb.getAuthor()));
|
||||
if (mb.getDate() != null) mb.setDate(Names.fixedDate());
|
||||
mb.setName(Names.identifier(mb.getName()));
|
||||
return Action.CONTINUE;
|
||||
}
|
||||
if (o instanceof CTPerson) {
|
||||
CTPerson p = (CTPerson) o;
|
||||
p.setAuthor(names.author(p.getAuthor()));
|
||||
lastPersonAuthor = p.getAuthor();
|
||||
if (p.getContact() != null) p.setContact(null);
|
||||
return Action.CONTINUE;
|
||||
}
|
||||
if (o instanceof CTPresenceInfo) {
|
||||
CTPresenceInfo pi = (CTPresenceInfo) o;
|
||||
pi.setProviderId("None");
|
||||
// the parent person was visited just before this (pre-order): the user id
|
||||
// (an email or directory id) becomes that person's anonymised name
|
||||
if (pi.getUserId() != null) pi.setUserId(lastPersonAuthor != null ? lastPersonAuthor : "Author");
|
||||
return Action.SKIP_CHILDREN;
|
||||
}
|
||||
if (o instanceof CTCommentExtensible) {
|
||||
CTCommentExtensible ce = (CTCommentExtensible) o;
|
||||
if (ce.getDateUtc() != null) ce.setDateUtc(Names.fixedDate());
|
||||
return Action.CONTINUE;
|
||||
}
|
||||
if (o instanceof org.pptx4j.pml.CTComment) {
|
||||
// a legacy PowerPoint comment: the author is an id into the authors part (kept); the
|
||||
// date goes; the text is ScrambleText's
|
||||
org.pptx4j.pml.CTComment c = (org.pptx4j.pml.CTComment) o;
|
||||
if (c.getDt() != null) c.setDt(Names.fixedDate());
|
||||
return Action.CONTINUE;
|
||||
}
|
||||
if (o instanceof org.pptx4j.pml.CTCommentAuthor) {
|
||||
org.pptx4j.pml.CTCommentAuthor a = (org.pptx4j.pml.CTCommentAuthor) o;
|
||||
a.setName(names.author(a.getName()));
|
||||
a.setInitials(names.initials(a.getInitials()));
|
||||
lastPersonAuthor = a.getName();
|
||||
return Action.CONTINUE; // its extLst may hold p15:presenceInfo
|
||||
}
|
||||
if (o instanceof org.pptx4j.com.microsoft.schemas.office.powerpoint.x2012.main.CTPresenceInfo) {
|
||||
org.pptx4j.com.microsoft.schemas.office.powerpoint.x2012.main.CTPresenceInfo pi =
|
||||
(org.pptx4j.com.microsoft.schemas.office.powerpoint.x2012.main.CTPresenceInfo) o;
|
||||
pi.setProviderId("None");
|
||||
if (pi.getUserId() != null) pi.setUserId(lastPersonAuthor != null ? lastPersonAuthor : "Author");
|
||||
return Action.SKIP_CHILDREN;
|
||||
}
|
||||
|
||||
// ---- references
|
||||
if (o instanceof CTBookmark) {
|
||||
CTBookmark b = (CTBookmark) o;
|
||||
b.setName(Names.identifier(b.getName()));
|
||||
return Action.SKIP_CHILDREN;
|
||||
}
|
||||
if (o instanceof CTFFName) {
|
||||
CTFFName n = (CTFFName) o;
|
||||
n.setVal(Names.identifier(n.getVal()));
|
||||
return Action.SKIP_CHILDREN;
|
||||
}
|
||||
if (o instanceof CTFFData) {
|
||||
return Action.CONTINUE; // its name, default, help text, entries are visited
|
||||
}
|
||||
if (o instanceof FldChar) {
|
||||
FldChar fc = (FldChar) o;
|
||||
if (fc.getFldData() != null) {
|
||||
fc.setFldData(null);
|
||||
notes.accept("w:fldData removed");
|
||||
}
|
||||
return Action.CONTINUE;
|
||||
}
|
||||
if (o instanceof CTSimpleField) {
|
||||
CTSimpleField f = (CTSimpleField) o;
|
||||
if (f.getFldData() != null) {
|
||||
f.setFldData(null);
|
||||
notes.accept("w:fldData removed");
|
||||
}
|
||||
return Action.CONTINUE;
|
||||
}
|
||||
if (o instanceof P.Hyperlink) {
|
||||
P.Hyperlink h = (P.Hyperlink) o;
|
||||
h.setTooltip(null);
|
||||
h.setDocLocation(null);
|
||||
if (h.getAnchor() != null) h.setAnchor(Names.identifier(h.getAnchor()));
|
||||
return Action.CONTINUE;
|
||||
}
|
||||
if (o instanceof Tag || o instanceof CTDataBinding) {
|
||||
return Action.REMOVE;
|
||||
}
|
||||
if (o instanceof CTSdtDate) {
|
||||
CTSdtDate d = (CTSdtDate) o;
|
||||
if (d.getFullDate() != null) d.setFullDate(Names.fixedDate());
|
||||
return Action.CONTINUE;
|
||||
}
|
||||
|
||||
// ---- style ids (the names are ScrambleText's)
|
||||
if (o instanceof Style) {
|
||||
Style s = (Style) o;
|
||||
s.setStyleId(names.styleId(s.getStyleId()));
|
||||
return Action.CONTINUE;
|
||||
}
|
||||
if (o instanceof Style.BasedOn) {
|
||||
Style.BasedOn r = (Style.BasedOn) o;
|
||||
r.setVal(names.styleId(r.getVal()));
|
||||
return Action.SKIP_CHILDREN;
|
||||
}
|
||||
if (o instanceof Style.Next) {
|
||||
Style.Next r = (Style.Next) o;
|
||||
r.setVal(names.styleId(r.getVal()));
|
||||
return Action.SKIP_CHILDREN;
|
||||
}
|
||||
if (o instanceof Style.Link) {
|
||||
Style.Link r = (Style.Link) o;
|
||||
r.setVal(names.styleId(r.getVal()));
|
||||
return Action.SKIP_CHILDREN;
|
||||
}
|
||||
if (o instanceof PPrBase.PStyle) {
|
||||
PPrBase.PStyle r = (PPrBase.PStyle) o;
|
||||
r.setVal(names.styleId(r.getVal()));
|
||||
return Action.SKIP_CHILDREN;
|
||||
}
|
||||
if (o instanceof RStyle) {
|
||||
RStyle r = (RStyle) o;
|
||||
r.setVal(names.styleId(r.getVal()));
|
||||
return Action.SKIP_CHILDREN;
|
||||
}
|
||||
if (o instanceof CTTblPrBase.TblStyle) {
|
||||
CTTblPrBase.TblStyle r = (CTTblPrBase.TblStyle) o;
|
||||
r.setVal(names.styleId(r.getVal()));
|
||||
return Action.SKIP_CHILDREN;
|
||||
}
|
||||
if (o instanceof Lvl.PStyle) {
|
||||
Lvl.PStyle r = (Lvl.PStyle) o;
|
||||
r.setVal(names.styleId(r.getVal()));
|
||||
return Action.SKIP_CHILDREN;
|
||||
}
|
||||
if (o instanceof Numbering.AbstractNum.NumStyleLink) {
|
||||
Numbering.AbstractNum.NumStyleLink r = (Numbering.AbstractNum.NumStyleLink) o;
|
||||
r.setVal(names.styleId(r.getVal()));
|
||||
return Action.SKIP_CHILDREN;
|
||||
}
|
||||
if (o instanceof Numbering.AbstractNum.StyleLink) {
|
||||
Numbering.AbstractNum.StyleLink r = (Numbering.AbstractNum.StyleLink) o;
|
||||
r.setVal(names.styleId(r.getVal()));
|
||||
return Action.SKIP_CHILDREN;
|
||||
}
|
||||
|
||||
// ---- settings
|
||||
if (o instanceof org.docx4j.wml.CTSettings) {
|
||||
// the document ids fingerprint a document across its copies
|
||||
org.docx4j.wml.CTSettings settings = (org.docx4j.wml.CTSettings) o;
|
||||
if (settings.getDocId14() != null) settings.getDocId14().setVal("00000001");
|
||||
if (settings.getDocId15() != null) settings.getDocId15().setVal("{00000000-0000-0000-0000-000000000001}");
|
||||
return Action.CONTINUE;
|
||||
}
|
||||
if (o instanceof CTMailMerge) {
|
||||
notes.accept("w:mailMerge removed");
|
||||
return Action.REMOVE;
|
||||
}
|
||||
if (o instanceof CTDocVars) {
|
||||
notes.accept("w:docVars removed");
|
||||
return Action.REMOVE;
|
||||
}
|
||||
if (o instanceof CTRel) {
|
||||
// w:attachedTemplate, w:printerSettings, mail-merge sources: relationships to
|
||||
// outside the package, or to a part STRICT removes
|
||||
notes.accept("settings relationship removed (attached template / printer settings / data source)");
|
||||
return Action.REMOVE;
|
||||
}
|
||||
if (o instanceof CTDocProtect) {
|
||||
CTDocProtect p = (CTDocProtect) o;
|
||||
p.setHash(null);
|
||||
p.setSalt(null);
|
||||
p.setCryptProviderType(null);
|
||||
p.setCryptAlgorithmClass(null);
|
||||
p.setCryptAlgorithmType(null);
|
||||
p.setCryptAlgorithmSid(null);
|
||||
p.setCryptSpinCount(null);
|
||||
p.setCryptProvider(null);
|
||||
p.setAlgIdExt(null);
|
||||
p.setAlgIdExtSource(null);
|
||||
p.setCryptProviderTypeExt(null);
|
||||
p.setCryptProviderTypeExtSource(null);
|
||||
return Action.SKIP_CHILDREN;
|
||||
}
|
||||
if (o instanceof org.pptx4j.pml.CTModifyVerifier) {
|
||||
// p:modifyVerifier: the hash and salt of the password to modify; nothing else in
|
||||
// it is structure, so the element goes (unlike w:documentProtection, whose
|
||||
// enforcement flags describe the document)
|
||||
notes.accept("p:modifyVerifier removed (password hash and salt)");
|
||||
return Action.REMOVE;
|
||||
}
|
||||
if (o instanceof CTWriteProtection) {
|
||||
CTWriteProtection p = (CTWriteProtection) o;
|
||||
p.setHash(null);
|
||||
p.setSalt(null);
|
||||
p.setCryptProviderType(null);
|
||||
p.setCryptAlgorithmClass(null);
|
||||
p.setCryptAlgorithmType(null);
|
||||
p.setCryptAlgorithmSid(null);
|
||||
p.setCryptSpinCount(null);
|
||||
p.setCryptProvider(null);
|
||||
p.setAlgIdExt(null);
|
||||
p.setAlgIdExtSource(null);
|
||||
p.setCryptProviderTypeExt(null);
|
||||
p.setCryptProviderTypeExtSource(null);
|
||||
return Action.SKIP_CHILDREN;
|
||||
}
|
||||
|
||||
// ---- embedded objects
|
||||
if (strict) {
|
||||
if (o instanceof CTPictureBase) {
|
||||
// w:object / w:pict: if it holds an OLE object or a control, its VML picture
|
||||
// is the object's footprint and gets the labelled placeholder
|
||||
CTPictureBase pict = (CTPictureBase) o;
|
||||
boolean holdsObject = (o instanceof CTObject && ((CTObject) o).getControl() != null)
|
||||
|| (o instanceof Pict && ((Pict) o).getControl() != null);
|
||||
for (Object child : pict.getAnyAndAny()) {
|
||||
if (XmlUtils.unwrap(child) instanceof org.docx4j.vml.officedrawing.CTOLEObject) holdsObject = true;
|
||||
}
|
||||
if (holdsObject) {
|
||||
for (Object child : pict.getAnyAndAny()) {
|
||||
Object shape = XmlUtils.unwrap(child);
|
||||
if (shape instanceof org.docx4j.vml.CTShape) {
|
||||
for (JAXBElement<?> el : ((org.docx4j.vml.CTShape) shape).getPathOrFormulasOrHandles()) {
|
||||
if (el.getValue() instanceof org.docx4j.vml.CTImageData) {
|
||||
org.docx4j.vml.CTImageData data = (org.docx4j.vml.CTImageData) el.getValue();
|
||||
String relId = data.getId() != null ? data.getId() : data.getRelid();
|
||||
if (relId != null) objectPreviews.add(currentPartName + "#" + relId);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return Action.CONTINUE;
|
||||
}
|
||||
if (o instanceof CTAltChunk) {
|
||||
notes.accept("w:altChunk replaced by a marker paragraph");
|
||||
replacement = Placeholders.altChunkRemoved();
|
||||
return Action.REPLACE;
|
||||
}
|
||||
if (o instanceof org.docx4j.vml.officedrawing.CTOLEObject) {
|
||||
notes.accept("o:OLEObject removed (its picture stays, as the labelled placeholder)");
|
||||
return Action.REMOVE;
|
||||
}
|
||||
if (o instanceof CTControl) {
|
||||
notes.accept("w:control removed");
|
||||
return Action.REMOVE;
|
||||
}
|
||||
if (o instanceof FontRel) {
|
||||
notes.accept("embedded font reference removed");
|
||||
return Action.REMOVE;
|
||||
}
|
||||
String simple = o.getClass().getSimpleName();
|
||||
if (simple.equals("CTExternalData")) {
|
||||
notes.accept("chart externalData removed");
|
||||
return Action.REMOVE;
|
||||
}
|
||||
if (simple.equals("CTGeoCache") || simple.equals("CTGeography")) {
|
||||
notes.accept("chart geography cache removed");
|
||||
return Action.REMOVE;
|
||||
}
|
||||
if (simple.equals("CTPivotSource") || simple.equals("CTPivotFmts")) {
|
||||
// a pivot chart's pivot table is gone (removed in a workbook, in another file for a
|
||||
// docx or pptx): the chart keeps its caches and cell references as a plain chart
|
||||
notes.accept("chart pivotSource removed");
|
||||
return Action.REMOVE;
|
||||
}
|
||||
|
||||
// ---- PresentationML
|
||||
if (o instanceof org.pptx4j.pml.CTEmbeddedFontList) {
|
||||
notes.accept("p:embeddedFontLst removed (the font data parts go with it)");
|
||||
return Action.REMOVE;
|
||||
}
|
||||
if (o instanceof org.pptx4j.pml.CTSmartTags) {
|
||||
notes.accept("p:smartTags removed");
|
||||
return Action.REMOVE;
|
||||
}
|
||||
if (o instanceof org.pptx4j.pml.CTRel) {
|
||||
// p:contentPart: ink, in a part the tool cannot read (its mc:Fallback picture stays)
|
||||
notes.accept("p:contentPart removed (ink)");
|
||||
return Action.REMOVE;
|
||||
}
|
||||
if (o instanceof org.pptx4j.pml.CTControlList) {
|
||||
// p:controls holds nothing but ActiveX controls (and mc branches of them)
|
||||
notes.accept("p:controls removed (ActiveX)");
|
||||
return Action.REMOVE;
|
||||
}
|
||||
if (o instanceof org.pptx4j.pml.CTControl) {
|
||||
notes.accept("p:control removed (ActiveX)");
|
||||
return Action.REMOVE;
|
||||
}
|
||||
if (o instanceof org.docx4j.dml.CTVideoFile
|
||||
|| o instanceof org.docx4j.dml.CTAudioFile
|
||||
|| o instanceof org.docx4j.dml.CTQuickTimeFile
|
||||
|| o instanceof org.docx4j.dml.CTAudioCD
|
||||
|| o instanceof org.docx4j.dml.CTEmbeddedWAVAudioFile) {
|
||||
// the media file (embedded: a part the tool cannot read; linked: an external
|
||||
// target) is gone; the picture the reference sat on stays as a picture
|
||||
notes.accept("media reference removed (" + o.getClass().getSimpleName().substring(2) + ")");
|
||||
return Action.REMOVE;
|
||||
}
|
||||
if (o instanceof org.pptx4j.pml.CTTransitionSoundAction) {
|
||||
notes.accept("p:transition sound removed");
|
||||
return Action.REMOVE;
|
||||
}
|
||||
if (o instanceof org.pptx4j.pml.CTTLMediaNodeAudio || o instanceof org.pptx4j.pml.CTTLMediaNodeVideo) {
|
||||
notes.accept("timing media node removed");
|
||||
return Action.REMOVE;
|
||||
}
|
||||
if (o instanceof org.pptx4j.pml.CTExtension
|
||||
&& holdsMedia(((org.pptx4j.pml.CTExtension) o).getAny())) {
|
||||
// p:extLst/p:ext holding p14:media: the whole ext goes (an empty p:ext is not valid)
|
||||
notes.accept("p14:media removed");
|
||||
return Action.REMOVE;
|
||||
}
|
||||
if (o instanceof org.docx4j.dml.CTOfficeArtExtension
|
||||
&& holdsMedia(((org.docx4j.dml.CTOfficeArtExtension) o).getAny())) {
|
||||
notes.accept("p14:media removed");
|
||||
return Action.REMOVE;
|
||||
}
|
||||
if (o instanceof org.pptx4j.pml.CTGraphicalObjectFrame) {
|
||||
return oleFrame((org.pptx4j.pml.CTGraphicalObjectFrame) o);
|
||||
}
|
||||
}
|
||||
|
||||
return Action.CONTINUE;
|
||||
}
|
||||
|
||||
private static boolean holdsMedia(Object any) {
|
||||
return XmlUtils.unwrap(any) instanceof org.pptx4j.com.microsoft.schemas.office.powerpoint.x2010.main.CTMedia;
|
||||
}
|
||||
|
||||
/**
|
||||
* A p:graphicFrame whose graphic data is a p:oleObj (in any mc branch): the
|
||||
* embedding part goes, so the frame is replaced by the object's preview picture
|
||||
* (the p:pic PowerPoint writes inside the mc:Fallback's p:oleObj), given the
|
||||
* frame's id and position, and the picture's relationship is recorded so
|
||||
* {@link MediaReplacer} labels it. A frame with no preview is removed.
|
||||
*/
|
||||
private Action oleFrame(org.pptx4j.pml.CTGraphicalObjectFrame frame) {
|
||||
|
||||
List<org.pptx4j.pml.CTOleObject> oles = new ArrayList<org.pptx4j.pml.CTOleObject>();
|
||||
if (frame.getGraphic() != null && frame.getGraphic().getGraphicData() != null) {
|
||||
collectOleObjects(frame.getGraphic().getGraphicData().getAny(), oles);
|
||||
}
|
||||
if (oles.isEmpty()) return Action.CONTINUE;
|
||||
|
||||
org.pptx4j.pml.Pic pic = null;
|
||||
for (org.pptx4j.pml.CTOleObject ole : oles) {
|
||||
if (ole.getPic() != null) pic = ole.getPic(); // the last one: PowerPoint's is in the mc:Fallback
|
||||
}
|
||||
if (pic == null) {
|
||||
notes.accept("p:oleObj removed with its frame (it had no preview picture)");
|
||||
return Action.REMOVE;
|
||||
}
|
||||
|
||||
org.docx4j.dml.CTNonVisualDrawingProps frameProps =
|
||||
frame.getNvGraphicFramePr() == null ? null : frame.getNvGraphicFramePr().getCNvPr();
|
||||
org.docx4j.dml.CTNonVisualDrawingProps props = new org.docx4j.dml.CTNonVisualDrawingProps();
|
||||
props.setId(frameProps == null ? 0 : frameProps.getId());
|
||||
props.setName("Object " + props.getId());
|
||||
if (pic.getNvPicPr() == null) pic.setNvPicPr(new org.pptx4j.pml.Pic.NvPicPr());
|
||||
pic.getNvPicPr().setCNvPr(props);
|
||||
if (pic.getNvPicPr().getCNvPicPr() == null) pic.getNvPicPr().setCNvPicPr(new org.docx4j.dml.CTNonVisualPictureProperties());
|
||||
if (pic.getNvPicPr().getNvPr() == null) pic.getNvPicPr().setNvPr(new org.pptx4j.pml.NvPr());
|
||||
if (pic.getSpPr() == null) pic.setSpPr(new org.docx4j.dml.CTShapeProperties());
|
||||
if (pic.getSpPr().getXfrm() == null) pic.getSpPr().setXfrm(frame.getXfrm());
|
||||
if (pic.getSpPr().getPrstGeom() == null && pic.getSpPr().getCustGeom() == null) {
|
||||
org.docx4j.dml.CTPresetGeometry2D rect = new org.docx4j.dml.CTPresetGeometry2D();
|
||||
rect.setPrst(org.docx4j.dml.STShapeType.RECT);
|
||||
rect.setAvLst(new org.docx4j.dml.CTGeomGuideList());
|
||||
pic.getSpPr().setPrstGeom(rect);
|
||||
}
|
||||
if (pic.getBlipFill() != null && pic.getBlipFill().getBlip() != null
|
||||
&& pic.getBlipFill().getBlip().getEmbed() != null) {
|
||||
objectPreviews.add(currentPartName + "#" + pic.getBlipFill().getBlip().getEmbed());
|
||||
}
|
||||
for (org.pptx4j.pml.CTOleObject ole : oles) {
|
||||
ole.setPic(null); // the picture now stands on its own
|
||||
}
|
||||
notes.accept("p:oleObj removed; its frame is now its preview picture, as the labelled placeholder");
|
||||
replacement = pic;
|
||||
return Action.REPLACE;
|
||||
}
|
||||
|
||||
private static void collectOleObjects(List<Object> any, List<org.pptx4j.pml.CTOleObject> into) {
|
||||
for (Object o : any) {
|
||||
Object u = XmlUtils.unwrap(o);
|
||||
if (u instanceof org.pptx4j.pml.CTOleObject) {
|
||||
into.add((org.pptx4j.pml.CTOleObject) u);
|
||||
} else if (u instanceof org.docx4j.mce.AlternateContent) {
|
||||
org.docx4j.mce.AlternateContent ac = (org.docx4j.mce.AlternateContent) u;
|
||||
for (org.docx4j.mce.AlternateContent.Choice c : ac.getChoice()) collectOleObjects(c.getAny(), into);
|
||||
if (ac.getFallback() != null) collectOleObjects(ac.getFallback().getAny(), into);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,318 @@
|
||||
/*
|
||||
* Copyright 2026, Plutext Pty Ltd.
|
||||
*
|
||||
* This file is part of docx4j.
|
||||
|
||||
docx4j is licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
||||
*/
|
||||
package org.docx4j.anon;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.net.URI;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
|
||||
import org.docx4j.openpackaging.Base;
|
||||
import org.docx4j.openpackaging.URIHelper;
|
||||
import org.docx4j.openpackaging.exceptions.Docx4JException;
|
||||
import org.docx4j.openpackaging.contenttype.ContentTypes;
|
||||
import org.docx4j.openpackaging.packages.OpcPackage;
|
||||
import org.docx4j.openpackaging.packages.PresentationMLPackage;
|
||||
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
|
||||
import org.docx4j.openpackaging.parts.DefaultXmlPart;
|
||||
import org.docx4j.openpackaging.parts.Part;
|
||||
import org.docx4j.openpackaging.parts.PartName;
|
||||
import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPart;
|
||||
import org.docx4j.openpackaging.parts.WordprocessingML.ImageBmpPart;
|
||||
import org.docx4j.openpackaging.parts.WordprocessingML.ImageGifPart;
|
||||
import org.docx4j.openpackaging.parts.WordprocessingML.ImageJpegPart;
|
||||
import org.docx4j.openpackaging.parts.WordprocessingML.ImagePngPart;
|
||||
import org.docx4j.openpackaging.parts.WordprocessingML.ImageTiffPart;
|
||||
import org.docx4j.openpackaging.parts.relationships.RelationshipsPart;
|
||||
import org.docx4j.openpackaging.parts.relationships.RelationshipsPart.AddPartBehaviour;
|
||||
import org.docx4j.relationships.Relationship;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* Replaces every image with a placeholder: raster parts (png, gif, jpeg, bmp,
|
||||
* tiff) have their bytes replaced in place with a 2x2 PNG (Word reads the
|
||||
* bytes, not the extension, and scales the pixels to the drawing's extent);
|
||||
* every other image class (EMF and WMF, which can carry text as glyph runs -
|
||||
* see CR-011 - EPS, JPEG XR, WebP) is removed and every relationship which
|
||||
* pointed at it retargeted to one shared placeholder PNG part, so the drawing
|
||||
* still shows a picture; an SVG part (XML, so possibly text) becomes a blank
|
||||
* 2x2 SVG.
|
||||
*
|
||||
* @since 17.2.0
|
||||
*/
|
||||
public class MediaReplacer {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(MediaReplacer.class);
|
||||
|
||||
/** a 2x2 PNG */
|
||||
static final byte[] PNG_IMAGE_DATA = Base64.getDecoder().decode(
|
||||
"iVBORw0KGgoAAAANSUhEUgAAAAIAAAACAgMAAAAP2OW3AAAADFBMVEUDAP//AAAA/wb//AAD4Tw1AAAACXBIWXMAAAsTAAALEwEAmpwYAAAADElEQVQI12NwYNgAAAF0APHJnpmVAAAAAElFTkSuQmCC");
|
||||
|
||||
static final String BLANK_SVG = "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"2\" height=\"2\" viewBox=\"0 0 2 2\"/>";
|
||||
|
||||
/** the shared placeholder's file name, under the package's media folder ({@link #placeholderPartName(OpcPackage)}) */
|
||||
public static final String PLACEHOLDER_FILE = "anon-placeholder.png";
|
||||
/** the labelled image a removed OLE object's or control's picture becomes */
|
||||
public static final String OBJECT_PLACEHOLDER_FILE = "anon-object-removed.png";
|
||||
|
||||
/** the placeholder's part name in a docx */
|
||||
public static final String PLACEHOLDER_PART_NAME = "/word/media/" + PLACEHOLDER_FILE;
|
||||
/** the labelled placeholder's part name in a docx */
|
||||
public static final String OBJECT_PLACEHOLDER_PART_NAME = "/word/media/" + OBJECT_PLACEHOLDER_FILE;
|
||||
|
||||
/** /word/media/ for a docx, /ppt/media/ for a pptx, /xl/media/ for an xlsx */
|
||||
static String mediaFolder(OpcPackage pkg) {
|
||||
if (pkg instanceof PresentationMLPackage) return "/ppt/media/";
|
||||
if (pkg instanceof WordprocessingMLPackage) return "/word/media/";
|
||||
if (pkg instanceof org.docx4j.openpackaging.packages.SpreadsheetMLPackage) return "/xl/media/";
|
||||
return "/media/";
|
||||
}
|
||||
|
||||
/** @since 17.2.1 */
|
||||
public static String placeholderPartName(OpcPackage pkg) {
|
||||
return mediaFolder(pkg) + PLACEHOLDER_FILE;
|
||||
}
|
||||
|
||||
/** @since 17.2.1 */
|
||||
public static String objectPlaceholderPartName(OpcPackage pkg) {
|
||||
return mediaFolder(pkg) + OBJECT_PLACEHOLDER_FILE;
|
||||
}
|
||||
|
||||
private final OpcPackage pkg;
|
||||
private final Set<String> objectPreviews;
|
||||
private ImagePngPart placeholder;
|
||||
private ImagePngPart objectPlaceholder;
|
||||
|
||||
public MediaReplacer(OpcPackage pkg) {
|
||||
this(pkg, Collections.<String>emptySet());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param pkg a docx or, since 17.2.1, a pptx
|
||||
* @param objectPreviews "partName#relId" of the pictures which stood in for a
|
||||
* removed OLE object or control: these get the labelled placeholder
|
||||
*/
|
||||
public MediaReplacer(OpcPackage pkg, Set<String> objectPreviews) {
|
||||
this.pkg = pkg;
|
||||
this.objectPreviews = objectPreviews;
|
||||
}
|
||||
|
||||
/** true if some relationship to this part is a removed object's picture */
|
||||
private boolean isObjectPreview(Part p) {
|
||||
if (objectPreviews.isEmpty()) return false;
|
||||
List<Base> sources = new ArrayList<Base>();
|
||||
sources.add(pkg);
|
||||
sources.addAll(pkg.getParts().getParts().values());
|
||||
for (Base source : sources) {
|
||||
RelationshipsPart rp = source.getRelationshipsPart();
|
||||
if (rp == null || rp.getRelationships() == null) continue;
|
||||
for (Relationship r : rp.getRelationships().getRelationship()) {
|
||||
if ("External".equals(r.getTargetMode())) continue;
|
||||
if (isObjectPreview(source, r) && rp.isTarget(p.getPartName(), r)) return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean isObjectPreview(Base source, Relationship r) {
|
||||
String sourceName = source instanceof Part ? ((Part) source).getPartName().getName() : "";
|
||||
return objectPreviews.contains(sourceName + "#" + r.getId());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return a line for the report saying what was done
|
||||
*/
|
||||
public String replace(Part p) throws Docx4JException {
|
||||
|
||||
if (p instanceof ImagePngPart
|
||||
|| p instanceof ImageGifPart
|
||||
|| p instanceof ImageJpegPart
|
||||
|| p instanceof ImageBmpPart
|
||||
|| p instanceof ImageTiffPart) {
|
||||
if (isObjectPreview(p)) {
|
||||
((BinaryPart) p).setBinaryData(Placeholders.objectRemovedPng());
|
||||
return "replaced with the labelled placeholder (it was a removed object's picture)";
|
||||
}
|
||||
((BinaryPart) p).setBinaryData(PNG_IMAGE_DATA);
|
||||
return "replaced with 2x2 pixels";
|
||||
}
|
||||
|
||||
if (p instanceof DefaultXmlPart) {
|
||||
// an SVG
|
||||
try {
|
||||
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
|
||||
dbf.setNamespaceAware(true);
|
||||
((DefaultXmlPart) p).setDocument(
|
||||
dbf.newDocumentBuilder().parse(new ByteArrayInputStream(BLANK_SVG.getBytes(StandardCharsets.UTF_8))));
|
||||
} catch (Exception e) {
|
||||
throw new Docx4JException("blank svg", e);
|
||||
}
|
||||
return "replaced with a blank svg";
|
||||
}
|
||||
|
||||
// a metafile, eps, jpeg xr, webp, or an image docx4j could not read:
|
||||
// point every relationship at the shared placeholder png (the labelled one where
|
||||
// the picture stood for a removed object) and drop the part
|
||||
boolean object = retargetToPlaceholder(p);
|
||||
return "relationships retargeted to " + (object ? objectPlaceholderPartName(pkg) : placeholderPartName(pkg)) + "; part removed";
|
||||
}
|
||||
|
||||
/** @return true if any relationship went to the labelled object placeholder */
|
||||
private boolean retargetToPlaceholder(Part p) throws Docx4JException {
|
||||
|
||||
boolean object = false;
|
||||
PartName oldName = p.getPartName();
|
||||
|
||||
List<Base> sources = new ArrayList<Base>();
|
||||
sources.add(pkg);
|
||||
sources.addAll(pkg.getParts().getParts().values());
|
||||
for (Base source : sources) {
|
||||
RelationshipsPart rp = source.getRelationshipsPart();
|
||||
if (rp == null || rp.getRelationships() == null) continue;
|
||||
// a snapshot: creating a placeholder part adds a relationship to the main document part
|
||||
for (Relationship r : new ArrayList<Relationship>(rp.getRelationships().getRelationship())) {
|
||||
if ("External".equals(r.getTargetMode())) continue;
|
||||
if (rp.isTarget(oldName, r)) {
|
||||
boolean preview = isObjectPreview(source, r);
|
||||
ImagePngPart png = preview ? objectPlaceholder() : placeholder();
|
||||
object |= preview;
|
||||
r.setTarget(relativize(rp.getSourceURI(), png.getPartName().getURI()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pkg.getParts().remove(oldName);
|
||||
pkg.getContentTypeManager().removeOverrideContentType(oldName);
|
||||
return object;
|
||||
}
|
||||
|
||||
private ImagePngPart placeholder() throws Docx4JException {
|
||||
if (placeholder == null) placeholder = placeholderPart(placeholderPartName(pkg), PNG_IMAGE_DATA);
|
||||
return placeholder;
|
||||
}
|
||||
|
||||
private ImagePngPart objectPlaceholder() throws Docx4JException {
|
||||
if (objectPlaceholder == null) objectPlaceholder = placeholderPart(objectPlaceholderPartName(pkg), Placeholders.objectRemovedPng());
|
||||
return objectPlaceholder;
|
||||
}
|
||||
|
||||
private ImagePngPart placeholderPart(String name, byte[] bytes) throws Docx4JException {
|
||||
Part existing = pkg.getParts().get(new PartName(name));
|
||||
if (existing instanceof ImagePngPart) return (ImagePngPart) existing;
|
||||
ImagePngPart part = new ImagePngPart(new PartName(name));
|
||||
part.setBinaryData(bytes);
|
||||
if (pkg instanceof WordprocessingMLPackage) {
|
||||
// registers the part, its content type and a relationship from the main document part
|
||||
((WordprocessingMLPackage) pkg).getMainDocumentPart().addTargetPart(part, AddPartBehaviour.REUSE_EXISTING);
|
||||
} else {
|
||||
// a pptx or xlsx: the retargeted relationships (from the slides or drawings) are the
|
||||
// ones which reach it; the presentation and workbook parts carry no image relationships
|
||||
part.setPackage(pkg);
|
||||
pkg.getParts().put(part);
|
||||
pkg.getContentTypeManager().addDefaultContentType("png", ContentTypes.IMAGE_PNG);
|
||||
}
|
||||
return part;
|
||||
}
|
||||
|
||||
static String relativize(URI source, URI target) {
|
||||
String rel = URIHelper.relativizeURI(source, target).toString();
|
||||
if (source.getPath().equals("/") && rel.startsWith("/")) {
|
||||
rel = rel.substring(1);
|
||||
}
|
||||
return rel;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a part from the package: every relationship which points at it (from
|
||||
* the package or any part), its own targets recursively, its content-type
|
||||
* override, and the part itself.
|
||||
*
|
||||
* @return the names of the parts removed (the part and, recursively, its targets)
|
||||
*/
|
||||
static List<PartName> removePart(OpcPackage pkg, Part p) {
|
||||
|
||||
List<PartName> removed = new ArrayList<PartName>();
|
||||
PartName name = p.getPartName();
|
||||
|
||||
List<Base> sources = new ArrayList<Base>();
|
||||
sources.add(pkg);
|
||||
sources.addAll(pkg.getParts().getParts().values());
|
||||
for (Base source : sources) {
|
||||
RelationshipsPart rp = source.getRelationshipsPart();
|
||||
if (rp == null || rp.getRelationships() == null) continue;
|
||||
List<Relationship> toGo = new ArrayList<Relationship>();
|
||||
for (Relationship r : rp.getRelationships().getRelationship()) {
|
||||
if ("External".equals(r.getTargetMode())) continue;
|
||||
try {
|
||||
if (rp.isTarget(name, r)) toGo.add(r);
|
||||
} catch (Exception e) {
|
||||
log.debug("cannot resolve " + r.getTarget() + ": " + e);
|
||||
}
|
||||
}
|
||||
for (Relationship r : toGo) {
|
||||
rp.removeRelationship(r);
|
||||
}
|
||||
}
|
||||
|
||||
// the part's own targets go with it (an embedded package's, an ActiveX part's binary)
|
||||
if (p.getRelationshipsPart() != null && p.getRelationshipsPart().getRelationships() != null) {
|
||||
for (Relationship r : new ArrayList<Relationship>(p.getRelationshipsPart().getRelationships().getRelationship())) {
|
||||
if ("External".equals(r.getTargetMode())) continue;
|
||||
Part target = p.getRelationshipsPart().getPart(r);
|
||||
if (target != null && target != p && stillReferencedElsewhere(pkg, target, p) == false) {
|
||||
removed.addAll(removePart(pkg, target));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pkg.getParts().remove(name);
|
||||
pkg.getContentTypeManager().removeOverrideContentType(name);
|
||||
removed.add(name);
|
||||
return removed;
|
||||
}
|
||||
|
||||
/** true if some part other than {@code except} (or the package) has a relationship to {@code target} */
|
||||
private static boolean stillReferencedElsewhere(OpcPackage pkg, Part target, Part except) {
|
||||
List<Base> sources = new ArrayList<Base>();
|
||||
sources.add(pkg);
|
||||
sources.addAll(pkg.getParts().getParts().values());
|
||||
for (Base source : sources) {
|
||||
if (source == except) continue;
|
||||
RelationshipsPart rp = source.getRelationshipsPart();
|
||||
if (rp == null || rp.getRelationships() == null) continue;
|
||||
for (Relationship r : rp.getRelationships().getRelationship()) {
|
||||
if ("External".equals(r.getTargetMode())) continue;
|
||||
try {
|
||||
if (rp.isTarget(target.getPartName(), r)) return true;
|
||||
} catch (Exception e) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
/*
|
||||
* Copyright 2026, Plutext Pty Ltd.
|
||||
*
|
||||
* This file is part of docx4j.
|
||||
|
||||
docx4j is licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
||||
*/
|
||||
package org.docx4j.anon;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.docx4j.docProps.core.CoreProperties;
|
||||
import org.docx4j.docProps.core.dc.elements.SimpleLiteral;
|
||||
import org.docx4j.docProps.coverPageProps.CoverPageProperties;
|
||||
import org.docx4j.docProps.extended.Properties;
|
||||
import org.docx4j.openpackaging.Base;
|
||||
import org.docx4j.openpackaging.exceptions.Docx4JException;
|
||||
import org.docx4j.openpackaging.packages.OpcPackage;
|
||||
import org.docx4j.openpackaging.parts.DocPropsCoverPagePart;
|
||||
import org.docx4j.openpackaging.parts.Part;
|
||||
import org.docx4j.openpackaging.parts.relationships.RelationshipsPart;
|
||||
import org.docx4j.relationships.Relationship;
|
||||
|
||||
/**
|
||||
* The package-level metadata: docProps core and app, the cover-page properties,
|
||||
* and every relationship which points outside the package.
|
||||
*
|
||||
* @since 17.2.0
|
||||
*/
|
||||
public class MetadataScrubber {
|
||||
|
||||
/** what an external target becomes; RFC 6761 reserves .invalid, so it resolves nowhere */
|
||||
public static final String EXTERNAL_PLACEHOLDER = "http://example.invalid/";
|
||||
|
||||
private final OpcPackage pkg;
|
||||
private int externals = 0;
|
||||
|
||||
public MetadataScrubber(OpcPackage pkg) {
|
||||
this.pkg = pkg;
|
||||
}
|
||||
|
||||
/**
|
||||
* docProps/core.xml: every descriptive property and identity cleared, the dates
|
||||
* fixed, the revision reset; docProps/app.xml: company, manager, template, the
|
||||
* heading and part titles (which are the document's headings), the hyperlink
|
||||
* list and base, the signature; the cover-page part's company details.
|
||||
*/
|
||||
public void scrubDocProps() throws Docx4JException {
|
||||
|
||||
if (pkg.getDocPropsCorePart() != null && pkg.getDocPropsCorePart().getContents() != null) {
|
||||
CoreProperties core = pkg.getDocPropsCorePart().getContents();
|
||||
core.setCategory(null);
|
||||
core.setContentStatus(null);
|
||||
core.setCreator(null);
|
||||
core.setDescription(null);
|
||||
core.setIdentifier(null);
|
||||
core.setKeywords(null);
|
||||
core.setSubject(null);
|
||||
core.setTitle(null);
|
||||
core.setLastModifiedBy(null);
|
||||
core.setLastPrinted(null);
|
||||
core.setVersion(null);
|
||||
if (core.getRevision() != null) core.setRevision("1");
|
||||
if (core.getCreated() != null) core.setCreated(fixedDateLiteral(core.getCreated()));
|
||||
if (core.getModified() != null) core.setModified(fixedDateLiteral(core.getModified()));
|
||||
}
|
||||
|
||||
if (pkg.getDocPropsExtendedPart() != null && pkg.getDocPropsExtendedPart().getContents() != null) {
|
||||
Properties app = pkg.getDocPropsExtendedPart().getContents();
|
||||
app.setCompany(null);
|
||||
app.setManager(null);
|
||||
app.setTemplate(null);
|
||||
app.setHeadingPairs(null);
|
||||
app.setTitlesOfParts(null);
|
||||
app.setHyperlinkBase(null);
|
||||
app.setHLinks(null);
|
||||
app.setDigSig(null);
|
||||
app.setTotalTime(null);
|
||||
app.setPresentationFormat(null); // pptx: "Widescreen", "On-screen Show (4:3)" - words which would survive the check
|
||||
}
|
||||
|
||||
for (Part p : new ArrayList<Part>(pkg.getParts().getParts().values())) {
|
||||
if (p instanceof DocPropsCoverPagePart) {
|
||||
CoverPageProperties cpp = ((DocPropsCoverPagePart) p).getContents();
|
||||
if (cpp != null) {
|
||||
cpp.setAbstract(null);
|
||||
cpp.setCompanyAddress(null);
|
||||
cpp.setCompanyEmail(null);
|
||||
cpp.setCompanyFax(null);
|
||||
cpp.setCompanyPhone(null);
|
||||
cpp.setPublishDate(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** the same literal (it carries xsi:type="dcterms:W3CDTF"), with the fixed date as its content */
|
||||
private static SimpleLiteral fixedDateLiteral(SimpleLiteral existing) {
|
||||
existing.getContent().clear();
|
||||
existing.getContent().add(Names.FIXED_DATE.toXMLFormat());
|
||||
return existing;
|
||||
}
|
||||
|
||||
/**
|
||||
* Every relationship of {@code TargetMode="External"}, in the package's own
|
||||
* relationships and in every part's (hyperlinks, linked images, linked OLE,
|
||||
* the attached template, mail-merge sources), gets a placeholder target,
|
||||
* numbered so distinct targets stay distinct.
|
||||
*
|
||||
* @return how many were replaced
|
||||
*/
|
||||
public int scrubExternalTargets() {
|
||||
externals = 0;
|
||||
scrubExternalTargets(pkg);
|
||||
for (Part p : new ArrayList<Part>(pkg.getParts().getParts().values())) {
|
||||
scrubExternalTargets(p);
|
||||
}
|
||||
return externals;
|
||||
}
|
||||
|
||||
private void scrubExternalTargets(Base base) {
|
||||
RelationshipsPart rp = base.getRelationshipsPart();
|
||||
if (rp == null || rp.getRelationships() == null) return;
|
||||
List<Relationship> rels = rp.getRelationships().getRelationship();
|
||||
for (Relationship r : rels) {
|
||||
if ("External".equals(r.getTargetMode())) {
|
||||
externals++;
|
||||
r.setTarget(EXTERNAL_PLACEHOLDER + externals);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
/*
|
||||
* Copyright 2026, Plutext Pty Ltd.
|
||||
*
|
||||
* This file is part of docx4j.
|
||||
|
||||
docx4j is licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
||||
*/
|
||||
package org.docx4j.anon;
|
||||
|
||||
import org.w3c.dom.Attr;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.NamedNodeMap;
|
||||
import org.w3c.dom.Node;
|
||||
import org.w3c.dom.NodeList;
|
||||
|
||||
/**
|
||||
* The 2018 comments docx4j does not bind, scrubbed as DOM in place:
|
||||
* <ul>
|
||||
* <li>PowerPoint's ([MS-PPTX] 2.3, namespace
|
||||
* {@code http://schemas.microsoft.com/office/powerpoint/2018/8/main}, prefix
|
||||
* p188): {@code /ppt/authors.xml} ({@code p188:authorLst}) and
|
||||
* {@code /ppt/comments/modernComment_*.xml} ({@code p188:cmLst}) - an author's
|
||||
* {@code name} and {@code initials} through {@link Names} (the same
|
||||
* {@code Author n} as everywhere else), its {@code userId} (a directory id or
|
||||
* email) replaced by that name and its {@code providerId} by {@code None}; every
|
||||
* {@code created}, {@code startDate} and {@code dueDate} fixed; the text of every
|
||||
* {@code a:t} scrambled. Comment ids, author ids, positions and the anchors
|
||||
* ({@code ac:txMkLst}) are structure and stay.</li>
|
||||
* <li>Excel's threaded comments ([MS-XLSX], namespace
|
||||
* {@code http://schemas.microsoft.com/office/spreadsheetml/2018/threadedcomments}):
|
||||
* {@code /xl/persons/person.xml} ({@code personList/person}: {@code displayName}
|
||||
* through {@link Names}, {@code userId} → the name, {@code providerId} → None)
|
||||
* and {@code /xl/threadedComments/threadedComment*.xml}
|
||||
* ({@code threadedComment}: {@code dT} fixed, the {@code text} scrambled; the
|
||||
* cell reference, ids, parent ids, mentions and done flags stay).</li>
|
||||
* </ul>
|
||||
* Anything else with text is left for {@link Verify} to find.
|
||||
*
|
||||
* @since 17.2.1
|
||||
*/
|
||||
public class ModernCommentsScrubber {
|
||||
|
||||
private final Names names;
|
||||
private final ScrambleText scrambler;
|
||||
|
||||
public ModernCommentsScrubber(Names names, ScrambleText scrambler) {
|
||||
this.names = names;
|
||||
this.scrambler = scrambler;
|
||||
}
|
||||
|
||||
public void scrub(Node n) {
|
||||
if (n.getNodeType() == Node.ELEMENT_NODE) {
|
||||
Element el = (Element) n;
|
||||
String mappedAuthor = null;
|
||||
NamedNodeMap attrs = el.getAttributes();
|
||||
// the name first, so the user id can take the mapped name
|
||||
Attr name = attribute(attrs, local(el).equals("person") ? "displayName" : "name");
|
||||
if (name != null && (local(el).equals("author") || local(el).equals("person"))) {
|
||||
mappedAuthor = names.author(name.getValue());
|
||||
name.setValue(mappedAuthor);
|
||||
}
|
||||
for (int i = 0; attrs != null && i < attrs.getLength(); i++) {
|
||||
Attr a = (Attr) attrs.item(i);
|
||||
String ln = local(a);
|
||||
if (ln.equals("initials")) {
|
||||
a.setValue(names.initials(a.getValue()));
|
||||
} else if (ln.equals("userId")) {
|
||||
a.setValue(mappedAuthor != null ? mappedAuthor : "Author");
|
||||
} else if (ln.equals("providerId")) {
|
||||
a.setValue("None");
|
||||
} else if (ln.equals("created") || ln.equals("startDate") || ln.equals("dueDate") || ln.equals("dT")) {
|
||||
a.setValue(Names.FIXED_DATE.toXMLFormat());
|
||||
}
|
||||
}
|
||||
}
|
||||
NodeList children = n.getChildNodes();
|
||||
for (int i = 0; i < children.getLength(); i++) {
|
||||
Node c = children.item(i);
|
||||
if (c.getNodeType() == Node.TEXT_NODE || c.getNodeType() == Node.CDATA_SECTION_NODE) {
|
||||
if (local(n).equals("t") || local(n).equals("text")) {
|
||||
c.setNodeValue(scrambler.scramble(c.getNodeValue()));
|
||||
}
|
||||
} else {
|
||||
scrub(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static Attr attribute(NamedNodeMap attrs, String localName) {
|
||||
for (int i = 0; attrs != null && i < attrs.getLength(); i++) {
|
||||
Attr a = (Attr) attrs.item(i);
|
||||
if (local(a).equals(localName)) return a;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static String local(Node n) {
|
||||
return n.getLocalName() == null ? n.getNodeName() : n.getLocalName();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,204 @@
|
||||
/*
|
||||
* Copyright 2026, Plutext Pty Ltd.
|
||||
*
|
||||
* This file is part of docx4j.
|
||||
|
||||
docx4j is licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
||||
*/
|
||||
package org.docx4j.anon;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.xml.datatype.DatatypeConfigurationException;
|
||||
import javax.xml.datatype.DatatypeFactory;
|
||||
import javax.xml.datatype.XMLGregorianCalendar;
|
||||
|
||||
import org.docx4j.openpackaging.packages.OpcPackage;
|
||||
import org.docx4j.openpackaging.packages.SpreadsheetMLPackage;
|
||||
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
|
||||
import org.docx4j.openpackaging.parts.WordprocessingML.StyleDefinitionsPart;
|
||||
import org.docx4j.wml.Style;
|
||||
import org.docx4j.wml.Styles;
|
||||
|
||||
/**
|
||||
* The consistent renamings the anonymiser applies, shared by its visitors:
|
||||
* <ul>
|
||||
* <li>authors become {@code Author 1}, {@code Author 2}... in order of first
|
||||
* appearance, the same name wherever it occurs (comments, tracked changes,
|
||||
* people.xml), so who-replied-to-whom and which changes are one reviewer's
|
||||
* survive (CR-019 decision 2); initials become {@code A1}, {@code A2}...;</li>
|
||||
* <li>a bookmark, form-field or sequence name becomes {@code bm} plus a hash
|
||||
* of the name: no pre-pass is needed for a REF in a header to still point at
|
||||
* a bookmark in the body;</li>
|
||||
* <li>a custom style id becomes {@code s} plus a hash; built-in styles (a name
|
||||
* Word knows: KnownStyles.xml plus the document's own latent-style list) keep
|
||||
* their names and ids, because Word identifies them by name;</li>
|
||||
* <li>every date becomes {@link #FIXED_DATE};</li>
|
||||
* <li>in a workbook (CR-019 phase 3), sheets become {@code Sheet1}, {@code Sheet2}...
|
||||
* in workbook order (read up front, so a formula in any part maps the same way)
|
||||
* and a defined name becomes {@code n_} plus a hash - an underscore keeps it from
|
||||
* ever reading as a cell reference; the {@code _xlnm.} built-ins keep their names.</li>
|
||||
* </ul>
|
||||
*
|
||||
* @since 17.2.0
|
||||
*/
|
||||
public class Names {
|
||||
|
||||
/** what every w:date, dateUtc and docProps date becomes */
|
||||
public static final XMLGregorianCalendar FIXED_DATE;
|
||||
static {
|
||||
try {
|
||||
FIXED_DATE = DatatypeFactory.newInstance().newXMLGregorianCalendar("2000-01-01T00:00:00Z");
|
||||
} catch (DatatypeConfigurationException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static XMLGregorianCalendar fixedDate() {
|
||||
return (XMLGregorianCalendar) FIXED_DATE.clone();
|
||||
}
|
||||
|
||||
private final Map<String, String> authors = new LinkedHashMap<String, String>();
|
||||
private final Map<String, String> initials = new LinkedHashMap<String, String>();
|
||||
private final Set<String> builtInStyleNames = new HashSet<String>();
|
||||
private final Set<String> builtInStyleIds = new HashSet<String>();
|
||||
/** sheet name (lower-cased: Excel matches them case-insensitively) to Sheet<n> */
|
||||
private final Map<String, String> sheets = new LinkedHashMap<String, String>();
|
||||
|
||||
/**
|
||||
* @param pkg a docx (its own latent-style names join the built-in list) or, since
|
||||
* 17.2.1, a pptx (no styles part: KnownStyles.xml alone)
|
||||
*/
|
||||
public Names(OpcPackage pkg) {
|
||||
for (Style s : StyleDefinitionsPart.getKnownStyles().values()) {
|
||||
if (s.getName() != null) builtInStyleNames.add(key(s.getName().getVal()));
|
||||
builtInStyleIds.add(s.getStyleId());
|
||||
}
|
||||
if (pkg instanceof WordprocessingMLPackage
|
||||
&& ((WordprocessingMLPackage) pkg).getMainDocumentPart() != null
|
||||
&& ((WordprocessingMLPackage) pkg).getMainDocumentPart().getStyleDefinitionsPart() != null) {
|
||||
try {
|
||||
Styles styles = ((WordprocessingMLPackage) pkg).getMainDocumentPart().getStyleDefinitionsPart().getContents();
|
||||
if (styles != null && styles.getLatentStyles() != null) {
|
||||
for (Styles.LatentStyles.LsdException e : styles.getLatentStyles().getLsdException()) {
|
||||
builtInStyleNames.add(key(e.getName()));
|
||||
}
|
||||
}
|
||||
if (styles != null) {
|
||||
for (Style s : styles.getStyle()) {
|
||||
if (s.getName() != null && isBuiltInStyleName(s.getName().getVal())) {
|
||||
builtInStyleIds.add(s.getStyleId());
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// no styles part contents: only KnownStyles then
|
||||
}
|
||||
}
|
||||
if (pkg instanceof SpreadsheetMLPackage && ((SpreadsheetMLPackage) pkg).getWorkbookPart() != null) {
|
||||
try {
|
||||
org.xlsx4j.sml.Workbook wb = ((SpreadsheetMLPackage) pkg).getWorkbookPart().getContents();
|
||||
if (wb != null && wb.getSheets() != null) {
|
||||
int n = 0;
|
||||
for (org.xlsx4j.sml.Sheet sheet : wb.getSheets().getSheet()) {
|
||||
n++;
|
||||
if (sheet.getName() != null) sheets.put(key(sheet.getName()), "Sheet" + n);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// no workbook contents: sheet names are then scrambled like any other text
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Sheet<n> for a sheet of the workbook (by name, case-insensitively); null for an unknown name. */
|
||||
public String sheet(String name) {
|
||||
if (name == null) return null;
|
||||
return sheets.get(key(name));
|
||||
}
|
||||
|
||||
/** true if the workbook has a sheet of this name */
|
||||
public boolean isSheet(String name) {
|
||||
return name != null && sheets.containsKey(key(name));
|
||||
}
|
||||
|
||||
/** The anonymised defined name: the _xlnm. built-ins as they are, else n_ + hash. */
|
||||
public static String definedName(String name) {
|
||||
if (name == null) return null;
|
||||
if (name.startsWith("_xlnm.") || name.startsWith("_xlfn.")) return name;
|
||||
return "n_" + hash(name);
|
||||
}
|
||||
|
||||
private static String key(String name) {
|
||||
return name == null ? "" : name.trim().toLowerCase(Locale.ROOT);
|
||||
}
|
||||
|
||||
/** A style name Word ships (heading 1, Table Grid, ...): kept, since Word identifies built-in styles by name. */
|
||||
public boolean isBuiltInStyleName(String name) {
|
||||
return name != null && builtInStyleNames.contains(key(name));
|
||||
}
|
||||
|
||||
/** The id of a built-in style in this document: kept. */
|
||||
public boolean isBuiltInStyleId(String styleId) {
|
||||
return styleId != null && builtInStyleIds.contains(styleId);
|
||||
}
|
||||
|
||||
/** The anonymised id of a style: itself for a built-in, else s + hash. */
|
||||
public String styleId(String styleId) {
|
||||
if (styleId == null || isBuiltInStyleId(styleId)) return styleId;
|
||||
return "s" + hash(styleId);
|
||||
}
|
||||
|
||||
/** The anonymised name of a bookmark, form field or sequence: bm + hash, stable across parts. */
|
||||
public static String identifier(String name) {
|
||||
if (name == null) return null;
|
||||
return "bm" + hash(name);
|
||||
}
|
||||
|
||||
/** Author n, allotted in order of first appearance. */
|
||||
public String author(String author) {
|
||||
if (author == null) return null;
|
||||
String mapped = authors.get(author);
|
||||
if (mapped == null) {
|
||||
mapped = "Author " + (authors.size() + 1);
|
||||
authors.put(author, mapped);
|
||||
}
|
||||
return mapped;
|
||||
}
|
||||
|
||||
/** An, allotted in order of first appearance. */
|
||||
public String initials(String value) {
|
||||
if (value == null) return null;
|
||||
String mapped = initials.get(value);
|
||||
if (mapped == null) {
|
||||
mapped = "A" + (initials.size() + 1);
|
||||
initials.put(value, mapped);
|
||||
}
|
||||
return mapped;
|
||||
}
|
||||
|
||||
/** How many distinct authors were renamed. */
|
||||
public int authorCount() {
|
||||
return authors.size();
|
||||
}
|
||||
|
||||
static String hash(String s) {
|
||||
return Integer.toUnsignedString(s.hashCode());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,239 @@
|
||||
package org.docx4j.anon;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
|
||||
import org.docx4j.openpackaging.contenttype.ContentTypes;
|
||||
import org.docx4j.openpackaging.exceptions.Docx4JException;
|
||||
import org.docx4j.openpackaging.parts.DefaultXmlPart;
|
||||
import org.docx4j.openpackaging.parts.Part;
|
||||
import org.docx4j.openpackaging.parts.PartName;
|
||||
|
||||
/**
|
||||
* The table the anonymiser consults for every part: what class of thing it is,
|
||||
* and so what the walk does with it (CR-019: "PartsAnalyzer's classification
|
||||
* becomes the table the walk consults: per part class, one of scramble / clear /
|
||||
* replace / remove / keep-unsafe").
|
||||
* <p>
|
||||
* Anything not named here is {@link Treatment#UNSCRUBBABLE}: a part the tool
|
||||
* cannot make clean is removed (STRICT) or kept and reported (KEEP), never
|
||||
* silently passed through.
|
||||
*/
|
||||
public class PartsAnalyzer {
|
||||
|
||||
public enum Treatment {
|
||||
/** structure only, nothing to do (a theme's colours, a chart style) */
|
||||
SAFE,
|
||||
/** a JAXB part the walk scrambles and scrubs */
|
||||
SCRUB,
|
||||
/** docProps and the like: cleared field by field */
|
||||
METADATA,
|
||||
/** an image: placeholder pixels of the same class */
|
||||
REPLACE_IMAGE,
|
||||
/** removed in either mode: custom XML, glossary, custom properties, thumbnails, signatures, labels */
|
||||
REMOVE,
|
||||
/** cannot be made clean (OLE, altChunk, VBA, embedded fonts, audio and video, unknown parts): removed in STRICT, kept and reported in KEEP */
|
||||
UNSCRUBBABLE
|
||||
}
|
||||
|
||||
public static Treatment classify(Part p) {
|
||||
|
||||
String name = p.getPartName().getName();
|
||||
|
||||
// ---- removed whatever the mode (the module has always removed these)
|
||||
if (p instanceof org.docx4j.openpackaging.parts.CustomXmlDataStoragePart
|
||||
|| p instanceof org.docx4j.openpackaging.parts.CustomXmlDataStoragePropertiesPart
|
||||
|| p instanceof org.docx4j.openpackaging.parts.opendope.JaxbCustomXmlDataStoragePart
|
||||
|| p instanceof org.docx4j.openpackaging.parts.opendope.ComponentsPart
|
||||
|| p instanceof org.docx4j.openpackaging.parts.opendope.ConditionsPart
|
||||
|| p instanceof org.docx4j.openpackaging.parts.opendope.QuestionsPart
|
||||
|| p instanceof org.docx4j.openpackaging.parts.opendope.StandardisedAnswersPart
|
||||
|| p instanceof org.docx4j.openpackaging.parts.opendope.XPathsPart
|
||||
|| p instanceof org.docx4j.openpackaging.parts.WordprocessingML.BibliographyPart // b:Sources: names and titles
|
||||
|| p instanceof org.docx4j.openpackaging.parts.WordprocessingML.GlossaryDocumentPart
|
||||
|| p instanceof org.docx4j.openpackaging.parts.DocPropsCustomPart
|
||||
|| name.startsWith("/word/glossary")
|
||||
|| name.startsWith("/docProps/thumbnail.")
|
||||
|| name.startsWith("/_xmlsignatures/") // a signature over the original content, naming the signer
|
||||
|| name.startsWith("/docMetadata/") // sensitivity label: tenant and label ids
|
||||
|| name.equals("/word/stylesWithEffects.xml")) {
|
||||
return Treatment.REMOVE;
|
||||
}
|
||||
|
||||
// ---- metadata, cleared field by field
|
||||
if (p instanceof org.docx4j.openpackaging.parts.DocPropsCorePart
|
||||
|| p instanceof org.docx4j.openpackaging.parts.DocPropsExtendedPart
|
||||
|| p instanceof org.docx4j.openpackaging.parts.DocPropsCoverPagePart) {
|
||||
return Treatment.METADATA;
|
||||
}
|
||||
|
||||
// ---- images: placeholder pixels
|
||||
if (p instanceof org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage
|
||||
|| p instanceof org.docx4j.openpackaging.parts.WordprocessingML.MetafilePart) {
|
||||
return Treatment.REPLACE_IMAGE;
|
||||
}
|
||||
if (p instanceof DefaultXmlPart && isSvg(p)) {
|
||||
return Treatment.REPLACE_IMAGE;
|
||||
}
|
||||
|
||||
// ---- the 2018 PowerPoint comments and their authors, and Excel's threaded comments and
|
||||
// persons: not bound, so a DefaultXmlPart, scrubbed as DOM (ModernCommentsScrubber)
|
||||
if (p instanceof DefaultXmlPart && isModernComments(p)) {
|
||||
return Treatment.SCRUB;
|
||||
}
|
||||
|
||||
// ---- SpreadsheetML (CR-019 phase 3): removed whatever the mode - a pivot cache is a copy
|
||||
// of its source data, and a pivot table, slicer or timeline without its cache is a
|
||||
// repair prompt, so they go together (the pivot's cells stay as values); the data
|
||||
// model is data; an add-in's custom data and a survey are text
|
||||
if (p instanceof org.docx4j.openpackaging.parts.SpreadsheetML.PivotCacheDefinition
|
||||
|| p instanceof org.docx4j.openpackaging.parts.SpreadsheetML.PivotCacheRecords
|
||||
|| p instanceof org.docx4j.openpackaging.parts.SpreadsheetML.PivotTable
|
||||
|| p instanceof org.docx4j.openpackaging.parts.SpreadsheetML.SlicerCachePart
|
||||
|| p instanceof org.docx4j.openpackaging.parts.SpreadsheetML.SlicersPart
|
||||
|| p instanceof org.docx4j.openpackaging.parts.SpreadsheetML.TimelineCachePart
|
||||
|| p instanceof org.docx4j.openpackaging.parts.SpreadsheetML.TimelinesPart
|
||||
|| p instanceof org.docx4j.openpackaging.parts.SpreadsheetML.DataModelPart
|
||||
|| p instanceof org.docx4j.openpackaging.parts.SpreadsheetML.CustomDataPart
|
||||
|| p instanceof org.docx4j.openpackaging.parts.SpreadsheetML.CustomDataPropertiesPart
|
||||
|| p instanceof org.docx4j.openpackaging.parts.SpreadsheetML.SurveyPart) {
|
||||
return Treatment.REMOVE;
|
||||
}
|
||||
|
||||
// ---- structure
|
||||
if (p instanceof org.docx4j.openpackaging.parts.DrawingML.ChartStylePart
|
||||
|| p instanceof org.docx4j.openpackaging.parts.DrawingML.ChartColorStylePart
|
||||
|| p instanceof org.docx4j.openpackaging.parts.relationships.RelationshipsPart) {
|
||||
return Treatment.SAFE;
|
||||
}
|
||||
|
||||
// ---- JAXB parts the walk covers
|
||||
if (p instanceof org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart
|
||||
|| p instanceof org.docx4j.openpackaging.parts.WordprocessingML.HeaderPart
|
||||
|| p instanceof org.docx4j.openpackaging.parts.WordprocessingML.FooterPart
|
||||
|| p instanceof org.docx4j.openpackaging.parts.WordprocessingML.FootnotesPart
|
||||
|| p instanceof org.docx4j.openpackaging.parts.WordprocessingML.EndnotesPart
|
||||
|| p instanceof org.docx4j.openpackaging.parts.WordprocessingML.CommentsPart
|
||||
|| p instanceof org.docx4j.openpackaging.parts.WordprocessingML.CommentsExtendedPart
|
||||
|| p instanceof org.docx4j.openpackaging.parts.WordprocessingML.CommentsIdsPart
|
||||
|| p instanceof org.docx4j.openpackaging.parts.WordprocessingML.CommentsExtensiblePart
|
||||
|| p instanceof org.docx4j.openpackaging.parts.WordprocessingML.PeoplePart
|
||||
|| p instanceof org.docx4j.openpackaging.parts.WordprocessingML.DocumentSettingsPart
|
||||
|| p instanceof org.docx4j.openpackaging.parts.WordprocessingML.WebSettingsPart
|
||||
|| p instanceof org.docx4j.openpackaging.parts.WordprocessingML.StyleDefinitionsPart
|
||||
|| p instanceof org.docx4j.openpackaging.parts.WordprocessingML.NumberingDefinitionsPart
|
||||
|| p instanceof org.docx4j.openpackaging.parts.WordprocessingML.FontTablePart
|
||||
|| p instanceof org.docx4j.openpackaging.parts.ThemePart
|
||||
|| p instanceof org.docx4j.openpackaging.parts.DrawingML.ThemeOverridePart
|
||||
|| p instanceof org.docx4j.openpackaging.parts.DrawingML.Chart
|
||||
|| p instanceof org.docx4j.openpackaging.parts.DrawingML.ChartExSpacePart
|
||||
|| p instanceof org.docx4j.openpackaging.parts.DrawingML.ChartShapePart
|
||||
|| p instanceof org.docx4j.openpackaging.parts.DrawingML.DiagramDataPart
|
||||
|| p instanceof org.docx4j.openpackaging.parts.DrawingML.DiagramDrawingPart
|
||||
|| p instanceof org.docx4j.openpackaging.parts.DrawingML.DiagramLayoutPart
|
||||
|| p instanceof org.docx4j.openpackaging.parts.DrawingML.DiagramLayoutHeaderPart
|
||||
|| p instanceof org.docx4j.openpackaging.parts.DrawingML.DiagramColorsPart
|
||||
|| p instanceof org.docx4j.openpackaging.parts.DrawingML.DiagramStylePart
|
||||
|| p instanceof org.docx4j.openpackaging.parts.DrawingML.Drawing) {
|
||||
return Treatment.SCRUB;
|
||||
}
|
||||
|
||||
// ---- PresentationML (CR-019 phase 2): the slides and their masters, layouts and
|
||||
// notes (a:t in every a:p), the presentation (section names, custom shows, the
|
||||
// modify verifier, the embedded-font list), legacy comments and their authors, tags,
|
||||
// table styles (names)
|
||||
if (p instanceof org.docx4j.openpackaging.parts.PresentationML.MainPresentationPart
|
||||
|| p instanceof org.docx4j.openpackaging.parts.PresentationML.SlidePart
|
||||
|| p instanceof org.docx4j.openpackaging.parts.PresentationML.SlideLayoutPart
|
||||
|| p instanceof org.docx4j.openpackaging.parts.PresentationML.SlideMasterPart
|
||||
|| p instanceof org.docx4j.openpackaging.parts.PresentationML.NotesSlidePart
|
||||
|| p instanceof org.docx4j.openpackaging.parts.PresentationML.NotesMasterPart
|
||||
|| p instanceof org.docx4j.openpackaging.parts.PresentationML.HandoutMasterPart
|
||||
|| p instanceof org.docx4j.openpackaging.parts.PresentationML.CommentsPart
|
||||
|| p instanceof org.docx4j.openpackaging.parts.PresentationML.CommentAuthorsPart
|
||||
|| p instanceof org.docx4j.openpackaging.parts.PresentationML.TagsPart
|
||||
|| p instanceof org.docx4j.openpackaging.parts.PresentationML.TableStylesPart) {
|
||||
return Treatment.SCRUB;
|
||||
}
|
||||
// structure, but walked (presProps carries the HTML publish target and the show's custom-show id)
|
||||
if (p instanceof org.docx4j.openpackaging.parts.PresentationML.PresentationPropertiesPart
|
||||
|| p instanceof org.docx4j.openpackaging.parts.PresentationML.ViewPropertiesPart) {
|
||||
return Treatment.SAFE;
|
||||
}
|
||||
|
||||
// ---- SpreadsheetML: the workbook (sheet and defined names, protection, file sharing),
|
||||
// the sheets (cells, formulas, headers, validations, filters), shared strings, styles
|
||||
// (custom style names), tables, comments and their authors, connections and query
|
||||
// tables (connection strings, commands, URLs), external links (cached values, sheet
|
||||
// names), form-control properties (linked-cell formulas, list items); VML (comment
|
||||
// and control shapes)
|
||||
if (p instanceof org.docx4j.openpackaging.parts.SpreadsheetML.WorkbookPart
|
||||
|| p instanceof org.docx4j.openpackaging.parts.SpreadsheetML.WorksheetPart
|
||||
|| p instanceof org.docx4j.openpackaging.parts.SpreadsheetML.ChartsheetPart
|
||||
|| p instanceof org.docx4j.openpackaging.parts.SpreadsheetML.SharedStrings
|
||||
|| p instanceof org.docx4j.openpackaging.parts.SpreadsheetML.Styles
|
||||
|| p instanceof org.docx4j.openpackaging.parts.SpreadsheetML.TablePart
|
||||
|| p instanceof org.docx4j.openpackaging.parts.SpreadsheetML.CommentsPart
|
||||
|| p instanceof org.docx4j.openpackaging.parts.SpreadsheetML.ConnectionsPart
|
||||
|| p instanceof org.docx4j.openpackaging.parts.SpreadsheetML.QueryTablePart
|
||||
|| p instanceof org.docx4j.openpackaging.parts.SpreadsheetML.ExternalLinkPart
|
||||
|| p instanceof org.docx4j.openpackaging.parts.SpreadsheetML.ControlPropertiesPart
|
||||
|| p instanceof org.docx4j.openpackaging.parts.VMLPart) {
|
||||
return Treatment.SCRUB;
|
||||
}
|
||||
if (p instanceof org.docx4j.openpackaging.parts.SpreadsheetML.CalcChain) {
|
||||
return Treatment.SAFE; // cell references only
|
||||
}
|
||||
|
||||
// ---- everything else: OLE and embedded packages, altChunk, VBA, embedded fonts
|
||||
// (FontDataPart included), printer settings, ActiveX, ink, web extensions, audio and
|
||||
// video, unknown XML and binaries (a workbook's metadata and rich-value parts among them)
|
||||
return Treatment.UNSCRUBBABLE;
|
||||
}
|
||||
|
||||
/**
|
||||
* The 2018 comments part (p188:cmLst) or its authors part (p188:authorLst), or Excel's
|
||||
* threaded comments or persons part, by content type
|
||||
*/
|
||||
static boolean isModernComments(Part p) {
|
||||
try {
|
||||
String ct = p.getContentType();
|
||||
return ContentTypes.PRESENTATIONML_MODERN_COMMENTS.equals(ct)
|
||||
|| ContentTypes.PRESENTATIONML_MODERN_COMMENT_AUTHORS.equals(ct)
|
||||
|| ContentTypes.SPREADSHEETML_THREADED_COMMENTS.equals(ct)
|
||||
|| ContentTypes.SPREADSHEETML_PERSONS.equals(ct);
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
static boolean isSvg(Part p) {
|
||||
try {
|
||||
String ct = p.getContentType();
|
||||
if (ct != null) return ct.equals(ContentTypes.IMAGE_SVG);
|
||||
} catch (Exception e) {
|
||||
// fall through
|
||||
}
|
||||
return p.getPartName().getName().toLowerCase().endsWith(".svg");
|
||||
}
|
||||
|
||||
/**
|
||||
* The parts the tool cannot make clean.
|
||||
*
|
||||
* @deprecated since 17.2.0: {@link Anonymize} consults {@link #classify(Part)}; this
|
||||
* remains for callers of the older API and no longer touches the cover page part
|
||||
*/
|
||||
@Deprecated
|
||||
public static HashSet<Part> identifyUnsafeParts(Set<Entry<PartName, Part>> parts) throws Docx4JException {
|
||||
|
||||
HashSet<Part> unsafeParts = new HashSet<Part>();
|
||||
for (Entry<PartName, Part> entry : parts) {
|
||||
if (classify(entry.getValue()) == Treatment.UNSCRUBBABLE) {
|
||||
unsafeParts.add(entry.getValue());
|
||||
}
|
||||
}
|
||||
return unsafeParts;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
/*
|
||||
* Copyright 2026, Plutext Pty Ltd.
|
||||
*
|
||||
* This file is part of docx4j.
|
||||
|
||||
docx4j is licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
||||
*/
|
||||
package org.docx4j.anon;
|
||||
|
||||
import java.awt.BasicStroke;
|
||||
import java.awt.Color;
|
||||
import java.awt.Font;
|
||||
import java.awt.FontMetrics;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.RenderingHints;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
import org.docx4j.wml.ObjectFactory;
|
||||
import org.docx4j.wml.P;
|
||||
import org.docx4j.wml.R;
|
||||
import org.docx4j.wml.Text;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* What stands in for a removed object. An object's footprint survives, because
|
||||
* page flow is what the anonymised document is for, but the footprint says
|
||||
* what it was: an OLE object or ActiveX control leaves its picture, and the
|
||||
* picture is a generated image labelled "OLE object removed by
|
||||
* docx4j anon"; an altChunk, which has no footprint of its own, leaves a
|
||||
* marker paragraph. The words of the markers are in {@link Verify}'s allowed
|
||||
* vocabulary.
|
||||
*
|
||||
* @since 17.2.0
|
||||
*/
|
||||
class Placeholders {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(Placeholders.class);
|
||||
|
||||
static final String OBJECT_REMOVED = "OLE object removed";
|
||||
static final String ALTCHUNK_REMOVED = "[altChunk removed by docx4j anon]";
|
||||
static final String BY = "by docx4j anon";
|
||||
|
||||
private static byte[] objectRemovedPng;
|
||||
|
||||
/**
|
||||
* A 480x240 PNG, light grey with a border, labelled {@link #OBJECT_REMOVED} /
|
||||
* {@link #BY}; Word scales it to the object's extent. Falls back to the 2x2
|
||||
* pixels if the image cannot be drawn (no java.desktop at run time).
|
||||
*/
|
||||
static synchronized byte[] objectRemovedPng() {
|
||||
if (objectRemovedPng != null) return objectRemovedPng;
|
||||
try {
|
||||
int w = 480, h = 240;
|
||||
BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
|
||||
Graphics2D g = img.createGraphics();
|
||||
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
|
||||
g.setColor(new Color(0xEE, 0xEE, 0xEE));
|
||||
g.fillRect(0, 0, w, h);
|
||||
g.setColor(new Color(0x88, 0x88, 0x88));
|
||||
g.setStroke(new BasicStroke(4));
|
||||
g.drawRect(2, 2, w - 4, h - 4);
|
||||
g.setColor(new Color(0x33, 0x33, 0x33));
|
||||
centred(g, OBJECT_REMOVED, new Font(Font.SANS_SERIF, Font.BOLD, 36), w, h / 2 - 12);
|
||||
centred(g, BY, new Font(Font.SANS_SERIF, Font.PLAIN, 24), w, h / 2 + 36);
|
||||
g.dispose();
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
ImageIO.write(img, "png", baos);
|
||||
objectRemovedPng = baos.toByteArray();
|
||||
} catch (Throwable t) {
|
||||
log.warn("cannot draw the placeholder image (" + t + "); using the 2x2 pixels");
|
||||
objectRemovedPng = MediaReplacer.PNG_IMAGE_DATA;
|
||||
}
|
||||
return objectRemovedPng;
|
||||
}
|
||||
|
||||
private static void centred(Graphics2D g, String text, Font font, int width, int baseline) {
|
||||
g.setFont(font);
|
||||
FontMetrics fm = g.getFontMetrics();
|
||||
g.drawString(text, (width - fm.stringWidth(text)) / 2, baseline);
|
||||
}
|
||||
|
||||
/** The paragraph an altChunk becomes. */
|
||||
static P altChunkRemoved() {
|
||||
ObjectFactory f = new ObjectFactory();
|
||||
P p = f.createP();
|
||||
R r = f.createR();
|
||||
Text t = f.createText();
|
||||
t.setValue(ALTCHUNK_REMOVED);
|
||||
r.getContent().add(f.createRT(t));
|
||||
p.getContent().add(r);
|
||||
return p;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
package org.docx4j.anon;
|
||||
|
||||
import org.docx4j.fonts.RunFontSelector;
|
||||
import org.docx4j.fonts.RunFontSelector.RunFontCharacterVisitor;
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
public class RunFontCharVisitorMinimal implements RunFontCharacterVisitor {
|
||||
|
||||
StringBuilder sb = new StringBuilder(1024);
|
||||
|
||||
@Override
|
||||
public void setDocument(Document document) {}
|
||||
|
||||
|
||||
public void addCharacterToCurrent(char c) {
|
||||
sb.append(c);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addCodePointToCurrent(int cp) {
|
||||
sb.append(
|
||||
new String(Character.toChars(cp)));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Object getResult() {
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private RunFontSelector runFontSelector;
|
||||
@Override
|
||||
public void setRunFontSelector(RunFontSelector runFontSelector) {
|
||||
this.runFontSelector = runFontSelector;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setFallbackFont(String fontname) {}
|
||||
|
||||
|
||||
@Override
|
||||
public void finishPrevious() {
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void createNew() {
|
||||
sb = new StringBuilder(1024);
|
||||
fontname = null;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void setMustCreateNewFlag(boolean val) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean isReusable() {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
String fontname;
|
||||
|
||||
public String getFontname() {
|
||||
return fontname;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void fontAction(String fontname) {
|
||||
this.fontname = fontname;
|
||||
|
||||
}
|
||||
};
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,232 @@
|
||||
/*
|
||||
* Copyright 2026, Plutext Pty Ltd.
|
||||
*
|
||||
* This file is part of docx4j.
|
||||
|
||||
docx4j is licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
||||
*/
|
||||
package org.docx4j.anon;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Scrubs a spreadsheet formula (a cell's {@code f}, a defined name, a chart's
|
||||
* {@code c:f}, a conditional format's or validation's formula, an
|
||||
* {@code xm:f}) so that it still computes over the anonymised workbook:
|
||||
* <ul>
|
||||
* <li>string literals ({@code "..."}) are scrambled;</li>
|
||||
* <li>numeric constants have their digits randomised ({@link ScrambleText#number}),
|
||||
* unless numbers are kept; a row range ({@code 1:1}) is a reference, not a
|
||||
* constant, and stays;</li>
|
||||
* <li>sheet names ({@code 'My Sheet'!A1}, {@code Sales!A1}) become the
|
||||
* workbook's {@code Sheet<n>} ({@link Names#sheet}); a sheet of an external
|
||||
* workbook ({@code '[1]Sheet'!A1}) is scrambled consistently with the external
|
||||
* link part;</li>
|
||||
* <li>defined names become {@code n_<hash>} ({@link Names#definedName});</li>
|
||||
* <li>structured references ({@code Sales[Amount]}, {@code Sales[[#This Row],[Amount]]})
|
||||
* keep their keywords and have the table and column names scrambled
|
||||
* consistently with the table part and the header cells;</li>
|
||||
* <li>cell and range references, function names, operators, the {@code _xlfn.}
|
||||
* prefixes, {@code TRUE}/{@code FALSE} and error literals stay.</li>
|
||||
* </ul>
|
||||
*
|
||||
* @since 17.2.1
|
||||
*/
|
||||
public class SmlFormulas {
|
||||
|
||||
private final ScrambleText scrambler;
|
||||
private final Names names;
|
||||
|
||||
/** A1-style reference: an optional column (with optional $) and row; also column-only and row-only when next to a colon */
|
||||
private static final Pattern CELL_REF = Pattern.compile("^\\$?[A-Za-z]{1,3}\\$?[0-9]+$");
|
||||
private static final Pattern COLUMN_REF = Pattern.compile("^\\$?[A-Za-z]{1,3}$");
|
||||
private static final Pattern R1C1 = Pattern.compile("^([Rr](\\[-?[0-9]+\\]|[0-9]+)?[Cc](\\[-?[0-9]+\\]|[0-9]+)?|[Rr]|[Cc])$");
|
||||
|
||||
public SmlFormulas(ScrambleText scrambler, Names names) {
|
||||
this.scrambler = scrambler;
|
||||
this.names = names;
|
||||
}
|
||||
|
||||
public String scrub(String formula) {
|
||||
if (formula == null || formula.isEmpty()) return formula;
|
||||
StringBuilder out = new StringBuilder();
|
||||
int i = 0, n = formula.length();
|
||||
while (i < n) {
|
||||
char c = formula.charAt(i);
|
||||
|
||||
if (c == '"') {
|
||||
// a string literal, "" escaping a quote
|
||||
int j = i + 1;
|
||||
StringBuilder lit = new StringBuilder();
|
||||
while (j < n) {
|
||||
if (formula.charAt(j) == '"') {
|
||||
if (j + 1 < n && formula.charAt(j + 1) == '"') {
|
||||
lit.append('"');
|
||||
j += 2;
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
lit.append(formula.charAt(j));
|
||||
j++;
|
||||
}
|
||||
out.append('"').append(scrambler.scramble(lit.toString()).replace("\"", "\"\"")).append('"');
|
||||
i = Math.min(j + 1, n);
|
||||
|
||||
} else if (c == '\'') {
|
||||
// a quoted sheet name (or '[n]Sheet' of an external book), '' escaping a quote
|
||||
int j = i + 1;
|
||||
StringBuilder name = new StringBuilder();
|
||||
while (j < n) {
|
||||
if (formula.charAt(j) == '\'') {
|
||||
if (j + 1 < n && formula.charAt(j + 1) == '\'') {
|
||||
name.append('\'');
|
||||
j += 2;
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
name.append(formula.charAt(j));
|
||||
j++;
|
||||
}
|
||||
out.append('\'').append(sheetName(name.toString()).replace("'", "''")).append('\'');
|
||||
i = Math.min(j + 1, n);
|
||||
|
||||
} else if (c == '[') {
|
||||
// a structured reference's brackets, or an external book number [1] before a sheet
|
||||
int j = matchingBracket(formula, i);
|
||||
String inner = formula.substring(i + 1, j);
|
||||
if (inner.matches("-?[0-9]+")) { // [1] before an external sheet, [-1] of R1C1
|
||||
out.append('[').append(inner).append(']');
|
||||
} else {
|
||||
out.append('[').append(structured(inner)).append(']');
|
||||
}
|
||||
i = j + 1;
|
||||
|
||||
} else if (c == '#') {
|
||||
// an error literal: #REF!, #N/A, #DIV/0!, #NAME?, #GETTING_DATA
|
||||
int j = i + 1;
|
||||
while (j < n && (Character.isLetterOrDigit(formula.charAt(j)) || "/_!?".indexOf(formula.charAt(j)) >= 0)) j++;
|
||||
out.append(formula, i, j);
|
||||
i = j;
|
||||
|
||||
} else if (Character.isDigit(c) || (c == '.' && i + 1 < n && Character.isDigit(formula.charAt(i + 1)))) {
|
||||
int j = i;
|
||||
while (j < n && (Character.isDigit(formula.charAt(j)) || formula.charAt(j) == '.')) j++;
|
||||
if (j < n && (formula.charAt(j) == 'E' || formula.charAt(j) == 'e')) {
|
||||
int k = j + 1;
|
||||
if (k < n && (formula.charAt(k) == '+' || formula.charAt(k) == '-')) k++;
|
||||
if (k < n && Character.isDigit(formula.charAt(k))) {
|
||||
while (k < n && Character.isDigit(formula.charAt(k))) k++;
|
||||
j = k;
|
||||
}
|
||||
}
|
||||
String number = formula.substring(i, j);
|
||||
boolean rowRange = (i > 0 && formula.charAt(i - 1) == ':') || (j < n && formula.charAt(j) == ':');
|
||||
out.append(rowRange ? number : scrambler.number(number));
|
||||
i = j;
|
||||
|
||||
} else if (Character.isLetter(c) || c == '_' || c == '$' || c == '\\') {
|
||||
// an identifier: function, cell reference, sheet name, table name, defined name
|
||||
int j = i;
|
||||
while (j < n && (Character.isLetterOrDigit(formula.charAt(j)) || "_.$\\".indexOf(formula.charAt(j)) >= 0)) j++;
|
||||
String ident = formula.substring(i, j);
|
||||
int k = j;
|
||||
while (k < n && formula.charAt(k) == ' ') k++;
|
||||
char next = k < n ? formula.charAt(k) : 0;
|
||||
out.append(identifier(ident, next, i > 0 ? formula.charAt(i - 1) : 0));
|
||||
i = j;
|
||||
|
||||
} else {
|
||||
out.append(c);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
return out.toString();
|
||||
}
|
||||
|
||||
private String identifier(String ident, char next, char previous) {
|
||||
if (next == '(') return ident; // a function
|
||||
if (R1C1.matcher(ident).matches()) return ident; // R1C1: R[-1]C, RC[1], R2C3 (Excel reserves R and C, so never a name)
|
||||
if (next == '!') return previous == ']' ? scrambler.consistent(ident) : sheetName(ident); // [1]Sheet1! is an external book's sheet
|
||||
if (next == '[') return scrambler.consistentIdentifier(ident); // a table's display name, as the table part maps it
|
||||
if (CELL_REF.matcher(ident).matches()) return ident;
|
||||
if (COLUMN_REF.matcher(ident).matches() && (next == ':' || previous == ':')) return ident;
|
||||
String upper = ident.toUpperCase(java.util.Locale.ROOT);
|
||||
if (upper.equals("TRUE") || upper.equals("FALSE")) return ident;
|
||||
if (ident.startsWith("_xlfn.") || ident.startsWith("_xlws.") || ident.startsWith("_xlpm.")) return ident;
|
||||
return Names.definedName(ident);
|
||||
}
|
||||
|
||||
/** a sheet name, with an optional [n] external-book prefix */
|
||||
private String sheetName(String name) {
|
||||
String prefix = "";
|
||||
String rest = name;
|
||||
if (name.startsWith("[")) {
|
||||
int close = name.indexOf(']');
|
||||
if (close > 0) {
|
||||
prefix = name.substring(0, close + 1);
|
||||
rest = name.substring(close + 1);
|
||||
}
|
||||
}
|
||||
if (prefix.isEmpty() && names.isSheet(rest)) return names.sheet(rest);
|
||||
if (prefix.isEmpty()) return scrambler.consistent(rest); // a sheet the workbook does not list
|
||||
// an external workbook's sheet: the same mapping as its external link part's sheetNames
|
||||
return prefix + scrambler.consistent(rest);
|
||||
}
|
||||
|
||||
/** the inside of a structured reference's brackets: keywords kept, column names scrambled consistently */
|
||||
private String structured(String inner) {
|
||||
String trimmed = inner.trim();
|
||||
if (trimmed.isEmpty()) return inner;
|
||||
if (trimmed.startsWith("#")) return inner; // [#Headers], [#This Row], [#All], [#Data], [#Totals]
|
||||
if (trimmed.startsWith("@")) return "@" + structured(trimmed.substring(1));
|
||||
if (trimmed.startsWith("[")) {
|
||||
// [[#This Row],[Amount]] or [[Col A]:[Col B]]
|
||||
StringBuilder sb = new StringBuilder();
|
||||
int i = 0;
|
||||
while (i < trimmed.length()) {
|
||||
char c = trimmed.charAt(i);
|
||||
if (c == '[') {
|
||||
int j = matchingBracket(trimmed, i);
|
||||
sb.append('[').append(structured(trimmed.substring(i + 1, j))).append(']');
|
||||
i = j + 1;
|
||||
} else {
|
||||
sb.append(c);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
return scrambler.consistent(inner);
|
||||
}
|
||||
|
||||
private static int matchingBracket(String s, int open) {
|
||||
int depth = 0;
|
||||
for (int i = open; i < s.length(); i++) {
|
||||
char c = s.charAt(i);
|
||||
if (c == '\'' && i + 1 < s.length()) {
|
||||
i++; // an escaped special character inside a structured reference
|
||||
continue;
|
||||
}
|
||||
if (c == '[') depth++;
|
||||
if (c == ']') {
|
||||
depth--;
|
||||
if (depth == 0) return i;
|
||||
}
|
||||
}
|
||||
return s.length() - 1;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,658 @@
|
||||
/*
|
||||
* Copyright 2026, Plutext Pty Ltd.
|
||||
*
|
||||
* This file is part of docx4j.
|
||||
|
||||
docx4j is licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
||||
*/
|
||||
package org.docx4j.anon;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import jakarta.xml.bind.JAXBElement;
|
||||
|
||||
import org.docx4j.XmlUtils;
|
||||
import org.docx4j.anon.JaxbGraphWalker.Action;
|
||||
import org.xlsx4j.sml.CTAuthors;
|
||||
import org.xlsx4j.sml.CTCellFormula;
|
||||
import org.xlsx4j.sml.CTCellSmartTagPr;
|
||||
import org.xlsx4j.sml.CTCellStyle;
|
||||
import org.xlsx4j.sml.CTCfRule;
|
||||
import org.xlsx4j.sml.CTConnection;
|
||||
import org.xlsx4j.sml.CTControls;
|
||||
import org.xlsx4j.sml.CTCustomFilter;
|
||||
import org.xlsx4j.sml.CTCustomWorkbookView;
|
||||
import org.xlsx4j.sml.CTDataRef;
|
||||
import org.xlsx4j.sml.CTDataValidation;
|
||||
import org.xlsx4j.sml.CTDbPr;
|
||||
import org.xlsx4j.sml.CTDdeItem;
|
||||
import org.xlsx4j.sml.CTDdeLink;
|
||||
import org.xlsx4j.sml.CTDefinedName;
|
||||
import org.xlsx4j.sml.CTExternalCell;
|
||||
import org.xlsx4j.sml.CTExternalDefinedName;
|
||||
import org.xlsx4j.sml.CTExternalSheetName;
|
||||
import org.xlsx4j.sml.CTFileSharing;
|
||||
import org.xlsx4j.sml.CTFilter;
|
||||
import org.xlsx4j.sml.CTFunctionGroup;
|
||||
import org.xlsx4j.sml.CTHeaderFooter;
|
||||
import org.xlsx4j.sml.CTHyperlink;
|
||||
import org.xlsx4j.sml.CTInputCells;
|
||||
import org.xlsx4j.sml.CTMap;
|
||||
import org.xlsx4j.sml.CTOleItem;
|
||||
import org.xlsx4j.sml.CTOleObjects;
|
||||
import org.xlsx4j.sml.CTParameter;
|
||||
import org.xlsx4j.sml.CTPhoneticRun;
|
||||
import org.xlsx4j.sml.CTPivotCaches;
|
||||
import org.xlsx4j.sml.CTQueryTable;
|
||||
import org.xlsx4j.sml.CTQueryTableField;
|
||||
import org.xlsx4j.sml.CTRElt;
|
||||
import org.xlsx4j.sml.CTRst;
|
||||
import org.xlsx4j.sml.CTSheetProtection;
|
||||
import org.xlsx4j.sml.CTSmartTagType;
|
||||
import org.xlsx4j.sml.CTTable;
|
||||
import org.xlsx4j.sml.CTTableColumn;
|
||||
import org.xlsx4j.sml.CTTableFormula;
|
||||
import org.xlsx4j.sml.CTTableStyle;
|
||||
import org.xlsx4j.sml.CTTableStyleInfo;
|
||||
import org.xlsx4j.sml.CTTextPr;
|
||||
import org.xlsx4j.sml.CTWebPr;
|
||||
import org.xlsx4j.sml.CTWebPublishItem;
|
||||
import org.xlsx4j.sml.CTWorkbookProtection;
|
||||
import org.xlsx4j.sml.CTXstringWhitespace;
|
||||
import org.xlsx4j.sml.Cell;
|
||||
import org.xlsx4j.sml.STCellType;
|
||||
import org.xlsx4j.sml.Sheet;
|
||||
|
||||
/**
|
||||
* The third visitor of the anonymiser's walk, for a workbook (CR-019 phase 3):
|
||||
* everything SpreadsheetML-typed that carries text, numbers, names or
|
||||
* identities. The DrawingML and chart classes a workbook shares with the other
|
||||
* formats are {@link ScrambleText}'s and {@link MarkupScrubber}'s.
|
||||
* <ul>
|
||||
* <li>Text: shared and inline strings (the same string always to the same
|
||||
* output, so a table column's name still equals its header cell), formula
|
||||
* string results, comments and their authors, headers and footers (codes
|
||||
* kept), hyperlink display text, data-validation prompts and errors,
|
||||
* conditional-format text, filter values, defined-name comments, custom cell
|
||||
* style names, table and query-table names, connection names and descriptions.</li>
|
||||
* <li>Numbers: cell values and formula constants have their digits randomised
|
||||
* ({@link ScrambleText#number}; decision 3 - kept with
|
||||
* {@link ScrambleText#setKeepNumbers}); booleans and errors stay.</li>
|
||||
* <li>Formulas ({@link SmlFormulas}): cells, defined names, tables' calculated
|
||||
* columns, validations, conditional formats, sparklines, form controls, chart
|
||||
* references; sheet names become {@code Sheet<n>} everywhere.</li>
|
||||
* <li>Identities and references: the file-sharing user name, comment authors,
|
||||
* every password hash and salt (sheet, workbook, file sharing); connection
|
||||
* strings, commands, source files and URLs; DDE and OLE link names.</li>
|
||||
* <li>STRICT: the pivot-cache list, the slicer, timeline and data-model
|
||||
* extensions, OLE objects and ActiveX controls, and a cell's rich-value
|
||||
* metadata indexes go (their parts are removed).</li>
|
||||
* </ul>
|
||||
*
|
||||
* @since 17.2.1
|
||||
*/
|
||||
public class SpreadsheetScrubber implements JaxbGraphWalker.Visitor {
|
||||
|
||||
private final ScrambleText scrambler;
|
||||
private final SmlFormulas formulas;
|
||||
private final Names names;
|
||||
private final boolean strict;
|
||||
private final Consumer<String> notes;
|
||||
/** "partName#relId" of the pictures which stood for a removed OLE object: MediaReplacer labels them */
|
||||
private final java.util.Set<String> objectPreviews;
|
||||
private String currentPartName = "";
|
||||
|
||||
/** Excel's built-in table and pivot style names, which name nothing */
|
||||
private static final Pattern BUILT_IN_TABLE_STYLE = Pattern.compile("^(TableStyle|PivotStyle)(Light|Medium|Dark)[0-9]+$");
|
||||
|
||||
public SpreadsheetScrubber(ScrambleText scrambler, Names names, boolean strict, Consumer<String> notes,
|
||||
java.util.Set<String> objectPreviews) {
|
||||
this.scrambler = scrambler;
|
||||
this.formulas = new SmlFormulas(scrambler, names);
|
||||
this.names = names;
|
||||
this.strict = strict;
|
||||
this.notes = notes;
|
||||
this.objectPreviews = objectPreviews;
|
||||
}
|
||||
|
||||
/** The part being walked: relationship ids are per part. */
|
||||
public void setCurrentPart(org.docx4j.openpackaging.parts.Part part) {
|
||||
currentPartName = part == null ? "" : part.getPartName().getName();
|
||||
}
|
||||
|
||||
public SmlFormulas formulas() {
|
||||
return formulas;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Action visit(Object o, JAXBElement<?> wrapper) {
|
||||
|
||||
// ---- text
|
||||
if (o instanceof CTRst) {
|
||||
richText((CTRst) o);
|
||||
return Action.SKIP_CHILDREN;
|
||||
}
|
||||
if (o instanceof Cell) {
|
||||
Cell c = (Cell) o;
|
||||
cellValue(c.getT(), c.getV(), v -> c.setV(v));
|
||||
if (strict && (c.getCm() != 0 || c.getVm() != 0)) {
|
||||
// rich-value and cell metadata indexes into parts the tool removes
|
||||
c.setCm(null);
|
||||
c.setVm(null);
|
||||
}
|
||||
return Action.CONTINUE; // its f and is
|
||||
}
|
||||
if (o instanceof CTExternalCell) {
|
||||
CTExternalCell c = (CTExternalCell) o;
|
||||
cellValue(c.getT(), c.getV(), v -> c.setV(v));
|
||||
c.setVm(null);
|
||||
return Action.SKIP_CHILDREN;
|
||||
}
|
||||
if (o instanceof CTCellFormula) {
|
||||
CTCellFormula f = (CTCellFormula) o;
|
||||
f.setValue(formulas.scrub(f.getValue()));
|
||||
return Action.SKIP_CHILDREN;
|
||||
}
|
||||
if (o instanceof CTTableFormula) {
|
||||
CTTableFormula f = (CTTableFormula) o;
|
||||
f.setValue(formulas.scrub(f.getValue()));
|
||||
return Action.SKIP_CHILDREN;
|
||||
}
|
||||
if (o instanceof CTHeaderFooter) {
|
||||
CTHeaderFooter hf = (CTHeaderFooter) o;
|
||||
hf.setOddHeader(scrambler.headerFooter(hf.getOddHeader()));
|
||||
hf.setOddFooter(scrambler.headerFooter(hf.getOddFooter()));
|
||||
hf.setEvenHeader(scrambler.headerFooter(hf.getEvenHeader()));
|
||||
hf.setEvenFooter(scrambler.headerFooter(hf.getEvenFooter()));
|
||||
hf.setFirstHeader(scrambler.headerFooter(hf.getFirstHeader()));
|
||||
hf.setFirstFooter(scrambler.headerFooter(hf.getFirstFooter()));
|
||||
return Action.SKIP_CHILDREN;
|
||||
}
|
||||
if (o instanceof CTHyperlink) {
|
||||
CTHyperlink h = (CTHyperlink) o;
|
||||
h.setDisplay(scrambler.scramble(h.getDisplay()));
|
||||
h.setTooltip(null);
|
||||
h.setLocation(formulas.scrub(h.getLocation())); // Sheet2!A1, or a defined name
|
||||
return Action.SKIP_CHILDREN;
|
||||
}
|
||||
if (o instanceof CTDataValidation) {
|
||||
CTDataValidation dv = (CTDataValidation) o;
|
||||
dv.setFormula1(formulas.scrub(dv.getFormula1()));
|
||||
dv.setFormula2(formulas.scrub(dv.getFormula2()));
|
||||
dv.setPrompt(scrambler.scramble(dv.getPrompt()));
|
||||
dv.setPromptTitle(scrambler.scramble(dv.getPromptTitle()));
|
||||
dv.setError(scrambler.scramble(dv.getError()));
|
||||
dv.setErrorTitle(scrambler.scramble(dv.getErrorTitle()));
|
||||
return Action.SKIP_CHILDREN;
|
||||
}
|
||||
if (o instanceof org.xlsx4j.com.microsoft.schemas.office.spreadsheetml.x2009.x9.main.CTDataValidation) {
|
||||
org.xlsx4j.com.microsoft.schemas.office.spreadsheetml.x2009.x9.main.CTDataValidation dv =
|
||||
(org.xlsx4j.com.microsoft.schemas.office.spreadsheetml.x2009.x9.main.CTDataValidation) o;
|
||||
if (dv.getFormula1() != null) dv.getFormula1().setF(formulas.scrub(dv.getFormula1().getF()));
|
||||
if (dv.getFormula2() != null) dv.getFormula2().setF(formulas.scrub(dv.getFormula2().getF()));
|
||||
dv.setPrompt(scrambler.scramble(dv.getPrompt()));
|
||||
dv.setPromptTitle(scrambler.scramble(dv.getPromptTitle()));
|
||||
dv.setError(scrambler.scramble(dv.getError()));
|
||||
dv.setErrorTitle(scrambler.scramble(dv.getErrorTitle()));
|
||||
return Action.SKIP_CHILDREN;
|
||||
}
|
||||
if (o instanceof CTCfRule) {
|
||||
CTCfRule rule = (CTCfRule) o;
|
||||
List<String> fs = rule.getFormula();
|
||||
for (int i = 0; i < fs.size(); i++) fs.set(i, formulas.scrub(fs.get(i)));
|
||||
rule.setText(scrambler.scramble(rule.getText()));
|
||||
return Action.CONTINUE; // its extLst (x14 rules with xm:f)
|
||||
}
|
||||
if (o instanceof CTCustomFilter) {
|
||||
CTCustomFilter f = (CTCustomFilter) o;
|
||||
f.setVal(scrambler.scramble(f.getVal()));
|
||||
return Action.SKIP_CHILDREN;
|
||||
}
|
||||
if (o instanceof CTFilter) {
|
||||
CTFilter f = (CTFilter) o;
|
||||
f.setVal(scrambler.scramble(f.getVal()));
|
||||
return Action.SKIP_CHILDREN;
|
||||
}
|
||||
if (o instanceof CTInputCells) {
|
||||
CTInputCells ic = (CTInputCells) o;
|
||||
ic.setVal(scrambler.number(ic.getVal()));
|
||||
return Action.SKIP_CHILDREN;
|
||||
}
|
||||
if (o instanceof CTCellSmartTagPr) {
|
||||
CTCellSmartTagPr pr = (CTCellSmartTagPr) o;
|
||||
pr.setKey(scrambler.scrambleLetters(pr.getKey()));
|
||||
pr.setVal(scrambler.scramble(pr.getVal()));
|
||||
return Action.SKIP_CHILDREN;
|
||||
}
|
||||
if (o instanceof CTSmartTagType) {
|
||||
CTSmartTagType st = (CTSmartTagType) o;
|
||||
st.setName(scrambler.scrambleLetters(st.getName()));
|
||||
st.setUrl(MetadataScrubber.EXTERNAL_PLACEHOLDER);
|
||||
return Action.SKIP_CHILDREN;
|
||||
}
|
||||
if (o instanceof org.xlsx4j.com.microsoft.schemas.office.spreadsheetml.x2009.x9.main.CTListItem) {
|
||||
org.xlsx4j.com.microsoft.schemas.office.spreadsheetml.x2009.x9.main.CTListItem li =
|
||||
(org.xlsx4j.com.microsoft.schemas.office.spreadsheetml.x2009.x9.main.CTListItem) o;
|
||||
li.setVal(scrambler.scramble(li.getVal()));
|
||||
return Action.SKIP_CHILDREN;
|
||||
}
|
||||
|
||||
// ---- names
|
||||
if (o instanceof Sheet) {
|
||||
Sheet sheet = (Sheet) o;
|
||||
String mapped = names.sheet(sheet.getName());
|
||||
sheet.setName(mapped != null ? mapped : scrambler.consistent(sheet.getName()));
|
||||
return Action.SKIP_CHILDREN;
|
||||
}
|
||||
if (o instanceof CTDefinedName) {
|
||||
CTDefinedName dn = (CTDefinedName) o;
|
||||
dn.setName(Names.definedName(dn.getName()));
|
||||
dn.setValue(formulas.scrub(dn.getValue()));
|
||||
dn.setComment(scrambler.scramble(dn.getComment()));
|
||||
dn.setDescription(scrambler.scramble(dn.getDescription()));
|
||||
dn.setHelp(scrambler.scramble(dn.getHelp()));
|
||||
dn.setStatusBar(scrambler.scramble(dn.getStatusBar()));
|
||||
dn.setCustomMenu(scrambler.scramble(dn.getCustomMenu()));
|
||||
return Action.SKIP_CHILDREN;
|
||||
}
|
||||
if (o instanceof CTExternalDefinedName) {
|
||||
CTExternalDefinedName dn = (CTExternalDefinedName) o;
|
||||
dn.setName(Names.definedName(dn.getName()));
|
||||
dn.setRefersTo(formulas.scrub(dn.getRefersTo()));
|
||||
return Action.SKIP_CHILDREN;
|
||||
}
|
||||
if (o instanceof CTExternalSheetName) {
|
||||
CTExternalSheetName sn = (CTExternalSheetName) o;
|
||||
sn.setVal(scrambler.consistent(sn.getVal())); // as the formulas' '[1]Sheet'! map it
|
||||
return Action.SKIP_CHILDREN;
|
||||
}
|
||||
if (o instanceof CTTable) {
|
||||
CTTable t = (CTTable) o;
|
||||
t.setName(scrambler.consistentIdentifier(t.getName()));
|
||||
t.setDisplayName(scrambler.consistentIdentifier(t.getDisplayName())); // as structured references map it; no spaces
|
||||
t.setComment(scrambler.scramble(t.getComment()));
|
||||
return Action.CONTINUE;
|
||||
}
|
||||
if (o instanceof CTTableColumn) {
|
||||
CTTableColumn col = (CTTableColumn) o;
|
||||
col.setName(scrambler.consistent(col.getName())); // must equal the header cell's text
|
||||
col.setTotalsRowLabel(scrambler.consistent(col.getTotalsRowLabel()));
|
||||
col.setUniqueName(scrambler.consistent(col.getUniqueName()));
|
||||
return Action.CONTINUE; // its formulas
|
||||
}
|
||||
if (o instanceof CTTableStyleInfo) {
|
||||
CTTableStyleInfo info = (CTTableStyleInfo) o;
|
||||
if (info.getName() != null && !BUILT_IN_TABLE_STYLE.matcher(info.getName()).matches()) {
|
||||
info.setName(scrambler.consistent(info.getName()));
|
||||
}
|
||||
return Action.SKIP_CHILDREN;
|
||||
}
|
||||
if (o instanceof CTTableStyle) {
|
||||
CTTableStyle ts = (CTTableStyle) o;
|
||||
if (ts.getName() != null && !BUILT_IN_TABLE_STYLE.matcher(ts.getName()).matches()) {
|
||||
ts.setName(scrambler.consistent(ts.getName()));
|
||||
}
|
||||
return Action.CONTINUE;
|
||||
}
|
||||
if (o instanceof CTCellStyle) {
|
||||
CTCellStyle cs = (CTCellStyle) o;
|
||||
if (cs.getBuiltinId() == null) cs.setName(scrambler.scrambleLetters(cs.getName()));
|
||||
return Action.SKIP_CHILDREN;
|
||||
}
|
||||
if (o instanceof CTQueryTable) {
|
||||
CTQueryTable qt = (CTQueryTable) o;
|
||||
qt.setName(scrambler.consistentIdentifier(qt.getName()));
|
||||
return Action.CONTINUE;
|
||||
}
|
||||
if (o instanceof CTQueryTableField) {
|
||||
CTQueryTableField f = (CTQueryTableField) o;
|
||||
f.setName(scrambler.consistent(f.getName())); // as the table column of the same name
|
||||
return Action.CONTINUE;
|
||||
}
|
||||
if (o instanceof CTDataRef) {
|
||||
CTDataRef ref = (CTDataRef) o;
|
||||
if (ref.getSheet() != null) {
|
||||
String mapped = names.sheet(ref.getSheet());
|
||||
ref.setSheet(mapped != null ? mapped : scrambler.consistent(ref.getSheet()));
|
||||
}
|
||||
if (ref.getName() != null) ref.setName(Names.definedName(ref.getName()));
|
||||
return Action.SKIP_CHILDREN;
|
||||
}
|
||||
if (o instanceof CTCustomWorkbookView) {
|
||||
CTCustomWorkbookView v = (CTCustomWorkbookView) o;
|
||||
v.setName(scrambler.scramble(v.getName()));
|
||||
return Action.CONTINUE;
|
||||
}
|
||||
if (o instanceof CTFunctionGroup) {
|
||||
CTFunctionGroup g = (CTFunctionGroup) o;
|
||||
g.setName(scrambler.scramble(g.getName()));
|
||||
return Action.SKIP_CHILDREN;
|
||||
}
|
||||
if (o instanceof CTWebPublishItem) {
|
||||
CTWebPublishItem item = (CTWebPublishItem) o;
|
||||
item.setTitle(scrambler.scramble(item.getTitle()));
|
||||
item.setDestinationFile(null);
|
||||
item.setDivId(scrambler.scrambleLetters(item.getDivId()));
|
||||
item.setSourceObject(scrambler.scrambleLetters(item.getSourceObject()));
|
||||
return Action.SKIP_CHILDREN;
|
||||
}
|
||||
if (o instanceof CTMap) {
|
||||
CTMap map = (CTMap) o;
|
||||
map.setName(scrambler.scrambleLetters(map.getName()));
|
||||
map.setRootElement(scrambler.scrambleLetters(map.getRootElement()));
|
||||
map.setSchemaID(scrambler.scrambleLetters(map.getSchemaID()));
|
||||
return Action.CONTINUE;
|
||||
}
|
||||
|
||||
// ---- identities and secrets
|
||||
if (o instanceof CTAuthors) {
|
||||
List<String> authors = ((CTAuthors) o).getAuthor();
|
||||
for (int i = 0; i < authors.size(); i++) authors.set(i, names.author(authors.get(i)));
|
||||
return Action.SKIP_CHILDREN;
|
||||
}
|
||||
if (o instanceof CTFileSharing) {
|
||||
CTFileSharing fs = (CTFileSharing) o;
|
||||
fs.setUserName(names.author(fs.getUserName()));
|
||||
fs.setReservationPassword(null);
|
||||
fs.setHashValue(null);
|
||||
fs.setSaltValue(null);
|
||||
fs.setAlgorithmName(null);
|
||||
fs.setSpinCount(null);
|
||||
return Action.SKIP_CHILDREN;
|
||||
}
|
||||
if (o instanceof CTSheetProtection) {
|
||||
CTSheetProtection p = (CTSheetProtection) o;
|
||||
p.setPassword(null);
|
||||
p.setHashValue(null);
|
||||
p.setSaltValue(null);
|
||||
p.setAlgorithmName(null);
|
||||
p.setSpinCount(null);
|
||||
return Action.SKIP_CHILDREN;
|
||||
}
|
||||
if (o instanceof CTWorkbookProtection) {
|
||||
CTWorkbookProtection p = (CTWorkbookProtection) o;
|
||||
p.setWorkbookPassword(null);
|
||||
p.setRevisionsPassword(null);
|
||||
p.setWorkbookHashValue(null);
|
||||
p.setWorkbookSaltValue(null);
|
||||
p.setWorkbookAlgorithmName(null);
|
||||
p.setWorkbookSpinCount(null);
|
||||
p.setRevisionsHashValue(null);
|
||||
p.setRevisionsSaltValue(null);
|
||||
p.setRevisionsAlgorithmName(null);
|
||||
p.setRevisionsSpinCount(null);
|
||||
return Action.SKIP_CHILDREN;
|
||||
}
|
||||
|
||||
// ---- connections and links
|
||||
if (o instanceof CTConnection) {
|
||||
CTConnection c = (CTConnection) o;
|
||||
if (isModelOrQueryConnection(c)) {
|
||||
// the data model (xl/model/item.data) and the Power Query mashup (customXml) are
|
||||
// removed whatever the mode; a connection to either would be a repair prompt
|
||||
notes.accept("connection removed (data model or Power Query): " + c.getName());
|
||||
return Action.REMOVE;
|
||||
}
|
||||
c.setName(scrambler.scrambleLetters(c.getName()));
|
||||
c.setDescription(scrambler.scramble(c.getDescription()));
|
||||
c.setSourceFile(null);
|
||||
c.setOdcFile(null);
|
||||
c.setSingleSignOnId(null);
|
||||
if (c.getOlapPr() != null) c.getOlapPr().setLocalConnection(null);
|
||||
return Action.CONTINUE;
|
||||
}
|
||||
if (o instanceof CTDbPr) {
|
||||
CTDbPr db = (CTDbPr) o;
|
||||
db.setConnection("Provider=None");
|
||||
db.setCommand(scrambler.scramble(db.getCommand()));
|
||||
db.setServerCommand(scrambler.scramble(db.getServerCommand()));
|
||||
return Action.SKIP_CHILDREN;
|
||||
}
|
||||
if (o instanceof CTWebPr) {
|
||||
CTWebPr web = (CTWebPr) o;
|
||||
if (web.getUrl() != null) web.setUrl(MetadataScrubber.EXTERNAL_PLACEHOLDER);
|
||||
web.setPost(null);
|
||||
web.setEditPage(null);
|
||||
return Action.CONTINUE;
|
||||
}
|
||||
if (o instanceof CTTextPr) {
|
||||
((CTTextPr) o).setSourceFile(null);
|
||||
return Action.CONTINUE;
|
||||
}
|
||||
if (o instanceof CTParameter) {
|
||||
CTParameter p = (CTParameter) o;
|
||||
p.setName(scrambler.scrambleLetters(p.getName()));
|
||||
p.setPrompt(scrambler.scramble(p.getPrompt()));
|
||||
p.setString(scrambler.scramble(p.getString()));
|
||||
p.setCell(formulas.scrub(p.getCell()));
|
||||
return Action.SKIP_CHILDREN;
|
||||
}
|
||||
if (o instanceof org.xlsx4j.com.microsoft.schemas.office.spreadsheetml.x2010.x11.main.CTOledbPr) {
|
||||
org.xlsx4j.com.microsoft.schemas.office.spreadsheetml.x2010.x11.main.CTOledbPr pr =
|
||||
(org.xlsx4j.com.microsoft.schemas.office.spreadsheetml.x2010.x11.main.CTOledbPr) o;
|
||||
pr.setConnection("Provider=None");
|
||||
if (pr.getDbCommand() != null) pr.getDbCommand().setText(scrambler.scramble(pr.getDbCommand().getText()));
|
||||
return Action.CONTINUE; // its dbTables
|
||||
}
|
||||
if (o instanceof org.xlsx4j.com.microsoft.schemas.office.spreadsheetml.x2010.x11.main.CTDataFeedPr) {
|
||||
((org.xlsx4j.com.microsoft.schemas.office.spreadsheetml.x2010.x11.main.CTDataFeedPr) o).setConnection(null);
|
||||
return Action.CONTINUE;
|
||||
}
|
||||
if (o instanceof org.xlsx4j.com.microsoft.schemas.office.spreadsheetml.x2010.x11.main.CTDbTable) {
|
||||
org.xlsx4j.com.microsoft.schemas.office.spreadsheetml.x2010.x11.main.CTDbTable t =
|
||||
(org.xlsx4j.com.microsoft.schemas.office.spreadsheetml.x2010.x11.main.CTDbTable) o;
|
||||
t.setName(scrambler.scramble(t.getName()));
|
||||
return Action.SKIP_CHILDREN;
|
||||
}
|
||||
if (o instanceof CTDdeLink) {
|
||||
CTDdeLink dde = (CTDdeLink) o;
|
||||
dde.setDdeService(scrambler.scrambleLetters(dde.getDdeService()));
|
||||
dde.setDdeTopic(scrambler.scrambleLetters(dde.getDdeTopic()));
|
||||
return Action.CONTINUE;
|
||||
}
|
||||
if (o instanceof CTDdeItem) {
|
||||
CTDdeItem item = (CTDdeItem) o;
|
||||
item.setName(scrambler.scrambleLetters(item.getName()));
|
||||
return Action.CONTINUE;
|
||||
}
|
||||
if (o instanceof CTOleItem) {
|
||||
CTOleItem item = (CTOleItem) o;
|
||||
item.setName(scrambler.scrambleLetters(item.getName()));
|
||||
return Action.SKIP_CHILDREN;
|
||||
}
|
||||
|
||||
if (o instanceof org.xlsx4j.sml.CTObjectPr) {
|
||||
// an OLE object's properties (KEEP: the object stays)
|
||||
org.xlsx4j.sml.CTObjectPr pr = (org.xlsx4j.sml.CTObjectPr) o;
|
||||
pr.setAltText(scrambler.scramble(pr.getAltText()));
|
||||
pr.setMacro(null);
|
||||
return Action.CONTINUE;
|
||||
}
|
||||
if (o instanceof org.xlsx4j.sml.CTControlPr) {
|
||||
// an ActiveX control's properties (KEEP: the control stays)
|
||||
org.xlsx4j.sml.CTControlPr pr = (org.xlsx4j.sml.CTControlPr) o;
|
||||
pr.setAltText(scrambler.scramble(pr.getAltText()));
|
||||
pr.setMacro(null);
|
||||
pr.setLinkedCell(formulas.scrub(pr.getLinkedCell()));
|
||||
pr.setListFillRange(formulas.scrub(pr.getListFillRange()));
|
||||
return Action.CONTINUE;
|
||||
}
|
||||
|
||||
// ---- sparklines and form controls (x14)
|
||||
if (o instanceof org.xlsx4j.com.microsoft.schemas.office.spreadsheetml.x2009.x9.main.CTSparklineGroup) {
|
||||
org.xlsx4j.com.microsoft.schemas.office.spreadsheetml.x2009.x9.main.CTSparklineGroup g =
|
||||
(org.xlsx4j.com.microsoft.schemas.office.spreadsheetml.x2009.x9.main.CTSparklineGroup) o;
|
||||
g.setF(formulas.scrub(g.getF()));
|
||||
return Action.CONTINUE;
|
||||
}
|
||||
if (o instanceof org.xlsx4j.com.microsoft.schemas.office.spreadsheetml.x2009.x9.main.CTSparkline) {
|
||||
org.xlsx4j.com.microsoft.schemas.office.spreadsheetml.x2009.x9.main.CTSparkline sp =
|
||||
(org.xlsx4j.com.microsoft.schemas.office.spreadsheetml.x2009.x9.main.CTSparkline) o;
|
||||
sp.setF(formulas.scrub(sp.getF()));
|
||||
return Action.SKIP_CHILDREN;
|
||||
}
|
||||
if (o instanceof org.xlsx4j.com.microsoft.schemas.office.spreadsheetml.x2009.x9.main.CTFormControlPr) {
|
||||
org.xlsx4j.com.microsoft.schemas.office.spreadsheetml.x2009.x9.main.CTFormControlPr pr =
|
||||
(org.xlsx4j.com.microsoft.schemas.office.spreadsheetml.x2009.x9.main.CTFormControlPr) o;
|
||||
pr.setFmlaLink(formulas.scrub(pr.getFmlaLink()));
|
||||
pr.setFmlaRange(formulas.scrub(pr.getFmlaRange()));
|
||||
pr.setFmlaTxbx(formulas.scrub(pr.getFmlaTxbx()));
|
||||
pr.setFmlaGroup(formulas.scrub(pr.getFmlaGroup()));
|
||||
return Action.CONTINUE; // its list items
|
||||
}
|
||||
|
||||
// ---- STRICT: what goes with the parts the tool removes
|
||||
if (strict) {
|
||||
if (o instanceof CTPivotCaches) {
|
||||
notes.accept("pivotCaches removed (the pivot tables and caches go; their cells stay as values)");
|
||||
return Action.REMOVE;
|
||||
}
|
||||
if (o instanceof CTOleObjects) {
|
||||
// the objects' preview pictures (objectPr r:id, the same image the VML shape shows)
|
||||
// become the labelled placeholder
|
||||
for (org.xlsx4j.sml.CTOleObject ole : ((CTOleObjects) o).getOleObject()) {
|
||||
if (ole.getObjectPr() != null && ole.getObjectPr().getId() != null) {
|
||||
objectPreviews.add(currentPartName + "#" + ole.getObjectPr().getId());
|
||||
}
|
||||
}
|
||||
notes.accept("oleObjects removed (their pictures stay, as the labelled placeholder)");
|
||||
return Action.REMOVE;
|
||||
}
|
||||
if (o instanceof CTControls) {
|
||||
notes.accept("controls removed (ActiveX)");
|
||||
return Action.REMOVE;
|
||||
}
|
||||
if (o instanceof org.xlsx4j.sml.CTExtension) {
|
||||
Object any = XmlUtils.unwrap(((org.xlsx4j.sml.CTExtension) o).getAny());
|
||||
if (any != null && REMOVED_EXTENSIONS.contains(any.getClass().getSimpleName())) {
|
||||
notes.accept(any.getClass().getSimpleName().substring(2) + " extension removed");
|
||||
return Action.REMOVE;
|
||||
}
|
||||
}
|
||||
if (o instanceof org.docx4j.dml.spreadsheetdrawing.CTTwoCellAnchor
|
||||
&& holdsSlicerOrTimeline((org.docx4j.dml.spreadsheetdrawing.CTTwoCellAnchor) o)) {
|
||||
notes.accept("slicer or timeline drawing removed");
|
||||
return Action.REMOVE;
|
||||
}
|
||||
}
|
||||
|
||||
return Action.CONTINUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* A connection into the data model (x15:connection model="1", or type 102: a worksheet
|
||||
* range fed to the model) or a Power Query connection (type 100, whose mashup lives in
|
||||
* the customXml the tool removes): removed with them. Classic ODBC, OLEDB, web and text
|
||||
* connections stay, scrubbed.
|
||||
*/
|
||||
private static boolean isModelOrQueryConnection(CTConnection c) {
|
||||
if (c.getType() != null && (c.getType() == 100 || c.getType() == 102)) return true;
|
||||
if (c.getExtLst() != null) {
|
||||
for (org.xlsx4j.sml.CTExtension ext : c.getExtLst().getExt()) {
|
||||
Object any = XmlUtils.unwrap(ext.getAny());
|
||||
if (any instanceof org.xlsx4j.com.microsoft.schemas.office.spreadsheetml.x2010.x11.main.CTConnection
|
||||
&& Boolean.TRUE.equals(((org.xlsx4j.com.microsoft.schemas.office.spreadsheetml.x2010.x11.main.CTConnection) any).isModel())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/** the x14/x15 workbook and sheet extensions which point at removed parts: pivot caches, slicers, timelines, the data model */
|
||||
private static final java.util.Set<String> REMOVED_EXTENSIONS = new java.util.HashSet<String>(java.util.Arrays.asList(
|
||||
"CTPivotCaches", "CTSlicerCaches", "CTSlicerRefs", "CTTimelineCacheRefs", "CTTimelineRefs", "CTDataModel"));
|
||||
|
||||
private static boolean holdsSlicerOrTimeline(org.docx4j.dml.spreadsheetdrawing.CTTwoCellAnchor anchor) {
|
||||
return holdsSlicerOrTimeline(anchor.getGraphicFrame()) || holdsSlicerOrTimeline(anchor.getAlternateContent());
|
||||
}
|
||||
|
||||
private static boolean holdsSlicerOrTimeline(Object o) {
|
||||
if (o == null) return false;
|
||||
Object u = XmlUtils.unwrap(o);
|
||||
if (u instanceof org.docx4j.mce.AlternateContent) {
|
||||
org.docx4j.mce.AlternateContent ac = (org.docx4j.mce.AlternateContent) u;
|
||||
for (org.docx4j.mce.AlternateContent.Choice c : ac.getChoice()) {
|
||||
for (Object x : c.getAny()) if (holdsSlicerOrTimeline(x)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
if (u instanceof org.docx4j.dml.spreadsheetdrawing.CTGraphicalObjectFrame) {
|
||||
org.docx4j.dml.spreadsheetdrawing.CTGraphicalObjectFrame frame = (org.docx4j.dml.spreadsheetdrawing.CTGraphicalObjectFrame) u;
|
||||
if (frame.getGraphic() != null && frame.getGraphic().getGraphicData() != null) {
|
||||
String uri = frame.getGraphic().getGraphicData().getUri();
|
||||
return uri != null && (uri.endsWith("/slicer") || uri.endsWith("/timeslicer"));
|
||||
}
|
||||
}
|
||||
if (u instanceof org.w3c.dom.Element) {
|
||||
// an xdr:graphicFrame the mc:Choice left as DOM: look for its graphicData
|
||||
org.w3c.dom.Element el = (org.w3c.dom.Element) u;
|
||||
org.w3c.dom.NodeList datas = el.getElementsByTagNameNS("http://schemas.openxmlformats.org/drawingml/2006/main", "graphicData");
|
||||
for (int i = 0; i < datas.getLength(); i++) {
|
||||
String uri = ((org.w3c.dom.Element) datas.item(i)).getAttribute("uri");
|
||||
if (uri.endsWith("/slicer") || uri.endsWith("/timeslicer")) return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/** a cell value by type: strings scrambled, numbers randomised, booleans and errors kept, dates fixed */
|
||||
private void cellValue(STCellType type, String v, Consumer<String> set) {
|
||||
if (v == null) return;
|
||||
if (type == null || type == STCellType.N) {
|
||||
set.accept(scrambler.number(v));
|
||||
} else if (type == STCellType.STR) {
|
||||
set.accept(scrambler.consistent(v));
|
||||
}
|
||||
// S: an index into the shared strings; B and E: nothing to hide; INLINE_STR: the is child;
|
||||
// a strict workbook's t="d" date cell is a serial number by the time it is loaded
|
||||
// (xlsx-preprocessor.xslt, StrictCellDates)
|
||||
}
|
||||
|
||||
/**
|
||||
* A rich text string: scrambled as one string ({@link ScrambleText#consistent}),
|
||||
* then dealt back to its runs by length, so a word split across runs stays one
|
||||
* word and the same text always gives the same output.
|
||||
*/
|
||||
private void richText(CTRst rst) {
|
||||
StringBuilder full = new StringBuilder();
|
||||
if (rst.getT() != null && rst.getT().getValue() != null) full.append(rst.getT().getValue());
|
||||
for (CTRElt r : rst.getR()) {
|
||||
if (r.getT() != null && r.getT().getValue() != null) full.append(r.getT().getValue());
|
||||
}
|
||||
String out = scrambler.consistent(full.toString());
|
||||
int at = 0;
|
||||
if (rst.getT() != null && rst.getT().getValue() != null) {
|
||||
at = deal(rst.getT(), out, at);
|
||||
}
|
||||
for (CTRElt r : rst.getR()) {
|
||||
if (r.getT() != null && r.getT().getValue() != null) at = deal(r.getT(), out, at);
|
||||
}
|
||||
for (CTPhoneticRun ph : rst.getRPh()) {
|
||||
if (ph.getT() != null) ph.getT().setValue(scrambler.scramble(ph.getT().getValue()));
|
||||
}
|
||||
}
|
||||
|
||||
private int deal(CTXstringWhitespace t, String out, int at) {
|
||||
int len = t.getValue().length();
|
||||
int end = Math.min(at + len, out.length());
|
||||
String piece = out.substring(Math.min(at, out.length()), end);
|
||||
if (piece.length() < len) piece = piece + scrambler.scramble(t.getValue().substring(piece.length()));
|
||||
t.setValue(piece);
|
||||
return at + len;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,437 @@
|
||||
/*
|
||||
* Copyright 2026, Plutext Pty Ltd.
|
||||
*
|
||||
* This file is part of docx4j.
|
||||
|
||||
docx4j is licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
||||
*/
|
||||
package org.docx4j.anon;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.docx4j.XmlUtils;
|
||||
import org.docx4j.openpackaging.Base;
|
||||
import org.docx4j.openpackaging.packages.OpcPackage;
|
||||
import org.docx4j.openpackaging.parts.JaxbXmlPart;
|
||||
import org.docx4j.openpackaging.parts.Part;
|
||||
import org.docx4j.openpackaging.parts.WordprocessingML.StyleDefinitionsPart;
|
||||
import org.docx4j.openpackaging.parts.XmlPart;
|
||||
import org.docx4j.openpackaging.parts.relationships.RelationshipsPart;
|
||||
import org.docx4j.relationships.Relationship;
|
||||
import org.docx4j.wml.Style;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.w3c.dom.Attr;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.NamedNodeMap;
|
||||
import org.w3c.dom.Node;
|
||||
import org.w3c.dom.NodeList;
|
||||
|
||||
/**
|
||||
* Proves the guarantee on one document: no token of the original survives in
|
||||
* the anonymised output.
|
||||
* <p>
|
||||
* A token is a run of three or more letters (any script), lower-cased. The
|
||||
* extraction takes, from every XML part, every text node, the values of the
|
||||
* attributes known to carry names, descriptions, instructions and identities
|
||||
* (see {@link #ATTRIBUTES}), and every external relationship target. A token of
|
||||
* the output which is also a token of the original is a leak unless it is in
|
||||
* the vocabulary the anonymiser itself writes:
|
||||
* <ul>
|
||||
* <li>a substring of a lorem ipsum word (the Latin scramble maps letters
|
||||
* positionally onto lorem text, so an output word is always a fragment of one
|
||||
* lorem word) - the blind spot is an original word which is itself such a
|
||||
* fragment ({@code sit}, {@code amet}, {@code class}); such a word is in the
|
||||
* lorem vocabulary and carries nothing;</li>
|
||||
* <li>a field keyword or format-switch word ({@link FieldInstructions#KEYWORDS});</li>
|
||||
* <li>a date or number picture ({@code yyyy}, {@code MMMM});</li>
|
||||
* <li>a word of a built-in style name (KnownStyles.xml and the document's own
|
||||
* latent-style list), which Word identifies built-in styles by;</li>
|
||||
* <li>the fixed words the tool writes: Author, Sheet, example, invalid,
|
||||
* http, None, General, true, false, app.xml's "Microsoft Office Word" (or
|
||||
* PowerPoint), the altChunk marker's "altChunk removed by docx4j anon", the
|
||||
* "Object n" name of the picture a removed OLE frame becomes, and the colour
|
||||
* names a number format code can carry ({@code [Red]}).</li>
|
||||
* </ul>
|
||||
* Digits are not tokens: they are randomised, and a 4-digit run would collide
|
||||
* by chance often enough to make the check useless. GUIDs (table style ids,
|
||||
* creation ids) are skipped: their hex runs are not words. In a formula (a
|
||||
* cell's f, a defined name, a chart's c:f, ...) only the string literals and the
|
||||
* names are tokens ({@link #formulaTokens}): function names, references and
|
||||
* error literals are the language; a header or footer's & codes likewise.
|
||||
*
|
||||
* @since 17.2.0
|
||||
*/
|
||||
public class Verify {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(Verify.class);
|
||||
|
||||
/** A token of the original found in the output. */
|
||||
public static class Leak {
|
||||
public final String partName;
|
||||
public final String where;
|
||||
public final String token;
|
||||
|
||||
Leak(String partName, String where, String token) {
|
||||
this.partName = partName;
|
||||
this.where = where;
|
||||
this.token = token;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return partName + " " + where + ": \"" + token + "\"";
|
||||
}
|
||||
}
|
||||
|
||||
/** (element local name, attribute local name) pairs whose values are extracted. "*" matches any element. */
|
||||
public static final Set<String> ATTRIBUTES = new HashSet<String>(Arrays.asList(
|
||||
"*/author", "*/initials", "*/contact", "*/userId",
|
||||
"alias/val", "tag/val",
|
||||
"hyperlink/tooltip", "hyperlink/docLocation", "hlinkClick/tooltip", "hlinkHover/tooltip",
|
||||
"docVar/name", "docVar/val",
|
||||
"fldSimple/instr",
|
||||
"docPr/name", "docPr/descr", "docPr/title",
|
||||
"cNvPr/name", "cNvPr/descr", "cNvPr/title",
|
||||
"imagedata/title", "shape/alt", "shape/title", "shape/href", "textpath/string",
|
||||
"listItem/displayText", "listItem/value",
|
||||
"name/val", "aliases/val", "docPart/val",
|
||||
"attr/val", "attr/name",
|
||||
"lvlText/val",
|
||||
"prSet/phldrT",
|
||||
"bookmarkStart/name", "moveFromRangeStart/name", "moveToRangeStart/name",
|
||||
"default/val", "listEntry/val", "helpText/val", "statusText/val",
|
||||
"placeholder/val",
|
||||
"connectString/val", "query/val", "mailSubject/val", "addressFieldName/val",
|
||||
"theme/name", "clrScheme/name", "fontScheme/name", "fmtScheme/name",
|
||||
"pivotSource/name", "strDim/name", "numDim/name", "lvl/name",
|
||||
// PresentationML (CR-019 phase 2): comment authors (legacy and 2018), tags, section and
|
||||
// custom-show names, layout names, sound file names, object names, table style names
|
||||
"cmAuthor/name", "author/name", "tag/name", "section/name", "custShow/name", "cSld/name",
|
||||
"snd/name", "oleObj/name", "control/name", "tblStyle/styleName",
|
||||
"hlinkClick/invalidUrl", "hlinkHover/invalidUrl",
|
||||
// SpreadsheetML (CR-019 phase 3): defined names, tables and columns, sheet names, hyperlink
|
||||
// text, validation messages, connections and query tables, filters, links, persons
|
||||
"definedName/name", "definedName/comment", "definedName/description", "definedName/help",
|
||||
"definedName/statusBar", "definedName/customMenu", "externalDefinedName/name",
|
||||
"table/name", "table/displayName", "table/comment",
|
||||
"tableColumn/name", "tableColumn/totalsRowLabel", "tableColumn/uniqueName",
|
||||
"tableStyleInfo/name", "tableStyle/name", "cellStyle/name",
|
||||
"sheet/name", "sheetName/val", "dataRef/sheet", "dataRef/name",
|
||||
"hyperlink/display",
|
||||
"dataValidation/prompt", "dataValidation/promptTitle", "dataValidation/error", "dataValidation/errorTitle",
|
||||
"cfRule/text", "customFilter/val", "filter/val", "listItem/val",
|
||||
"connection/name", "connection/description", "connection/sourceFile", "connection/odcFile",
|
||||
"dbPr/connection", "dbPr/command", "dbPr/serverCommand", "webPr/url", "webPr/post", "textPr/sourceFile",
|
||||
"oledbPr/connection", "dataFeedPr/connection", "dbTable/name", "olapPr/localConnection",
|
||||
"parameter/name", "parameter/prompt", "parameter/string",
|
||||
"queryTable/name", "queryTableField/name",
|
||||
"fileSharing/userName", "ddeLink/ddeService", "ddeLink/ddeTopic", "ddeItem/name", "oleItem/name",
|
||||
"person/displayName", "cellSmartTagPr/key", "cellSmartTagPr/val", "smartTagType/name", "smartTagType/url",
|
||||
"webPublishItem/title", "webPublishItem/destinationFile", "customWorkbookView/name", "functionGroup/name",
|
||||
"map/name", "map/rootElement", "map/schemaID",
|
||||
"Relationship/Target"));
|
||||
|
||||
/** attributes whose value is a formula: only its literals and names are tokens */
|
||||
static final Set<String> FORMULA_ATTRIBUTES = new HashSet<String>(Arrays.asList(
|
||||
"externalDefinedName/refersTo", "hyperlink/location", "parameter/cell",
|
||||
"formControlPr/fmlaLink", "formControlPr/fmlaRange", "formControlPr/fmlaTxbx", "formControlPr/fmlaGroup"));
|
||||
|
||||
/** elements whose text is a formula */
|
||||
static final Set<String> FORMULA_ELEMENTS = new HashSet<String>(Arrays.asList(
|
||||
"f", "formula", "formula1", "formula2", "definedName", "calculatedColumnFormula", "totalsRowFormula",
|
||||
"FmlaLink", "FmlaRange", "FmlaTxbx", "FmlaGroup", "FmlaMacro"));
|
||||
|
||||
/** elements whose text is a spreadsheet header or footer, with & codes */
|
||||
static final Set<String> HEADER_FOOTER_ELEMENTS = new HashSet<String>(Arrays.asList(
|
||||
"oddHeader", "oddFooter", "evenHeader", "evenFooter", "firstHeader", "firstFooter"));
|
||||
|
||||
private static final Pattern PICTURE = Pattern.compile("^(d+|m+|y+|h+|s+)$");
|
||||
|
||||
private static final Set<String> FIXED_WORDS = new HashSet<String>(Arrays.asList(
|
||||
"author", "sheet", "example", "invalid", "http", "none", "general", "true", "false",
|
||||
"microsoft", "office", "word", "powerpoint", "excel", "macintosh", // app.xml Application
|
||||
"provider", // the connection string a data connection becomes (Provider=None)
|
||||
// the altChunk marker paragraph (Placeholders.ALTCHUNK_REMOVED)
|
||||
"altchunk", "removed", "docx", "anon",
|
||||
// the picture a removed OLE graphic frame becomes in a pptx ("Object n")
|
||||
"object",
|
||||
// the colour names of a number format code ([Red]-#,##0), chart and cell formats
|
||||
"black", "blue", "cyan", "green", "magenta", "red", "white", "yellow"));
|
||||
|
||||
/** a GUID, braced or not: structure (table style ids, creation ids), never text */
|
||||
private static final Pattern GUID = Pattern.compile(
|
||||
"\\{?[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}\\}?");
|
||||
|
||||
/** The tokens of one package: per part, token to the first place it was seen. */
|
||||
public static class Extraction {
|
||||
public final Map<String, Map<String, String>> byPart = new LinkedHashMap<String, Map<String, String>>();
|
||||
/** words of the document's own latent-style names: built-in, allowed */
|
||||
public final Set<String> styleWords = new HashSet<String>();
|
||||
|
||||
public Set<String> allTokens() {
|
||||
Set<String> all = new HashSet<String>();
|
||||
for (Map<String, String> m : byPart.values()) all.addAll(m.keySet());
|
||||
return all;
|
||||
}
|
||||
|
||||
void add(String partName, String token, String where) {
|
||||
Map<String, String> m = byPart.get(partName);
|
||||
if (m == null) {
|
||||
m = new HashMap<String, String>();
|
||||
byPart.put(partName, m);
|
||||
}
|
||||
String existing = m.get(token);
|
||||
// prefer a place where a built-in style word is not allowed, so the check is not
|
||||
// disarmed by the same word turning up in a style name first
|
||||
if (existing == null || (STYLE_NAME_PLACES.contains(existing) && !STYLE_NAME_PLACES.contains(where))) {
|
||||
m.put(token, where);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The check: extract both, compare.
|
||||
*/
|
||||
public static List<Leak> noOriginalText(OpcPackage original, OpcPackage anonymised) {
|
||||
return compare(extract(original), extract(anonymised));
|
||||
}
|
||||
|
||||
/**
|
||||
* The tokens of the anonymised package which the original also had, less the
|
||||
* anonymiser's own vocabulary.
|
||||
*/
|
||||
public static List<Leak> compare(Extraction original, Extraction anonymised) {
|
||||
Set<String> originalTokens = original.allTokens();
|
||||
Set<String> allowedStyleWords = new HashSet<String>(anonymised.styleWords);
|
||||
allowedStyleWords.addAll(knownStyleWords());
|
||||
|
||||
List<Leak> leaks = new ArrayList<Leak>();
|
||||
for (Map.Entry<String, Map<String, String>> part : anonymised.byPart.entrySet()) {
|
||||
for (Map.Entry<String, String> e : part.getValue().entrySet()) {
|
||||
String token = e.getKey();
|
||||
if (!originalTokens.contains(token)) continue;
|
||||
if (isAllowed(token, e.getValue(), allowedStyleWords)) continue;
|
||||
leaks.add(new Leak(part.getKey(), e.getValue(), token));
|
||||
}
|
||||
}
|
||||
return leaks;
|
||||
}
|
||||
|
||||
/** the places a built-in style name legitimately appears */
|
||||
private static final Set<String> STYLE_NAME_PLACES = new HashSet<String>(Arrays.asList(
|
||||
"name/@val", "aliases/@val", "instrText/text()", "delInstrText/text()", "fldSimple/@instr"));
|
||||
|
||||
static boolean isAllowed(String token, String where, Set<String> styleWords) {
|
||||
if (FIXED_WORDS.contains(token)) return true;
|
||||
if (FieldInstructions.KEYWORDS.contains(token.toUpperCase(Locale.ROOT))) return true;
|
||||
if (PICTURE.matcher(token).matches()) return true;
|
||||
if (STYLE_NAME_PLACES.contains(where) && styleWords.contains(token)) return true;
|
||||
return isLoremFragment(token);
|
||||
}
|
||||
|
||||
/** true if the token is a substring of a word of {@link Lorem#WORDS} */
|
||||
static boolean isLoremFragment(String token) {
|
||||
return Lorem.isFragment(token);
|
||||
}
|
||||
|
||||
private static Set<String> knownStyleWords;
|
||||
|
||||
static synchronized Set<String> knownStyleWords() {
|
||||
if (knownStyleWords == null) {
|
||||
Set<String> words = new HashSet<String>();
|
||||
for (Style s : StyleDefinitionsPart.getKnownStyles().values()) {
|
||||
if (s.getName() != null) words.addAll(tokens(s.getName().getVal()));
|
||||
}
|
||||
knownStyleWords = words;
|
||||
}
|
||||
return knownStyleWords;
|
||||
}
|
||||
|
||||
/** Every XML part's text nodes and named attributes, and every external target. */
|
||||
public static Extraction extract(OpcPackage pkg) {
|
||||
Extraction x = new Extraction();
|
||||
|
||||
externalTargets(pkg, x);
|
||||
for (Part p : new ArrayList<Part>(pkg.getParts().getParts().values())) {
|
||||
externalTargets(p, x);
|
||||
Document doc = null;
|
||||
try {
|
||||
if (p instanceof JaxbXmlPart) {
|
||||
JaxbXmlPart<?> jp = (JaxbXmlPart<?>) p;
|
||||
Object contents = jp.getContents();
|
||||
if (contents == null) continue;
|
||||
doc = XmlUtils.marshaltoW3CDomDocument(contents, jp.getJAXBContext());
|
||||
} else if (p instanceof XmlPart) {
|
||||
doc = ((XmlPart) p).getDocument();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("cannot extract " + p.getPartName().getName() + ": " + e);
|
||||
x.add(p.getPartName().getName(), " | ||||