Statistics
| Revision:

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

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