Statistics
| Revision:

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

History | View | Annotate | Download (1.8 kB)

1 1000 mdecorde
package org.txm.scripts.importer;
2 881 mdecorde
3 881 mdecorde
import java.io.File;
4 881 mdecorde
import java.io.InputStream;
5 881 mdecorde
6 881 mdecorde
import javax.xml.stream.XMLInputFactory;
7 881 mdecorde
import javax.xml.stream.XMLResolver;
8 881 mdecorde
import javax.xml.stream.XMLStreamConstants;
9 881 mdecorde
import javax.xml.stream.XMLStreamException;
10 881 mdecorde
import javax.xml.stream.XMLStreamReader;
11 881 mdecorde
import javax.xml.stream.events.DTD;
12 881 mdecorde
import javax.xml.stream.events.EntityReference;
13 881 mdecorde
import org.txm.utils.*
14 927 mdecorde
import org.txm.utils.io.*;
15 881 mdecorde
16 881 mdecorde
public class CopyXMLFiles {
17 881 mdecorde
        File xmlFile;
18 881 mdecorde
        File outDir;
19 881 mdecorde
        def dtdFiles = []
20 881 mdecorde
21 881 mdecorde
        public CopyXMLFiles(File xmlFile) {
22 881 mdecorde
                this.xmlFile = xmlFile;
23 881 mdecorde
        }
24 881 mdecorde
25 881 mdecorde
        public boolean copy(File outDir) {
26 881 mdecorde
                XMLInputFactory factory;
27 881 mdecorde
                XMLStreamReader parser;
28 881 mdecorde
                InputStream inputData = xmlFile.toURI().toURL().openStream();
29 881 mdecorde
                factory = XMLInputFactory.newInstance();
30 881 mdecorde
                factory.setXMLResolver(new XMLResolver() {
31 881 mdecorde
                        @Override
32 881 mdecorde
                        public Object resolveEntity(String publicID, String systemID,
33 881 mdecorde
                                        String baseURI, String namespace) throws XMLStreamException {
34 881 mdecorde
                                //println "Resolving $publicID $systemID $baseURI $namespace"
35 881 mdecorde
                                File srcFile = new File(xmlFile.getParentFile(), systemID);
36 881 mdecorde
                                dtdFiles << srcFile
37 881 mdecorde
                                return srcFile.toURI().toURL().openStream();
38 881 mdecorde
                        }
39 881 mdecorde
                });
40 881 mdecorde
41 881 mdecorde
                parser = factory.createXMLStreamReader(inputData);
42 881 mdecorde
43 881 mdecorde
                for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) {
44 881 mdecorde
45 881 mdecorde
                }
46 881 mdecorde
47 881 mdecorde
                dtdFiles << xmlFile
48 881 mdecorde
                for (File dtd : dtdFiles) {
49 881 mdecorde
                        File cpy = new File(outDir, dtd.getName());
50 881 mdecorde
                        FileCopy.copy(dtd, cpy);
51 881 mdecorde
                }
52 881 mdecorde
                return outDir.listFiles().size() == dtdFiles.size();
53 881 mdecorde
        }
54 881 mdecorde
55 881 mdecorde
        public static void main(def args) {
56 881 mdecorde
                File xmlFile = new File("/home/mdecorde/Téléchargements/Inscriptions1.xml")
57 881 mdecorde
                File outDir = new File("/home/mdecorde/Téléchargements/test")
58 881 mdecorde
                outDir.mkdir()
59 881 mdecorde
                CopyXMLFiles cdf = new CopyXMLFiles(xmlFile)
60 881 mdecorde
                println cdf.copy(outDir)
61 881 mdecorde
        }
62 881 mdecorde
}