]> git.argeo.org Git - gpl/argeo-suite.git/blob - publishing/org.argeo.publishing.ui/src/org/argeo/docbook/DbkUtils.java
Manage mixin supertypes.
[gpl/argeo-suite.git] / publishing / org.argeo.publishing.ui / src / org / argeo / docbook / DbkUtils.java
1 package org.argeo.docbook;
2
3 import static org.argeo.docbook.DbkType.para;
4
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;
10
11 import javax.jcr.ImportUUIDBehavior;
12 import javax.jcr.Node;
13 import javax.jcr.NodeIterator;
14 import javax.jcr.RepositoryException;
15
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;
23
24 /** Utilities around DocBook. */
25 public class DbkUtils {
26 private final static Log log = LogFactory.getLog(DbkUtils.class);
27
28 /** Get or add a DocBook element. */
29 public static Node getOrAddDbk(Node parent, DbkType child) {
30 try {
31 if (!parent.hasNode(child.get())) {
32 return addDbk(parent, child);
33 } else {
34 return parent.getNode(child.get());
35 }
36 } catch (RepositoryException e) {
37 throw new JcrException("Cannot get or add element " + child.get() + " to " + parent, e);
38 }
39 }
40
41 /** Add a DocBook element to this node. */
42 public static Node addDbk(Node parent, DbkType child) {
43 try {
44 Node node = parent.addNode(child.get(), child.get());
45 return node;
46 } catch (RepositoryException e) {
47 throw new JcrException("Cannot add element " + child.get() + " to " + parent, e);
48 }
49 }
50
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());
54 }
55
56 public static String getTitle(Node node) {
57 return JcrxApi.getXmlValue(node, DbkType.title.get());
58 }
59
60 public static void setTitle(Node node, String txt) {
61 Node titleNode = getOrAddDbk(node, DbkType.title);
62 JcrxApi.setXmlValue(node, titleNode, txt);
63 }
64
65 public static Node getMetadata(Node infoContainer) {
66 try {
67 if (!infoContainer.hasNode(DbkType.info.get()))
68 return null;
69 Node info = infoContainer.getNode(DbkType.info.get());
70 if (!info.hasNode(EntityType.local.get()))
71 return null;
72 return info.getNode(EntityType.local.get());
73 } catch (RepositoryException e) {
74 throw new JcrException("Cannot retrieve metadata from " + infoContainer, e);
75 }
76 }
77
78 public static Node getChildByRole(Node parent, String role) {
79 try {
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))
85 return n;
86 }
87 return null;
88 } catch (RepositoryException e) {
89 throw new JcrException("Cannot get child from " + parent + " with role " + role, e);
90 }
91 }
92
93 public static Node addParagraph(Node node, String txt) {
94 Node p = addDbk(node, para);
95 JcrxApi.setXmlValue(node, p, txt);
96 return p;
97 }
98
99 public static Node insertImageAfter(Node sibling) {
100 try {
101
102 Node parent = sibling.getParent();
103 Node mediaNode = addDbk(parent, DbkType.mediaobject);
104 // TODO optimise?
105 parent.orderBefore(mediaNode.getName() + "[" + mediaNode.getIndex() + "]",
106 sibling.getName() + "[" + sibling.getIndex() + "]");
107 parent.orderBefore(sibling.getName() + "[" + sibling.getIndex() + "]",
108 mediaNode.getName() + "[" + mediaNode.getIndex() + "]");
109
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);
118 //
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);
123 return mediaNode;
124 } catch (RepositoryException e) {
125 throw new JcrException("Cannot insert empty image after " + sibling, e);
126 }
127 }
128
129 public static void exportXml(Node node, OutputStream out) throws IOException {
130 try {
131 node.getSession().exportDocumentView(node.getPath(), out, false, false);
132 } catch (RepositoryException e) {
133 throw new JcrException("Cannot export " + node + " to XML", e);
134 }
135 }
136
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);
143 try {
144 Files.createDirectories(directory);
145 try (OutputStream out = Files.newOutputStream(filePath)) {
146 exportXml(docBookNode, out);
147 }
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);
153 }
154 }
155
156 public static void importXml(Node baseNode, InputStream in) throws IOException {
157 try {
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);
162 }
163
164 }
165
166 /** Singleton. */
167 private DbkUtils() {
168 }
169
170 }