Statistics
| Revision:

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

History | View | Annotate | Download (4.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.xpath.*
33

    
34
import org.txm.importer.graal.PersonalNamespaceContext
35
import org.txm.utils.xml.DomUtils
36
import org.w3c.dom.Document
37

    
38
// TODO: Auto-generated Javadoc
39
/**
40
 * The Class RenameTag : using Stax or DOM depending on the file size
41
 */
42
class RenameTag extends StaxIdentityParser{
43

    
44
        public static int DOMMAXSIZE = 1000000;
45
        String newname, oldname;
46
        File xmlfile;
47

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

    
69
                if (!(xmlfile.canRead() && xmlfile.canWrite())) {
70
                        println "Error: $xmlfile is not readable or writable"
71
                }
72

    
73
                if (xmlfile.length() <= DOMMAXSIZE) {
74
                        String xpath = "//"+oldname;
75
                        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
76
                        domFactory.setNamespaceAware(true); // never forget this!
77
                        DocumentBuilder builder = domFactory.newDocumentBuilder();
78
                        Document doc = builder.parse(xmlfile);
79

    
80
                        XPathFactory xpathfactory = XPathFactory.newInstance();
81
                        def _xpath = xpathfactory.newXPath();
82
                        _xpath.setNamespaceContext(new PersonalNamespaceContext());
83
                        def expr = _xpath.compile(xpath);
84
                        def nodes = expr.evaluate(doc, XPathConstants.NODESET);
85

    
86
                        for(def node : nodes) {
87
                                doc.renameNode(node, null, newname)
88
                        }
89
                        return DomUtils.save(doc, xmlfile)
90
                }
91
                else {
92
                        RenameTag builder = new RenameTag(xmlfile, oldname, newname);
93
                        File temp = File.createTempFile("temp", ".xml", xmlfile.getParentFile());
94
                        if(builder.process(temp)) {
95
                                if (!(xmlfile.delete() && temp.renameTo(xmlfile))) println "Warning can't rename file "+temp+" to "+xmlfile
96
                                return true;
97
                        }
98
                        else
99
                                return false;
100
                }
101
        }
102

    
103
        /**
104
         * Rewrite processStartElement to update the localname if localname == oldname
105
         */
106
        public void processStartElement()
107
        {
108
                String prefix = parser.getPrefix();
109
                if (INCLUDE == localname && XI == prefix) {
110
                        processingXInclude();
111
                        return;
112
                }
113

    
114
                if (localname == oldname) {
115
                        localname = newname;
116

    
117
                        if(prefix.length() > 0)
118
                                writer.writeStartElement(Nscontext.getNamespaceURI(prefix), localname)
119
                        else
120
                                writer.writeStartElement(localname);
121

    
122
                        for(int i = 0 ; i < parser.getNamespaceCount() ; i++)
123
                        {
124
                                writer.writeNamespace(parser.getNamespacePrefix(i), parser.getNamespaceURI(i));
125
                        }
126

    
127
                        for(int i = 0 ; i < parser.getAttributeCount() ; i++)
128
                                writer.writeAttribute(parser.getAttributeLocalName(i), parser.getAttributeValue(i));
129
                } else {
130
                        super.processStartElement();
131
                }
132
        }
133

    
134
        public static void main(String[] args)
135
        {
136
                File xmlfile = new File("/home/mdecorde/xml/xmlçàé/essai.xml");
137
                File temp = new File("/home/mdecorde/xml/xmlçàé/renamed.xml");
138
                String oldname = "machin"
139
                String newname = "titre"
140
                long time = System.currentTimeMillis();
141
                RenameTag.rename(xmlfile, oldname, newname);
142
                //                RenameTag builder = new RenameTag(xmlfile, oldname, newname);
143
                //                builder.process(temp);
144
                println "elapsed: "+(System.currentTimeMillis()-time);
145
        }
146
}