protected String getMceIgnorable(Body body) { List<Object> content = body.getContent(); // To avoid the traversing a large docx, // we'll try to use a hack here. // The idea is to force JAXB to include // namespace declarations for w14 and w15, by // using them in an innocuous manner. // It works by adding the following to the first // paragraph encountered: // <w:p w14:textId="fdcbd571" w14:paraId="fdcbd571" > // <w:pPr> // <w15:collapsed w:val="false"/> // </w:pPr> // // If this turns out to cause problems, it could be // made configurable in docx4j.properties if (content.size() == 0) { return null; } P p = null; for (Object o : content) { if (o instanceof P) { p = (P) o; break; } } if (p == null) { // No top level paragraph, so // do the work of traversing the document log.debug("traversing for w14, w15"); IgnorablePrefixFinder finder = new IgnorablePrefixFinder(); if (body.getSectPr() != null && body.getSectPr().getFootnoteColumns() != null) { finder.needW15 = true; } new TraversalUtil(content, finder); String mceIgnorableVal = ""; if (finder.needW14) { mceIgnorableVal = "w14"; } if (finder.needW15) { mceIgnorableVal += " w15"; } return mceIgnorableVal; } else { // The quick hack // For W14, we'll check/set paraId, textId if (p.getParaId() == null) { // Values MUST be greater than 0 and less than 0x80000000 // So let's String uuid = java.util.UUID.randomUUID().toString(); // That's 32 digits, but 8'll do nicely /* * 8 can create a number too large - using 7 * Bob Fleischman - July 24, 2014 */ uuid = uuid.replace("-", "").substring(0, 7); p.setParaId(uuid); p.setTextId(uuid); } // For W15, collapse /* * Bob Fleischman - commented this out to generat docs without the namespace issue PPr ppr = p.getPPr(); if (ppr==null) { ppr = Context.getWmlObjectFactory().createPPr(); p.setPPr(ppr); } if (ppr.getCollapsed()==null) { BooleanDefaultTrue notCollapsed = new BooleanDefaultTrue(); notCollapsed.setVal(Boolean.FALSE); ppr.setCollapsed(notCollapsed); } */ } return "w14 w15"; }
@Override public List<Object> apply(Object o) { // NB: the tests in this method have to be comprehensive, // so if support for glow etc is introduced, tests for those // will need to be added if (o instanceof org.docx4j.wml.P) { P p = (P) o; // W14? if (p.getParaId() != null) { needW14 = true; } // W15? if (!needW15) { PPr ppr = p.getPPr(); if (ppr != null) { if (ppr.getCollapsed() != null) { needW15 = true; } if (ppr.getSectPr() != null && ppr.getSectPr().getFootnoteColumns() != null) { needW15 = true; } } } } else if (o instanceof SdtElement) { SdtPr sdtPr = ((SdtElement) o).getSdtPr(); if (sdtPr != null) { if (contains( sdtPr.getRPrOrAliasOrLock(), "http://schemas.microsoft.com/office/word/2010/wordml", w14SdtPrNames)) { needW14 = true; } if (contains( sdtPr.getRPrOrAliasOrLock(), "http://schemas.microsoft.com/office/word/2012/wordml", w15SdtPrNames)) { needW15 = true; } } } else if (o instanceof Tr) { // TODO does this need to be unwrapped? if (((Tr) o).getParaId() != null) { needW14 = true; } } else if (o instanceof CTObject) { if (((CTObject) o).getAnchorId() != null) { needW14 = true; } } else if (o instanceof Pict) { if (((Pict) o).getAnchorId() != null) { needW14 = true; } } return null; }