Support media in DocBook servlet.
[gpl/argeo-suite.git] / publishing / org.argeo.publishing.ui / src / org / argeo / docbook / DbkUtils.java
index 19f7cd40fd905dd46c543785e3d292cb655caf34..44346b25dbb669c5bfb1c88442509c2c25983d11 100644 (file)
@@ -3,10 +3,12 @@ package org.argeo.docbook;
 import static org.argeo.docbook.DbkType.para;
 
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.OutputStream;
 import java.nio.file.Files;
 import java.nio.file.Path;
 
+import javax.jcr.ImportUUIDBehavior;
 import javax.jcr.Node;
 import javax.jcr.NodeIterator;
 import javax.jcr.RepositoryException;
@@ -51,6 +53,16 @@ public class DbkUtils {
                return Jcr.getName(node).equals(type.get());
        }
 
+       /** Whether this node is a DocBook type. */
+       public static boolean isDbk(Node node) {
+               String name = Jcr.getName(node);
+               for (DbkType type : DbkType.values()) {
+                       if (name.equals(type.get()))
+                               return true;
+               }
+               return false;
+       }
+
        public static String getTitle(Node node) {
                return JcrxApi.getXmlValue(node, DbkType.title.get());
        }
@@ -97,11 +109,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?
@@ -123,7 +130,7 @@ 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);
                }
@@ -156,6 +163,16 @@ public class DbkUtils {
                }
        }
 
+       public static void importXml(Node baseNode, InputStream in) throws IOException {
+               try {
+                       baseNode.getSession().importXML(baseNode.getPath(), in,
+                                       ImportUUIDBehavior.IMPORT_UUID_COLLISION_REPLACE_EXISTING);
+               } catch (RepositoryException e) {
+                       throw new JcrException("Cannot import XML to " + baseNode, e);
+               }
+
+       }
+
        /** Singleton. */
        private DbkUtils() {
        }