1 package org.collectionspace.services.imports.nuxeo;
\r
3 import java.io.BufferedInputStream;
\r
5 import java.io.FileFilter;
\r
6 import java.io.FileInputStream;
\r
7 import java.io.IOException;
\r
8 import java.util.ArrayList;
\r
9 import java.util.List;
\r
11 import org.dom4j.Document;
\r
12 import org.dom4j.DocumentException;
\r
13 import org.dom4j.io.SAXReader;
\r
14 import org.nuxeo.common.utils.FileTreeIterator;
\r
15 import org.nuxeo.common.utils.FileUtils;
\r
16 import org.nuxeo.common.utils.Path;
\r
17 import org.nuxeo.ecm.core.api.impl.blob.StreamingBlob;
\r
18 import org.nuxeo.ecm.core.io.ExportConstants;
\r
19 import org.nuxeo.ecm.core.io.ExportedDocument;
\r
20 import org.nuxeo.ecm.core.io.impl.AbstractDocumentReader;
\r
21 import org.nuxeo.ecm.core.io.impl.ExportedDocumentImpl;
\r
22 import org.nuxeo.ecm.core.io.impl.plugins.XMLDirectoryReader;
\r
23 import org.nuxeo.runtime.services.streaming.FileSource;
\r
25 public class LoggedXMLDirectoryReader extends AbstractDocumentReader {
\r
27 protected Document loadXML(File file) throws IOException {
\r
28 String filename = file.getCanonicalPath();
\r
29 System.out.println("~~~~~~~~~~~~~~~~~~~ LoggedXMLDirectoryReader :: "+filename);
\r
30 BufferedInputStream in = null;
\r
32 in = new BufferedInputStream(new FileInputStream(file));
\r
33 System.out.println("~~~~~~~~~~~~~~~~~~~ LoggedXMLDirectoryReader :: "+filename+" :: DONE");
\r
34 reportList.add("READ: "+filename);
\r
35 return new SAXReader().read(in);
\r
36 } catch (DocumentException e) {
\r
37 IOException ioe = new IOException("Failed to read file document "
\r
38 + file + ": " + e.getMessage());
\r
39 ioe.setStackTrace(e.getStackTrace());
\r
40 System.out.println("~~~~~~~~~~~~~~~~~~~ LoggedXMLDirectoryReader :: "+filename+" :: ERROR");
\r
41 reportList.add("ERROR: "+filename);
\r
50 private File source;
\r
52 private FileTreeIterator iterator;
\r
54 public LoggedXMLDirectoryReader(String sourcePath) {
\r
55 this(new File(sourcePath));
\r
58 public LoggedXMLDirectoryReader(File source) {
\r
59 this.source = source;
\r
60 iterator = new FileTreeIterator(source);
\r
61 iterator.setFilter(new FileFilter() {
\r
62 public boolean accept(File pathname) {
\r
63 return pathname.isDirectory();
\r
68 public Object getSource() {
\r
72 public void setSource(File source) {
\r
73 this.source = source;
\r
76 public void close() {
\r
81 private List<String> reportList = new ArrayList<String>();
\r
82 public String report(){
\r
83 StringBuffer result = new StringBuffer();
\r
84 for (String s: reportList){
\r
85 result.append(s).append("\r\n");
\r
87 return result.toString();
\r
92 public ExportedDocument read() throws IOException {
\r
93 if (iterator.hasNext()) {
\r
94 File dir = iterator.next();
\r
98 // read document files
\r
99 ExportedDocument xdoc = new ExportedDocumentImpl();
\r
100 for (File file : dir.listFiles()) {
\r
101 if (file.isFile()) {
\r
102 String name = file.getName();
\r
103 if (ExportConstants.DOCUMENT_FILE.equals(name)) {
\r
104 Document doc = loadXML(file);
\r
105 xdoc.setDocument(doc);
\r
106 Path relPath = computeRelativePath(dir);
\r
107 xdoc.setPath(relPath);
\r
108 reportList.add(relPath.toString());
\r
109 } else if (name.endsWith(".xml")) {
\r
111 FileUtils.getFileNameNoExt(file.getName()),
\r
113 } else { // presume a blob
\r
114 xdoc.putBlob(file.getName(), new StreamingBlob(
\r
115 new FileSource(file)));
\r
124 /*NXP-1688 Rux: the path was somehow left over when migrated from
\r
125 core 1.3.4 to 1.4.0. Pull back.*/
\r
126 private Path computeRelativePath(File file) {
\r
127 /*NXP-2507 Rux: preserve directory structure with slashes instead OS name separator*/
\r
129 file.getAbsolutePath().substring(source.getAbsolutePath().length());
\r
130 subPathS = subPathS.replace(File.separatorChar, '/');
\r
131 return new Path(subPathS);
\r