Refactoring splitter

This commit is contained in:
Georgy Litvinov 2020-01-14 21:31:57 +01:00
parent 53f704d133
commit dbdf7ca2a5

View file

@ -35,8 +35,9 @@ public class PageSplitter {
StyleWithProperties style = null; StyleWithProperties style = null;
if ((nodeName.equals(TEXT_P) || nodeName.equals(TEXT_H))) { if ((nodeName.equals(TEXT_P) || nodeName.equals(TEXT_H))) {
//If SPB not the first node //If SPB not the first node
if (handleParagraph(childFirstPart, child)){ Node paraFirstPart = handleParagraph(child);
onode.insertBefore(childFirstPart, child); if (paraFirstPart != null){
onode.insertBefore(paraFirstPart, child);
style = ofr.getParStyle(Misc.getAttribute(child, TEXT_STYLE_NAME)); style = ofr.getParStyle(Misc.getAttribute(child, TEXT_STYLE_NAME));
} }
} else if (nodeName.equals(TABLE_TABLE)) { } else if (nodeName.equals(TABLE_TABLE)) {
@ -169,9 +170,9 @@ public class PageSplitter {
} }
} else if (nodeName.equals(TEXT_H) || nodeName.equals(TEXT_P)) { } else if (nodeName.equals(TEXT_H) || nodeName.equals(TEXT_P)) {
Node paraFirstPart = handleParagraph(listItemChild);
if (handleParagraph(listItemChildFirstPart, listItemChild)){ if (paraFirstPart != null){
listItemFirstPart.appendChild(listItemChildFirstPart); listItemFirstPart.appendChild(paraFirstPart);
dataMoved=true; dataMoved=true;
} }
} }
@ -427,15 +428,14 @@ public class PageSplitter {
if ((cellChildNode.getNodeType() == Node.ELEMENT_NODE)) { if ((cellChildNode.getNodeType() == Node.ELEMENT_NODE)) {
String nodeName = cellChildNode.getNodeName(); String nodeName = cellChildNode.getNodeName();
if (containsSPB(cellChildNode)){ if (containsSPB(cellChildNode)){
Node cellChildFirstPart = cellChildNode.cloneNode(false);
if (nodeName.equals(TEXT_SOFT_PAGE_BREAK)){ if (nodeName.equals(TEXT_SOFT_PAGE_BREAK)){
// remove inner soft page break node // remove inner soft page break node
cellNode.removeChild(cellChildNode); cellNode.removeChild(cellChildNode);
} else if (nodeName.equals(TEXT_H) || nodeName.equals(TEXT_P)) { } else if (nodeName.equals(TEXT_H) || nodeName.equals(TEXT_P)) {
Node paraFirstPart = handleParagraph(cellChildNode);
if (handleParagraph(cellChildFirstPart, cellChildNode)){ if (paraFirstPart != null){
cellFirstPart.appendChild(cellChildFirstPart); cellFirstPart.appendChild(paraFirstPart);
dataMoved=true; dataMoved=true;
} }
} }
@ -472,9 +472,9 @@ public class PageSplitter {
sectionNode.removeChild(sectionChildNode); sectionNode.removeChild(sectionChildNode);
} else if (nodeName.equals(TEXT_H) || nodeName.equals(TEXT_P)) { } else if (nodeName.equals(TEXT_H) || nodeName.equals(TEXT_P)) {
Node paraFirstPart = handleParagraph(sectionChildNode);
if (handleParagraph(sectionChildFirstPart, sectionChildNode)){ if (paraFirstPart != null){
sectionFirstPart.appendChild(sectionChildFirstPart); sectionFirstPart.appendChild(paraFirstPart);
dataMoved=true; dataMoved=true;
} }
} else if (nodeName.equals(TEXT_TABLE_OF_CONTENT)) { } else if (nodeName.equals(TEXT_TABLE_OF_CONTENT)) {
@ -517,10 +517,11 @@ public class PageSplitter {
} }
private static boolean handleParagraph(Node paraBefore, Node paraAfter) { private static Node handleParagraph(Node para) {
Node paraBefore = para.cloneNode(false);
boolean dataMoved = false; boolean dataMoved = false;
int i = 0; int i = 0;
NodeList paraChildNodes = paraAfter.getChildNodes(); NodeList paraChildNodes = para.getChildNodes();
while (paraChildNodes.getLength() > i) { while (paraChildNodes.getLength() > i) {
Node paraChildNode = paraChildNodes.item(i); Node paraChildNode = paraChildNodes.item(i);
//NOT TEXT NODES //NOT TEXT NODES
@ -565,13 +566,13 @@ public class PageSplitter {
} }
// In case paragraph is empty add space to prevent it's removing // In case paragraph is empty add space to prevent it's removing
if (paraNextNode == null && paraPrevNode == null){ if (paraNextNode == null && paraPrevNode == null){
Document doc = paraAfter.getOwnerDocument(); Document doc = para.getOwnerDocument();
Node space = doc.createTextNode(" "); Node space = doc.createTextNode(" ");
paraAfter.insertBefore(space, paraChildNode); para.insertBefore(space, paraChildNode);
} }
// remove inner soft page break node // remove inner soft page break node
paraAfter.removeChild(paraChildNode); para.removeChild(paraChildNode);
/* Check if next node in para is text and first char is a letter /* Check if next node in para is text and first char is a letter
* Check if last node in paraFirstPart is text and last char is a letter * Check if last node in paraFirstPart is text and last char is a letter
@ -599,9 +600,13 @@ public class PageSplitter {
} }
} }
removeIndent(paraAfter); if (dataMoved) {
removeIndent(para);
return paraBefore;
}
return dataMoved;
return null;
} }
private static void removeIndent(Node paraAfter) { private static void removeIndent(Node paraAfter) {
String baseStyleName = Misc.getAttribute(paraAfter, TEXT_STYLE_NAME); String baseStyleName = Misc.getAttribute(paraAfter, TEXT_STYLE_NAME);