Added split method for alphabetical index

This commit is contained in:
Georgy Litvinov 2020-02-06 16:07:00 +01:00
parent 2b7d161bf9
commit f0a377345e

View file

@ -5,6 +5,7 @@ import org.w3c.dom.Element;
import org.w3c.dom.Node; import org.w3c.dom.Node;
import org.w3c.dom.NodeList; import org.w3c.dom.NodeList;
import pro.litvinovg.xml.Debug;
import writer2latex.office.OfficeReader; import writer2latex.office.OfficeReader;
import writer2latex.office.StyleWithProperties; import writer2latex.office.StyleWithProperties;
import static writer2latex.office.XMLString.*; import static writer2latex.office.XMLString.*;
@ -14,7 +15,7 @@ public class ODFPageSplitter {
static Node truncatedListItemNodeContent = null; static Node truncatedListItemNodeContent = null;
static OfficeReader officeReader = null; static OfficeReader officeReader = null;
public static Node splitSoftPageBreak(Node onode,OfficeReader ofr){ public static Node splitText(Node onode,OfficeReader ofr){
//Find par node with soft page break inside and split it //Find par node with soft page break inside and split it
officeReader = ofr; officeReader = ofr;
Document document = onode.getOwnerDocument(); Document document = onode.getOwnerDocument();
@ -49,6 +50,12 @@ public class ODFPageSplitter {
if (handleList(childFirstPart, child)){ if (handleList(childFirstPart, child)){
onode.insertBefore(childFirstPart, child); onode.insertBefore(childFirstPart, child);
} }
} else if (nodeName.equals(TEXT_ALPHABETICAL_INDEX)) {
Node indexFirstPart = splitABIndex(child);
if (indexFirstPart != null) {
onode.insertBefore(indexFirstPart, child);
}
} else if (nodeName.equals(TEXT_SECTION)) { } else if (nodeName.equals(TEXT_SECTION)) {
Node sectionFirstPart = handleSection(child); Node sectionFirstPart = handleSection(child);
if (sectionFirstPart != null) { if (sectionFirstPart != null) {
@ -64,6 +71,10 @@ public class ODFPageSplitter {
//HACK //HACK
i++; i++;
continue; continue;
} else {
Debug.printNode(child);
System.exit(1);
} }
//TODO: IF fo:break before in original table - don't create SPB //TODO: IF fo:break before in original table - don't create SPB
@ -89,43 +100,86 @@ public class ODFPageSplitter {
//Debug.printNode(onode); //Debug.printNode(onode);
return onode; return onode;
} }
private static Node splitABIndex(Node abIndex) {
Node abIndexFirstPart = abIndex.cloneNode(false);
NodeList childs = abIndex.getChildNodes();
int i = 0;
while (childs.getLength() > i) {
Node child = childs.item(i);
String childName = child.getNodeName();
if (childName.equals(TEXT_ALPHABETICAL_INDEX_SOURCE)) {
abIndexFirstPart.appendChild(child.cloneNode(true));
} else
if (childName.equals(TEXT_INDEX_BODY)) {
Node indexBodyFirstPart = splitTextIndexBody(child);
abIndexFirstPart.appendChild(indexBodyFirstPart);
}
i++;
}
return abIndexFirstPart;
}
private static Node splitTextIndexBody(Node indexBody) {
Node indexBodyFirstPart = indexBody.cloneNode(false);
NodeList childs = indexBody.getChildNodes();
int i = 0;
while (childs.getLength() > i) {
Node child = childs.item(i);
if (!containsSPB(child)) {
indexBodyFirstPart.appendChild(child);
} else {
String childName = child.getNodeName();
if (childName.equals(TEXT_P)) {
Node paraFirstPart = handleParagraph(child);
if (paraFirstPart != null) {
indexBodyFirstPart.appendChild(paraFirstPart);
}
return indexBodyFirstPart;
} else
if (childName.equals(TEXT_SOFT_PAGE_BREAK)) {
indexBody.removeChild(child);
return indexBodyFirstPart;
}
}
}
return indexBodyFirstPart;
}
private static boolean handleList(Node listFirstPart, Node list){ private static boolean handleList(Node listFirstPart, Node list){
NodeList listNodes = list.getChildNodes(); NodeList listNodes = list.getChildNodes();
int i = 0; int i = 0;
boolean dataMoved = false; boolean dataMoved = false;
while (listNodes.getLength() > i) { while (listNodes.getLength() > i) {
Node listChild = listNodes.item(i); Node child = listNodes.item(i);
if(listChild.getNodeType() == Node.ELEMENT_NODE){ if(child.getNodeType() == Node.ELEMENT_NODE){
String nodeName = listChild.getNodeName(); String nodeName = child.getNodeName();
if (nodeName.equals(TEXT_LIST_HEADER)) { if (nodeName.equals(TEXT_LIST_HEADER)) {
if(containsSPB(listChild)){ if(containsSPB(child)){
//Remove inner SPB //Remove inner SPB
removeSPB(listChild); removeSPB(child);
//HACK :( //HACK :(
break; break;
} }
listFirstPart.appendChild(listChild.cloneNode(true)); listFirstPart.appendChild(child.cloneNode(true));
//Get next element //Get next element
i++; i++;
} else if (nodeName.equals(TEXT_LIST_ITEM)) { } else if (nodeName.equals(TEXT_LIST_ITEM)) {
if (containsSPB(listChild)){ if (containsSPB(child)){
Node listItemFirstPart = listChild.cloneNode(false); Node listItemFirstPart = child.cloneNode(false);
//remove SPB, move previous nodes to firstPart. //remove SPB, move previous nodes to firstPart.
if (handleListItem(listItemFirstPart,listChild)){ if (handleListItem(listItemFirstPart,child)){
dataMoved = true; dataMoved = true;
//Add first part of list item to previous list item //Add first part of list item to previous list item
listFirstPart.appendChild(listItemFirstPart); listFirstPart.appendChild(listItemFirstPart);
//Get list parent node and move cutted node //Get list parent node and move cutted node
//After First Part and SPB but before this list; //After First Part and SPB but before this list;
//TODO!!!!!!!!!! //TODO!!!!!!!!!!
truncatedListItemNodeContent = listChild; truncatedListItemNodeContent = child;
listFirstPart.getParentNode(); listFirstPart.getParentNode();
//If List item is empty - remove it //If List item is empty - remove it
if (!listChild.hasChildNodes()){ if (!child.hasChildNodes()){
list.removeChild(listChild); list.removeChild(child);
} }
@ -138,7 +192,7 @@ public class ODFPageSplitter {
break; break;
} else { } else {
// Not with SPB yet, move node, set dataMoved=true // Not with SPB yet, move node, set dataMoved=true
listFirstPart.appendChild(listChild); listFirstPart.appendChild(child);
dataMoved = true; dataMoved = true;
} }
@ -535,14 +589,13 @@ public class ODFPageSplitter {
} }
private static Node handleParagraph(Node para) { private static Node handleParagraph(Node para) {
//System.out.println("handlePara"); //System.out.println("handlePara");
Node paraBefore = para.cloneNode(false); Node paraFirstPart = para.cloneNode(false);
boolean dataMoved = false; boolean dataMoved = false;
int i = 0; int i = 0;
NodeList сhilds = para.getChildNodes(); NodeList childs = para.getChildNodes();
while (сhilds.getLength() > i) { while (childs.getLength() > i) {
//System.out.println("In WHILE HANDLE PARA"); //System.out.println("In WHILE HANDLE PARA");
Node child = childs.item(i);
Node child = сhilds.item(i);
//NOT TEXT NODES //NOT TEXT NODES
if ((child.getNodeType() == Node.ELEMENT_NODE)) { if ((child.getNodeType() == Node.ELEMENT_NODE)) {
String childName = child.getNodeName(); String childName = child.getNodeName();
@ -557,11 +610,9 @@ public class ODFPageSplitter {
if (childName.equals(TEXT_NOTE)){ if (childName.equals(TEXT_NOTE)){
Element textNote = (Element) child; Element textNote = (Element) child;
//System.out.println("handle TextNote in para"); //System.out.println("handle TextNote in para");
splitSoftPageBreak(textNote.getElementsByTagName(TEXT_NOTE_BODY).item(0), officeReader); splitText(textNote.getElementsByTagName(TEXT_NOTE_BODY).item(0), officeReader);
} else } else
if (childName.equals(TEXT_SPAN)){ if (childName.equals(TEXT_SPAN)){
Element textNote = (Element) child;
//System.out.println("handle SPAN");
handleSpan(child); handleSpan(child);
} else { } else {
//Debug.printNode(child); //Debug.printNode(child);
@ -571,24 +622,24 @@ public class ODFPageSplitter {
break; break;
//Other elements //Other elements
} else if (childName.equals(TEXT_BOOKMARK_START)){ } else if (childName.equals(TEXT_BOOKMARK_START)){
paraBefore.appendChild(child.cloneNode(true)); paraFirstPart.appendChild(child.cloneNode(true));
i++; i++;
} else { } else {
dataMoved = appendChild(paraBefore, child); dataMoved = appendChild(paraFirstPart, child);
} }
//TEXT NODES //TEXT NODES
} else { } else {
dataMoved = appendChild(paraBefore, child); dataMoved = appendChild(paraFirstPart, child);
} }
} }
addHyphen(para,paraBefore); addHyphen(para,paraFirstPart);
if (dataMoved) { if (dataMoved) {
removeIndent(para); removeIndent(para);
return paraBefore; return paraFirstPart;
} }
@ -603,7 +654,7 @@ public class ODFPageSplitter {
if (child.getNodeType() == Node.ELEMENT_NODE) { if (child.getNodeType() == Node.ELEMENT_NODE) {
if (containsSPB(child)) { if (containsSPB(child)) {
if (child.getNodeName().equals(TEXT_NOTE)) { if (child.getNodeName().equals(TEXT_NOTE)) {
splitSoftPageBreak(((Element)child).getElementsByTagName(TEXT_NOTE_BODY).item(0), officeReader); splitText(((Element)child).getElementsByTagName(TEXT_NOTE_BODY).item(0), officeReader);
} else if (child.getNodeName().equals(TEXT_SPAN)){ } else if (child.getNodeName().equals(TEXT_SPAN)){
handleSpan(child); handleSpan(child);
} else { } else {