Statistics
| Revision:

root / tmp / org.txm.core / src / groovy / org / txm / importer / RemoveTag.groovy @ 187

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