Statistics
| Revision:

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

History | View | Annotate | Download (4.5 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 java.io.File;
31

    
32
import org.w3c.dom.Document;
33
import java.io.File;
34

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

    
38
import javax.xml.parsers.*;
39
import javax.xml.xpath.*;
40
import javax.xml.transform.*;
41
import javax.xml.transform.dom.DOMSource;
42
import javax.xml.transform.stream.StreamResult;
43
import org.txm.importer.graal.PersonalNamespaceContext;
44

    
45
// TODO: Auto-generated Javadoc
46
/**
47
 * The Class RenameTag : using Stax or DOM depending on the file size
48
 */
49
class RenameTag extends StaxIdentityParser{
50

    
51
        public static int DOMMAXSIZE = 1000000;
52
        String newname, oldname;
53
        File xmlfile;
54

    
55
        public RenameTag(File xmlfile, String oldname, String newname)
56
        {
57
                super(xmlfile.toURI().toURL());
58
                this.oldname = oldname;
59
                this.newname = newname;
60
                this.xmlfile = xmlfile;
61
        }
62
        /**
63
         * Rename an element - DOM method
64
         *
65
         * @param xmlfile the xmlfile
66
         * @param oldname the oldname
67
         * @param newname the newname
68
         * @return true, if successful
69
         */
70
        public static boolean rename(File xmlfile, String oldname, String newname)
71
        {
72
                if (!xmlfile.exists()) {
73
                        println "Error: $xmlfile does not exists"
74
                }
75

    
76
                if (!(xmlfile.canRead() && xmlfile.canWrite())) {
77
                        println "Error: $xmlfile is not readable or writable"
78
                }
79

    
80
                if (xmlfile.length() <= DOMMAXSIZE) {
81
                        String xpath = "//"+oldname;
82
                        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
83
                        domFactory.setXIncludeAware(true);
84
                        domFactory.setNamespaceAware(true); // never forget this!
85
                        DocumentBuilder builder = domFactory.newDocumentBuilder();
86
                        Document doc = builder.parse(xmlfile);
87

    
88
                        XPathFactory xpathfactory = XPathFactory.newInstance();
89
                        def _xpath = xpathfactory.newXPath();
90
                        _xpath.setNamespaceContext(new PersonalNamespaceContext());
91
                        def expr = _xpath.compile(xpath);
92
                        def nodes = expr.evaluate(doc, XPathConstants.NODESET);
93

    
94
                        for(def node : nodes) {
95
                                doc.renameNode(node, null, newname)
96
                        }
97
                        return org.txm.utils.xml.DomUtils.save(doc, xmlfile)
98
                }
99
                else {
100
                        RenameTag builder = new RenameTag(xmlfile, oldname, newname);
101
                        File temp = File.createTempFile("temp", ".xml", xmlfile.getParentFile());
102
                        if(builder.process(temp)) {
103
                                if (!(xmlfile.delete() && temp.renameTo(xmlfile))) println "Warning can't rename file "+temp+" to "+xmlfile
104
                                return true;
105
                        }
106
                        else
107
                                return false;
108
                }
109
        }
110

    
111
        /**
112
         * Rewrite processStartElement to update the localname if localname == oldname
113
         */
114
        public void processStartElement()
115
        {
116
                String prefix = parser.getPrefix();
117
                if (INCLUDE == localname && XI == prefix) {
118
                        processingXInclude();
119
                        return;
120
                }
121

    
122
                if (localname == oldname) {
123
                        localname = newname;
124

    
125
                        if(prefix.length() > 0)
126
                                writer.writeStartElement(Nscontext.getNamespaceURI(prefix), localname)
127
                        else
128
                                writer.writeStartElement(localname);
129

    
130
                        for(int i = 0 ; i < parser.getNamespaceCount() ; i++)
131
                        {
132
                                writer.writeNamespace(parser.getNamespacePrefix(i), parser.getNamespaceURI(i));
133
                        }
134

    
135
                        for(int i = 0 ; i < parser.getAttributeCount() ; i++)
136
                                writer.writeAttribute(parser.getAttributeLocalName(i), parser.getAttributeValue(i));
137
                } else {
138
                        super.processStartElement();
139
                }
140
        }
141

    
142
        public static void main(String[] args)
143
        {
144
                File xmlfile = new File("/home/mdecorde/xml/xmlçàé/essai.xml");
145
                File temp = new File("/home/mdecorde/xml/xmlçàé/renamed.xml");
146
                String oldname = "machin"
147
                String newname = "titre"
148
                long time = System.currentTimeMillis();
149
                RenameTag.rename(xmlfile, oldname, newname);
150
                //                RenameTag builder = new RenameTag(xmlfile, oldname, newname);
151
                //                builder.process(temp);
152
                println "elapsed: "+(System.currentTimeMillis()-time);
153
        }
154
}