import static org.argeo.docbook.DbkType.para;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
+
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.RepositoryException;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
import org.argeo.entity.EntityType;
import org.argeo.jcr.Jcr;
import org.argeo.jcr.JcrException;
+import org.argeo.jcr.JcrUtils;
import org.argeo.jcr.JcrxApi;
/** Utilities around DocBook. */
public class DbkUtils {
+ private final static Log log = LogFactory.getLog(DbkUtils.class);
+
/** Get or add a DocBook element. */
public static Node getOrAddDbk(Node parent, DbkType child) {
try {
public static Node insertImageAfter(Node sibling) {
try {
- // FIXME make it more robust
- if (DbkType.imagedata.get().equals(sibling.getName())) {
- sibling = sibling.getParent().getParent();
- }
-
Node parent = sibling.getParent();
Node mediaNode = addDbk(parent, DbkType.mediaobject);
// TODO optimise?
// // TODO make it more robust and generic
// String fileRef = mediaNode.getName();
// imageDataNode.setProperty(DocBookNames.DBK_FILEREF, fileRef);
- return imageDataNode;
+ return mediaNode;
} catch (RepositoryException e) {
throw new JcrException("Cannot insert empty image after " + sibling, e);
}
}
+ public static void exportXml(Node node, OutputStream out) throws IOException {
+ try {
+ node.getSession().exportDocumentView(node.getPath(), out, false, false);
+ } catch (RepositoryException e) {
+ throw new JcrException("Cannot export " + node + " to XML", e);
+ }
+ }
+
+ public static void exportToFs(Node baseNode, DbkType type, Path directory) {
+ String fileName = Jcr.getName(baseNode) + ".dbk.xml";
+ Path filePath = directory.resolve(fileName);
+ Node docBookNode = Jcr.getNode(baseNode, type.get());
+ if (docBookNode == null)
+ throw new IllegalArgumentException("No " + type.get() + " under " + baseNode);
+ try {
+ Files.createDirectories(directory);
+ try (OutputStream out = Files.newOutputStream(filePath)) {
+ exportXml(docBookNode, out);
+ }
+ JcrUtils.copyFilesToFs(baseNode, directory, true);
+ if (log.isDebugEnabled())
+ log.debug("DocBook " + baseNode + " exported to " + filePath.toAbsolutePath());
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
/** Singleton. */
private DbkUtils() {
}
+
}