1 package org
.argeo
.docbook
;
3 import static org
.argeo
.docbook
.DbkType
.para
;
5 import java
.io
.IOException
;
6 import java
.io
.InputStream
;
7 import java
.io
.OutputStream
;
8 import java
.nio
.file
.Files
;
9 import java
.nio
.file
.Path
;
11 import javax
.jcr
.ImportUUIDBehavior
;
12 import javax
.jcr
.Node
;
13 import javax
.jcr
.NodeIterator
;
14 import javax
.jcr
.RepositoryException
;
16 import org
.apache
.commons
.logging
.Log
;
17 import org
.apache
.commons
.logging
.LogFactory
;
18 import org
.argeo
.entity
.EntityType
;
19 import org
.argeo
.jcr
.Jcr
;
20 import org
.argeo
.jcr
.JcrException
;
21 import org
.argeo
.jcr
.JcrUtils
;
22 import org
.argeo
.jcr
.JcrxApi
;
24 /** Utilities around DocBook. */
25 public class DbkUtils
{
26 private final static Log log
= LogFactory
.getLog(DbkUtils
.class);
28 /** Get or add a DocBook element. */
29 public static Node
getOrAddDbk(Node parent
, DbkType child
) {
31 if (!parent
.hasNode(child
.get())) {
32 return addDbk(parent
, child
);
34 return parent
.getNode(child
.get());
36 } catch (RepositoryException e
) {
37 throw new JcrException("Cannot get or add element " + child
.get() + " to " + parent
, e
);
41 /** Add a DocBook element to this node. */
42 public static Node
addDbk(Node parent
, DbkType child
) {
44 Node node
= parent
.addNode(child
.get(), child
.get());
46 } catch (RepositoryException e
) {
47 throw new JcrException("Cannot add element " + child
.get() + " to " + parent
, e
);
51 /** Whether this DocBook element is of this type. */
52 public static boolean isDbk(Node node
, DbkType type
) {
53 return Jcr
.getName(node
).equals(type
.get());
56 public static String
getTitle(Node node
) {
57 return JcrxApi
.getXmlValue(node
, DbkType
.title
.get());
60 public static void setTitle(Node node
, String txt
) {
61 Node titleNode
= getOrAddDbk(node
, DbkType
.title
);
62 JcrxApi
.setXmlValue(node
, titleNode
, txt
);
65 public static Node
getMetadata(Node infoContainer
) {
67 if (!infoContainer
.hasNode(DbkType
.info
.get()))
69 Node info
= infoContainer
.getNode(DbkType
.info
.get());
70 if (!info
.hasNode(EntityType
.local
.get()))
72 return info
.getNode(EntityType
.local
.get());
73 } catch (RepositoryException e
) {
74 throw new JcrException("Cannot retrieve metadata from " + infoContainer
, e
);
78 public static Node
getChildByRole(Node parent
, String role
) {
80 NodeIterator baseSections
= parent
.getNodes();
81 while (baseSections
.hasNext()) {
82 Node n
= baseSections
.nextNode();
83 String r
= Jcr
.get(n
, DbkAttr
.role
.name());
84 if (r
!= null && r
.equals(role
))
88 } catch (RepositoryException e
) {
89 throw new JcrException("Cannot get child from " + parent
+ " with role " + role
, e
);
93 public static Node
addParagraph(Node node
, String txt
) {
94 Node p
= addDbk(node
, para
);
95 JcrxApi
.setXmlValue(node
, p
, txt
);
99 public static Node
insertImageAfter(Node sibling
) {
102 Node parent
= sibling
.getParent();
103 Node mediaNode
= addDbk(parent
, DbkType
.mediaobject
);
105 parent
.orderBefore(mediaNode
.getName() + "[" + mediaNode
.getIndex() + "]",
106 sibling
.getName() + "[" + sibling
.getIndex() + "]");
107 parent
.orderBefore(sibling
.getName() + "[" + sibling
.getIndex() + "]",
108 mediaNode
.getName() + "[" + mediaNode
.getIndex() + "]");
110 Node imageNode
= addDbk(mediaNode
, DbkType
.imageobject
);
111 Node imageDataNode
= addDbk(imageNode
, DbkType
.imagedata
);
112 // Node infoNode = imageNode.addNode(DocBookTypes.INFO, DocBookTypes.INFO);
113 // Node fileNode = JcrUtils.copyBytesAsFile(mediaFolder, EntityType.box.get(), new byte[0]);
114 // fileNode.addMixin(EntityType.box.get());
115 // fileNode.setProperty(EntityNames.SVG_WIDTH, 0);
116 // fileNode.setProperty(EntityNames.SVG_LENGTH, 0);
117 // fileNode.addMixin(NodeType.MIX_MIMETYPE);
119 // // we assume this is a folder next to the main DocBook document
120 // // TODO make it more robust and generic
121 // String fileRef = mediaNode.getName();
122 // imageDataNode.setProperty(DocBookNames.DBK_FILEREF, fileRef);
124 } catch (RepositoryException e
) {
125 throw new JcrException("Cannot insert empty image after " + sibling
, e
);
129 public static void exportXml(Node node
, OutputStream out
) throws IOException
{
131 node
.getSession().exportDocumentView(node
.getPath(), out
, false, false);
132 } catch (RepositoryException e
) {
133 throw new JcrException("Cannot export " + node
+ " to XML", e
);
137 public static void exportToFs(Node baseNode
, DbkType type
, Path directory
) {
138 String fileName
= Jcr
.getName(baseNode
) + ".dbk.xml";
139 Path filePath
= directory
.resolve(fileName
);
140 Node docBookNode
= Jcr
.getNode(baseNode
, type
.get());
141 if (docBookNode
== null)
142 throw new IllegalArgumentException("No " + type
.get() + " under " + baseNode
);
144 Files
.createDirectories(directory
);
145 try (OutputStream out
= Files
.newOutputStream(filePath
)) {
146 exportXml(docBookNode
, out
);
148 JcrUtils
.copyFilesToFs(baseNode
, directory
, true);
149 if (log
.isDebugEnabled())
150 log
.debug("DocBook " + baseNode
+ " exported to " + filePath
.toAbsolutePath());
151 } catch (IOException e
) {
152 throw new RuntimeException(e
);
156 public static void importXml(Node baseNode
, InputStream in
) throws IOException
{
158 baseNode
.getSession().importXML(baseNode
.getPath(), in
,
159 ImportUUIDBehavior
.IMPORT_UUID_COLLISION_REPLACE_EXISTING
);
160 } catch (RepositoryException e
) {
161 throw new JcrException("Cannot import XML to " + baseNode
, e
);