Statistics
| Revision:

root / tmp / org.txm.groovy.core / src / groovy / org / txm / sw / ReplaceXmlDomNode.groovy @ 479

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

    
30
import java.io.File;
31

    
32
import javax.xml.namespace.NamespaceContext;
33
import org.w3c.dom.*;
34
import javax.xml.parsers.*;
35
import org.xml.sax.SAXException;
36
import javax.xml.stream.*;
37
import javax.xml.xpath.*;
38

    
39
import org.txm.sw.ReplaceXmlDomNode;
40
import org.txm.importer.graal.PersonalNamespaceContext;
41
import javax.xml.namespace.NamespaceContext;
42
import javax.xml.transform.*;
43
import javax.xml.transform.dom.DOMSource;
44
import javax.xml.transform.stream.StreamResult;
45

    
46
// TODO: Auto-generated Javadoc
47
/**
48
 * The Class ReplaceXmlDomNode.
49
 */
50
class ReplaceXmlDomNode
51
{
52
        
53
        /** The ns context. */
54
        NamespaceContext nsContext = new PersonalNamespaceContext();
55
        
56
        /**
57
         * Instantiates a new replace xml dom node.
58
         *
59
         * @param src the src
60
         * @param target the target
61
         * @param xpathsrc the xpathsrc
62
         * @param xpathtarget the xpathtarget
63
         */
64
        public ReplaceXmlDomNode(File src, File target, String xpathsrc, String xpathtarget)
65
        {
66
                def factory = DocumentBuilderFactory.newInstance();
67
                factory.setXIncludeAware(true)
68
                def builder = factory.newDocumentBuilder()
69
                
70
                def docsrc = builder.parse(src);
71
                def doctarget = builder.parse(target);
72
                
73
                Node srcnode = null;
74
                Node targetnode = null;
75
                
76
                factory = XPathFactory.newInstance();
77
                XPath XpathObj = factory.newXPath();
78
                XpathObj.setNamespaceContext(nsContext);
79
                
80
                XPathExpression expr = XpathObj.compile(xpathsrc);
81
                for(Node node : (NodeList) expr.evaluate(docsrc, XPathConstants.NODESET))
82
                {
83
                        srcnode = node;
84
                        break;
85
                }
86
                
87
                XPathExpression expr2 = XpathObj.compile(xpathtarget);
88
                for(Node node : (NodeList) expr2.evaluate(doctarget, XPathConstants.NODESET))
89
                {
90
                        targetnode = node;
91
                        break;
92
                }
93
                
94
                //println "replace : "+targetnode
95
                //println "by : "+srcnode
96
                if(targetnode != null && srcnode != null)
97
                {
98
                        Node parent = targetnode.getParentNode();
99
                        parent.removeChild(targetnode);
100
                        parent.appendChild(doctarget.importNode(srcnode, true));
101
                        
102
                        save(doctarget, target);
103
                }
104
                else
105
                {
106
                        println "Error: "
107
                        println "target node = "+targetnode
108
                        println "src node = "+srcnode
109
                }
110
                
111
        }
112
        
113
        /**
114
         * Save.
115
         *
116
         * @param doc the doc
117
         * @param outfile the outfile
118
         * @return true, if successful
119
         */
120
        private boolean save(def doc, File outfile)
121
        {
122
                try {
123
                        // Création de la source DOM
124
                                Source source = new DOMSource(doc);
125
                        
126
                        // Création du fichier de sortie
127
                                Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outfile), "UTF-8")); 
128
                        Result resultat = new StreamResult(writer);
129
                        
130
                        // Configuration du transformer
131
                        TransformerFactory fabrique = new net.sf.saxon.TransformerFactoryImpl();
132
                        Transformer transformer = fabrique.newTransformer();
133
                        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
134
                        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
135
                        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
136
                        
137
                        // Transformation
138
                        transformer.transform(source, resultat);
139
                        writer.close();
140
                        doc = null;
141
                        return true;
142
                } catch (Exception e) {
143
                        e.printStackTrace();
144
                        return false;
145
                }
146
        }
147
        
148
        /**
149
         * The main method.
150
         *
151
         * @param args the arguments
152
         */
153
        public static void main(String[] args)
154
        {
155
                String home = System.getProperty("user.home")
156
                File src = new File(home, "xml/subberoul/subcorpus0000.xml")
157
                File target = new File(home, "xml/subberoul/master.xml")
158
                String xpathsrc = "//subcorpus"
159
                String xpathtarget = "//subcorpus[@name=\"subcorpus0000\"]"
160
                
161
                new ReplaceXmlDomNode(src, target, xpathsrc, xpathtarget)
162
        }
163
}