Statistics
| Revision:

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

History | View | Annotate | Download (4 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.transcriber;
29

    
30
import org.txm.importer.*;
31
import org.txm.utils.*;
32
import org.txm.metadatas.*;
33

    
34
import java.io.File;
35

    
36
import org.w3c.dom.Document;
37
import org.w3c.dom.Element;
38

    
39
import javax.xml.parsers.*;
40
import javax.xml.xpath.*;
41
import javax.xml.transform.*;
42
import javax.xml.transform.dom.DOMSource;
43
import javax.xml.transform.stream.StreamResult;
44

    
45
// TODO: Auto-generated Javadoc
46
/** remove part of xml given an id. */
47
public class ChangeSpeaker {
48
        File outfile;
49
        
50
        /** The doc. */
51
        Document doc;
52
        
53
        /**
54
         * Instantiates a new change speaker.
55
         *
56
         * @param transcriptionfile the transcriptionfile
57
         * @param outfile the outfile
58
         * @param id the id
59
         * @param newid the newid
60
         */
61
        public ChangeSpeaker(File transcriptionfile, File outfile, String id, String newid)
62
        {
63
                this.outfile = outfile;
64
                
65
                String xpath = "//Turn";
66
                DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
67
                domFactory.setNamespaceAware(true); // never forget this!
68
                DocumentBuilder builder = domFactory.newDocumentBuilder();
69
                doc = builder.parse(transcriptionfile);
70
                
71
                def expr = XPathFactory.newInstance().newXPath().compile(xpath);
72
                def nodes = expr.evaluate(doc, XPathConstants.NODESET);
73
                
74
                
75
                
76
                for(def node : nodes)
77
                {
78
                        if (node == null) continue;
79
                        
80
                        Element elem = (Element)node;
81
                        if(elem.getAttribute("speaker").contains(id))
82
                        {
83
                                String value = elem.getAttribute("speaker");
84
                                value = value.replace(id, newid);
85
                                elem.setAttribute("speaker", value);
86
                        }
87
                }
88
                
89
                def expr2 = XPathFactory.newInstance().newXPath().compile("//Speaker[@id='"+id+"']");
90
                def nodes2 = expr2.evaluate(doc, XPathConstants.NODESET);
91
                
92
                for(def node : nodes2)
93
                {
94
                        if (node == null) continue;
95
                        
96
                        Element elem = (Element)node;
97
                        elem.setAttribute("id", newid);
98
                }
99
                save()
100
        }
101
        
102
        /**
103
         * Save.
104
         *
105
         * @return true, if successful
106
         */
107
        private boolean save()
108
        {
109
                try {
110
                        // Création de la source DOM
111
                        Source source = new DOMSource(doc);
112
                        
113
                        // Création du fichier de sortie
114
                        Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outfile), "UTF-8")); 
115
                        Result resultat = new StreamResult(writer);
116
                        
117
                        // Configuration du transformer
118
                        TransformerFactory fabrique = new net.sf.saxon.TransformerFactoryImpl();
119
                        Transformer transformer = fabrique.newTransformer();
120
                        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
121
                        transformer.setOutputProperty(OutputKeys.INDENT, "yes"); 
122
                        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); 
123
                        
124
                        // Transformation
125
                        transformer.transform(source, resultat);
126
                        writer.close();
127
                        return true;
128
                } catch (Exception e) {
129
                        org.txm.utils.logger.Log.printStackTrace(e);
130
                        return false;
131
                }
132
        }
133
        
134
        /**
135
         * The main method.
136
         *
137
         * @param args the arguments
138
         */
139
        public static void main(String[] args)
140
        {
141
                String homedir = System.getProperty("user.home")
142
                File trs1 = new File(homedir, "xml/concattrs/int18_1.trs")
143
                new ChangeSpeaker(trs1, trs1, "spk2", "spk4");
144
                new ChangeSpeaker(trs1, trs1, "spk3", "spk2");
145
                new ChangeSpeaker(trs1, trs1, "spk4", "spk3");
146
        }
147
}