Statistics
| Revision:

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

History | View | Annotate | Download (7 kB)

1 881 mdecorde
// Copyright © 2010-2013 ENS de Lyon.
2 881 mdecorde
// Copyright © 2007-2010 ENS de Lyon, CNRS, INRP, University of
3 881 mdecorde
// Lyon 2, University of Franche-Comté, University of Nice
4 881 mdecorde
// Sophia Antipolis, University of Paris 3.
5 881 mdecorde
//
6 881 mdecorde
// The TXM platform is free software: you can redistribute it
7 881 mdecorde
// and/or modify it under the terms of the GNU General Public
8 881 mdecorde
// License as published by the Free Software Foundation,
9 881 mdecorde
// either version 2 of the License, or (at your option) any
10 881 mdecorde
// later version.
11 881 mdecorde
//
12 881 mdecorde
// The TXM platform is distributed in the hope that it will be
13 881 mdecorde
// useful, but WITHOUT ANY WARRANTY; without even the implied
14 881 mdecorde
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 881 mdecorde
// PURPOSE. See the GNU General Public License for more
16 881 mdecorde
// details.
17 881 mdecorde
//
18 881 mdecorde
// You should have received a copy of the GNU General
19 881 mdecorde
// Public License along with the TXM platform. If not, see
20 881 mdecorde
// http://www.gnu.org/licenses.
21 881 mdecorde
22 881 mdecorde
//
23 881 mdecorde
// This file is part of the TXM platform.
24 881 mdecorde
//
25 881 mdecorde
// The TXM platform is free software: you can redistribute it and/or modif y
26 881 mdecorde
// it under the terms of the GNU General Public License as published by
27 881 mdecorde
// the Free Software Foundation, either version 3 of the License, or
28 881 mdecorde
// (at your option) any later version.
29 881 mdecorde
//
30 881 mdecorde
// The TXM platform is distributed in the hope that it will be useful,
31 881 mdecorde
// but WITHOUT ANY WARRANTY; without even the implied warranty of
32 881 mdecorde
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33 881 mdecorde
// GNU General Public License for more details.
34 881 mdecorde
//
35 881 mdecorde
// You should have received a copy of the GNU General Public License
36 881 mdecorde
// along with the TXM platform.  If not, see <http://www.gnu.org/licenses/>.
37 881 mdecorde
//
38 881 mdecorde
//
39 881 mdecorde
//
40 881 mdecorde
// $LastChangedDate: 2016-03-01 10:10:58 +0100 (mar. 01 mars 2016) $
41 881 mdecorde
// $LastChangedRevision: 3133 $
42 881 mdecorde
// $LastChangedBy: mdecorde $
43 881 mdecorde
//
44 1000 mdecorde
package org.txm.scripts.importer;
45 881 mdecorde
46 881 mdecorde
/**
47 881 mdecorde
 * @author mdecorde
48 881 mdecorde
 */
49 881 mdecorde
50 881 mdecorde
import java.io.File;
51 881 mdecorde
import javax.xml.transform.Transformer
52 881 mdecorde
import javax.xml.transform.TransformerFactory
53 881 mdecorde
import javax.xml.transform.stream.StreamResult
54 1370 mdecorde
import javax.xml.transform.stream.StreamSource;
55 1370 mdecorde
56 1370 mdecorde
import org.txm.utils.io.IOUtils
57 1370 mdecorde
58 881 mdecorde
import net.sf.saxon.*;
59 881 mdecorde
import net.sf.saxon.s9api.*;
60 881 mdecorde
61 881 mdecorde
// TODO: Auto-generated Javadoc
62 881 mdecorde
/**
63 881 mdecorde
 * apply a xsl file to a file : use process(File infile, File outfile, String[]
64 881 mdecorde
 * args) to apply the xslt file loaded by the constructor args =
65 881 mdecorde
 * ["name1","value1","name2",value2, ...]
66 881 mdecorde
 *
67 881 mdecorde
 * @author mdecorde
68 881 mdecorde
 */
