From: Nick Burch Date: Wed, 11 Jan 2012 14:02:40 +0000 (+0000) Subject: Fix bug #52449 - Support writing XWPF documents with glossaries (plus fix some indenting) X-Git-Tag: REL_3_8_FINAL~85 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=57c4509faabf7025bc52ecfe90684512b2cf01a4;p=poi.git Fix bug #52449 - Support writing XWPF documents with glossaries (plus fix some indenting) git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1230045 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index 139408242d..38c7666b96 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + 52449 - Support writing XWPF documents with glossaries (Glossaries are not yet supported, but can now be written out again without changes) 52446 - Handle files which have been truncated by a few bytes in NPropertyTable 52438 - Update CellDateFormatter to handle times without seconds 52389 - Support ?/? as well as #/# fractions, and tighten DataFormatter rules for fraction matching diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java index 6b4423ba8d..4cb412931e 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java @@ -20,6 +20,7 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -188,6 +189,20 @@ public class XWPFDocument extends POIXMLDocument implements Document, IBody { picData.onDocumentRead(); registerPackagePictureData(picData); pictures.add(picData); + } else if (relation.equals(XWPFRelation.GLOSSARY_DOCUMENT.getRelation())) { + // We don't currently process the glossary itself + // Until we do, we do need to load the glossary child parts of it + for (POIXMLDocumentPart gp : p.getRelations()) { + // Trigger the onDocumentRead for all the child parts + // Otherwise we'll hit issues on Styles, Settings etc on save + try { + Method onDocumentRead = gp.getClass().getDeclaredMethod("onDocumentRead"); + onDocumentRead.setAccessible(true); + onDocumentRead.invoke(gp); + } catch(Exception e) { + throw new POIXMLException(e); + } + } } } initHyperlinks(); diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRelation.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRelation.java index 9f7ef404c6..89524de671 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRelation.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRelation.java @@ -58,6 +58,12 @@ public final class XWPFRelation extends POIXMLRelation { "/word/document.xml", null ); + public static final XWPFRelation GLOSSARY_DOCUMENT = new XWPFRelation( + "application/vnd.openxmlformats-officedocument.wordprocessingml.document.glossary+xml", + "http://schemas.openxmlformats.org/officeDocument/2006/relationships/glossaryDocument", + "/word/glossary/document.xml", + null + ); public static final XWPFRelation NUMBERING = new XWPFRelation( "application/vnd.openxmlformats-officedocument.wordprocessingml.numbering+xml", diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFSettings.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFSettings.java index 84b9fdf66c..5471fde0cb 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFSettings.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFSettings.java @@ -149,6 +149,9 @@ public class XWPFSettings extends POIXMLDocumentPart { @Override protected void commit() throws IOException { + if (ctSettings == null) { + throw new IllegalStateException("Unable to write out settings that were never read in!"); + } XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS); xmlOptions.setSaveSyntheticDocumentElement(new QName(CTSettings.type.getName().getNamespaceURI(), "settings")); diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFStyles.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFStyles.java index f45d113ca5..35a0c6027e 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFStyles.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFStyles.java @@ -70,41 +70,44 @@ public class XWPFStyles extends POIXMLDocumentPart{ public XWPFStyles() { } - /** - * Read document - */ - @Override - protected void onDocumentRead ()throws IOException{ - StylesDocument stylesDoc; - try { - InputStream is = getPackagePart().getInputStream(); - stylesDoc = StylesDocument.Factory.parse(is); - ctStyles = stylesDoc.getStyles(); - latentStyles = new XWPFLatentStyles(ctStyles.getLatentStyles(), this); - - } catch (XmlException e) { - throw new POIXMLException(); - } - //get any Style - for(CTStyle style : ctStyles.getStyleList()) { - listStyle.add(new XWPFStyle(style, this)); - } - } + /** + * Read document + */ + @Override + protected void onDocumentRead() throws IOException{ + StylesDocument stylesDoc; + try { + InputStream is = getPackagePart().getInputStream(); + stylesDoc = StylesDocument.Factory.parse(is); + ctStyles = stylesDoc.getStyles(); + latentStyles = new XWPFLatentStyles(ctStyles.getLatentStyles(), this); + } catch (XmlException e) { + throw new POIXMLException("Unable to read styles", e); + } + + // Build up all the style objects + for(CTStyle style : ctStyles.getStyleList()) { + listStyle.add(new XWPFStyle(style, this)); + } + } - @Override - protected void commit() throws IOException { - XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS); - xmlOptions.setSaveSyntheticDocumentElement(new QName(CTStyles.type.getName().getNamespaceURI(), "styles")); - Map map = new HashMap(); - map.put("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "r"); - map.put("http://schemas.openxmlformats.org/wordprocessingml/2006/main", "w"); - xmlOptions.setSaveSuggestedPrefixes(map); - PackagePart part = getPackagePart(); - OutputStream out = part.getOutputStream(); - ctStyles.save(out, xmlOptions); - out.close(); - } - + @Override + protected void commit() throws IOException { + if (ctStyles == null) { + throw new IllegalStateException("Unable to write out styles that were never read in!"); + } + + XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS); + xmlOptions.setSaveSyntheticDocumentElement(new QName(CTStyles.type.getName().getNamespaceURI(), "styles")); + Map map = new HashMap(); + map.put("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "r"); + map.put("http://schemas.openxmlformats.org/wordprocessingml/2006/main", "w"); + xmlOptions.setSaveSuggestedPrefixes(map); + PackagePart part = getPackagePart(); + OutputStream out = part.getOutputStream(); + ctStyles.save(out, xmlOptions); + out.close(); + } /** * Sets the ctStyles diff --git a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFStyles.java b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFStyles.java index 156e1b5802..3c13504cdf 100644 --- a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFStyles.java +++ b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFStyles.java @@ -68,8 +68,18 @@ public class TestXWPFStyles extends TestCase { assertTrue(styles.styleExist(strStyleName)); } -// protected void tearDown() throws Exception { -// super.tearDown(); -// } + /** + * Bug #52449 - We should be able to write a file containing + * both regular and glossary styles without error + */ + public void test52449() throws Exception { + XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("52449.docx"); + XWPFStyles styles = doc.getStyles(); + assertNotNull(styles); + + XWPFDocument docIn = XWPFTestDataSamples.writeOutAndReadBack(doc); + styles = docIn.getStyles(); + assertNotNull(styles); + } } diff --git a/test-data/document/52449.docx b/test-data/document/52449.docx new file mode 100644 index 0000000000..f294b85dbd Binary files /dev/null and b/test-data/document/52449.docx differ