refactoring

This commit is contained in:
Georgy Litvinov 2020-01-14 21:44:27 +01:00
parent dbdf7ca2a5
commit a93679a41b

View file

@ -453,26 +453,24 @@ public class PageSplitter {
} }
return dataMoved; return dataMoved;
} }
private static boolean handleSection(Node sectionFirstPart, Node sectionNode) { private static boolean handleSection(Node sectionFirstPart, Node section) {
boolean dataMoved = false; boolean dataMoved = false;
// Node counter // Node counter
int i = 0; int i = 0;
NodeList sectionChildNodes = sectionNode.getChildNodes(); NodeList sectionChilds = section.getChildNodes();
while (sectionChildNodes.getLength() > i) { while (sectionChilds.getLength() > i) {
Node sectionChildNode = sectionChildNodes.item(0); Node child = sectionChilds.item(0);
if ((sectionChildNode.getNodeType() == Node.ELEMENT_NODE)) { if ((child.getNodeType() == Node.ELEMENT_NODE)) {
String nodeName = sectionChildNode.getNodeName(); String nodeName = child.getNodeName();
// System.out.println("Nodename " + nodeName); if (containsSPB(child)){
if (containsSPB(sectionChildNode)){ Node childFirstPart = child.cloneNode(false);
// System.out.println("Contains spb");
Node sectionChildFirstPart = sectionChildNode.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
sectionNode.removeChild(sectionChildNode); section.removeChild(child);
} else if (nodeName.equals(TEXT_H) || nodeName.equals(TEXT_P)) { } else if (nodeName.equals(TEXT_H) || nodeName.equals(TEXT_P)) {
Node paraFirstPart = handleParagraph(sectionChildNode); Node paraFirstPart = handleParagraph(child);
if (paraFirstPart != null){ if (paraFirstPart != null){
sectionFirstPart.appendChild(paraFirstPart); sectionFirstPart.appendChild(paraFirstPart);
dataMoved=true; dataMoved=true;
@ -480,34 +478,34 @@ public class PageSplitter {
} else if (nodeName.equals(TEXT_TABLE_OF_CONTENT)) { } else if (nodeName.equals(TEXT_TABLE_OF_CONTENT)) {
//HACK //HACK
removeSPB(sectionNode); removeSPB(section);
i++; i++;
continue; continue;
} else if (nodeName.equals(TABLE_TABLE)) { } else if (nodeName.equals(TABLE_TABLE)) {
if (handleTableTable(sectionChildFirstPart, sectionChildNode)){ if (handleTableTable(childFirstPart, child)){
sectionFirstPart.appendChild(sectionChildFirstPart); sectionFirstPart.appendChild(childFirstPart);
dataMoved=true; dataMoved=true;
} }
} else if (nodeName.equals(TEXT_SECTION)) { } else if (nodeName.equals(TEXT_SECTION)) {
if (handleSection(sectionChildFirstPart, sectionChildNode)){ if (handleSection(childFirstPart, child)){
sectionFirstPart.appendChild(sectionChildFirstPart); sectionFirstPart.appendChild(childFirstPart);
dataMoved=true; dataMoved=true;
} }
} else if (nodeName.equals(TEXT_LIST)) { } else if (nodeName.equals(TEXT_LIST)) {
if (handleList(sectionChildFirstPart, sectionChildNode)){ if (handleList(childFirstPart, child)){
sectionFirstPart.appendChild(sectionChildFirstPart); sectionFirstPart.appendChild(childFirstPart);
dataMoved=true; dataMoved=true;
} }
} }
//split node with spb and exit //split node with spb and exit
break; break;
} else { } else {
sectionFirstPart.appendChild(sectionChildNode); sectionFirstPart.appendChild(child);
dataMoved = true; dataMoved = true;
} }
} else { } else {
//Append text nodes //Append text nodes
sectionFirstPart.appendChild(sectionChildNode); sectionFirstPart.appendChild(child);
dataMoved = true; dataMoved = true;
} }
@ -521,17 +519,17 @@ public class PageSplitter {
Node paraBefore = para.cloneNode(false); Node paraBefore = para.cloneNode(false);
boolean dataMoved = false; boolean dataMoved = false;
int i = 0; int i = 0;
NodeList paraChildNodes = para.getChildNodes(); NodeList сhilds = para.getChildNodes();
while (paraChildNodes.getLength() > i) { while (сhilds.getLength() > i) {
Node paraChildNode = paraChildNodes.item(i); Node сhild = сhilds.item(i);
//NOT TEXT NODES //NOT TEXT NODES
if ((paraChildNode.getNodeType() == Node.ELEMENT_NODE)) { if ((сhild.getNodeType() == Node.ELEMENT_NODE)) {
String nodeName = paraChildNode.getNodeName(); String childName = сhild.getNodeName();
//SPB FOUND //SPB FOUND
if (containsSPB(paraChildNode)){ if (containsSPB(сhild)){
if (nodeName.equals(TEXT_SOFT_PAGE_BREAK)){ if (childName.equals(TEXT_SOFT_PAGE_BREAK)){
//Next node in paragraph. If it is text node go further //Next node in paragraph. If it is text node go further
Node paraNextNode = paraChildNodes.item(i+1); Node paraNextNode = сhilds.item(i+1);
Node paraPrevNode = paraBefore.getLastChild(); Node paraPrevNode = paraBefore.getLastChild();
String nextText = null; String nextText = null;
String prevText = null; String prevText = null;
@ -568,34 +566,34 @@ public class PageSplitter {
if (paraNextNode == null && paraPrevNode == null){ if (paraNextNode == null && paraPrevNode == null){
Document doc = para.getOwnerDocument(); Document doc = para.getOwnerDocument();
Node space = doc.createTextNode(" "); Node space = doc.createTextNode(" ");
para.insertBefore(space, paraChildNode); para.insertBefore(space, сhild);
} }
// remove inner soft page break node // remove inner soft page break node
para.removeChild(paraChildNode); para.removeChild(сhild);
/* 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
* If both true - add * If both true - add
*/ */
} else { } else {
System.out.println("ERROR: SPB INSIDE Paragraph Element in inner element " + nodeName); System.out.println("ERROR: SPB INSIDE Paragraph Element in inner element " + childName);
//checkSoftPageBreak(internalNode, true); //checkSoftPageBreak(internalNode, true);
//paraFirstPart.appendChild(internalNode); //paraFirstPart.appendChild(internalNode);
//dataMoved = true; //dataMoved = true;
} }
break; break;
//ELEMENT WITHOUT SPB //ELEMENT WITHOUT SPB
} else if (nodeName.equals(TEXT_BOOKMARK_START)){ } else if (childName.equals(TEXT_BOOKMARK_START)){
paraBefore.appendChild(paraChildNode.cloneNode(true)); paraBefore.appendChild(сhild.cloneNode(true));
i++; i++;
} else { } else {
paraBefore.appendChild(paraChildNode); paraBefore.appendChild(сhild);
dataMoved = true; dataMoved = true;
} }
//TEXT NODES //TEXT NODES
} else { } else {
paraBefore.appendChild(paraChildNode); paraBefore.appendChild(сhild);
dataMoved = true; dataMoved = true;
} }