Improve logical backups.
[lgpl/argeo-commons.git] / org.argeo.maintenance / src / org / argeo / maintenance / backup / BackupContentHandler.java
index 099323833dfe6da23fdfc07a641428ccbf5d15d6..e29483e975e82e8ef752f3821cc41f2c2872b265 100644 (file)
@@ -12,13 +12,16 @@ import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 
 import org.apache.commons.io.IOUtils;
+import org.argeo.jcr.JcrException;
 import org.xml.sax.Attributes;
 import org.xml.sax.SAXException;
 import org.xml.sax.helpers.DefaultHandler;
 
+/** XML handler serialising a JCR system view. */
 public class BackupContentHandler extends DefaultHandler {
        final static int MAX_DEPTH = 1024;
        final static String SV_NAMESPACE_URI = "http://www.jcp.org/jcr/sv/1.0";
+       final static String SV_PREFIX = "sv";
        // elements
        final static String NODE = "node";
        final static String PROPERTY = "property";
@@ -36,6 +39,8 @@ public class BackupContentHandler extends DefaultHandler {
        private Session session;
        private Set<String> contentPaths = new TreeSet<>();
 
+//     private boolean inSystem = false;
+
        public BackupContentHandler(Writer out, Session session) {
                super();
                this.out = out;
@@ -70,15 +75,20 @@ public class BackupContentHandler extends DefaultHandler {
                if (isNode) {
                        String nodeName = attributes.getValue(SV_NAMESPACE_URI, NAME);
                        currentDepth = currentDepth + 1;
-                       if (currentDepth > 0)
-                               currentPath[currentDepth - 1] = nodeName;
+//                     if (currentDepth >= 0)
+                       currentPath[currentDepth] = nodeName;
 //                     System.out.println(getCurrentPath() + " , depth=" + currentDepth);
+//                     if ("jcr:system".equals(nodeName)) {
+//                             inSystem = true;
+//                     }
                }
+//             if (inSystem)
+//                     return;
 
                if (SV_NAMESPACE_URI.equals(uri))
                        try {
                                out.write("<");
-                               out.write(localName);
+                               out.write(SV_PREFIX + ":" + localName);
                                if (isProperty)
                                        currentPropertyIsMultiple = false; // always reset
                                for (int i = 0; i < attributes.getLength(); i++) {
@@ -87,7 +97,7 @@ public class BackupContentHandler extends DefaultHandler {
                                                String attrName = attributes.getLocalName(i);
                                                String attrValue = attributes.getValue(i);
                                                out.write(" ");
-                                               out.write(attrName);
+                                               out.write(SV_PREFIX + ":" + attrName);
                                                out.write("=");
                                                out.write("\"");
                                                out.write(attrValue);
@@ -113,17 +123,19 @@ public class BackupContentHandler extends DefaultHandler {
                                                }
                                        }
                                }
-                               if (currentDepth == 0) {
-                                       out.write(" xmlns=\"" + SV_NAMESPACE_URI + "\"");
+                               if (isNode && currentDepth == 0) {
+                                       // out.write(" xmlns=\"" + SV_NAMESPACE_URI + "\"");
+                                       out.write(" xmlns:" + SV_PREFIX + "=\"" + SV_NAMESPACE_URI + "\"");
                                }
                                out.write(">");
                                if (isNode)
                                        out.write("\n");
                                else if (isProperty && currentPropertyIsMultiple)
                                        out.write("\n");
-                       } catch (IOException | RepositoryException e) {
-                               // TODO Auto-generated catch block
-                               e.printStackTrace();
+                       } catch (IOException e) {
+                               throw new RuntimeException(e);
+                       } catch (RepositoryException e) {
+                               throw new JcrException(e);
                        }
        }
 
@@ -131,10 +143,21 @@ public class BackupContentHandler extends DefaultHandler {
        public void endElement(String uri, String localName, String qName) throws SAXException {
                if (localName.equals(NODE)) {
 //                     System.out.println("endElement " + getCurrentPath() + " , depth=" + currentDepth);
-                       if (currentDepth > 0)
-                               currentPath[currentDepth - 1] = null;
+//                     if (currentDepth > 0)
+                       currentPath[currentDepth] = null;
                        currentDepth = currentDepth - 1;
+                       assert currentDepth >= 0;
+//                     if (inSystem) {
+//                             // System.out.println("Skip " + getCurrentPath()+" ,
+//                             // currentDepth="+currentDepth);
+//                             if (currentDepth == 0) {
+//                                     inSystem = false;
+//                                     return;
+//                             }
+//                     }
                }
+//             if (inSystem)
+//                     return;
                boolean isValue = localName.equals(VALUE);
                if (SV_NAMESPACE_URI.equals(uri))
                        try {
@@ -143,7 +166,7 @@ public class BackupContentHandler extends DefaultHandler {
                                }
                                currentEncoded = null;
                                out.write("</");
-                               out.write(localName);
+                               out.write(SV_PREFIX + ":" + localName);
                                out.write(">");
                                if (!isValue)
                                        out.write("\n");
@@ -152,34 +175,34 @@ public class BackupContentHandler extends DefaultHandler {
                                                out.write("\n");
                                }
                        } catch (IOException e) {
-                               // TODO Auto-generated catch block
-                               e.printStackTrace();
+                               throw new RuntimeException(e);
                        }
        }
 
        @Override
        public void characters(char[] ch, int start, int length) throws SAXException {
+//             if (inSystem)
+//                     return;
                try {
                        out.write(ch, start, length);
                } catch (IOException e) {
-                       // TODO Auto-generated catch block
-                       e.printStackTrace();
+                       throw new RuntimeException(e);
                }
        }
 
        protected String getCurrentName() {
                assert currentDepth >= 0;
-               if (currentDepth == 0)
-                       return "jcr:root";
-               return currentPath[currentDepth - 1];
+//             if (currentDepth == 0)
+//                     return "jcr:root";
+               return currentPath[currentDepth];
        }
 
        protected String getCurrentPath() {
-               if (currentDepth == 0)
-                       return "/";
-               StringBuilder sb = new StringBuilder("/");
-               for (int i = 0; i < currentDepth; i++) {
-                       if (i != 0)
+//             if (currentDepth == 0)
+//                     return "/";
+               StringBuilder sb = new StringBuilder();
+               for (int i = 0; i <= currentDepth; i++) {
+//                     if (i != 0)
                                sb.append('/');
                        sb.append(currentPath[i]);
                }
@@ -190,6 +213,4 @@ public class BackupContentHandler extends DefaultHandler {
                return contentPaths;
        }
 
-       
-       
 }