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