1 package org.collectionspace.services.imports.nuxeo;
3 import java.io.BufferedInputStream;
5 import java.io.FileFilter;
6 import java.io.FileInputStream;
7 import java.io.IOException;
8 import java.util.ArrayList;
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;
24 public class LoggedXMLDirectoryReader extends AbstractDocumentReader {
26 protected Document loadXML(File file) throws IOException {
27 String filename = file.getCanonicalPath();
28 System.out.println("~~~~~~~~~~~~~~~~~~~ LoggedXMLDirectoryReader :: "+filename);
29 BufferedInputStream in = null;
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);
51 private FileTreeIterator iterator;
53 public LoggedXMLDirectoryReader(String sourcePath) {
54 this(new File(sourcePath));
57 public LoggedXMLDirectoryReader(File source) {
59 iterator = new FileTreeIterator(source);
60 iterator.setFilter(new FileFilter() {
61 public boolean accept(File pathname) {
62 return pathname.isDirectory();
67 public Object getSource() {
71 public void setSource(File source) {
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");
86 return result.toString();
91 public ExportedDocument read() throws IOException {
92 if (iterator.hasNext()) {
93 File dir = iterator.next();
97 // read document files
98 ExportedDocument xdoc = new ExportedDocumentImpl();
99 for (File file : dir.listFiles()) {
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")) {
110 FileUtils.getFileNameNoExt(file.getName()),
112 } else { // presume a blob
113 xdoc.putBlob(file.getName(), new StreamingBlob(
114 new FileSource(file)));
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*/
128 file.getAbsolutePath().substring(source.getAbsolutePath().length());
129 subPathS = subPathS.replace(File.separatorChar, '/');
130 return new Path(subPathS);