private static List<Object> groupBodyContent(List<Object> bodyElts) {
List<Object> resultElts = new ArrayList<Object>();
List<Object> paragraphElts = null;
SdtBlock sdtBorders = null;
SdtBlock sdtShading = null;
PBdr lastBorders = null;
PBdr currentBorders = null;
CTShd lastShading = null;
CTShd currentShading = null;
P paragraph = null;
for (Object o : bodyElts) {
if (o instanceof JAXBElement) {
o = ((JAXBElement)o).getValue();
}
if (o instanceof P) {
paragraph = (P)o;
paragraphElts = groupRuns(paragraph.getContent());
paragraph.getContent().clear();
if (paragraphElts != null) {
paragraph.getContent().addAll(paragraphElts);
}
currentBorders = null;
currentShading = null;
if (paragraph.getPPr() != null ) {
// TODO: use effective ppr properties!
// ie take styles into account
currentBorders = paragraph.getPPr().getPBdr();
currentShading = paragraph.getPPr().getShd();
}
if ( bordersChanged(currentBorders, lastBorders )) {
// could mean null to borders; borders to null; or bordersA to bordersB
if (currentBorders == null) {
sdtBorders = null;
} else {
sdtBorders = createSdt(TAG_BORDERS);
resultElts.add(sdtBorders);
}
}
if (shadingChanged(currentShading, lastShading )) {
// handle change to shading before addElement
if (currentShading == null) {
sdtShading = null;
} else {
sdtShading = createSdt(TAG_SHADING);
// need to set margins, so there isn't a white strip
// between paragraphs. hmm, model.properties.paragraph
// won't translate this. so do it at the fo level
if (sdtBorders!=null) {
sdtBorders.getSdtContent().getContent().add(sdtShading);
}
else {
resultElts.add(sdtShading);
}
}
}
}
else if (o instanceof Tbl) {
groupTable((Tbl)o);
}
if (sdtShading!=null) {
sdtShading.getSdtContent().getContent().add(o);
}
else if (sdtBorders!=null) {
sdtBorders.getSdtContent().getContent().add(o);
}
else {