From d769a03fda388680323eac85fee96bcdf55d0eda Mon Sep 17 00:00:00 2001 From: Tim Allison Date: Fri, 4 Nov 2016 15:06:17 +0000 Subject: [PATCH] POI-60341: handle missing sdtpr in an sdtbody git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1768048 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/xwpf/usermodel/AbstractXWPFSDT.java | 54 ++++++++++--------- .../poi/xwpf/usermodel/TestXWPFSDT.java | 10 ++++ 2 files changed, 39 insertions(+), 25 deletions(-) diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/AbstractXWPFSDT.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/AbstractXWPFSDT.java index 2cf42f186..4db163de2 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/AbstractXWPFSDT.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/AbstractXWPFSDT.java @@ -1,18 +1,18 @@ -/* ==================================================================== - Licensed to the Apache Software Foundation (ASF) under one or more - contributor license agreements. See the NOTICE file distributed with - this work for additional information regarding copyright ownership. - The ASF licenses this file to You 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. +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You 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.apache.poi.xwpf.usermodel; @@ -33,18 +33,22 @@ public abstract class AbstractXWPFSDT implements ISDTContents { private final IBody part; public AbstractXWPFSDT(CTSdtPr pr, IBody part) { - - CTString[] aliases = pr.getAliasArray(); - if (aliases != null && aliases.length > 0) { - title = aliases[0].getVal(); - } else { + if (pr == null) { title = ""; - } - CTString[] tags = pr.getTagArray(); - if (tags != null && tags.length > 0) { - tag = tags[0].getVal(); - } else { tag = ""; + } else { + CTString[] aliases = pr.getAliasArray(); + if (aliases != null && aliases.length > 0) { + title = aliases[0].getVal(); + } else { + title = ""; + } + CTString[] tags = pr.getTagArray(); + if (tags != null && tags.length > 0) { + tag = tags[0].getVal(); + } else { + tag = ""; + } } this.part = part; diff --git a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFSDT.java b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFSDT.java index b008763ca..0f625fbd9 100644 --- a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFSDT.java +++ b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFSDT.java @@ -17,6 +17,7 @@ package org.apache.poi.xwpf.usermodel; +import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -131,6 +132,15 @@ public final class TestXWPFSDT extends TestCase { } } + public void test60341() throws IOException { + //handle sdtbody without an sdtpr + XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("Bug60341.docx"); + List sdts = extractAllSDTs(doc); + assertEquals(1, sdts.size()); + assertEquals("", sdts.get(0).getTag()); + assertEquals("", sdts.get(0).getTitle()); + } + private List extractAllSDTs(XWPFDocument doc) { List sdts = new ArrayList();