]> source.dussan.org Git - poi.git/commitdiff
Fix bug #52449 - Support writing XWPF documents with glossaries (plus fix some indenting)
authorNick Burch <nick@apache.org>
Wed, 11 Jan 2012 14:02:40 +0000 (14:02 +0000)
committerNick Burch <nick@apache.org>
Wed, 11 Jan 2012 14:02:40 +0000 (14:02 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1230045 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/status.xml
src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java
src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRelation.java
src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFSettings.java
src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFStyles.java
src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFStyles.java
test-data/document/52449.docx [new file with mode: 0644]

index 139408242dcfe2aa34ba7475aecbe1064a161911..38c7666b965e9d0a906f63776e5c9c5518912410 100644 (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>
index 6b4423ba8d0ba9a9f34edcf779c3029d4d0e3652..4cb412931e609b81f83cc49cd3a69ba51edd460a 100644 (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();
index 9f7ef404c69c94760d0f976a4dfe8811943255d2..89524de671c0686e60bf4f5131679f9153020ffb 100644 (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",
index 84b9fdf66ca7429e48a139a317200a8a7afdde47..5471fde0cbee71689aa80acf9c223adaecea978a 100644 (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"));
index f45d113ca5e7e301649531bf3d13d9a397762f5c..35a0c6027e1bffcc808d4f00cf0fb0543000ca0e 100644 (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
index 156e1b580239e287d96dff172142c01adfbf3553..3c13504cdfe957cf945307ded53559efdb565089 100644 (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);
+       }
 
 }
diff --git a/test-data/document/52449.docx b/test-data/document/52449.docx
new file mode 100644 (file)
index 0000000..f294b85
Binary files /dev/null and b/test-data/document/52449.docx differ