diff --git a/src/main/java/w2phtml/xhtml/content/Separator.java b/src/main/java/w2phtml/xhtml/content/Separator.java index 877ab47..2eabb62 100644 --- a/src/main/java/w2phtml/xhtml/content/Separator.java +++ b/src/main/java/w2phtml/xhtml/content/Separator.java @@ -55,32 +55,22 @@ public class Separator { headingPath = new int[10]; } - public Node processOutlineLevel(Node currentNode, Node hnode, int pageNum) { + public Node whatToDoWithOutlineNodeOnPage(Node currentNode, Node hnode, int pageNum) { // Get outline level - Node outlineNode = null; - if (currentNode.getNodeName().contentEquals(TABLE_TABLE)) { - outlineNode = getOulineNodeFromTable(currentNode); - } else { - outlineNode = currentNode; + if (isBadOutlineNode(currentNode)) { + return hnode; } - + Node outlineNode = getRealOntlineNode(currentNode); Integer curLevel = getOutlineLevel(outlineNode); - if (curLevel < 1) { - return hnode; - } - String title = getOutlineTitle(outlineNode); - if (title == null || title.isEmpty()) { - return hnode; - } - - if (noSplitNeeded(pageNum, curLevel)) { -// System.out.println("No split needed"); + if (!greenstoneSeparation && + !needPagination() && + !needCurrentLevelSplitOnThisPage(curLevel,pageNum)) { return hnode; } - if (isSplitTime(curLevel, pageNum)) { + if (needCurrentLevelSplitOnThisPage(curLevel, pageNum)) { prevPageContainer = hnode; } @@ -94,7 +84,7 @@ public class Separator { updateHeadingPath(curLevel); - if (isSplitTime(curLevel, pageNum)) { + if (needCurrentLevelSplitOnThisPage(curLevel, pageNum)) { hnode = splitFiles(hnode); if (rdfSeparation) { setContentPath(hnode,curLevel); @@ -107,13 +97,36 @@ public class Separator { addToCommentStack(curLevel.toString()); } - if (pagination) { + if (needPagination()) { hnode = openPage(hnode, pageNum); } return hnode; } + private boolean isBadOutlineNode(Node currentNode) { + Node outlineNode = getRealOntlineNode(currentNode); + Integer curLevel = getOutlineLevel(outlineNode); + if (curLevel < 1) { + return true; + } + String title = getOutlineTitle(outlineNode); + if (title == null || title.isEmpty()) { + return true; + } + return false; + } + + private Node getRealOntlineNode(Node currentNode) { + Node outlineNode = null; + if (currentNode.getNodeName().contentEquals(TABLE_TABLE)) { + outlineNode = getOulineNodeFromTable(currentNode); + } else { + outlineNode = currentNode; + } + return outlineNode; + } + private int getOutlineLevel(Node currentNode) { String levelAttr = getAttribute(currentNode, XMLString.TEXT_OUTLINE_LEVEL); if (levelAttr == null || levelAttr.isEmpty()) { @@ -159,11 +172,10 @@ public class Separator { } - private boolean noSplitNeeded(int pageNum, int curLevel) { - return !greenstoneSeparation && !needPageSplits() && !isSplitTime(curLevel,pageNum); - } - private boolean needChapterSplits() { + if (splitLevel > 0 ) { + return true; + } if (greenstoneSeparation) { return true; } @@ -173,7 +185,7 @@ public class Separator { return false; } - private boolean needPageSplits() { + private boolean needPagination() { if (pagination) { return true; } @@ -212,10 +224,13 @@ public class Separator { ((Element) hnode).setAttribute("path", path.toString()); } - private boolean isSplitTime(int curLevel, int pageNum) { - if ((rdfSeparation || splitLevel >= curLevel )) { + private boolean needCurrentLevelSplitOnThisPage(int curLevel, int curPage) { + if (!needChapterSplits()) { + return false; + } + if ( splitLevel >= curLevel ) { if (alignSplitToPages) { - if (lastSplitPageNum != pageNum) { + if (lastSplitPageNum != curPage) { return true; } } else { @@ -226,7 +241,7 @@ public class Separator { } public Node processPageBreak(Node currentNode, Node hnode, Integer pageNum) { - if (!pagination) { + if (!needPagination()) { return hnode; } if (pageOpened) { @@ -245,7 +260,7 @@ public class Separator { * Opens main document section heading tag */ public Node startDocument(Node hnode, String title, int pageNum) { - if (!greenstoneSeparation && !pagination) { + if (!greenstoneSeparation && !needPagination()) { return hnode; } if (greenstoneSeparation) { @@ -259,7 +274,7 @@ public class Separator { // Method to close open tags at the end of the document public Node endDocument(Node hnode) { - if (!greenstoneSeparation && !pagination) { + if (!greenstoneSeparation && !needPagination()) { return hnode; } if (pageOpened) { @@ -357,7 +372,6 @@ public class Separator { public static String getOutlineTitle(Node currentNode) { Node content = currentNode.cloneNode(true); - String title = null; NodeList lineBreaks = ((Element) content).getElementsByTagName(XMLString.TEXT_LINE_BREAK); NodeList textTabs = ((Element) content).getElementsByTagName(XMLString.TEXT_TAB); @@ -365,7 +379,7 @@ public class Separator { replaceWithSpaces(textTabs); deleteNotesIn(content); deleteCommentsIn(content); - title = content.getTextContent().trim(); + String title = content.getTextContent().trim(); return title; } @@ -435,7 +449,7 @@ public class Separator { return hnode; } hnode = exitPageContainer(hnode); - if (pagination) { + if (needPagination()) { hnode = exitPageDiv(hnode); } pageOpened = false; @@ -443,7 +457,7 @@ public class Separator { } public Node openPage(Node hnode, Integer pageNum) { - if (pageOpened == true || !pagination) { + if (pageOpened == true || !needPagination()) { return hnode; } hnode = openPageDiv(hnode, pageNum); @@ -505,19 +519,14 @@ public class Separator { } public Node breaksOrOutline(Node currentNode, Node hnode, int pageNum) { - Node outlineNode = null; - if (currentNode.getNodeName().contentEquals(TABLE_TABLE)) { - outlineNode = getOulineNodeFromTable(currentNode); - } else { - outlineNode = currentNode; - } + Node outlineNode = getRealOntlineNode(currentNode); Integer curLevel = getOutlineLevel(outlineNode); String title = getOutlineTitle(outlineNode); if (curLevel < 1 || title == null || title.isEmpty()) { hnode = processPageBreak(currentNode, hnode, pageNum); } else { - hnode = processOutlineLevel(currentNode, hnode, pageNum); + hnode = whatToDoWithOutlineNodeOnPage(currentNode, hnode, pageNum); } return hnode; } diff --git a/src/main/java/w2phtml/xhtml/content/TextParser.java b/src/main/java/w2phtml/xhtml/content/TextParser.java index ac185b3..1115c13 100644 --- a/src/main/java/w2phtml/xhtml/content/TextParser.java +++ b/src/main/java/w2phtml/xhtml/content/TextParser.java @@ -1211,7 +1211,7 @@ public class TextParser extends Parser { if (currentMasterPage == null && style != null) { //Document wasn't started yet. hnode = startDocument(hnode, style, newPageNumber); - hnode = docSep.processOutlineLevel(currentNode, hnode, pageNum); + hnode = docSep.whatToDoWithOutlineNodeOnPage(currentNode, hnode, pageNum); } else if (hasMasterPage(style) || hasBreakBefore(style) || breakBeforeNextNode) { if (hasBreakBefore(style)) { @@ -1235,7 +1235,7 @@ public class TextParser extends Parser { return hnode; } else { - hnode = docSep.processOutlineLevel(currentNode, hnode, pageNum); + hnode = docSep.whatToDoWithOutlineNodeOnPage(currentNode, hnode, pageNum); } if (checkHardBreakAfter(style)) {