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
This commit is contained in:
PJ Fanning 2022-08-28 12:01:53 +00:00
parent 7fd339952b
commit ab5cb372e5
5 changed files with 30 additions and 21 deletions

View File

@ -22,6 +22,7 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import org.apache.poi.openxml4j.opc.PackageNamespaces; import org.apache.poi.openxml4j.opc.PackageNamespaces;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.xmlbeans.XmlOptions; import org.apache.xmlbeans.XmlOptions;
public class POIXMLTypeLoader { 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/relationships", "r");
map.put("http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes", "vt"); 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/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("http://schemas.microsoft.com/office/word/2006/wordml", "wne");
map.put(MS_OFFICE_URN, "o"); map.put(MS_OFFICE_URN, "o");
map.put(MS_EXCEL_URN, "x"); map.put(MS_EXCEL_URN, "x");

View File

@ -101,6 +101,7 @@ import org.openxmlformats.schemas.wordprocessingml.x2006.main.StylesDocument;
*/ */
@SuppressWarnings("unused") @SuppressWarnings("unused")
public class XWPFDocument extends POIXMLDocument implements Document, IBody { 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); private static final Logger LOG = LogManager.getLogger(XWPFDocument.class);
protected List<XWPFFooter> footers = new ArrayList<>(); protected List<XWPFFooter> footers = new ArrayList<>();

View File

@ -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.STOnOff1;
import org.openxmlformats.schemas.officeDocument.x2006.sharedTypes.STVerticalAlignRun; import org.openxmlformats.schemas.officeDocument.x2006.sharedTypes.STVerticalAlignRun;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*; import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList; import org.w3c.dom.NodeList;
import org.w3c.dom.Text; import org.w3c.dom.Text;
import org.xml.sax.InputSource; 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.getPictArray()));
pictTextObjs.addAll(Arrays.asList(r.getDrawingArray())); pictTextObjs.addAll(Arrays.asList(r.getDrawingArray()));
for (XmlObject o : pictTextObjs) { 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) { for (XmlObject t : ts) {
NodeList kids = t.getDomNode().getChildNodes(); NodeList kids = t.getDomNode().getChildNodes();
for (int n = 0; n < kids.getLength(); n++) { for (int n = 0; n < kids.getLength(); n++) {
@ -1351,12 +1352,15 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun {
while (c.toNextSelection()) { while (c.toNextSelection()) {
XmlObject o = c.getObject(); XmlObject o = c.getObject();
if (o instanceof CTRubyContent) { if (o instanceof CTRubyContent) {
String tagName = o.getDomNode().getNodeName(); final Node node = o.getDomNode();
if ("w:rt".equals(tagName)) { if (XWPFDocument.NS_OOXML_WP_MAIN.equals(node.getNamespaceURI())) {
inRT = true; final String tagName = node.getLocalName();
} else if ("w:rubyBase".equals(tagName)) { if ("rt".equals(tagName)) {
inRT = false; inRT = true;
inBase = true; } else if ("rubyBase".equals(tagName)) {
inRT = false;
inBase = true;
}
} }
} else { } else {
if (extractPhonetic && inRT) { if (extractPhonetic && inRT) {
@ -1372,11 +1376,11 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun {
private void _getText(XmlObject o, StringBuilder text) { private void _getText(XmlObject o, StringBuilder text) {
if (o instanceof CTText) { 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) // Field Codes (w:instrText, defined in spec sec. 17.16.23)
// come up as instances of CTText, but we don't want them // come up as instances of CTText, but we don't want them
// in the normal text output // 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()); text.append(((CTText) o).getStringValue());
} }
} }
@ -1405,15 +1409,17 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun {
// definitions around line 5642 of the XSDs // definitions around line 5642 of the XSDs
// This bit works around it, and replicates the above // This bit works around it, and replicates the above
// rules for that case // rules for that case
String tagName = o.getDomNode().getNodeName(); final Node node = o.getDomNode();
if ("w:tab".equals(tagName) || "tab".equals(tagName)) { if (XWPFDocument.NS_OOXML_WP_MAIN.equals(node.getNamespaceURI())) {
text.append('\t'); switch (node.getLocalName()) {
} case "tab":
if ("w:br".equals(tagName) || "br".equals(tagName)) { text.append('\t');
text.append('\n'); break;
} case "br":
if ("w:cr".equals(tagName) || "cr".equals(tagName)) { case "cr":
text.append('\n'); text.append('\n');
break;
}
} }
} }
if (o instanceof CTFtnEdnRef) { if (o instanceof CTFtnEdnRef) {

View File

@ -29,8 +29,9 @@ import org.apache.poi.poifs.crypt.dsig.SignatureLine;
import org.apache.xmlbeans.XmlException; import org.apache.xmlbeans.XmlException;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPicture; 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 { 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 static final String MS_VML_URN = "urn:schemas-microsoft-com:vml";
private CTSignatureLine line; private CTSignatureLine line;

View File

@ -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.getSampleFileName;
import static org.apache.poi.openxml4j.OpenXML4JTestDataSamples.openSampleStream; import static org.apache.poi.openxml4j.OpenXML4JTestDataSamples.openSampleStream;
import static org.apache.poi.openxml4j.opc.PackagingURIHelper.createPartName; 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.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
@ -100,7 +101,6 @@ import org.xml.sax.SAXParseException;
public final class TestPackage { public final class TestPackage {
private static final Logger LOG = LogManager.getLogger(TestPackage.class); 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 String CONTENT_EXT_PROPS = "application/vnd.openxmlformats-officedocument.extended-properties+xml";
private static final POIDataSamples xlsSamples = POIDataSamples.getSpreadSheetInstance(); private static final POIDataSamples xlsSamples = POIDataSamples.getSpreadSheetInstance();