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