]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
6d2ed5713e2112d81fb385bf42e0beafe4121521
[tmp/jakarta-migration.git] /
1 package org.collectionspace.services.imports.nuxeo;
2
3 import java.io.BufferedInputStream;
4 import java.io.File;
5 import java.io.FileFilter;
6 import java.io.FileInputStream;
7 import java.io.IOException;
8 import java.util.ArrayList;
9 import java.util.List;
10
11 import org.dom4j.Document;
12 import org.dom4j.DocumentException;
13 import org.dom4j.io.SAXReader;
14 import org.nuxeo.common.utils.FileTreeIterator;
15 import org.nuxeo.common.utils.FileUtils;
16 import org.nuxeo.common.utils.Path;
17 import org.nuxeo.ecm.core.api.impl.blob.StreamingBlob;
18 import org.nuxeo.ecm.core.io.ExportConstants;
19 import org.nuxeo.ecm.core.io.ExportedDocument;
20 import org.nuxeo.ecm.core.io.impl.AbstractDocumentReader;
21 import org.nuxeo.ecm.core.io.impl.ExportedDocumentImpl;
22 import org.nuxeo.runtime.services.streaming.FileSource;
23
24 public class LoggedXMLDirectoryReader extends AbstractDocumentReader {
25
26     protected Document loadXML(File file) throws IOException {
27         String filename = file.getCanonicalPath();
28         System.out.println("~~~~~~~~~~~~~~~~~~~ LoggedXMLDirectoryReader :: "+filename);
29         BufferedInputStream in = null;
30         try {
31             in = new BufferedInputStream(new FileInputStream(file));
32             System.out.println("~~~~~~~~~~~~~~~~~~~ LoggedXMLDirectoryReader :: "+filename+" :: DONE");
33             reportList.add("READ: "+filename);
34             return new SAXReader().read(in);
35         } catch (DocumentException e) {
36             IOException ioe = new IOException("Failed to read file document "
37                     + file + ": " + e.getMessage());
38             ioe.setStackTrace(e.getStackTrace());
39             System.out.println("~~~~~~~~~~~~~~~~~~~ LoggedXMLDirectoryReader :: "+filename+" :: ERROR");
40             reportList.add("ERROR: "+filename);
41             throw ioe;
42         } finally {
43             if (in != null) {
44                 in.close();
45             }
46         }
47     }
48
49     private File source;
50
51     private FileTreeIterator iterator;
52
53     public LoggedXMLDirectoryReader(String sourcePath) {
54         this(new File(sourcePath));
55     }
56
57     public LoggedXMLDirectoryReader(File source) {
58         this.source = source;
59         iterator = new FileTreeIterator(source);
60         iterator.setFilter(new FileFilter() {
61             public boolean accept(File pathname) {
62                 return pathname.isDirectory();
63             }
64         });
65     }
66
67     public Object getSource() {
68         return source;
69     }
70
71     public void setSource(File source) {
72         this.source = source;
73     }
74
75     public void close() {
76         source = null;
77         iterator = null;
78     }
79
80     private List<String> reportList = new ArrayList<String>();
81     public String report(){
82         StringBuffer result = new StringBuffer();
83         for (String s: reportList){
84             result.append(s).append("\r\n");
85         }
86         return result.toString();
87     }
88
89
90     @Override
91     public ExportedDocument read() throws IOException {
92         if (iterator.hasNext()) {
93             File dir = iterator.next();
94             if (dir == null) {
95                 return null;
96             }
97             // read document files
98             ExportedDocument xdoc = new ExportedDocumentImpl();
99             for (File file : dir.listFiles()) {
100                 if (file.isFile()) {
101                     String name = file.getName();
102                     if (ExportConstants.DOCUMENT_FILE.equals(name)) {
103                         Document doc = loadXML(file);
104                         xdoc.setDocument(doc);
105                         Path relPath = computeRelativePath(dir);
106                         xdoc.setPath(relPath);
107                         reportList.add(relPath.toString());
108                     } else if (name.endsWith(".xml")) {
109                         xdoc.putDocument(
110                                 FileUtils.getFileNameNoExt(file.getName()),
111                                 loadXML(file));
112                     } else { // presume a blob
113                         xdoc.putBlob(file.getName(), new StreamingBlob(
114                                 new FileSource(file)));
115                     }
116                 }
117             }
118             return xdoc;
119         }
120         return null;
121     }
122
123     /*NXP-1688 Rux: the path was somehow left over when migrated from
124     core 1.3.4 to 1.4.0. Pull back.*/
125     private Path computeRelativePath(File file) {
126         /*NXP-2507 Rux: preserve directory structure with slashes instead OS name separator*/
127         String subPathS =
128             file.getAbsolutePath().substring(source.getAbsolutePath().length());
129         subPathS = subPathS.replace(File.separatorChar, '/');
130         return new Path(subPathS);
131     }
132
133 }