From ab5cb372e5175d6ac88b14c7a3a84dcaefff5b6a Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Sun, 28 Aug 2022 12:01:53 +0000 Subject: [PATCH] do not rely on xml namespace prefixes - use the URIs git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1903728 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/ooxml/POIXMLTypeLoader.java | 3 +- .../poi/xwpf/usermodel/XWPFDocument.java | 1 + .../apache/poi/xwpf/usermodel/XWPFRun.java | 42 +++++++++++-------- .../poi/xwpf/usermodel/XWPFSignatureLine.java | 3 +- .../apache/poi/openxml4j/opc/TestPackage.java | 2 +- 5 files changed, 30 insertions(+), 21 deletions(-) diff --git a/poi-ooxml/src/main/java/org/apache/poi/ooxml/POIXMLTypeLoader.java b/poi-ooxml/src/main/java/org/apache/poi/ooxml/POIXMLTypeLoader.java index b30d4d0e0c..f2b6a721ae 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/ooxml/POIXMLTypeLoader.java +++ b/poi-ooxml/src/main/java/org/apache/poi/ooxml/POIXMLTypeLoader.java @@ -22,6 +22,7 @@ import java.util.HashMap; import java.util.Map; import org.apache.poi.openxml4j.opc.PackageNamespaces; +import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.xmlbeans.XmlOptions; public class POIXMLTypeLoader { @@ -61,7 +62,7 @@ public class POIXMLTypeLoader { map.put("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "r"); map.put("http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes", "vt"); map.put("http://schemas.openxmlformats.org/presentationml/2006/main", "p"); - map.put("http://schemas.openxmlformats.org/wordprocessingml/2006/main", "w"); + map.put(XWPFDocument.NS_OOXML_WP_MAIN, "w"); map.put("http://schemas.microsoft.com/office/word/2006/wordml", "wne"); map.put(MS_OFFICE_URN, "o"); map.put(MS_EXCEL_URN, "x"); diff --git a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java index 77886d52e9..2066e6ff74 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java @@ -101,6 +101,7 @@ import org.openxmlformats.schemas.wordprocessingml.x2006.main.StylesDocument; */ @SuppressWarnings("unused") public class XWPFDocument extends POIXMLDocument implements Document, IBody { + public static final String NS_OOXML_WP_MAIN = "http://schemas.openxmlformats.org/wordprocessingml/2006/main"; private static final Logger LOG = LogManager.getLogger(XWPFDocument.class); protected List footers = new ArrayList<>(); diff --git a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFRun.java b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFRun.java index c46229a0cf..13decacd78 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFRun.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFRun.java @@ -63,6 +63,7 @@ import org.openxmlformats.schemas.officeDocument.x2006.sharedTypes.STHexColorRGB import org.openxmlformats.schemas.officeDocument.x2006.sharedTypes.STOnOff1; import org.openxmlformats.schemas.officeDocument.x2006.sharedTypes.STVerticalAlignRun; import org.openxmlformats.schemas.wordprocessingml.x2006.main.*; +import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.w3c.dom.Text; import org.xml.sax.InputSource; @@ -108,7 +109,7 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun { pictTextObjs.addAll(Arrays.asList(r.getPictArray())); pictTextObjs.addAll(Arrays.asList(r.getDrawingArray())); for (XmlObject o : pictTextObjs) { - XmlObject[] ts = o.selectPath("declare namespace w='http://schemas.openxmlformats.org/wordprocessingml/2006/main' .//w:t"); + XmlObject[] ts = o.selectPath("declare namespace w='" + XWPFDocument.NS_OOXML_WP_MAIN + "' .//w:t"); for (XmlObject t : ts) { NodeList kids = t.getDomNode().getChildNodes(); for (int n = 0; n < kids.getLength(); n++) { @@ -1351,12 +1352,15 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun { while (c.toNextSelection()) { XmlObject o = c.getObject(); if (o instanceof CTRubyContent) { - String tagName = o.getDomNode().getNodeName(); - if ("w:rt".equals(tagName)) { - inRT = true; - } else if ("w:rubyBase".equals(tagName)) { - inRT = false; - inBase = true; + final Node node = o.getDomNode(); + if (XWPFDocument.NS_OOXML_WP_MAIN.equals(node.getNamespaceURI())) { + final String tagName = node.getLocalName(); + if ("rt".equals(tagName)) { + inRT = true; + } else if ("rubyBase".equals(tagName)) { + inRT = false; + inBase = true; + } } } else { if (extractPhonetic && inRT) { @@ -1372,11 +1376,11 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun { private void _getText(XmlObject o, StringBuilder text) { if (o instanceof CTText) { - String tagName = o.getDomNode().getNodeName(); + final Node node = o.getDomNode(); // Field Codes (w:instrText, defined in spec sec. 17.16.23) // come up as instances of CTText, but we don't want them // in the normal text output - if (!"w:instrText".equals(tagName)) { + if (!("instrText".equals(node.getLocalName()) && XWPFDocument.NS_OOXML_WP_MAIN.equals(node.getNamespaceURI()))) { text.append(((CTText) o).getStringValue()); } } @@ -1405,15 +1409,17 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun { // definitions around line 5642 of the XSDs // This bit works around it, and replicates the above // rules for that case - String tagName = o.getDomNode().getNodeName(); - if ("w:tab".equals(tagName) || "tab".equals(tagName)) { - text.append('\t'); - } - if ("w:br".equals(tagName) || "br".equals(tagName)) { - text.append('\n'); - } - if ("w:cr".equals(tagName) || "cr".equals(tagName)) { - text.append('\n'); + final Node node = o.getDomNode(); + if (XWPFDocument.NS_OOXML_WP_MAIN.equals(node.getNamespaceURI())) { + switch (node.getLocalName()) { + case "tab": + text.append('\t'); + break; + case "br": + case "cr": + text.append('\n'); + break; + } } } if (o instanceof CTFtnEdnRef) { diff --git a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFSignatureLine.java b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFSignatureLine.java index 9942124470..46d66bf196 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFSignatureLine.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFSignatureLine.java @@ -29,8 +29,9 @@ import org.apache.poi.poifs.crypt.dsig.SignatureLine; import org.apache.xmlbeans.XmlException; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPicture; +import static org.apache.poi.xwpf.usermodel.XWPFDocument.NS_OOXML_WP_MAIN; + public class XWPFSignatureLine extends SignatureLine { - static final String NS_OOXML_WP_MAIN = "http://schemas.openxmlformats.org/wordprocessingml/2006/main"; private static final String MS_VML_URN = "urn:schemas-microsoft-com:vml"; private CTSignatureLine line; diff --git a/poi-ooxml/src/test/java/org/apache/poi/openxml4j/opc/TestPackage.java b/poi-ooxml/src/test/java/org/apache/poi/openxml4j/opc/TestPackage.java index e3355b4b6a..e4478a16a9 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/openxml4j/opc/TestPackage.java +++ b/poi-ooxml/src/test/java/org/apache/poi/openxml4j/opc/TestPackage.java @@ -22,6 +22,7 @@ import static org.apache.poi.openxml4j.OpenXML4JTestDataSamples.getSampleFile; import static org.apache.poi.openxml4j.OpenXML4JTestDataSamples.getSampleFileName; import static org.apache.poi.openxml4j.OpenXML4JTestDataSamples.openSampleStream; import static org.apache.poi.openxml4j.opc.PackagingURIHelper.createPartName; +import static org.apache.poi.xwpf.usermodel.XWPFDocument.NS_OOXML_WP_MAIN; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -100,7 +101,6 @@ import org.xml.sax.SAXParseException; public final class TestPackage { private static final Logger LOG = LogManager.getLogger(TestPackage.class); - private static final String NS_OOXML_WP_MAIN = "http://schemas.openxmlformats.org/wordprocessingml/2006/main"; private static final String CONTENT_EXT_PROPS = "application/vnd.openxmlformats-officedocument.extended-properties+xml"; private static final POIDataSamples xlsSamples = POIDataSamples.getSpreadSheetInstance(); -- 2.39.5