刘光辉
3 小时以前 06f314545ac3514d7ce28939920866ae8188d31c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package jnpf.fileStorage.dms;
 
import jnpf.exception.DataException;
import org.junit.Test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import static org.junit.Assert.*;
 
public class DmsPageWatermarkAnchorsTest {
    @Test
    public void fixesOnlyMarkedDrawingsAndPreservesOtherArchiveParts() throws Exception {
        byte[] xml = ("<w:document xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" "
                + "xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\">"
                + anchor("DMS_CUSTOM_test_0_1")
                + anchor("original-image")
                + anchor("DMS_CUSTOM_test_0_3")
                + "</w:document>").getBytes(StandardCharsets.UTF_8);
        byte[] source = archive(xml);
        byte[] output = DmsPageWatermarkAnchors.fix(source, "DMS_CUSTOM_test", 2);
        javax.xml.parsers.DocumentBuilderFactory factory = javax.xml.parsers.DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        org.w3c.dom.Document document = factory.newDocumentBuilder().parse(new ByteArrayInputStream(part(output, "word/document.xml")));
        org.w3c.dom.NodeList anchors = document.getElementsByTagNameNS("*", "anchor");
        assertEquals("0", ((org.w3c.dom.Element) anchors.item(0)).getAttribute("layoutInCell"));
        assertEquals("1", ((org.w3c.dom.Element) anchors.item(1)).getAttribute("layoutInCell"));
        assertEquals("0", ((org.w3c.dom.Element) anchors.item(2)).getAttribute("layoutInCell"));
        assertPageAnchored((org.w3c.dom.Element) anchors.item(0));
        assertPageAnchored((org.w3c.dom.Element) anchors.item(2));
        org.w3c.dom.Element original = (org.w3c.dom.Element) anchors.item(1);
        assertEquals("column", ((org.w3c.dom.Element) original
                .getElementsByTagNameNS("*", "positionH").item(0)).getAttribute("relativeFrom"));
        assertEquals("123", original.getElementsByTagNameNS("*", "posOffset").item(0).getTextContent());
        assertArrayEquals(new byte[]{1, 2, 3}, part(output, "word/media/original.png"));
        assertArrayEquals(xml, part(source, "word/document.xml"));
        assertThrows(DataException.class, () -> DmsPageWatermarkAnchors.fix(source, "DMS_CUSTOM_test", 3));
    }
 
    private String anchor(String name) {
        return "<wp:anchor layoutInCell=\"1\" simplePos=\"1\">"
                + "<wp:simplePos x=\"12\" y=\"34\"/>"
                + "<wp:positionH relativeFrom=\"column\"><wp:posOffset>123</wp:posOffset></wp:positionH>"
                + "<wp:positionV relativeFrom=\"paragraph\"><wp:posOffset>456</wp:posOffset></wp:positionV>"
                + "<wp:docPr name=\"" + name + "\"/></wp:anchor>";
    }
 
    private void assertPageAnchored(org.w3c.dom.Element anchor) {
        assertEquals("1", anchor.getAttribute("locked"));
        assertEquals("0", anchor.getAttribute("simplePos"));
        org.w3c.dom.Element simple = (org.w3c.dom.Element) anchor
                .getElementsByTagNameNS("*", "simplePos").item(0);
        assertEquals("0", simple.getAttribute("x"));
        assertEquals("0", simple.getAttribute("y"));
        org.w3c.dom.Element horizontal = (org.w3c.dom.Element) anchor
                .getElementsByTagNameNS("*", "positionH").item(0);
        org.w3c.dom.Element vertical = (org.w3c.dom.Element) anchor
                .getElementsByTagNameNS("*", "positionV").item(0);
        assertEquals("page", horizontal.getAttribute("relativeFrom"));
        assertEquals("page", vertical.getAttribute("relativeFrom"));
        assertEquals("123", horizontal.getElementsByTagNameNS("*", "posOffset").item(0).getTextContent());
        assertEquals("456", vertical.getElementsByTagNameNS("*", "posOffset").item(0).getTextContent());
    }
 
    private byte[] archive(byte[] xml) throws Exception {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        try (ZipOutputStream zip = new ZipOutputStream(output)) {
            zip.putNextEntry(new ZipEntry("word/document.xml"));
            zip.write(xml);
            zip.closeEntry();
            zip.putNextEntry(new ZipEntry("word/media/original.png"));
            zip.write(new byte[]{1, 2, 3});
            zip.closeEntry();
        }
        return output.toByteArray();
    }
 
    private byte[] part(byte[] source, String name) throws Exception {
        try (ZipInputStream zip = new ZipInputStream(new ByteArrayInputStream(source))) {
            ZipEntry entry;
            while ((entry = zip.getNextEntry()) != null) {
                if (name.equals(entry.getName())) return zip.readAllBytes();
            }
        }
        throw new AssertionError("Missing " + name);
    }
}