Browse Source

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
tags/REL_3_8_FINAL
Nick Burch 12 years ago
parent
commit
57c4509faa

+ 1
- 0
src/documentation/content/xdocs/status.xml View File

@@ -34,6 +34,7 @@

<changes>
<release version="3.8-beta6" date="2012-??-??">
<action dev="poi-developers" type="fix">52449 - Support writing XWPF documents with glossaries (Glossaries are not yet supported, but can now be written out again without changes)</action>
<action dev="poi-developers" type="fix">52446 - Handle files which have been truncated by a few bytes in NPropertyTable</action>
<action dev="poi-developers" type="fix">52438 - Update CellDateFormatter to handle times without seconds</action>
<action dev="poi-developers" type="add">52389 - Support ?/? as well as #/# fractions, and tighten DataFormatter rules for fraction matching</action>

+ 15
- 0
src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java View File

@@ -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();

+ 6
- 0
src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRelation.java View File

@@ -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",

+ 3
- 0
src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFSettings.java View File

@@ -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"));

+ 37
- 34
src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFStyles.java View File

@@ -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<String,String> map = new HashMap<String,String>();
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<String,String> map = new HashMap<String,String>();
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

+ 13
- 3
src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFStyles.java View File

@@ -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);
}

}

BIN
test-data/document/52449.docx View File


Loading…
Cancel
Save