1 package org
.argeo
.docbook
;
4 import javax
.jcr
.NodeIterator
;
5 import javax
.jcr
.RepositoryException
;
6 import javax
.jcr
.nodetype
.NodeType
;
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
;
17 /** Utilities around DocBook. */
18 public class DbkUtils
{
19 public static String
getTitle(Node node
) {
20 return JcrxApi
.getXmlValue(node
, DocBookTypes
.TITLE
);
23 public static void setTitle(Node node
, String txt
) {
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
);
32 public static Node
getMetadata(Node infoContainer
) {
34 if (!infoContainer
.hasNode(DocBookTypes
.INFO
))
36 Node info
= infoContainer
.getNode(DocBookTypes
.INFO
);
37 if (!info
.hasNode(EntityType
.local
.get()))
39 return info
.getNode(EntityType
.local
.get());
40 } catch (RepositoryException e
) {
41 throw new JcrException("Cannot retrieve metadata from " + infoContainer
, e
);
45 public static Node
getChildByRole(Node parent
, String role
) {
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
))
55 } catch (RepositoryException e
) {
56 throw new JcrException("Cannot get child from " + parent
+ " with role " + role
, e
);
60 public static Node
addParagraph(Node node
, String txt
) {
62 Node para
= node
.addNode(DocBookTypes
.PARA
, DocBookTypes
.PARA
);
63 JcrxApi
.setXmlValue(node
, para
, txt
);
65 } catch (RepositoryException e
) {
66 throw new JcrException("Cannot add empty paragraph to " + node
, e
);
70 public static Node
insertImageAfter(Node sibling
) {
72 Node parent
= sibling
.getParent();
73 Node mediaNode
= parent
.addNode(DocBookTypes
.MEDIAOBJECT
, DocBookTypes
.MEDIAOBJECT
);
75 parent
.orderBefore(mediaNode
.getName() + "[" + mediaNode
.getIndex() + "]",
76 sibling
.getName() + "[" + sibling
.getIndex() + "]");
77 parent
.orderBefore(sibling
.getName() + "[" + sibling
.getIndex() + "]",
78 mediaNode
.getName() + "[" + mediaNode
.getIndex() + "]");
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
);
88 } catch (RepositoryException e
) {
89 throw new JcrException("Cannot insert empty image after " + sibling
, e
);