Statistics
| Revision:

root / tmp / org.txm.groovy.core / src / groovy / org / txm / importer / CopyXMLFiles.groovy @ 479

History | View | Annotate | Download (1.7 kB)

1
package org.txm.importer;
2

    
3
import java.io.File;
4
import java.io.InputStream;
5

    
6
import javax.xml.stream.XMLInputFactory;
7
import javax.xml.stream.XMLResolver;
8
import javax.xml.stream.XMLStreamConstants;
9
import javax.xml.stream.XMLStreamException;
10
import javax.xml.stream.XMLStreamReader;
11
import javax.xml.stream.events.DTD;
12
import javax.xml.stream.events.EntityReference;
13
import org.txm.utils.*
14

    
15
public class CopyXMLFiles {
16
        File xmlFile;
17
        File outDir;
18
        def dtdFiles = []
19
        
20
        public CopyXMLFiles(File xmlFile) {
21
                this.xmlFile = xmlFile;
22
        }
23
        
24
        public boolean copy(File outDir) {
25
                XMLInputFactory factory;
26
                XMLStreamReader parser;
27
                InputStream inputData = xmlFile.toURI().toURL().openStream();
28
                factory = XMLInputFactory.newInstance();
29
                factory.setXMLResolver(new XMLResolver() {
30
                        @Override
31
                        public Object resolveEntity(String publicID, String systemID,
32
                                        String baseURI, String namespace) throws XMLStreamException {
33
                                //println "Resolving $publicID $systemID $baseURI $namespace"
34
                                File srcFile = new File(xmlFile.getParentFile(), systemID);
35
                                dtdFiles << srcFile
36
                                return srcFile.toURI().toURL().openStream();
37
                        }
38
                });
39
        
40
                parser = factory.createXMLStreamReader(inputData);
41
                
42
                for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) {
43
                        
44
                }
45
                
46
                dtdFiles << xmlFile
47
                for (File dtd : dtdFiles) {
48
                        File cpy = new File(outDir, dtd.getName());
49
                        FileCopy.copy(dtd, cpy);
50
                }
51
                return outDir.listFiles().size() == dtdFiles.size();
52
        }
53
        
54
        public static void main(def args) {
55
                File xmlFile = new File("/home/mdecorde/Téléchargements/Inscriptions1.xml")
56
                File outDir = new File("/home/mdecorde/Téléchargements/test")
57
                outDir.mkdir()
58
                CopyXMLFiles cdf = new CopyXMLFiles(xmlFile)
59
                println cdf.copy(outDir)
60
        }
61
}