X-Git-Url: https://git.argeo.org/?p=gpl%2Fargeo-suite.git;a=blobdiff_plain;f=publishing%2Forg.argeo.publishing.ui%2Fsrc%2Forg%2Fargeo%2Fdocbook%2FDbkUtils.java;h=226df4b544786178fe21107741247b5c7cfd818b;hp=29e29cda8faf85c121ff1137093ea44cd1847372;hb=f4c6893c66cd619e699cb9392ff8c45367796154;hpb=70010c4afc5799622fcad5b075740d94da074798 diff --git a/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/DbkUtils.java b/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/DbkUtils.java index 29e29cd..226df4b 100644 --- a/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/DbkUtils.java +++ b/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/DbkUtils.java @@ -2,17 +2,27 @@ package org.argeo.docbook; 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 { @@ -87,11 +97,6 @@ public class DbkUtils { 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? @@ -113,13 +118,41 @@ public class DbkUtils { // // 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() { } + }