From 5256b06d2077847e319d4279cddb5efec17804f6 Mon Sep 17 00:00:00 2001 From: Georgy Litvinov Date: Tue, 14 Jan 2020 19:56:17 +0100 Subject: [PATCH] Refactoring --- .../java/writer2latex/xhtml/PageSplitter.java | 122 ++++++++---------- 1 file changed, 57 insertions(+), 65 deletions(-) diff --git a/src/main/java/writer2latex/xhtml/PageSplitter.java b/src/main/java/writer2latex/xhtml/PageSplitter.java index 65427d0..854be2b 100644 --- a/src/main/java/writer2latex/xhtml/PageSplitter.java +++ b/src/main/java/writer2latex/xhtml/PageSplitter.java @@ -1,16 +1,13 @@ package writer2latex.xhtml; -import javax.print.Doc; - import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import writer2latex.office.OfficeReader; -import writer2latex.office.OfficeStyle; import writer2latex.office.StyleWithProperties; -import writer2latex.office.XMLString; +import static writer2latex.office.XMLString.*; import writer2latex.util.Misc; public class PageSplitter { @@ -21,7 +18,7 @@ public class PageSplitter { //Find par node with soft page break inside and split it officeReader = ofr; Document document = onode.getOwnerDocument(); - Element softPageBreak = document.createElement(XMLString.TEXT_SOFT_PAGE_BREAK); + Element softPageBreak = document.createElement(TEXT_SOFT_PAGE_BREAK); NodeList nodes = onode.getChildNodes(); int i = 0; @@ -36,44 +33,39 @@ public class PageSplitter { //Create Duplicate Node! Element childFirstPart = (Element) child.cloneNode(false); StyleWithProperties style = null; - if ((nodeName.equals(XMLString.TEXT_P) || nodeName.equals(XMLString.TEXT_H))) { + if ((nodeName.equals(TEXT_P) || nodeName.equals(TEXT_H))) { //If SPB not the first node if (handleParagraph(childFirstPart, child)){ onode.insertBefore(childFirstPart, child); - style = ofr.getParStyle(Misc.getAttribute(child, XMLString.TEXT_STYLE_NAME)); -// OfficeStyle styleCopy = style.Clone(); + style = ofr.getParStyle(Misc.getAttribute(child, TEXT_STYLE_NAME)); } - } else if (nodeName.equals(XMLString.TABLE_TABLE)) { + } else if (nodeName.equals(TABLE_TABLE)) { if (handleTableTable(childFirstPart, child)){ onode.insertBefore(childFirstPart, child); - style = ofr.getTableStyle(Misc.getAttribute(child, XMLString.TABLE_STYLE_NAME)); - + style = ofr.getTableStyle(Misc.getAttribute(child, TABLE_STYLE_NAME)); } - } else if (nodeName.equals(XMLString.TEXT_LIST)) { + } else if (nodeName.equals(TEXT_LIST)) { if (handleList(childFirstPart, child)){ onode.insertBefore(childFirstPart, child); } - } else if (nodeName.equals(XMLString.TEXT_SECTION)) { - //System.out.println("Get into Section "); - + } else if (nodeName.equals(TEXT_SECTION)) { if (handleSection(childFirstPart, child)){ onode.insertBefore(childFirstPart, child); - style = ofr.getSectionStyle(Misc.getAttribute(child, XMLString.TEXT_SECTION)); - //System.out.println("Data moved"); + style = ofr.getSectionStyle(Misc.getAttribute(child, TEXT_SECTION)); } - } else if (nodeName.equals(XMLString.TEXT_TABLE_OF_CONTENT)){ + } else if (nodeName.equals(TEXT_TABLE_OF_CONTENT)){ //HACK containsSPB(childFirstPart); i++; continue; - } else if (nodeName.equals(XMLString.TEXT_SOFT_PAGE_BREAK)){ + } else if (nodeName.equals(TEXT_SOFT_PAGE_BREAK)){ //HACK i++; continue; } //TODO: IF fo:break before in original table - don't create SPB - if (style == null || !"page".equals(style.getProperty(XMLString.FO_BREAK_BEFORE))){ + if (style == null || !"page".equals(style.getProperty(FO_BREAK_BEFORE))){ onode.insertBefore(softPageBreak.cloneNode(false), child); } //HACK! @@ -103,7 +95,7 @@ public class PageSplitter { Node listChild = listNodes.item(i); if(listChild.getNodeType() == Node.ELEMENT_NODE){ String nodeName = listChild.getNodeName(); - if (nodeName.equals(XMLString.TEXT_LIST_HEADER)) { + if (nodeName.equals(TEXT_LIST_HEADER)) { if(containsSPB(listChild)){ //Remove inner SPB removeSPB(listChild); @@ -115,7 +107,7 @@ public class PageSplitter { i++; - } else if (nodeName.equals(XMLString.TEXT_LIST_ITEM)) { + } else if (nodeName.equals(TEXT_LIST_ITEM)) { if (containsSPB(listChild)){ Node listItemFirstPart = listChild.cloneNode(false); //remove SPB, move previous nodes to firstPart. @@ -138,7 +130,7 @@ public class PageSplitter { } //Add text:continue-numbering="true" if (dataMoved){ - ((Element) list).setAttribute(XMLString.TEXT_CONTINUE_NUMBERING, "true"); + ((Element) list).setAttribute(TEXT_CONTINUE_NUMBERING, "true"); } break; } else { @@ -167,16 +159,16 @@ public class PageSplitter { if (containsSPB(listItemChild)){ Node listItemChildFirstPart = listItemChild.cloneNode(false); //Break if SPB - if (nodeName.equals(XMLString.TEXT_SOFT_PAGE_BREAK)) { + if (nodeName.equals(TEXT_SOFT_PAGE_BREAK)) { //Remove SPB.Return result listItem.removeChild(listItemChild); - } else if (nodeName.equals(XMLString.TEXT_LIST)) { + } else if (nodeName.equals(TEXT_LIST)) { if (handleList(listItemChildFirstPart, listItemChild)){ listItemFirstPart.appendChild(listItemChildFirstPart); dataMoved=true; } - } else if (nodeName.equals(XMLString.TEXT_H) || nodeName.equals(XMLString.TEXT_P)) { + } else if (nodeName.equals(TEXT_H) || nodeName.equals(TEXT_P)) { if (handleParagraph(listItemChildFirstPart, listItemChild)){ listItemFirstPart.appendChild(listItemChildFirstPart); @@ -228,29 +220,29 @@ public class PageSplitter { //System.out.println("Table child node " + tableChildNodeName); if (containsSPB(tableChildNode)){ Node tableChildFirstPart = tableChildNode.cloneNode(false); - if (tableChildNodeName.equals(XMLString.TEXT_SOFT_PAGE_BREAK)) { + if (tableChildNodeName.equals(TEXT_SOFT_PAGE_BREAK)) { // remove inner soft page break node table.removeChild(tableChildNode); - } else if (tableChildNodeName.equals(XMLString.TABLE_TABLE_ROW_GROUP)) { + } else if (tableChildNodeName.equals(TABLE_TABLE_ROW_GROUP)) { if (handleTableRowGroup(tableChildFirstPart, tableChildNode)){ dataMoved = true; tableFirstPart.appendChild(tableChildFirstPart); } - } else if ( tableChildNodeName.equals(XMLString.TABLE_TABLE_ROWS)) { + } else if ( tableChildNodeName.equals(TABLE_TABLE_ROWS)) { if (handleTableRows(tableChildFirstPart, tableChildNode)){ dataMoved = true; tableFirstPart.appendChild(tableChildFirstPart); } - } else if ( tableChildNodeName.equals(XMLString.TABLE_TABLE_ROW)) { + } else if ( tableChildNodeName.equals(TABLE_TABLE_ROW)) { if (handleTableRow(tableChildFirstPart, tableChildNode)){ dataMoved = true; tableFirstPart.appendChild(tableChildFirstPart); } - } else if (tableChildNodeName.equals(XMLString.TABLE_TABLE_COLUMN) - || tableChildNodeName.equals(XMLString.TABLE_TABLE_COLUMN_GROUP) - || tableChildNodeName.equals(XMLString.TABLE_TABLE_HEADER_ROWS) - || tableChildNodeName.equals(XMLString.TABLE_TABLE_HEADER_COLUMNS)) { + } else if (tableChildNodeName.equals(TABLE_TABLE_COLUMN) + || tableChildNodeName.equals(TABLE_TABLE_COLUMN_GROUP) + || tableChildNodeName.equals(TABLE_TABLE_HEADER_ROWS) + || tableChildNodeName.equals(TABLE_TABLE_HEADER_COLUMNS)) { //Remove Soft Page Break removeSPB(tableChildNode); } @@ -259,10 +251,10 @@ public class PageSplitter { } else { //Before SPB //Description nodes - if (tableChildNodeName.equals(XMLString.TABLE_TABLE_COLUMN) - || tableChildNodeName.equals(XMLString.TABLE_TABLE_COLUMN_GROUP) - || tableChildNodeName.equals(XMLString.TABLE_TABLE_HEADER_ROWS) - || tableChildNodeName.equals(XMLString.TABLE_TABLE_HEADER_COLUMNS)) { + if (tableChildNodeName.equals(TABLE_TABLE_COLUMN) + || tableChildNodeName.equals(TABLE_TABLE_COLUMN_GROUP) + || tableChildNodeName.equals(TABLE_TABLE_HEADER_ROWS) + || tableChildNodeName.equals(TABLE_TABLE_HEADER_COLUMNS)) { //Append to clone table tableFirstPart.appendChild(tableChildNode.cloneNode(true)); //increment counter @@ -291,25 +283,25 @@ public class PageSplitter { if (containsSPB(tableRowGroupChildNode)){ Node tableRowGroupChildFirstPart = tableRowGroupChildNode.cloneNode(false); - if (nodeName.equals(XMLString.TEXT_SOFT_PAGE_BREAK)){ + if (nodeName.equals(TEXT_SOFT_PAGE_BREAK)){ // remove inner soft page break node tableRowGroup.removeChild(tableRowGroupChildNode); - } else if (nodeName.equals(XMLString.TABLE_TABLE_HEADER_ROWS)){ + } else if (nodeName.equals(TABLE_TABLE_HEADER_ROWS)){ //Nothing IF table-header-rows found - it is description node, //Not needed to set dataMoved = true, not needed to append First part - } else if (nodeName.equals(XMLString.TABLE_TABLE_ROW)){ + } else if (nodeName.equals(TABLE_TABLE_ROW)){ if (handleTableRow(tableRowGroupChildFirstPart, tableRowGroupChildNode)){ dataMoved = true; tableRowGroupFistPart.appendChild(tableRowGroupChildFirstPart); } - } else if (nodeName.equals(XMLString.TABLE_TABLE_ROW_GROUP)){ + } else if (nodeName.equals(TABLE_TABLE_ROW_GROUP)){ if (handleTableRowGroup(tableRowGroupChildFirstPart, tableRowGroupChildNode)){ dataMoved = true; tableRowGroupFistPart.appendChild(tableRowGroupChildFirstPart); } - } else if (nodeName.equals(XMLString.TABLE_TABLE_ROWS)){ + } else if (nodeName.equals(TABLE_TABLE_ROWS)){ if (handleTableRows(tableRowGroupChildFirstPart, tableRowGroupChildNode)){ dataMoved = true; tableRowGroupFistPart.appendChild(tableRowGroupChildFirstPart); @@ -317,7 +309,7 @@ public class PageSplitter { } break; } else { - if (nodeName.equals(XMLString.TABLE_TABLE_HEADER_ROWS)){ + if (nodeName.equals(TABLE_TABLE_HEADER_ROWS)){ tableRowGroupFistPart.appendChild(tableRowGroupChildNode.cloneNode(true)); //increment counter i++; @@ -349,11 +341,11 @@ public class PageSplitter { if (containsSPB(tableRowsChildNode)){ Node tableRowGroupChildFirstPart = tableRowsChildNode.cloneNode(false); - if (nodeName.equals(XMLString.TEXT_SOFT_PAGE_BREAK)){ + if (nodeName.equals(TEXT_SOFT_PAGE_BREAK)){ // remove inner soft page break node tableRows.removeChild(tableRowsChildNode); - } else if (nodeName.equals(XMLString.TABLE_TABLE_ROW)){ + } else if (nodeName.equals(TABLE_TABLE_ROW)){ if (handleTableRow(tableRowGroupChildFirstPart, tableRowsChildNode)){ dataMoved = true; tableRowsFistPart.appendChild(tableRowGroupChildFirstPart); @@ -387,13 +379,13 @@ public class PageSplitter { if (containsSPB(tableRowChildNode)){ Node tableRowGroupChildFirstPart = tableRowChildNode.cloneNode(false); - if (nodeName.equals(XMLString.TABLE_TABLE_CELL)){ + if (nodeName.equals(TABLE_TABLE_CELL)){ if (handleCell(tableRowGroupChildFirstPart, tableRowChildNode)){ dataMoved = true; tableRowFistPart.appendChild(tableRowGroupChildFirstPart); } - } else if (nodeName.equals(XMLString.TABLE_COVERED_TABLE_CELL)){ + } else if (nodeName.equals(TABLE_COVERED_TABLE_CELL)){ //Implement handleCoveredCell in future if (handleCell(tableRowGroupChildFirstPart, tableRowChildNode)){ dataMoved = true; @@ -406,7 +398,7 @@ public class PageSplitter { tableRowFistPart.appendChild(tableRowChildNode.cloneNode(true)); Node emptyCell = tableRowChildNode.cloneNode(false); Document document = tableRow.getOwnerDocument(); - Element textP = document.createElement(XMLString.TEXT_P); + Element textP = document.createElement(TEXT_P); emptyCell.appendChild(textP); tableRow.insertBefore(emptyCell, tableRowChildNode); tableRow.removeChild(tableRowChildNode); @@ -436,11 +428,11 @@ public class PageSplitter { String nodeName = cellChildNode.getNodeName(); if (containsSPB(cellChildNode)){ Node cellChildFirstPart = cellChildNode.cloneNode(false); - if (nodeName.equals(XMLString.TEXT_SOFT_PAGE_BREAK)){ + if (nodeName.equals(TEXT_SOFT_PAGE_BREAK)){ // remove inner soft page break node cellNode.removeChild(cellChildNode); - } else if (nodeName.equals(XMLString.TEXT_H) || nodeName.equals(XMLString.TEXT_P)) { + } else if (nodeName.equals(TEXT_H) || nodeName.equals(TEXT_P)) { if (handleParagraph(cellChildFirstPart, cellChildNode)){ cellFirstPart.appendChild(cellChildFirstPart); @@ -475,33 +467,33 @@ public class PageSplitter { if (containsSPB(sectionChildNode)){ // System.out.println("Contains spb"); Node sectionChildFirstPart = sectionChildNode.cloneNode(false); - if (nodeName.equals(XMLString.TEXT_SOFT_PAGE_BREAK)){ + if (nodeName.equals(TEXT_SOFT_PAGE_BREAK)){ // remove inner soft page break node sectionNode.removeChild(sectionChildNode); - } else if (nodeName.equals(XMLString.TEXT_H) || nodeName.equals(XMLString.TEXT_P)) { + } else if (nodeName.equals(TEXT_H) || nodeName.equals(TEXT_P)) { if (handleParagraph(sectionChildFirstPart, sectionChildNode)){ sectionFirstPart.appendChild(sectionChildFirstPart); dataMoved=true; } - } else if (nodeName.equals(XMLString.TEXT_TABLE_OF_CONTENT)) { + } else if (nodeName.equals(TEXT_TABLE_OF_CONTENT)) { //HACK removeSPB(sectionNode); i++; continue; - } else if (nodeName.equals(XMLString.TABLE_TABLE)) { + } else if (nodeName.equals(TABLE_TABLE)) { if (handleTableTable(sectionChildFirstPart, sectionChildNode)){ sectionFirstPart.appendChild(sectionChildFirstPart); dataMoved=true; } - } else if (nodeName.equals(XMLString.TEXT_SECTION)) { + } else if (nodeName.equals(TEXT_SECTION)) { if (handleSection(sectionChildFirstPart, sectionChildNode)){ sectionFirstPart.appendChild(sectionChildFirstPart); dataMoved=true; } - } else if (nodeName.equals(XMLString.TEXT_LIST)) { + } else if (nodeName.equals(TEXT_LIST)) { if (handleList(sectionChildFirstPart, sectionChildNode)){ sectionFirstPart.appendChild(sectionChildFirstPart); dataMoved=true; @@ -536,7 +528,7 @@ public class PageSplitter { String nodeName = paraChildNode.getNodeName(); //SPB FOUND if (containsSPB(paraChildNode)){ - if (nodeName.equals(XMLString.TEXT_SOFT_PAGE_BREAK)){ + if (nodeName.equals(TEXT_SOFT_PAGE_BREAK)){ //Next node in paragraph. If it is text node go further Node paraNextNode = paraChildNodes.item(i+1); Node paraPrevNode = paraBefore.getLastChild(); @@ -593,7 +585,7 @@ public class PageSplitter { } break; //ELEMENT WITHOUT SPB - } else if (nodeName.equals(XMLString.TEXT_BOOKMARK_START)){ + } else if (nodeName.equals(TEXT_BOOKMARK_START)){ paraBefore.appendChild(paraChildNode.cloneNode(true)); i++; } else { @@ -612,19 +604,19 @@ public class PageSplitter { return dataMoved; } private static void removeIndent(Node paraAfter) { - String baseStyleName = Misc.getAttribute(paraAfter, XMLString.TEXT_STYLE_NAME); + String baseStyleName = Misc.getAttribute(paraAfter, TEXT_STYLE_NAME); String newStyleName = officeReader.cloneParStyle(baseStyleName); - Node styleAttr = paraAfter.getAttributes().getNamedItem(XMLString.TEXT_STYLE_NAME); + Node styleAttr = paraAfter.getAttributes().getNamedItem(TEXT_STYLE_NAME); styleAttr.setTextContent(newStyleName); - StyleWithProperties newStyle = officeReader.getParStyle(Misc.getAttribute(paraAfter, XMLString.TEXT_STYLE_NAME)); - newStyle.setParProperty(XMLString.FO_TEXT_INDENT, "0"); + StyleWithProperties newStyle = officeReader.getParStyle(Misc.getAttribute(paraAfter, TEXT_STYLE_NAME)); + newStyle.setParProperty(FO_TEXT_INDENT, "0"); } // Returns true if soft-page-break found. Removes it if removeFound = true private static void removeSPB(Node node) { if (node.getNodeType() == Node.ELEMENT_NODE) { - if (node.getNodeName().equals(XMLString.TEXT_SOFT_PAGE_BREAK)) { + if (node.getNodeName().equals(TEXT_SOFT_PAGE_BREAK)) { Node parent = node.getParentNode(); parent.removeChild(node); return; @@ -642,7 +634,7 @@ public class PageSplitter { } private static boolean containsSPB(Node node) { if (node.getNodeType() == Node.ELEMENT_NODE) { - if (node.getNodeName().equals(XMLString.TEXT_SOFT_PAGE_BREAK)) { + if (node.getNodeName().equals(TEXT_SOFT_PAGE_BREAK)) { return true; } if (node.hasChildNodes()) {