Statistics
| Revision:

root / tmp / org.txm.groovy.core / src / groovy / org / txm / macro / edition / RemoveTag.groovy @ 479

History | View | Annotate | Download (3.5 kB)

1
// Copyright © 2010-2013 ENS de Lyon.
2
// Copyright © 2007-2010 ENS de Lyon, CNRS, INRP, University of
3
// Lyon 2, University of Franche-Comté, University of Nice
4
// Sophia Antipolis, University of Paris 3.
5
// 
6
// The TXM platform is free software: you can redistribute it
7
// and/or modify it under the terms of the GNU General Public
8
// License as published by the Free Software Foundation,
9
// either version 2 of the License, or (at your option) any
10
// later version.
11
// 
12
// The TXM platform is distributed in the hope that it will be
13
// useful, but WITHOUT ANY WARRANTY; without even the implied
14
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15
// PURPOSE. See the GNU General Public License for more
16
// details.
17
// 
18
// You should have received a copy of the GNU General
19
// Public License along with the TXM platform. If not, see
20
// http://www.gnu.org/licenses.
21
// 
22
// 
23
// 
24
// $LastChangedDate:$
25
// $LastChangedRevision:$
26
// $LastChangedBy:$ 
27
//
28
package org.txm.macro.edition;
29

    
30
import javax.xml.parsers.*
31
import javax.xml.transform.*
32
import javax.xml.transform.dom.DOMSource
33
import javax.xml.transform.stream.StreamResult
34
import javax.xml.xpath.*
35

    
36
import org.txm.metadatas.*
37
import org.txm.utils.*
38
import org.w3c.dom.Document
39
import org.w3c.dom.Element
40

    
41
/**
42
 * Removes tags of XML file given a XPath. 
43
 * @author mdecorde
44
 *
45
 */
46
public class RemoveTag {
47
        File outfile
48
        String xpath
49
        Document doc
50
        
51
        public RemoveTag(def root, def outfile, def xpath)
52
        {
53
                this.doc = root
54
                this.outfile = outfile
55
                this.xpath = xpath
56
                
57
                process()
58
        }
59
        
60
        /**
61
         *
62
         * @param xmlfile the xmlfile
63
         * @param outfile the outfile
64
         * @param xpath the XPath
65
         */
66
        public RemoveTag(File xmlfile, File outfile, String xpath)
67
        {
68
                this.outfile = outfile
69
                this.xpath = xpath
70
                
71
                def domFactory = DocumentBuilderFactory.newInstance();
72
                domFactory.setNamespaceAware(true); // never forget this!
73
                DocumentBuilder builder = domFactory.newDocumentBuilder();
74
                this.doc = builder.parse(xmlfile);
75
                
76
                process()
77
        }
78
        
79
        private void process() {
80
                def expr = XPathFactory.newInstance().newXPath().compile(xpath);
81
                def nodes = expr.evaluate(doc, XPathConstants.NODESET);
82
                
83
                if (nodes != null)
84
                for(def node : nodes)
85
                {
86
                        //println "Remove node "+node
87
                        Element elem = (Element)node;
88
                        elem.getParentNode().removeChild(node);
89
                }
90
                save()
91
                doc = null
92
        }
93
        
94
        /**
95
         * Save.
96
         *
97
         * @return true, if successful
98
         */
99
        private boolean save()
100
        {
101
                if (outfile == null) return true;
102
        
103
                try {
104
                        // Création de la source DOM
105
                        Source source = new DOMSource(doc);
106
                        
107
                        // Création du fichier de sortie
108
                                Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outfile), "UTF-8")); 
109
                        Result resultat = new StreamResult(writer);
110
                        
111
                        // Configuration du transformer
112
                        TransformerFactory fabrique = new net.sf.saxon.TransformerFactoryImpl();
113
                        Transformer transformer = fabrique.newTransformer();
114
                        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
115
                        transformer.setOutputProperty(OutputKeys.INDENT, "yes"); 
116
                        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); 
117
                        
118
                        // Transformation
119
                        transformer.transform(source, resultat);
120
                        writer.close();
121
                        return true;
122
                } catch (Exception e) {
123
                        org.txm.utils.logger.Log.printStackTrace(e);
124
                        return false;
125
                }
126
        }
127
        
128
        public static void main(String[] args) {
129
                RemoveTag rt = new RemoveTag(
130
                        new File("/home/mdecorde/TXM/corpora/graal/import.xml"),
131
                        new File("/home/mdecorde/TXM/corpora/graal/import-o.xml"),
132
                        "//edition[@name='courante']"
133
                        )
134
        }
135
}