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;
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;
25 public class LoggedXMLDirectoryReader extends AbstractDocumentReader {
27 private final Logger logger = LoggerFactory.getLogger(LoggedXMLDirectoryReader.class);
30 protected Document loadXML(File file) throws IOException {
31 String filename = file.getCanonicalPath();
33 if (logger.isTraceEnabled()) {
34 logger.trace("~~~~~~~~~~~~~~~~~~~ LoggedXMLDirectoryReader :: "+filename);
36 BufferedInputStream in = null;
38 in = new BufferedInputStream(new FileInputStream(file));
39 if (logger.isTraceEnabled()) {
40 logger.trace("~~~~~~~~~~~~~~~~~~~ LoggedXMLDirectoryReader :: "+filename+" :: DONE");
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);
60 private FileTreeIterator iterator;
62 public LoggedXMLDirectoryReader(String sourcePath) {
63 this(new File(sourcePath));
66 public LoggedXMLDirectoryReader(File source) {
68 iterator = new FileTreeIterator(source);
69 iterator.setFilter(new FileFilter() {
70 public boolean accept(File pathname) {
71 return pathname.isDirectory();
76 public Object getSource() {
80 public void setSource(File source) {
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");
95 return result.toString();
100 public ExportedDocument read() throws IOException {
101 if (iterator.hasNext()) {
102 File dir = iterator.next();
106 // read document files
107 ExportedDocument xdoc = new ExportedDocumentImpl();
108 for (File file : dir.listFiles()) {
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")) {
119 FileUtils.getFileNameNoExt(file.getName()),
121 } else { // presume a blob
122 xdoc.putBlob(file.getName(), new StreamingBlob(
123 new FileSource(file)));
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*/
137 file.getAbsolutePath().substring(source.getAbsolutePath().length());
138 subPathS = subPathS.replace(File.separatorChar, '/');
139 return new Path(subPathS);