Statistics
| Revision:

root / tmp / org.txm.core / src / java / org / txm / scripts / importer / CopyXMLFiles.groovy @ 1000

History | View | Annotate | Download (1.8 kB)

1
package org.txm.scripts.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
import org.txm.utils.io.*;
15

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