Statistics
| Revision:

root / tmp / org.txm.core / src / java / org / txm / scripts / importer / RenameTag.groovy @ 1000

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.scripts.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.PersonalNamespaceContext;
44
import org.txm.importer.StaxIdentityParser
45

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

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

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

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

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

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

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

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

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

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

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

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

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