1   package nl.dedicon.pipeline.braille.step;
2   
3   import java.util.ArrayList;
4   import java.util.Arrays;
5   import java.util.List;
6   import org.apache.commons.lang3.StringUtils;
7   import org.w3c.dom.Document;
8   import org.w3c.dom.Element;
9   import org.w3c.dom.Node;
10  import org.w3c.dom.NodeList;
11  
12  /**
13   * Move notes right after the parent node of their corresponding noteref
14   * 
15   * @author Paul Rambags
16   */
17  public class JacketManager {
18      
19      public static final String[] JACKET_CLASSES = { "flap", "jacket_copy", "jacketcopy" };
20      
21      private static final String CLASS = "class";
22      private static final String LEVEL1 = "level1";
23      private static final String H1 = "h1";
24      private static final String P = "p";
25      
26      private final List<Element> jackets = new ArrayList<>();      // Element(level1)
27      
28      /**
29       * Collect all jackets (there should be at most one)
30       * 
31       * @param document Document
32       */
33      public void collect(Document document) {
34          String namespace = document.getDocumentElement().getNamespaceURI();
35          NodeList level1List = document.getElementsByTagNameNS(namespace, LEVEL1);
36          for (int i = 0; i < level1List.getLength(); ++i) {
37              Element level1 = (Element)level1List.item(i);
38              String classValue = level1.getAttribute(CLASS);
39              // wait for version 3.7 of commons-lang3
40              // if (StringUtils.equalsAny(classValue, JACKET_CLASSES)) {
41              if (Arrays.asList(JACKET_CLASSES).contains(classValue)) {
42                  jackets.add(level1);
43              }
44          }
45      }
46  
47      /**
48       * set the jacket-header
49       * an existing header will become a paragraph
50       * 
51       * @param header Jacket header
52       */
53      public void setHeader(String header) {
54          if (StringUtils.isBlank(header)) {
55              return;
56          }
57  
58          for (Element jacket : jackets) {
59              Node headerNode = Utils.getChild(jacket, H1);
60              if (headerNode != null) {
61                  Utils.renameNode(headerNode, P);
62              }
63              Node newHeaderNode = Utils.addChildBefore(jacket, jacket.getFirstChild(), H1);
64              newHeaderNode.setTextContent(header);
65          }
66      }
67  
68      /**
69       * move jackets to the front
70       */
71      public void move() {
72          if (jackets.isEmpty()) {
73              return;
74          }
75          
76          Document document = jackets.get(0).getOwnerDocument();
77          Element dtbook = document.getDocumentElement();
78          Node book = Utils.getChild(dtbook, "book");
79  
80          if (book == null) {
81              return;
82          }
83          
84          Node frontMatter = Utils.getChild(book, "frontmatter");
85          if (frontMatter == null) {
86              frontMatter = Utils.addChild(book, "frontmatter");
87          }
88  
89          Node refChild = frontMatter.getFirstChild();
90          for (Element jacket : jackets) {
91              frontMatter.insertBefore(jacket, refChild);
92              refChild = jacket.getNextSibling();
93          }
94      }
95  }