]> git.argeo.org Git - gpl/argeo-suite.git/blob - argeo/docbook/DbkUtils.java
Prepare next development cycle
[gpl/argeo-suite.git] / argeo / docbook / DbkUtils.java
1 package org.argeo.docbook;
2
3 import javax.jcr.Node;
4 import javax.jcr.NodeIterator;
5 import javax.jcr.RepositoryException;
6 import javax.jcr.nodetype.NodeType;
7
8 import org.argeo.docbook.ui.DocBookNames;
9 import org.argeo.docbook.ui.DocBookTypes;
10 import org.argeo.entity.EntityNames;
11 import org.argeo.entity.EntityType;
12 import org.argeo.jcr.Jcr;
13 import org.argeo.jcr.JcrException;
14 import org.argeo.jcr.JcrUtils;
15 import org.argeo.jcr.JcrxApi;
16
17 /** Utilities around DocBook. */
18 public class DbkUtils {
19 public static String getTitle(Node node) {
20 return JcrxApi.getXmlValue(node, DocBookTypes.TITLE);
21 }
22
23 public static void setTitle(Node node, String txt) {
24 try {
25 Node titleNode = JcrUtils.getOrAdd(node, DocBookTypes.TITLE, DocBookTypes.TITLE);
26 JcrxApi.setXmlValue(node, titleNode, txt);
27 } catch (RepositoryException e) {
28 throw new JcrException("Cannot add empty paragraph to " + node, e);
29 }
30 }
31
32 public static Node getMetadata(Node infoContainer) {
33 try {
34 if (!infoContainer.hasNode(DocBookTypes.INFO))
35 return null;
36 Node info = infoContainer.getNode(DocBookTypes.INFO);
37 if (!info.hasNode(EntityType.local.get()))
38 return null;
39 return info.getNode(EntityType.local.get());
40 } catch (RepositoryException e) {
41 throw new JcrException("Cannot retrieve metadata from " + infoContainer, e);
42 }
43 }
44
45 public static Node getChildByRole(Node parent, String role) {
46 try {
47 NodeIterator baseSections = parent.getNodes();
48 while (baseSections.hasNext()) {
49 Node n = baseSections.nextNode();
50 String r = Jcr.get(n, DocBookNames.DBK_ROLE);
51 if (r != null && r.equals(role))
52 return n;
53 }
54 return null;
55 } catch (RepositoryException e) {
56 throw new JcrException("Cannot get child from " + parent + " with role " + role, e);
57 }
58 }
59
60 public static Node addParagraph(Node node, String txt) {
61 try {
62 Node para = node.addNode(DocBookTypes.PARA, DocBookTypes.PARA);
63 JcrxApi.setXmlValue(node, para, txt);
64 return para;
65 } catch (RepositoryException e) {
66 throw new JcrException("Cannot add empty paragraph to " + node, e);
67 }
68 }
69
70 public static Node insertImageAfter(Node sibling) {
71 try {
72 Node parent = sibling.getParent();
73 Node mediaNode = parent.addNode(DocBookTypes.MEDIAOBJECT, DocBookTypes.MEDIAOBJECT);
74 // TODO optimise?
75 parent.orderBefore(mediaNode.getName() + "[" + mediaNode.getIndex() + "]",
76 sibling.getName() + "[" + sibling.getIndex() + "]");
77 parent.orderBefore(sibling.getName() + "[" + sibling.getIndex() + "]",
78 mediaNode.getName() + "[" + mediaNode.getIndex() + "]");
79
80 Node imageNode = mediaNode.addNode(DocBookTypes.IMAGEOBJECT, DocBookTypes.IMAGEOBJECT);
81 Node infoNode = imageNode.addNode(DocBookTypes.INFO, DocBookTypes.INFO);
82 Node imageDataNode = JcrUtils.copyBytesAsFile(infoNode, EntityType.box.get(), new byte[0]);
83 imageDataNode.addMixin(EntityType.box.get());
84 imageDataNode.setProperty(EntityNames.SVG_WIDTH, 0);
85 imageDataNode.setProperty(EntityNames.SVG_LENGTH, 0);
86 imageDataNode.addMixin(NodeType.MIX_MIMETYPE);
87 return imageDataNode;
88 } catch (RepositoryException e) {
89 throw new JcrException("Cannot insert empty image after " + sibling, e);
90 }
91 }
92
93 /** Singleton. */
94 private DbkUtils() {
95 }
96 }