Refactored. Extracted class PageContainer

This commit is contained in:
Georgy Litvinov 2020-01-29 12:18:05 +01:00
parent 3e53edd42a
commit 2ed1b4ba7f
7 changed files with 66 additions and 43 deletions

View file

@ -1,4 +1,4 @@
package writer2latex.xhtml; package pro.litvinovg.xml;
import org.w3c.dom.Document; import org.w3c.dom.Document;
@ -9,15 +9,16 @@ import org.w3c.dom.ls.LSSerializer;
public class Debug { public class Debug {
public static void printNode(Node node){ public static void printNode(Node node){
Document document = node.getOwnerDocument(); Document document = node.getOwnerDocument();
DOMImplementationLS domImplLS = (DOMImplementationLS) document DOMImplementationLS domImplLS = (DOMImplementationLS) document
.getImplementation(); .getImplementation();
LSSerializer serializer = domImplLS.createLSSerializer(); LSSerializer serializer = domImplLS.createLSSerializer();
String str = serializer.writeToString(node); String str = serializer.writeToString(node);
System.out.println(str); System.out.println(str);
}
public static void printStackTrace() {
for (StackTraceElement ste : Thread.currentThread().getStackTrace()) {
System.out.println(ste);
}
} }
} }

View file

@ -26,8 +26,9 @@
package writer2latex.office; package writer2latex.office;
import org.w3c.dom.Node; import org.w3c.dom.Node;
import pro.litvinovg.xml.Debug;
import writer2latex.util.Misc; import writer2latex.util.Misc;
import writer2latex.xhtml.Debug;
/** <p> Class representing a master page in OOo Writer </p> */ /** <p> Class representing a master page in OOo Writer </p> */
public class MasterPage extends OfficeStyle { public class MasterPage extends OfficeStyle {

View file

@ -26,11 +26,12 @@ package writer2latex.office;
//import org.w3c.dom.Element; //import org.w3c.dom.Element;
import org.w3c.dom.Node; import org.w3c.dom.Node;
import pro.litvinovg.xml.Debug;
import writer2latex.util.Calc; import writer2latex.util.Calc;
//import org.w3c.dom.NamedNodeMap; //import org.w3c.dom.NamedNodeMap;
//import java.util.Hashtable; //import java.util.Hashtable;
import writer2latex.util.Misc; import writer2latex.util.Misc;
import writer2latex.xhtml.Debug;
/** <p> Class representing a style in OOo which contains a style:properties /** <p> Class representing a style in OOo which contains a style:properties
* element </p> * element </p>

View file

@ -58,6 +58,7 @@ import writer2latex.util.ExportNameCollection;
import writer2latex.util.Misc; import writer2latex.util.Misc;
import writer2latex.xhtml.content.DrawParser; import writer2latex.xhtml.content.DrawParser;
import writer2latex.xhtml.content.MathParser; import writer2latex.xhtml.content.MathParser;
import writer2latex.xhtml.content.PageContainer;
import writer2latex.xhtml.content.TableParser; import writer2latex.xhtml.content.TableParser;
import writer2latex.xhtml.content.TextParser; import writer2latex.xhtml.content.TextParser;
import writer2latex.xhtml.l10n.L10n; import writer2latex.xhtml.l10n.L10n;
@ -104,6 +105,7 @@ public class Converter extends BasicConverter {
private XhtmlDocument htmlDoc; // current outfile private XhtmlDocument htmlDoc; // current outfile
private Document htmlDOM; // current DOM, usually within htmlDoc private Document htmlDOM; // current DOM, usually within htmlDoc
private boolean bNeedHeaderFooter = false; private boolean bNeedHeaderFooter = false;
public PageContainer pageContainer = null;
//private int nTocFileIndex = -1; //private int nTocFileIndex = -1;
//private int nAlphabeticalIndex = -1; //private int nAlphabeticalIndex = -1;
@ -122,6 +124,8 @@ public class Converter extends BasicConverter {
super(); super();
config = new XhtmlConfig(); config = new XhtmlConfig();
this.nType = nType; this.nType = nType;
pageContainer = new PageContainer();
} }
// override methods to read templates, style sheets and resources // override methods to read templates, style sheets and resources

View file

@ -0,0 +1,34 @@
package writer2latex.xhtml.content;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import pro.litvinovg.xml.Debug;
public class PageContainer {
private List<String> pageContanerStyles = null;
public PageContainer() {
pageContanerStyles = new ArrayList<String>(Arrays.asList(""));
}
public void setRootStyle(String style){
pageContanerStyles.set(0, style);
}
public void addStyle(String style){
pageContanerStyles.add(style);
}
public void removeStyle() {
int size = pageContanerStyles.size();
if (size < 2) {
System.out.println("Tried to remove last container (page) style!");
Debug.printStackTrace();
System.exit(1);
}
pageContanerStyles.remove(size-1);
}
public String getCurrentStyle() {
return pageContanerStyles.get(pageContanerStyles.size()-1);
}
}

View file

@ -1,16 +1,17 @@
package writer2latex.xhtml; package writer2latex.xhtml.content;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.w3c.dom.Element; 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.XMLString; import writer2latex.office.XMLString;
import writer2latex.xhtml.Converter;
import writer2latex.xhtml.XhtmlConfig;
import static writer2latex.util.Misc.*; import static writer2latex.util.Misc.*;
@ -35,7 +36,7 @@ public class Separator {
private static Converter converter = null; private static Converter converter = null;
private XhtmlConfig config = null; private XhtmlConfig config = null;
private Node prevPageNode = null; private Node prevPageNode = null;
private List<String> pageContanerStyles = new ArrayList<String>(Arrays.asList("")); private PageContainer pageContainer = null;
public Separator(XhtmlConfig config,Converter converter) { public Separator(XhtmlConfig config,Converter converter) {
this.config = config; this.config = config;
@ -45,6 +46,7 @@ public class Separator {
splitLevel = config.getXhtmlSplitLevel(); splitLevel = config.getXhtmlSplitLevel();
splitByPages = pageSeparation.equals(DIV) ? true : false; splitByPages = pageSeparation.equals(DIV) ? true : false;
breakStyle = config.getPageBreakStyle(); breakStyle = config.getPageBreakStyle();
pageContainer = converter.pageContainer;
} }
public Node processOutlineLevel(Node currentNode, Node hnode, int pageNum) { public Node processOutlineLevel(Node currentNode, Node hnode, int pageNum) {
@ -335,9 +337,7 @@ public class Separator {
if (!className.equals("pageContainer")) { if (!className.equals("pageContainer")) {
System.out.println("Can't exit not my container!"); System.out.println("Can't exit not my container!");
Debug.printNode(hnode); Debug.printNode(hnode);
for (StackTraceElement ste : Thread.currentThread().getStackTrace()) { Debug.printStackTrace();
System.out.println(ste);
}
System.exit(1); System.exit(1);
} }
Element parentNode = (Element) hnode.getParentNode(); Element parentNode = (Element) hnode.getParentNode();
@ -346,15 +346,13 @@ public class Separator {
private Node enterPageContainer(Node hnode) { private Node enterPageContainer(Node hnode) {
if (hnode == null) { if (hnode == null) {
System.out.println("Enter page container error. hnode is null"); System.out.println("Enter page container error. hnode is null");
for (StackTraceElement ste : Thread.currentThread().getStackTrace()) { Debug.printStackTrace();
System.out.println(ste);
}
System.exit(1); System.exit(1);
} }
Element container = converter.createElement("div"); Element container = converter.createElement("div");
container.setAttribute("class", "pageContainer"); container.setAttribute("class", "pageContainer");
if (!getLastPageContainerStyle().isEmpty()) { if (!pageContainer.getCurrentStyle().isEmpty()) {
container.setAttribute("style", getLastPageContainerStyle()); container.setAttribute("style", pageContainer.getCurrentStyle());
} }
hnode.appendChild(container); hnode.appendChild(container);
return container; return container;
@ -377,23 +375,7 @@ public class Separator {
return comment; return comment;
} }
public void setPageContainerStyle(String style){
pageContanerStyles.set(0, style);
}
public void addPageContainerStyle(String style){
pageContanerStyles.add(style);
}
public void rmPageContainerStyle() {
int size = pageContanerStyles.size();
if (size < 2) {
System.out.println("Tried to remove last container (page) style!");
System.exit(1);
}
pageContanerStyles.remove(size-1);
}
private String getLastPageContainerStyle() {
return pageContanerStyles.get(pageContanerStyles.size()-1);
}
} }

View file

@ -34,7 +34,6 @@ import org.w3c.dom.Element;
import writer2latex.util.Misc; import writer2latex.util.Misc;
import writer2latex.xhtml.Converter; import writer2latex.xhtml.Converter;
import writer2latex.xhtml.Separator;
import writer2latex.xhtml.PageSplitter; import writer2latex.xhtml.PageSplitter;
import writer2latex.xhtml.Parser; import writer2latex.xhtml.Parser;
import writer2latex.xhtml.StyleInfo; import writer2latex.xhtml.StyleInfo;
@ -80,7 +79,7 @@ public class TextParser extends Parser {
private String sCurrentListLabel = null; private String sCurrentListLabel = null;
private ListStyle currentListStyle = null; private ListStyle currentListStyle = null;
private int nCurrentListLevel = 0; private int nCurrentListLevel = 0;
Separator docSep = null; public Separator docSep = null;
// Mode used to handle floats (depends on source doc type and config) // Mode used to handle floats (depends on source doc type and config)
@ -125,6 +124,7 @@ public class TextParser extends Parser {
private boolean inFooter = false; private boolean inFooter = false;
private String endnotesContext = null; private String endnotesContext = null;
private String footnotesContext = null; private String footnotesContext = null;
PageContainer pageContainer = null;
public TextParser(OfficeReader ofr, XhtmlConfig config, Converter converter) { public TextParser(OfficeReader ofr, XhtmlConfig config, Converter converter) {
super(ofr,config,converter); super(ofr,config,converter);
@ -143,7 +143,7 @@ public class TextParser extends Parser {
DrawParser.FLOATING : DrawParser.ABSOLUTE; DrawParser.FLOATING : DrawParser.ABSOLUTE;
outlineNumbering = new ListCounter(ofr.getOutlineStyle()); outlineNumbering = new ListCounter(ofr.getOutlineStyle());
bDisplayHiddenText = config.displayHiddenText(); bDisplayHiddenText = config.displayHiddenText();
pageContainer = converter.pageContainer;
docSep = new Separator(config, converter); docSep = new Separator(config, converter);
} }
@ -555,7 +555,7 @@ public class TextParser extends Parser {
footnotesContext = lastFootnotesContext; footnotesContext = lastFootnotesContext;
if (removeStyleAtExit) { if (removeStyleAtExit) {
docSep.rmPageContainerStyle(); pageContainer.removeStyle();
} }
return hnode; return hnode;
@ -573,7 +573,7 @@ public class TextParser extends Parser {
int colCount = sectionStyle.getColCount(); int colCount = sectionStyle.getColCount();
if (colCount > 1 ) { if (colCount > 1 ) {
String styleValue = "column-count: " + colCount + ";"; String styleValue = "column-count: " + colCount + ";";
docSep.addPageContainerStyle(styleValue); pageContainer.addStyle(styleValue);
result = true; result = true;
} }
String collectEndnotes = sectionStyle.getSectionProperty("endnote", false); String collectEndnotes = sectionStyle.getSectionProperty("endnote", false);
@ -1815,7 +1815,7 @@ public class TextParser extends Parser {
MasterPage mp = ofr.getFullMasterPage(currentMasterPage); MasterPage mp = ofr.getFullMasterPage(currentMasterPage);
PageLayout layout = ofr.getPageLayout(mp.getPageLayoutName()); PageLayout layout = ofr.getPageLayout(mp.getPageLayoutName());
String containerStyle = "column-count: " + layout.getColCount() + ";"; String containerStyle = "column-count: " + layout.getColCount() + ";";
docSep.setPageContainerStyle(containerStyle); pageContainer.setRootStyle(containerStyle);
} }
private void fitPageNumberToMasterPageStyle() { private void fitPageNumberToMasterPageStyle() {