Code is working, but ugly.

This commit is contained in:
Georgy Litvinov 2020-01-09 18:36:59 +01:00
parent 525aa8de1d
commit c13cb3abce
6 changed files with 91 additions and 23 deletions

View file

@ -8,21 +8,24 @@ import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import writer2latex.office.OfficeReader;
import writer2latex.office.OfficeStyle;
import writer2latex.office.StyleWithProperties;
import writer2latex.office.XMLString;
import writer2latex.util.Misc;
public class PageSplitter {
static Node truncatedListItemNodeContent = null;
static OfficeReader officeReader = null;
protected static Node splitSoftPageBreak(Node onode,OfficeReader ofr){
//Find par node with soft page break inside and split it
officeReader = ofr;
Document document = onode.getOwnerDocument();
Element softPageBreak = document.createElement(XMLString.TEXT_SOFT_PAGE_BREAK);
NodeList nodes = onode.getChildNodes();
int i = 0;
//Loop through the content nodes and split paragraph nodes with soft page break
//Loop through the content nodes and split nodes with soft page break
while (i < nodes.getLength()){
Node child = nodes.item(i);
//System.out.println("splitSoftPageBreak ");
@ -30,8 +33,6 @@ public class PageSplitter {
//Necessary check if node is an Element
if ((child.getNodeType() == Node.ELEMENT_NODE) && containsSPB(child)){
String nodeName = child.getNodeName();
//DEBUG
//System.out.println("----------CURRENT NODE IS-------" + nodeName);
//Create Duplicate Node!
Element childFirstPart = (Element) child.cloneNode(false);
StyleWithProperties style = null;
@ -39,7 +40,8 @@ public class PageSplitter {
//If SPB not the first node
if (handleParagraph(childFirstPart, child)){
onode.insertBefore(childFirstPart, child);
style = ofr.getTableStyle(Misc.getAttribute(child, XMLString.TEXT_STYLE_NAME));
style = ofr.getParStyle(Misc.getAttribute(child, XMLString.TEXT_STYLE_NAME));
// OfficeStyle styleCopy = style.Clone();
}
} else if (nodeName.equals(XMLString.TABLE_TABLE)) {
if (handleTableTable(childFirstPart, child)){
@ -50,14 +52,13 @@ public class PageSplitter {
} else if (nodeName.equals(XMLString.TEXT_LIST)) {
if (handleList(childFirstPart, child)){
onode.insertBefore(childFirstPart, child);
style = ofr.getTableStyle(Misc.getAttribute(child, XMLString.TEXT_LIST_STYLE));
}
} else if (nodeName.equals(XMLString.TEXT_SECTION)) {
//System.out.println("Get into Section ");
if (handleSection(childFirstPart, child)){
onode.insertBefore(childFirstPart, child);
style = ofr.getTableStyle(Misc.getAttribute(child, XMLString.TEXT_SECTION));
style = ofr.getSectionStyle(Misc.getAttribute(child, XMLString.TEXT_SECTION));
//System.out.println("Data moved");
}
} else if (nodeName.equals(XMLString.TEXT_TABLE_OF_CONTENT)){
@ -524,25 +525,21 @@ public class PageSplitter {
}
private static boolean handleParagraph(Node paraFirstPart, Node paraNode) {
private static boolean handleParagraph(Node paraBefore, Node paraAfter) {
boolean dataMoved = false;
int i = 0;
NodeList paraChildNodes = paraNode.getChildNodes();
NodeList paraChildNodes = paraAfter.getChildNodes();
while (paraChildNodes.getLength() > i) {
Node paraChildNode = paraChildNodes.item(i);
//NOT TEXT NODES
if ((paraChildNode.getNodeType() == Node.ELEMENT_NODE)) {
String nodeName = paraChildNode.getNodeName();
//System.out.println(nodeName);
//SPB FOUND
if (containsSPB(paraChildNode)){
if (nodeName.equals(XMLString.TEXT_SOFT_PAGE_BREAK)){
//Next node in paragraph. If it is text node go further
Node paraNextNode = paraChildNodes.item(i+1);
Node paraPrevNode = paraFirstPart.getLastChild();
Node paraPrevNode = paraBefore.getLastChild();
String nextText = null;
String prevText = null;
if (paraNextNode != null && paraPrevNode != null ){
@ -576,13 +573,13 @@ public class PageSplitter {
}
// In case paragraph is empty add space to prevent it's removing
if (paraNextNode == null && paraPrevNode == null){
Document doc = paraNode.getOwnerDocument();
Document doc = paraAfter.getOwnerDocument();
Node space = doc.createTextNode(" ");
paraNode.insertBefore(space, paraChildNode);
paraAfter.insertBefore(space, paraChildNode);
}
// remove inner soft page break node
paraNode.removeChild(paraChildNode);
paraAfter.removeChild(paraChildNode);
/* 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
@ -597,19 +594,33 @@ public class PageSplitter {
break;
//ELEMENT WITHOUT SPB
} else if (nodeName.equals(XMLString.TEXT_BOOKMARK_START)){
paraFirstPart.appendChild(paraChildNode.cloneNode(true));
paraBefore.appendChild(paraChildNode.cloneNode(true));
i++;
} else {
paraFirstPart.appendChild(paraChildNode);
paraBefore.appendChild(paraChildNode);
dataMoved = true;
}
//TEXT NODES
} else {
paraFirstPart.appendChild(paraChildNode);
paraBefore.appendChild(paraChildNode);
dataMoved = true;
}
}
//StyleWithProperties style = officeReader.getParStyle(Misc.getAttribute(paraAfter, XMLString.TEXT_STYLE_NAME));
String newStyleName = officeReader.cloneParStyle(Misc.getAttribute(paraAfter, XMLString.TEXT_STYLE_NAME));
Node styleAttr = paraAfter.getAttributes().getNamedItem(XMLString.TEXT_STYLE_NAME);
styleAttr.setTextContent(newStyleName);
StyleWithProperties newStyle = officeReader.getParStyle(Misc.getAttribute(paraAfter, XMLString.TEXT_STYLE_NAME));
//String propVal = style.getParProperty(XMLString.FO_TEXT_INDENT, true);
//System.out.println(propVal);
newStyle.setParProperty(XMLString.FO_TEXT_INDENT, "0");
//propVal = newStyle.getParProperty(XMLString.FO_TEXT_INDENT, true);
//System.out.println(propVal);
//System.out.println(Misc.getAttribute(paraAfter, XMLString.TEXT_STYLE_NAME));
return dataMoved;
}
// Returns true if soft-page-break found. Removes it if removeFound = true