]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
125976ff291482899a803f1e59160534692b921b
[tmp/jakarta-migration.git] /
1 package org.collectionspace.services.imports.nuxeo;\r
2 \r
3 import java.io.BufferedInputStream;\r
4 import java.io.File;\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
10 \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
24 \r
25 public class LoggedXMLDirectoryReader extends AbstractDocumentReader {\r
26 \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
31         try {\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
42             throw ioe;\r
43         } finally {\r
44             if (in != null) {\r
45                 in.close();\r
46             }\r
47         }\r
48     }\r
49 \r
50     private File source;\r
51 \r
52     private FileTreeIterator iterator;\r
53 \r
54     public LoggedXMLDirectoryReader(String sourcePath) {\r
55         this(new File(sourcePath));\r
56     }\r
57 \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
64             }\r
65         });\r
66     }\r
67 \r
68     public Object getSource() {\r
69         return source;\r
70     }\r
71 \r
72     public void setSource(File source) {\r
73         this.source = source;\r
74     }\r
75 \r
76     public void close() {\r
77         source = null;\r
78         iterator = null;\r
79     }\r
80 \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
86         }\r
87         return result.toString();\r
88     }\r
89 \r
90 \r
91     @Override\r
92     public ExportedDocument read() throws IOException {\r
93         if (iterator.hasNext()) {\r
94             File dir = iterator.next();\r
95             if (dir == null) {\r
96                 return null;\r
97             }\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
110                         xdoc.putDocument(\r
111                                 FileUtils.getFileNameNoExt(file.getName()),\r
112                                 loadXML(file));\r
113                     } else { // presume a blob\r
114                         xdoc.putBlob(file.getName(), new StreamingBlob(\r
115                                 new FileSource(file)));\r
116                     }\r
117                 }\r
118             }\r
119             return xdoc;\r
120         }\r
121         return null;\r
122     }\r
123 \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
128         String subPathS =\r
129             file.getAbsolutePath().substring(source.getAbsolutePath().length());\r
130         subPathS = subPathS.replace(File.separatorChar, '/');\r
131         return new Path(subPathS);\r
132     }\r
133 \r
134 }