69 881 mdecorde
70 881 mdecorde
public class ApplyXsl {
71 881 mdecorde
72 881 mdecorde
//        /** The proc. */
73 881 mdecorde
//        private Processor proc;
74 881 mdecorde
//
75 881 mdecorde
//        /** The comp. */
76 881 mdecorde
//        private XsltCompiler comp;
77 881 mdecorde
//
78 881 mdecorde
//        /** The exp. */
79 881 mdecorde
//        private XsltExecutable exp;
80 881 mdecorde
//
81 881 mdecorde
//        /** The source. */
82 881 mdecorde
//        private XdmNode source;
83 881 mdecorde
//
84 881 mdecorde
//        /** The out. */
85 881 mdecorde
//        private Serializer out;
86 881 mdecorde
//
87 881 mdecorde
//        /** The trans. */
88 881 mdecorde
//        private XsltTransformer trans;
89 881 mdecorde
90 881 mdecorde
        TransformerFactory tFactory;
91 881 mdecorde
        Transformer transformer;
92 881 mdecorde
93 881 mdecorde
        /**
94 881 mdecorde
         * initialize with the xslt file.
95 881 mdecorde
         *
96 881 mdecorde
         * @param xsltfile the xsltfile
97 881 mdecorde
         */
98 881 mdecorde
        public ApplyXsl(String xsltfile) {
99 881 mdecorde
//                proc = new Processor(false);
100 881 mdecorde
//                comp = proc.newXsltCompiler();
101 881 mdecorde
//                exp = comp.compile(new StreamSource(new File(xsltfile)));
102 881 mdecorde
                tFactory = new net.sf.saxon.TransformerFactoryImpl();
103 881 mdecorde
                transformer = tFactory.newTransformer(new StreamSource(xsltfile));
104 881 mdecorde
105 881 mdecorde
        }
106 881 mdecorde
107 881 mdecorde
        /**
108 881 mdecorde
         * initialize with the xslt file.
109 881 mdecorde
         *
110 881 mdecorde
         * @param xsltfile the xsltfile
111 881 mdecorde
         */
112 881 mdecorde
        public ApplyXsl(File xsltfile) {
113 881 mdecorde
//                proc = new Processor(false);
114 881 mdecorde
//                comp = proc.newXsltCompiler();
115 881 mdecorde
//                exp = comp.compile(new StreamSource(xsltfile));
116 881 mdecorde
                tFactory = new net.sf.saxon.TransformerFactoryImpl();
117 881 mdecorde
                transformer = tFactory.newTransformer(new StreamSource(xsltfile));
118 881 mdecorde
        }
119 881 mdecorde
120 881 mdecorde
        /**
121 881 mdecorde
         * Set in and out file.
122 881 mdecorde
         *
123 881 mdecorde
         * @param xmlinfile the xmlinfile
124 881 mdecorde
         * @param xmloutfile the xmloutfile
125 881 mdecorde
         * @return true, if successful
126 881 mdecorde
         */
127 881 mdecorde
        private boolean SetInOutSource(String xmlinfile, String xmloutfile) {
128 881 mdecorde
//                if (proc == null || exp == null || comp == null)
129 881 mdecorde
//                        return false;
130 881 mdecorde
//
131 881 mdecorde
//                source = proc.newDocumentBuilder().build(
132 881 mdecorde
//                                new StreamSource(new File(xmlinfile)));
133 881 mdecorde
//                out = new Serializer();
134 881 mdecorde
//                out.setOutputFile(new File(xmloutfile));
135 881 mdecorde
//
136 881 mdecorde
//                trans = exp.load();
137 881 mdecorde
//                transformer.set.setInitialContextNode(source);
138 881 mdecorde
//                transformer.setDestination(out);
139 881 mdecorde
                return true;
140 881 mdecorde
        }
141 881 mdecorde
142 881 mdecorde
        /**
143 881 mdecorde
         * Set a xslt param.
144 881 mdecorde
         *
145 881 mdecorde
         * @param name the name
146 881 mdecorde
         * @param value the value
147 881 mdecorde
         * @return true, if successful
148 881 mdecorde
         */
149 881 mdecorde
        public boolean SetParam(String name, Object value) {
150 881 mdecorde
//                if (trans != null)
151 881 mdecorde
//                        trans.setParameter(new QName(name), new XdmAtomicValue("" + value));
152 881 mdecorde
//                else
153 881 mdecorde
//                        return false;
154 881 mdecorde
                return true;
155 881 mdecorde
        }
156 881 mdecorde
157 881 mdecorde
        /**
158 881 mdecorde
         * Force java to clean memory after processing a xslt. If not we might catch
159 881 mdecorde
         * a JavaHeapspace Exception
160 881 mdecorde
         *
161 881 mdecorde
         */
162 881 mdecorde
        private void cleanMemory() {
163 881 mdecorde
//                source = null;
164 881 mdecorde
//                trans = null;
165 881 mdecorde
//                out = null;
166 881 mdecorde
        }
167 881 mdecorde
168 881 mdecorde
        /**
169 881 mdecorde
         * Process files with xslt args.
170 881 mdecorde
         *
171 881 mdecorde
         * @param xmlinfile file to be processed
172 881 mdecorde
         * @param xmloutfile output file
173 881 mdecorde
         * @param args xslt args ["arg1","arg1value","arg2","arg2value"...]
174 881 mdecorde
         * @return true, if successful
175 881 mdecorde
         */
176 881 mdecorde
177 881 mdecorde
        public boolean process(String xmlinfile, String xmloutfile, String[] args) {
178 881 mdecorde
                if (!this.SetInOutSource(xmlinfile, xmloutfile))
179 881 mdecorde
                        return false;
180 881 mdecorde
                for (int i = 0; i < args.length; i = i + 2)// set params
181 881 mdecorde
                {
182 881 mdecorde
                        if (!this.SetParam(args[i], args[i + 1]))
183 881 mdecorde
                                return false;
184 881 mdecorde
                }
185 881 mdecorde
186 881 mdecorde
                transformer.transform(new StreamSource(xmlinfile), new StreamResult(new BufferedOutputStream(new FileOutputStream(xmloutfile))));
187 881 mdecorde
                cleanMemory(); // save memory
188 881 mdecorde
                // System.out.println("Done");
189 881 mdecorde
                return true;
190 881 mdecorde
        }
191 881 mdecorde
192 881 mdecorde
        /**
193 881 mdecorde
         * Process files without xslt args.
194 881 mdecorde
         *
195 881 mdecorde
         * @param xmlinfile the xmlinfile
196 881 mdecorde
         * @param xmloutfile the xmloutfile
197 881 mdecorde
         * @return true, if successful
198 881 mdecorde
         */
199 881 mdecorde
        public boolean process(String xmlinfile, String xmloutfile) {
200 881 mdecorde
                if (!this.SetInOutSource(xmlinfile, xmloutfile))
201 881 mdecorde
                        return false;
202 881 mdecorde
203 881 mdecorde
                transformer.transform(new StreamSource(xmlinfile), new StreamResult(new BufferedOutputStream(new FileOutputStream(xmloutfile))));
204 881 mdecorde
                cleanMemory();
205 881 mdecorde
                // System.out.println("Done");
206 881 mdecorde
                return true;
207 881 mdecorde
        }
208 881 mdecorde
209 881 mdecorde
        /**
210 881 mdecorde
         * The main method.
211 881 mdecorde
         *
212 881 mdecorde
         * @param args the arguments
213 881 mdecorde
         */
214 881 mdecorde
        public static void main(String[] args)
215 881 mdecorde
        {
216 881 mdecorde
                String xslfile = "pathtoxslfile";
217 881 mdecorde
                ApplyXsl a = new ApplyXsl(xslfile);
218 881 mdecorde
219 881 mdecorde
                File srcdirectory = new File("pathtodirectorytoprocess")
220 881 mdecorde
                assert(srcdirectory.exists())
221 881 mdecorde
                assert(srcdirectory.isDirectory())
222 881 mdecorde
                assert(srcdirectory.canRead())
223 881 mdecorde
                assert(srcdirectory.canExecute())
224 881 mdecorde
225 1615 mdecorde
                List<File> files = srcdirectory.listFiles(IOUtils.HIDDENFILE_FILTER);
226 881 mdecorde
227 881 mdecorde
                files = files.findAll{item-> item.getName().endsWith(".xml")}
228 881 mdecorde
229 881 mdecorde
                long bigstart = System.currentTimeMillis()
230 881 mdecorde
                for(File infile : files)
231 881 mdecorde
                {
232 881 mdecorde
                        String outfile = infile.getAbsolutePath()+".out"
233 881 mdecorde
                        if(infile.canRead())
234 881 mdecorde
                        {
235 881 mdecorde
                                long start = System.currentTimeMillis()
236 881 mdecorde
                                println "Process : "+infile.getName()
237 881 mdecorde
                                // String[] params = ["pbval1",1,"pbval2",2];
238 881 mdecorde
                                // a.process(infile.getAbsolutePath(),outfile); // no parameters
239 881 mdecorde
                                // a.process( infile.getAbsolutePath(),outfile,params);
240 881 mdecorde
                                println "Done : "+(System.currentTimeMillis()-start)+"ms"
241 881 mdecorde
                        }
242 881 mdecorde
                }
243 881 mdecorde
                println "Total time : "+(System.currentTimeMillis()-bigstart)+"ms"
244 881 mdecorde
        }
245 881 mdecorde
}