diff --git a/src/main/java/w2phtml/pageSplitters/ListBreaksFix.java b/src/main/java/w2phtml/pageSplitters/ListBreaksFix.java new file mode 100644 index 0000000..11eaa84 --- /dev/null +++ b/src/main/java/w2phtml/pageSplitters/ListBreaksFix.java @@ -0,0 +1,73 @@ +package w2phtml.pageSplitters; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathConstants; +import javax.xml.xpath.XPathExpressionException; +import javax.xml.xpath.XPathFactory; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.w3c.dom.Document; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +import pro.litvinovg.w2phtml.gui.ConversionExecutor; +import pro.litvinovg.xml.Debug; +import w2phtml.office.OfficeReader; +import w2phtml.office.StyleWithProperties; +import w2phtml.util.Misc; + +import static w2phtml.office.XMLString.TEXT_LIST_ITEM; +import static w2phtml.office.XMLString.TEXT_SOFT_PAGE_BREAK; +import static w2phtml.office.XMLString.TEXT_STYLE_NAME; + +import java.util.Iterator; + +import static w2phtml.office.XMLString.TEXT_P; +import static w2phtml.office.XMLString.TEXT_H; +import static w2phtml.office.XMLString.FO_BREAK_BEFORE; + + + +public class ListBreaksFix { + private static final Logger logger = LoggerFactory.getLogger(ListBreaksFix.class); + private OfficeReader ofr; + + public ListBreaksFix(OfficeReader ofr) { + this.ofr = ofr; + } + // Hack to fix hard breaks splitting in lists + public void addSPBsBeforeBreaksInLists(Node onode) { + try { + DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); + DocumentBuilder builder = builderFactory.newDocumentBuilder(); + Document document = onode.getOwnerDocument(); + XPath xPath = XPathFactory.newInstance().newXPath(); + String expression = "//*[starts-with(local-name(),'list-item')]//*[starts-with(local-name(),'p') ]"; + NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(document, XPathConstants.NODESET); + for (int i = 0; i < nodeList.getLength(); i++) { + Node paragraph = nodeList.item(i); + if (hasBreakBefore(paragraph)) { + Node spb = document.createElement(TEXT_SOFT_PAGE_BREAK); + paragraph.getParentNode().insertBefore(spb, paragraph); + } + } + } catch (XPathExpressionException e) { + logger.error("Error fixing hard breaks in lists" + e.getLocalizedMessage()); + e.printStackTrace(); + } catch (ParserConfigurationException e) { + logger.error("Error fixing hard breaks in lists" + e.getLocalizedMessage()); + e.printStackTrace(); + } + } + private boolean hasBreakBefore(Node node) { + StyleWithProperties style = ofr.getParStyle(Misc.getAttribute(node,TEXT_STYLE_NAME)); + if (style != null && "page".equals(style.getProperty(FO_BREAK_BEFORE))) { + return true; + } + return false; + } +}