Statistics
| Revision:

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

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
                parser.close();
47
                inputData.close();
48
                
49
                dtdFiles << xmlFile
50
                for (File dtd : dtdFiles) {
51
                        File cpy = new File(outDir, dtd.getName());
52
                        FileCopy.copy(dtd, cpy);
53
                }
54
                return outDir.listFiles().size() == dtdFiles.size();
55
        }
56
        
57
        public static void main(def args) {
58
                File xmlFile = new File("/home/mdecorde/Téléchargements/Inscriptions1.xml")
59
                File outDir = new File("/home/mdecorde/Téléchargements/test")
60
                outDir.mkdir()
61
                CopyXMLFiles cdf = new CopyXMLFiles(xmlFile)
62
                println cdf.copy(outDir)
63
        }
64
}