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