diff --git a/src/main/java/writer2latex/util/Misc.java b/src/main/java/writer2latex/util/Misc.java index b607e98..48334d1 100644 --- a/src/main/java/writer2latex/util/Misc.java +++ b/src/main/java/writer2latex/util/Misc.java @@ -313,7 +313,26 @@ public class Misc{ public static final boolean isElement(Node node) { return node.getNodeType()==Node.ELEMENT_NODE; } - + + public static boolean isRoot(Node node) { + if (node.getParentNode() == null) { + return true; + } + return false; + } + + public static boolean isElement(String name, Node node) { + + if (!isElement(node)){ + return false; + } + + if (((Element) node).getTagName().equals(name)){ + return true; + } + return false; + } + /* Utility method to determine if a Node is a specific Element */ public static final boolean isElement(Node node, String sTagName) { diff --git a/src/main/java/writer2latex/xhtml/GreenstoneTags.java b/src/main/java/writer2latex/xhtml/GreenstoneTags.java index 6b59d91..e0a8f9e 100644 --- a/src/main/java/writer2latex/xhtml/GreenstoneTags.java +++ b/src/main/java/writer2latex/xhtml/GreenstoneTags.java @@ -10,7 +10,7 @@ import org.w3c.dom.Node; import org.w3c.dom.NodeList; import writer2latex.office.XMLString; -import writer2latex.util.Misc; +import static writer2latex.util.Misc.*; //LinkedList stringList = new LinkedList(); @@ -24,14 +24,14 @@ public class GreenstoneTags { private static LinkedList headerStack = new LinkedList(); private static boolean pageOpened = false; //headings none - private static String headingTags = "sections"; + private static String headingSeparation = "sections"; //sections div none - private static String pageTags = "sections"; + private static String pageSeparation = "sections"; protected static Node processHeading(Node currentNode, Node hnode, int pageNum) { //Get outline level - String sLevel = Misc.getAttribute(currentNode, XMLString.TEXT_OUTLINE_LEVEL); + String sLevel = getAttribute(currentNode, XMLString.TEXT_OUTLINE_LEVEL); String title = getTitle(currentNode).trim(); if (sLevel == null || sLevel.isEmpty()) { return hnode; @@ -45,13 +45,13 @@ public class GreenstoneTags { hnode = closePage(hnode); } - if (headingTags.equals(SECTIONS)){ + if (headingSeparation.equals(SECTIONS)){ closeHeadingSections(hnode, nLevel); - openHeadingSection(hnode, title); + openHeadingComment(hnode, title); headerStack.offerFirst(Integer.parseInt(sLevel)); } - if (!pageTags.equals(NONE) && !headingTags.equals(NONE)){ + if (!noPageSeparation() && !noHeadingSeparation()){ hnode = openPage(hnode, pageNum); } @@ -59,7 +59,7 @@ public class GreenstoneTags { } protected static Node processPageBreak(Node currentNode, Node hnode, Integer pageNum){ - if (pageTags.equals(NONE)){ + if (noPageSeparation()){ return hnode; } if (pageOpened) { @@ -74,15 +74,15 @@ public class GreenstoneTags { * Opens main document section heading tag */ protected static Node StartDocument(Node hnode, String title, String heading, String pages, int pageNum){ - headingTags = heading; - pageTags = pages; + headingSeparation = heading; + pageSeparation = pages; - if (headingTags.equals(NONE) && pageTags.equals(NONE)){ + if (noHeadingSeparation() && noPageSeparation()){ return hnode; } - if(headingTags.equals(SECTIONS)){ + if(headingSeparation.equals(SECTIONS)){ //Create global section - openHeadingSection(hnode, title); + openHeadingComment(hnode, title); } hnode = openPage(hnode, pageNum); @@ -90,50 +90,43 @@ public class GreenstoneTags { } //Method to close open tags at the end of the document protected static Node endDocument(Node hnode){ - if (headingTags.equals(NONE) && pageTags.equals(NONE)){ + if (noHeadingSeparation() && noPageSeparation()){ return hnode; } if (pageOpened){ hnode = closePage(hnode); } - if (headingTags.equals(SECTIONS)){ + if (headingSeparation.equals(SECTIONS)){ closeHeadingSections(hnode, 0); //Close global section - closeSection(hnode); + addCloseComment(hnode); } return hnode; } - private static Node openPageDiv(Node hnode,int pageNum){ - if (hnode == null){ - return hnode; + private static Node openPageDiv(Node node,int pageNum){ + if (node == null){ + return node; } - Document doc = hnode.getOwnerDocument(); + Document doc = node.getOwnerDocument(); Element openBlock = (Element) doc.createElement("div"); openBlock.setAttribute("class", "pageNum"); openBlock.setAttribute("page", Integer.toString(pageNum)); // insert open section comment before header node - hnode.appendChild((Node)openBlock); - hnode = openBlock; + node.appendChild((Node)openBlock); + node = openBlock; return openBlock; } - private static Node exitPageDiv(Node hnode){ + private static Node exitPageDiv(Node node){ - while ( hnode.getParentNode() != null - && - !( - hnode.getNodeType() == Node.ELEMENT_NODE - && - ((Element) hnode).getTagName().equals("div") - ) - ){ - hnode = hnode.getParentNode(); + while ( !isRoot(node) && !isElement("div", node) ){ + node = node.getParentNode(); } - Node result = hnode.getParentNode(); - if (hnode.getChildNodes().getLength() == 0){ - result.removeChild(hnode); + Node result = node.getParentNode(); + if (node.getChildNodes().getLength() == 0){ + result.removeChild(node); } return result; @@ -147,7 +140,7 @@ public class GreenstoneTags { int i = 0; while (i < contentNodes.getLength()) { Node child = contentNodes.item(i); - if (child.getNodeType() == Node.ELEMENT_NODE){ + if (isElement(child) ){ if (child.getNodeName().equals(XMLString.TEXT_TAB) || child.getNodeName().equals(XMLString.TEXT_LINE_BREAK) ){ Document doc = child.getOwnerDocument(); @@ -169,30 +162,25 @@ public class GreenstoneTags { } - private static void openPageSection(Node hnode, Integer pageNum){ + private static void openPageComment(Node hnode, Integer pageNum){ Document doc = hnode.getOwnerDocument(); - String commentText = "
\n\n" + pageNum - + "\n" + pageNum + "\n"; - Node openSection = doc.createComment(commentText); + Node openSection = doc.createComment(openPageCommentText(pageNum)); // insert open section comment before header node hnode.appendChild(openSection); } - private static void openHeadingSection(Node hnode, String title){ + + private static void openHeadingComment(Node hnode, String title){ Document doc = hnode.getOwnerDocument(); - String commentText = "
\n\n" + title - + "\n"; - Node openSection = doc.createComment(commentText); + Node openSection = doc.createComment(openHeadingCommentText(title)); // insert open section comment before header node hnode.appendChild(openSection); - } - private static void closeSection(Node hnode){ - Document doc = hnode.getOwnerDocument(); - String commentText = "
"; - Node closeSection = doc.createComment(commentText); + private static void addCloseComment(Node node){ + Document doc = node.getOwnerDocument(); + Node closeSection = doc.createComment("
"); //insert open section comment before header node - hnode.appendChild(closeSection); + node.appendChild(closeSection); } private static void closeHeadingSections(Node hnode, int nLevel){ @@ -201,7 +189,7 @@ public class GreenstoneTags { } //Close all sections with level less than current while (nLevel <= headerStack.peek()) { - closeSection(hnode); + addCloseComment(hnode); headerStack.poll(); if (headerStack.isEmpty()) { break; @@ -209,32 +197,48 @@ public class GreenstoneTags { } } protected static Node closePage(Node hnode){ - if (pageTags.equals(SECTIONS)){ + if (pageSeparation.equals(SECTIONS)){ //If section is empty. In case we are closing section // the last comment is opened page section if (hnode.getLastChild().getNodeType() == Node.COMMENT_NODE){ hnode.removeChild(hnode.getLastChild()); } else { - closeSection(hnode); + addCloseComment(hnode); } - } else if (pageTags.equals(DIV)){ + } else if (pageSeparation.equals(DIV)){ hnode = exitPageDiv(hnode); } pageOpened = false; return hnode; } protected static Node openPage(Node hnode, Integer pageNum){ - if (pageTags.equals(SECTIONS)){ - openPageSection(hnode, pageNum); + if (pageSeparation.equals(SECTIONS)){ + openPageComment(hnode, pageNum); pageOpened = true; } - else if (pageTags.equals(DIV)){ + else if (pageSeparation.equals(DIV)){ hnode = openPageDiv(hnode, pageNum); pageOpened = true; } return hnode; } - + private static boolean noHeadingSeparation() { + return headingSeparation.equals(NONE); + } + private static boolean noPageSeparation() { + return pageSeparation.equals(NONE); + } + private static String openHeadingCommentText(String title) { + String comment = "
\n\n" + title + + "\n"; + return comment; + } + private static String openPageCommentText(Integer pageNum) { + String comment = "
\n\n" + pageNum + + "\n" + pageNum + "\n"; + return comment; + } + } \ No newline at end of file