PageRenderTime 47ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/docx4j-core-tests/src/test/java/org/docx4j/jaxb/AlternateContentPreprocessorTest.java

http://github.com/plutext/docx4j
Java | 305 lines | 162 code | 71 blank | 72 comment | 7 complexity | cfd4cd9fc281d1ad69f9aeef00609d9a MD5 | raw file
Possible License(s): Apache-2.0
  1. package org.docx4j.jaxb;
  2. import static org.junit.Assert.assertTrue;
  3. import java.io.ByteArrayInputStream;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.util.List;
  7. import javax.xml.bind.Binder;
  8. import org.apache.commons.io.IOUtils;
  9. import org.docx4j.XmlUtils;
  10. import org.docx4j.jaxb.Context;
  11. import org.docx4j.jaxb.JaxbValidationEventHandler;
  12. import org.docx4j.model.structure.HeaderFooterPolicy;
  13. import org.docx4j.model.structure.SectionWrapper;
  14. import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
  15. import org.docx4j.openpackaging.parts.WordprocessingML.HeaderPart;
  16. import org.docx4j.wml.Comments;
  17. import org.docx4j.wml.Document;
  18. import org.docx4j.wml.Hdr;
  19. import org.docx4j.wml.P;
  20. import org.docx4j.wml.R;
  21. import org.junit.Assert;
  22. import org.junit.Ignore;
  23. import org.junit.Test;
  24. import org.w3c.dom.Node;
  25. /**
  26. * TODO: As of docx4j 3.3.8, mc:AlternateContent
  27. * is supported in w:r, so these tests need to be re-worked.
  28. *
  29. */
  30. public class AlternateContentPreprocessorTest {
  31. /**
  32. * This tests that an oval wrapped in AlternateContent
  33. * is resolved to the Fallback; it tests the usual unmarshal
  34. * method in JaxbXmlPart ie unmarshal( java.io.InputStream is )
  35. * since CommentsPart uses that.
  36. */
  37. @Test
  38. public void testAlternateContent() throws Exception {
  39. String inputfilepath = System.getProperty("user.dir")
  40. + "/src/test/resources/2010/2010-mcAlternateContent.docx";
  41. WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath));
  42. // Since the JAXB binding stuff seems to remember
  43. // old artifacts, we'll first create a 'clean' object here
  44. // from something we've marshalled to.
  45. org.w3c.dom.Document xmlNode = XmlUtils.marshaltoW3CDomDocument(
  46. wordMLPackage.getMainDocumentPart().getCommentsPart().getJaxbElement());
  47. Binder<Node> binder = Context.jc.createBinder();
  48. Object jaxbElement = (Comments) binder.unmarshal(xmlNode);
  49. List<Object> list = XmlUtils.getJAXBNodesViaXPath(binder, jaxbElement,
  50. "//w:pict/v:oval", false );
  51. int count = list.size();
  52. assertTrue("expected oval but got " + count, count==1 );
  53. }
  54. /**
  55. * Test we can recover from "Errors limit exceeded"
  56. * with recent (non-MOXy) JAXB implementations (eg RI 2.2.11)
  57. *
  58. * @throws Exception
  59. */
  60. @Test
  61. public void testAlternateContentErrorLimit() throws Exception {
  62. int ERROR_LIMIT = 10;
  63. String inputfilepath = System.getProperty("user.dir")
  64. + "/src/test/resources/2010/2010-mcAlternateContent.docx";
  65. for (int i=0; i< (ERROR_LIMIT+5); i++) {
  66. WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath));
  67. org.w3c.dom.Document xmlNode = XmlUtils.marshaltoW3CDomDocument(
  68. wordMLPackage.getMainDocumentPart().getCommentsPart().getJaxbElement());
  69. Binder<Node> binder = Context.jc.createBinder();
  70. Object jaxbElement = (Comments) binder.unmarshal(xmlNode);
  71. List<Object> list = XmlUtils.getJAXBNodesViaXPath(binder, jaxbElement,
  72. "//w:pict/v:oval", false );
  73. int count = list.size();
  74. assertTrue("expected oval but got " + count, count==1 );
  75. }
  76. }
  77. /**
  78. * This tests that an oval wrapped in AlternateContent
  79. * is resolved to the Fallback; it tests the unmarshal
  80. * method in MainDocumentPart
  81. */
  82. @Test
  83. public void testAlternateContentMDP() throws Exception {
  84. String inputfilepath = System.getProperty("user.dir")
  85. + "/src/test/resources/2010/2010-mcAlternateContent-MDP.docx";
  86. WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath));
  87. // Since the JAXB binding stuff seems to remember
  88. // old artifacts, we'll first create a 'clean' object here
  89. // from something we've marshalled to.
  90. org.w3c.dom.Document xmlNode = XmlUtils.marshaltoW3CDomDocument(
  91. wordMLPackage.getMainDocumentPart().getJaxbElement());
  92. Binder<Node> binder = Context.jc.createBinder();
  93. Object jaxbElement = (org.docx4j.wml.Document) binder.unmarshal(xmlNode);
  94. List<Object> list = XmlUtils.getJAXBNodesViaXPath(binder, jaxbElement,
  95. "//w:pict/v:oval", false );
  96. int count = list.size();
  97. assertTrue("expected oval but got " + count, count==1 );
  98. }
  99. /**
  100. * This tests that an oval wrapped in AlternateContent
  101. * is resolved to the Fallback, even in the presence
  102. * of other unrecognised content (eg glow); it uses the unmarshal
  103. * method in MainDocumentPart
  104. */
  105. @Test
  106. public void testGlowAndAlternateContent() throws Exception {
  107. String inputfilepath = System.getProperty("user.dir")
  108. + "/src/test/resources/2010/2010-glow-then-AlternateContent.docx";
  109. WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath));
  110. // Since the JAXB binding stuff seems to remember
  111. // old artifacts, we'll first create a 'clean' object here
  112. // from something we've marshalled to.
  113. org.w3c.dom.Document xmlNode = XmlUtils.marshaltoW3CDomDocument(
  114. wordMLPackage.getMainDocumentPart().getJaxbElement());
  115. Binder<Node> binder = Context.jc.createBinder();
  116. Object jaxbElement = (org.docx4j.wml.Document) binder.unmarshal(xmlNode);
  117. List<Object> list = XmlUtils.getJAXBNodesViaXPath(binder, jaxbElement,
  118. "//w:pict/v:oval", false );
  119. int count = list.size();
  120. assertTrue("expected oval but got " + count, count==1 );
  121. }
  122. /**
  123. * This tests that an oval wrapped in AlternateContent
  124. * is resolved to the Fallback; it tests
  125. * unmarshal( java.io.InputStream is )
  126. * in HeaderPart
  127. */
  128. @Test
  129. public void testHeaderDocx() throws Exception {
  130. String inputfilepath = System.getProperty("user.dir")
  131. + "/src/test/resources/2010/2010-mcAlternateContent-in-header.docx";
  132. WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage
  133. .load(new java.io.File(inputfilepath));
  134. List<SectionWrapper> sectionWrappers = wordMLPackage.getDocumentModel()
  135. .getSections();
  136. HeaderPart header = null;
  137. for (SectionWrapper sw : sectionWrappers) {
  138. HeaderFooterPolicy hfp = sw.getHeaderFooterPolicy();
  139. if (hfp.getDefaultHeader() != null) {
  140. header = hfp.getDefaultHeader();
  141. }
  142. }
  143. // Since the JAXB binding stuff seems to remember
  144. // old artifacts, we'll first create a 'clean' object here
  145. // from something we've marshalled to.
  146. org.w3c.dom.Document xmlNode = XmlUtils.marshaltoW3CDomDocument(
  147. header.getJaxbElement());
  148. Binder<Node> binder = Context.jc.createBinder();
  149. Object jaxbElement = (Hdr) binder.unmarshal(xmlNode);
  150. List<Object> list = XmlUtils.getJAXBNodesViaXPath(binder, jaxbElement,
  151. "//w:pict/v:oval", false );
  152. int count = list.size();
  153. assertTrue("expected oval but got " + count, count==1 );
  154. }
  155. // There is currently no test for unmarshal(org.w3c.dom.Element el)
  156. // method in JaxbXml/Part, but I did test that by commenting out
  157. // the override in HeaderPart, and running the below test.
  158. /**
  159. * This tests that an oval wrapped in AlternateContent
  160. * is resolved to the Fallback; it tests unmarshal(org.w3c.dom.Element el)
  161. * in HeaderPart.
  162. */
  163. @Test
  164. public void testHeaderFlatOPC() throws Exception {
  165. String inputfilepath = System.getProperty("user.dir")
  166. + "/src/test/resources/2010/2010-mcAlternateContent-in-header.xml";
  167. WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath));
  168. /*
  169. * That unmarshalled via binder
  170. *
  171. * DEBUG org.docx4j.openpackaging.parts.JaxbXmlPartXPathAware .unmarshal line 559 -
  172. * For org.docx4j.openpackaging.parts.WordprocessingML.HeaderPart, unmarshall via binder
  173. *
  174. * but MOXy 2.5.2 and 2.6.3 seems to ignore event handler for that?
  175. *
  176. * http://stackoverflow.com/questions/37225221/moxy-validationevents-triggered-by-unmarshaller-but-not-binder
  177. *
  178. * docx4j 3.3.1 has a workaround for this in JaxbXmlPartXPathAware
  179. */
  180. List<SectionWrapper> sectionWrappers = wordMLPackage.getDocumentModel()
  181. .getSections();
  182. HeaderPart header = null;
  183. for (SectionWrapper sw : sectionWrappers) {
  184. HeaderFooterPolicy hfp = sw.getHeaderFooterPolicy();
  185. if (hfp.getDefaultHeader() != null) {
  186. header = hfp.getDefaultHeader();
  187. }
  188. }
  189. String headerXML = header.getXML();
  190. assertTrue(headerXML.contains("v:oval"));
  191. // Since the JAXB binding stuff seems to remember
  192. // old artifacts, we'll first create a 'clean' object here
  193. // from something we've marshalled to.
  194. org.w3c.dom.Document xmlNode = XmlUtils.marshaltoW3CDomDocument(header
  195. .getJaxbElement());
  196. Binder<Node> binder = Context.jc.createBinder();
  197. JaxbValidationEventHandler eventHandler = new JaxbValidationEventHandler();
  198. binder.setEventHandler(eventHandler);
  199. Object jaxbElement = (Hdr) binder.unmarshal(xmlNode);
  200. List<Object> list = XmlUtils.getJAXBNodesViaXPath(binder, jaxbElement,
  201. "//w:pict/v:oval", false );
  202. int count = list.size();
  203. assertTrue("expected oval but got " + count, count==1 );
  204. }
  205. @Test
  206. @Ignore
  207. public void testAltContentMarkSupported() throws Exception {
  208. String inputfilepath = System.getProperty("user.dir")
  209. + "/src/test/resources/parts/document_altContent.xml";
  210. // mark supported
  211. byte[] bytes = IOUtils.toByteArray(new FileInputStream(new File(inputfilepath)));
  212. Document doc = (Document)XmlUtils.unmarshal(new ByteArrayInputStream(bytes));
  213. //System.out.println(XmlUtils.marshaltoString(doc));
  214. P p = (P)doc.getBody().getContent().get(0);
  215. R r = (R)p.getContent().get(0);
  216. Assert.assertTrue(
  217. XmlUtils.unwrap(r.getContent().get(0))
  218. instanceof org.docx4j.wml.Pict);
  219. }
  220. @Test
  221. @Ignore
  222. public void testAltContentMarkNotSupported() throws Exception {
  223. String inputfilepath = System.getProperty("user.dir")
  224. + "/src/test/resources/parts/document_altContent.xml";
  225. // mark not supported
  226. try {
  227. Object o = XmlUtils.unmarshal(new FileInputStream(new File(inputfilepath)));
  228. System.out.println(XmlUtils.marshaltoString(o));
  229. Assert.fail("Expect an exception, since mark not supported");
  230. } catch (javax.xml.bind.UnmarshalException e){
  231. System.out.println("OK " + e.getMessage() + " OK");
  232. }
  233. }
  234. }