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;
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());
}
return p;
}
+ /**
+ * Removes a paragraph if it empty. The sesison is not saved.
+ *
+ * @return true if the paragraph was empty and it was removed
+ */
+ public static boolean removeIfEmptyParagraph(Node node) {
+ try {
+ if (isDbk(node, DbkType.para)) {
+ NodeIterator nit = node.getNodes();
+ if (!nit.hasNext()) {
+ node.remove();
+ return true;
+ }
+ Node first = nit.nextNode();
+ if (nit.hasNext())
+ return false;
+ if (first.getName().equals(Jcr.JCR_XMLTEXT)) {
+ String str = JcrxApi.getXmlValue(first);
+ if (str != null && str.trim().equals("")) {
+ node.remove();
+ return true;
+ }
+ } else {
+ return false;
+ }
+ }
+ return false;
+ } catch (RepositoryException e) {
+ throw new JcrException("Cannot remove possibly empty paragraph", e);
+ }
+ }
+
public static Node insertImageAfter(Node sibling) {
try {
}
}
+ 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() {
}