From 3dbbd238832f635af58843fa66f51dfdb32733e3 Mon Sep 17 00:00:00 2001 From: Georgy Litvinov Date: Sun, 21 Apr 2019 20:02:37 +0300 Subject: [PATCH] Refactoring --- ...nstoneTags.java => DocumentSeparator.java} | 30 ++++++++--- .../writer2latex/xhtml/TextConverter.java | 51 ++++++++++--------- 2 files changed, 49 insertions(+), 32 deletions(-) rename src/main/java/writer2latex/xhtml/{GreenstoneTags.java => DocumentSeparator.java} (87%) diff --git a/src/main/java/writer2latex/xhtml/GreenstoneTags.java b/src/main/java/writer2latex/xhtml/DocumentSeparator.java similarity index 87% rename from src/main/java/writer2latex/xhtml/GreenstoneTags.java rename to src/main/java/writer2latex/xhtml/DocumentSeparator.java index 0a6fe9b..2d62171 100644 --- a/src/main/java/writer2latex/xhtml/GreenstoneTags.java +++ b/src/main/java/writer2latex/xhtml/DocumentSeparator.java @@ -14,12 +14,12 @@ import static writer2latex.util.Misc.*; //LinkedList stringList = new LinkedList(); -public class GreenstoneTags { +public class DocumentSeparator { private static final String NONE = "none"; private static final String SECTIONS = "sections"; private static final String DIV = "div"; - + private int splitLevel = 0; private static LinkedList headerStack = new LinkedList(); private static boolean pageOpened = false; @@ -27,8 +27,18 @@ public class GreenstoneTags { private static String headingSeparation = "sections"; //sections div none private static String pageSeparation = "sections"; + private Converter converter = null; + private XhtmlConfig config = null; - protected static Node processHeading(Node currentNode, Node hnode, int pageNum) { + public DocumentSeparator(XhtmlConfig config,Converter converter) { + this.config = config; + this.converter = converter; + headingSeparation = config.getHeadingTags(); + pageSeparation = config.getPageTags(); + splitLevel = config.getXhtmlSplitLevel(); + } + + protected Node processHeading(Node currentNode, Node hnode, int pageNum) { //Get outline level String sLevel = getAttribute(currentNode, XMLString.TEXT_OUTLINE_LEVEL); @@ -39,15 +49,21 @@ public class GreenstoneTags { if (title == null || title.isEmpty()) { return hnode; } - int nLevel = Integer.parseInt(sLevel); + int curLevel = Integer.parseInt(sLevel); if (pageOpened) { hnode = closePage(hnode); } if (headingSeparation.equals(SECTIONS)){ - closeCommentHeadings(hnode, nLevel); + closeCommentHeadings(hnode, curLevel); } + //System.out.println("before split " + curLevel + " Has content? " + converter.getOutFileIndex()); + //System.out.println(hnode.getNodeName()); + /*if (splitLevel >= curLevel && (converter.outFileHasContent() || converter.getOutFileIndex() == 1)) { + System.out.println("Split happened"); + hnode = converter.nextOutFile(); + }*/ //Place to split headings if (headingSeparation.equals(SECTIONS)){ @@ -76,9 +92,7 @@ public class GreenstoneTags { /** * Opens main document section heading tag */ - protected static Node StartDocument(Node hnode, String title, String heading, String pages, int pageNum){ - headingSeparation = heading; - pageSeparation = pages; + protected Node StartDocument(Node hnode, String title, int pageNum){ if (noHeadingSeparation() && noPageSeparation()){ return hnode; diff --git a/src/main/java/writer2latex/xhtml/TextConverter.java b/src/main/java/writer2latex/xhtml/TextConverter.java index 52c6bab..27d3259 100644 --- a/src/main/java/writer2latex/xhtml/TextConverter.java +++ b/src/main/java/writer2latex/xhtml/TextConverter.java @@ -72,6 +72,8 @@ public class TextConverter extends ConverterHelper { private String sCurrentListLabel = null; private ListStyle currentListStyle = null; private int nCurrentListLevel = 0; + DocumentSeparator docSep = null; + // Mode used to handle floats (depends on source doc type and config) private int nFloatMode; @@ -104,8 +106,8 @@ public class TextConverter extends ConverterHelper { //Current master page name private String nextMasterPage = null; - private String headingTags = config.getHeadingTags(); - private String pageTags = config.getPageTags(); + private String headingSeparator = config.getHeadingTags(); + private String pageSeparator = config.getPageTags(); private boolean breakBeforeNextNode = false; private boolean inTable = false; private boolean inList = false; @@ -130,6 +132,8 @@ public class TextConverter extends ConverterHelper { DrawConverter.FLOATING : DrawConverter.ABSOLUTE; outlineNumbering = new ListCounter(ofr.getOutlineStyle()); bDisplayHiddenText = config.displayHiddenText(); + + docSep = new DocumentSeparator(config, converter); } /** Converts an office node as a complete text document @@ -155,7 +159,7 @@ public class TextConverter extends ConverterHelper { extractRealTOC(onode); } //Split pages - if (!pageTags.equals("none")) { + if (!pageSeparator.equals("none")) { onode = (Element) PageSplitter.splitSoftPageBreak(onode,ofr); } @@ -167,11 +171,11 @@ public class TextConverter extends ConverterHelper { // Add footnotes and endnotes footCv.insertFootnotes(hnode,true); - if (!pageTags.equals("none")) { + if (!pageSeparator.equals("none")) { addFooter(hnode); } endCv.insertEndnotes(hnode); - hnode = (Element) GreenstoneTags.endDocument(hnode); + hnode = (Element) docSep.endDocument(hnode); // Generate all indexes bInToc = true; tocCv.generate(); @@ -318,9 +322,8 @@ public class TextConverter extends ConverterHelper { StyleWithProperties style = ofr.getParStyle(Misc.getAttribute(child,XMLString.TEXT_STYLE_NAME)); int nOutlineLevel = getOutlineLevel((Element)child); Node rememberNode = hnode; - hnode = processPageBreaks(child, hnode, style); - //hnode = GreenstoneTags.processHeading(child, hnode, pageNum); - hnode = maybeSplit(hnode,style,nOutlineLevel); + hnode = processPageBreaks(child, hnode, style); + //hnode = maybeSplit(hnode,style,nOutlineLevel); nCharacterCount+=OfficeReader.getCharacterCount(child); handleHeading((Element)child,(Element)hnode,rememberNode!=hnode); } @@ -364,7 +367,7 @@ public class TextConverter extends ConverterHelper { } hnode = processPageBreaks(child, hnode,style); inList = true; - hnode = maybeSplit(hnode,null); + //hnode = maybeSplit(hnode,null); if (listIsOnlyHeadings(child)) { nDontSplitLevel--; hnode = handleFakeList(child,nLevel+1,styleName,hnode); @@ -378,7 +381,7 @@ public class TextConverter extends ConverterHelper { else if (nodeName.equals(XMLString.TABLE_TABLE)) { StyleWithProperties style = ofr.getTableStyle(Misc.getAttribute(child, XMLString.TABLE_STYLE_NAME)); hnode = processPageBreaks(child, hnode,style); - hnode = maybeSplit(hnode,style); + //hnode = maybeSplit(hnode,style); inTable = true; getTableCv().handleTable(child,hnode); inTable = false; @@ -388,14 +391,14 @@ public class TextConverter extends ConverterHelper { } else if (nodeName.equals(XMLString.TEXT_SECTION)) { hnode = processPageBreaks(child, hnode,null); - hnode = maybeSplit(hnode,null); + // hnode = maybeSplit(hnode,null); nDontSplitLevel--; hnode = handleSection(child,hnode); nDontSplitLevel++; } else if (nodeName.equals(XMLString.TEXT_TABLE_OF_CONTENT)) { if (!ofr.getTocReader((Element)child).isByChapter()) { - hnode = maybeSplit(hnode,null,1); + // hnode = maybeSplit(hnode,null,1); } tocCv.handleIndex((Element)child,(Element)hnode); } @@ -521,10 +524,10 @@ public class TextConverter extends ConverterHelper { String sStyleName = Misc.getAttribute(onode,XMLString.TEXT_STYLE_NAME); ////closePageSection Element div = (Element) hnode; - if (headingTags == null) { + if (headingSeparator == null) { div = converter.createElement("div"); // close page before enter - hnode = GreenstoneTags.closePage(hnode); + hnode = docSep.closePage(hnode); hnode.appendChild(div); converter.addTarget(div, sName + "|region"); @@ -533,17 +536,17 @@ public class TextConverter extends ConverterHelper { applyStyle(sectionInfo, div); sections.push(onode); //open page after enter - div = (Element) GreenstoneTags.openPage(div, pageNum); + div = (Element) docSep.openPage(div, pageNum); } Node newhnode = traverseBlockText(onode, div); Node result = null; - if (headingTags == null) { + if (headingSeparator == null) { //close page before exit - newhnode = GreenstoneTags.closePage(newhnode); + newhnode = docSep.closePage(newhnode); result = newhnode.getParentNode(); //Open page after exit - result = (Element) GreenstoneTags.openPage(result, pageNum); + result = (Element) docSep.openPage(result, pageNum); sections.pop(); } else { result = newhnode; @@ -1692,7 +1695,7 @@ public class TextConverter extends ConverterHelper { } if (currentMasterPage == null && style != null) { hnode = StartMasterPage(hnode, style, newPageNumber); - hnode = GreenstoneTags.processHeading(currentNode, hnode, pageNum); + hnode = docSep.processHeading(currentNode, hnode, pageNum); } else if (checkMasterPageBreak(style) || checkHardBreakBefore(style) || breakBeforeNextNode) { // Insert footnotes inFootnote = true; @@ -1710,9 +1713,9 @@ public class TextConverter extends ConverterHelper { fitPageNumberToMasterPageStyle(); } if (hasOutlineLevel(currentNode)) { - hnode = GreenstoneTags.processHeading(currentNode, hnode, pageNum); + hnode = docSep.processHeading(currentNode, hnode, pageNum); } else { - hnode = GreenstoneTags.processPageBreak(currentNode, hnode, pageNum); + hnode = docSep.processPageBreak(currentNode, hnode, pageNum); } @@ -1722,7 +1725,7 @@ public class TextConverter extends ConverterHelper { return hnode; } else { - hnode = GreenstoneTags.processHeading(currentNode, hnode, pageNum); + hnode = docSep.processHeading(currentNode, hnode, pageNum); } @@ -1756,10 +1759,10 @@ public class TextConverter extends ConverterHelper { //Start tagging String sTitle = converter.getTitle(); - hnode = GreenstoneTags.StartDocument(hnode, sTitle,headingTags,pageTags, pageNum); + hnode = docSep.StartDocument(hnode, sTitle,pageNum); //Print header - if (!pageTags.equals("none")) { + if (!pageSeparator.equals("none")) { addHeader(hnode); } return hnode;