Statistics
| Revision:

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

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