Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ASP-28550] Docx with un-accepted changes #72

Open
wants to merge 6 commits into
base: trunk-airslate
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ subprojects {
apply plugin: 'de.thetaphi.forbiddenapis'
apply plugin: 'com.github.spotbugs'

version = '5.2.3-AIRSLATE-26.2'
version = '5.2.3-AIRSLATE-27.1'
ext {
bouncyCastleVersion = '1.70'
commonsCodecVersion = '1.15'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.apache.poi.ooxml.POIXMLDocumentPart;
import org.apache.poi.ooxml.util.POIXMLUnits;
Expand Down Expand Up @@ -146,7 +147,7 @@ private void buildRunsInOrderFromXml(XmlObject object) {
}
if (o instanceof CTRunTrackChange) {
for (CTR r : ((CTRunTrackChange) o).getRArray()) {
XWPFRun cr = new XWPFRun(r, this);
XWPFRun cr = new XWPFTrackChangeRun((CTRunTrackChange) o, r, this);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

раз уж мы тут вносим измнения, то может добавим сюда вот эти вот методы?
https://github.com/pdffiller/dadadocs-scripting-template-engine/pull/475/files#diff-52316947a9ce35960fd4534be4ce3cd1b30dc8d78081839e75a67628a04ce86dR26

runs.add(cr);
iruns.add(cr);
}
Expand Down Expand Up @@ -1987,4 +1988,36 @@ public boolean removeIRunElement(int irunPos) {
}
return false;
}

public boolean isInsCTP() {
CTP ctp = getCTP();
return (ctp.getPPr() != null && ctp.getPPr().getRPr() != null && ctp.getPPr().getRPr().getIns() != null);
}

public boolean isInsParagraph() {
if (!isInsCTP()) {
return false;
}
List<XWPFRun> runs = getRuns().stream()
.filter(run -> run instanceof XWPFTrackChangeRun)
.collect(Collectors.toList());

return (runs.size() == getRuns().size());
}

public boolean isDelCTP() {
CTP ctp = getCTP();
return (ctp.getPPr() != null && ctp.getPPr().getRPr() != null && ctp.getPPr().getRPr().getDel() != null);
}

public boolean isDelParagraph() {
if (!isDelCTP()) {
return false;
}
List<XWPFRun> runs = getRuns().stream()
.filter(run -> run instanceof XWPFTrackChangeRun)
.collect(Collectors.toList());

return (runs.size() == getRuns().size());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.apache.poi.xwpf.usermodel;

import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRunTrackChange;

public class XWPFTrackChangeRun extends XWPFRun {
private CTRunTrackChange trackChange;

public XWPFTrackChangeRun(CTRunTrackChange trackChange, CTR r, IRunBody p) {
super(r, p);
this.trackChange = trackChange;
}

public CTRunTrackChange getTrackChange() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Тут и в методах ниже this не обяз. писать для доступа к проперти

return trackChange;
}

public String getAuthor() {
return trackChange.getAuthor();
}

public void setAuthor(String author) {
trackChange.setAuthor(author);
}

public java.util.Calendar getDate() {
return trackChange.getDate();
}

public void setDate(java.util.Calendar date) {
trackChange.setDate(date);
}

public boolean isDel() {
return trackChange.getDomNode().getLocalName().equals("del");
}

public boolean isIns() {
return trackChange.getDomNode().getLocalName().equals("ins");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@ Licensed to the Apache Software Foundation (ASF) under one or more

package org.apache.poi.xwpf.usermodel;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.IOException;
import java.math.BigInteger;
import java.util.List;
Expand All @@ -34,21 +28,10 @@ Licensed to the Apache Software Foundation (ASF) under one or more
import org.openxmlformats.schemas.drawingml.x2006.picture.PicDocument;
import org.openxmlformats.schemas.drawingml.x2006.picture.impl.PicDocumentImpl;
import org.openxmlformats.schemas.officeDocument.x2006.sharedTypes.STOnOff1;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBookmark;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBorder;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTInd;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTJc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTOnOff;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPBdr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSpacing;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTextAlignment;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STBorder;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STJc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STLineSpacingRule;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTextAlignment;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;

import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* Tests for XWPF Paragraphs
Expand All @@ -74,6 +57,65 @@ void testHeaderParagraph() throws IOException {
}
}

@Test
void testUnAcceptedChanges() throws IOException {
try (XWPFDocument xml = XWPFTestDataSamples.openSampleDocument("UnAcceptedChangesTest.docx")) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Еще нужны тесты на remove аля
testRemoveRuns
Вот сюда нужно добавить новый вид ранов
testRemoveAndInsertRunsWithOtherIRunElement

Мб еще есть тесты в этом классе где мы вариантивно создаем разные раны - туда нужно тоже добавить новый ран

List<XWPFParagraph> paragraphs = xml.getParagraphs();
XWPFRun delRun = paragraphs.get(0).getRuns().get(3);
XWPFRun insRun = paragraphs.get(0).getRuns().get(4);

assertTrue(delRun instanceof XWPFTrackChangeRun);
assertTrue(insRun instanceof XWPFTrackChangeRun);
assertTrue(paragraphs.get(0).getRuns().get(1) instanceof XWPFTrackChangeRun);

assertEquals("2022-10-26T13:13:55Z", ((XWPFTrackChangeRun) delRun).getDate().toString());
assertEquals("Pavlo Vavilov", ((XWPFTrackChangeRun) delRun).getAuthor());

assertTrue(((XWPFTrackChangeRun) delRun).isDel());
assertTrue(((XWPFTrackChangeRun) insRun).isIns());
assertFalse(((XWPFTrackChangeRun) delRun).isIns());
assertFalse(((XWPFTrackChangeRun) insRun).isDel());
}
}

@Test
void testIsInsCTP() throws IOException {
try (XWPFDocument xml = XWPFTestDataSamples.openSampleDocument("UnAcceptedChangesTest.docx")) {
List<XWPFParagraph> paragraphs = xml.getParagraphs();
boolean insCTP = paragraphs.get(5).isInsCTP();
assertTrue(insCTP);
}
}

@Test
void testIsInsParagraph() throws IOException {
try (XWPFDocument xml = XWPFTestDataSamples.openSampleDocument("UnAcceptedChangesTest.docx")) {
List<XWPFParagraph> paragraphs = xml.getParagraphs();
boolean insCTP = paragraphs.get(5).isInsParagraph();
assertFalse(insCTP);
boolean insCTP2 = paragraphs.get(6).isInsParagraph();
assertTrue(insCTP2);
}
}

@Test
void testIsDelCTP() throws IOException {
try (XWPFDocument xml = XWPFTestDataSamples.openSampleDocument("UnAcceptedChangesTest.docx")) {
List<XWPFParagraph> paragraphs = xml.getParagraphs();
boolean delCTP = paragraphs.get(3).isDelCTP();
assertTrue(delCTP);
}
}

@Test
void testIsDelParagraph() throws IOException {
try (XWPFDocument xml = XWPFTestDataSamples.openSampleDocument("UnAcceptedChangesTest.docx")) {
List<XWPFParagraph> paragraphs = xml.getParagraphs();
boolean delCTP = paragraphs.get(3).isDelParagraph();
assertTrue(delCTP);
}
}

/**
* Check that we get the right paragraphs from the document
*/
Expand Down
Binary file added test-data/document/UnAcceptedChangesTest.docx
Binary file not shown.