Statistics
| Revision:

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

History | View | Annotate | Download (4.5 kB)

1 881 mdecorde
// Copyright © 2010-2013 ENS de Lyon.
2 881 mdecorde
// Copyright © 2007-2010 ENS de Lyon, CNRS, INRP, University of
3 881 mdecorde
// Lyon 2, University of Franche-Comté, University of Nice
4 881 mdecorde
// Sophia Antipolis, University of Paris 3.
5 881 mdecorde
//
6 881 mdecorde
// The TXM platform is free software: you can redistribute it
7 881 mdecorde
// and/or modify it under the terms of the GNU General Public
8 881 mdecorde
// License as published by the Free Software Foundation,
9 881 mdecorde
// either version 2 of the License, or (at your option) any
10 881 mdecorde
// later version.
11 881 mdecorde
//
12 881 mdecorde
// The TXM platform is distributed in the hope that it will be
13 881 mdecorde
// useful, but WITHOUT ANY WARRANTY; without even the implied
14 881 mdecorde
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 881 mdecorde
// PURPOSE. See the GNU General Public License for more
16 881 mdecorde
// details.
17 881 mdecorde
//
18 881 mdecorde
// You should have received a copy of the GNU General
19 881 mdecorde
// Public License along with the TXM platform. If not, see
20 881 mdecorde
// http://www.gnu.org/licenses.
21 881 mdecorde
//
22 881 mdecorde
//
23 881 mdecorde
//
24 881 mdecorde
// $LastChangedDate:$
25 881 mdecorde
// $LastChangedRevision:$
26 881 mdecorde
// $LastChangedBy:$
27 881 mdecorde
//
28 1000 mdecorde
package org.txm.scripts.importer
29 881 mdecorde
30 881 mdecorde
import java.io.File;
31 881 mdecorde
32 881 mdecorde
import org.w3c.dom.Document;
33 881 mdecorde
import java.io.File;
34 881 mdecorde
35 881 mdecorde
import org.w3c.dom.Document;
36 881 mdecorde
import org.w3c.dom.Element;
37 881 mdecorde
38 881 mdecorde
import javax.xml.parsers.*;
39 881 mdecorde
import javax.xml.xpath.*;
40 881 mdecorde
import javax.xml.transform.*;
41 881 mdecorde
import javax.xml.transform.dom.DOMSource;
42 881 mdecorde
import javax.xml.transform.stream.StreamResult;
43 881 mdecorde
import org.txm.importer.PersonalNamespaceContext;
44 986 mdecorde
import org.txm.importer.StaxIdentityParser
45 881 mdecorde
46 881 mdecorde
// TODO: Auto-generated Javadoc
47 881 mdecorde
/**
48 881 mdecorde
 * The Class RenameTag : using Stax or DOM depending on the file size
49 881 mdecorde
 */
50 881 mdecorde
class RenameTag extends StaxIdentityParser{
51 881 mdecorde
52 881 mdecorde
        public static int DOMMAXSIZE = 1000000;
53 881 mdecorde
        String newname, oldname;
54 881 mdecorde
        File xmlfile;
55 881 mdecorde
56 881 mdecorde
        public RenameTag(File xmlfile, String oldname, String newname)
57 881 mdecorde
        {
58 881 mdecorde
                super(xmlfile.toURI().toURL());
59 881 mdecorde
                this.oldname = oldname;
60 881 mdecorde
                this.newname = newname;
61 881 mdecorde
                this.xmlfile = xmlfile;
62 881 mdecorde
        }
63 881 mdecorde
        /**
64 881 mdecorde
         * Rename an element - DOM method
65 881 mdecorde
         *
66 881 mdecorde
         * @param xmlfile the xmlfile
67 881 mdecorde
         * @param oldname the oldname
68 881 mdecorde
         * @param newname the newname
69 881 mdecorde
         * @return true, if successful
70 881 mdecorde
         */
71 881 mdecorde
        public static boolean rename(File xmlfile, String oldname, String newname)
72 881 mdecorde
        {
73 881 mdecorde
                if (!xmlfile.exists()) {
74 881 mdecorde
                        println "Error: $xmlfile does not exists"
75 881 mdecorde
                }
76 881 mdecorde
77 881 mdecorde
                if (!(xmlfile.canRead() && xmlfile.canWrite())) {
78 881 mdecorde
                        println "Error: $xmlfile is not readable or writable"
79 881 mdecorde
                }
80 881 mdecorde
81 881 mdecorde
                if (xmlfile.length() <= DOMMAXSIZE) {
82 881 mdecorde
                        String xpath = "//"+oldname;
83 881 mdecorde
                        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
84 881 mdecorde
                        domFactory.setXIncludeAware(true);
85 881 mdecorde
                        domFactory.setNamespaceAware(true); // never forget this!
86 881 mdecorde
                        DocumentBuilder builder = domFactory.newDocumentBuilder();
87 881 mdecorde
                        Document doc = builder.parse(xmlfile);
88 881 mdecorde
89 881 mdecorde
                        XPathFactory xpathfactory = XPathFactory.newInstance();
90 881 mdecorde
                        def _xpath = xpathfactory.newXPath();
91 881 mdecorde
                        _xpath.setNamespaceContext(new PersonalNamespaceContext());
92 881 mdecorde
                        def expr = _xpath.compile(xpath);
93 881 mdecorde
                        def nodes = expr.evaluate(doc, XPathConstants.NODESET);
94 881 mdecorde
95 881 mdecorde
                        for(def node : nodes) {
96 881 mdecorde
                                doc.renameNode(node, null, newname)
97 881 mdecorde
                        }
98 881 mdecorde
                        return org.txm.utils.xml.DomUtils.save(doc, xmlfile)
99 881 mdecorde
                }
100 881 mdecorde
                else {
101 881 mdecorde
                        RenameTag builder = new RenameTag(xmlfile, oldname, newname);
102 881 mdecorde
                        File temp = File.createTempFile("temp", ".xml", xmlfile.getParentFile());
103 881 mdecorde
                        if(builder.process(temp)) {
104 881 mdecorde
                                if (!(xmlfile.delete() && temp.renameTo(xmlfile))) println "Warning can't rename file "+temp+" to "+xmlfile
105 881 mdecorde
                                return true;
106 881 mdecorde
                        }
107 881 mdecorde
                        else
108 881 mdecorde
                                return false;
109 881 mdecorde
                }
110 881 mdecorde
        }
111 881 mdecorde
112 881 mdecorde
        /**
113 881 mdecorde
         * Rewrite processStartElement to update the localname if localname == oldname
114 881 mdecorde
         */
115 881 mdecorde
        public void processStartElement()
116 881 mdecorde
        {
117 881 mdecorde
                String prefix = parser.getPrefix();
118 881 mdecorde
                if (INCLUDE == localname && XI == prefix) {
119 881 mdecorde
                        processingXInclude();
120 881 mdecorde
                        return;
121 881 mdecorde
                }
122 881 mdecorde
123 881 mdecorde
                if (localname == oldname) {
124 881 mdecorde
                        localname = newname;
125 881 mdecorde
126 881 mdecorde
                        if(prefix.length() > 0)
127 881 mdecorde
                                writer.writeStartElement(Nscontext.getNamespaceURI(prefix), localname)
128 881 mdecorde
                        else
129 881 mdecorde
                                writer.writeStartElement(localname);
130 881 mdecorde
131 881 mdecorde
                        for(int i = 0 ; i < parser.getNamespaceCount() ; i++)
132 881 mdecorde
                        {
133 881 mdecorde
                                writer.writeNamespace(parser.getNamespacePrefix(i), parser.getNamespaceURI(i));
134 881 mdecorde
                        }
135 881 mdecorde
136 881 mdecorde
                        for(int i = 0 ; i < parser.getAttributeCount() ; i++)
137 881 mdecorde
                                writer.writeAttribute(parser.getAttributeLocalName(i), parser.getAttributeValue(i));
138 881 mdecorde
                } else {
139 881 mdecorde
                        super.processStartElement();
140 881 mdecorde
                }
141 881 mdecorde
        }
142 881 mdecorde
143 881 mdecorde
        public static void main(String[] args)
144 881 mdecorde
        {
145 881 mdecorde
                File xmlfile = new File("/home/mdecorde/xml/xmlçàé/essai.xml");
146 881 mdecorde
                File temp = new File("/home/mdecorde/xml/xmlçàé/renamed.xml");
147 881 mdecorde
                String oldname = "machin"
148 881 mdecorde
                String newname = "titre"
149 881 mdecorde
                long time = System.currentTimeMillis();
150 881 mdecorde
                RenameTag.rename(xmlfile, oldname, newname);
151 881 mdecorde
                //                RenameTag builder = new RenameTag(xmlfile, oldname, newname);
152 881 mdecorde
                //                builder.process(temp);
153 881 mdecorde
                println "elapsed: "+(System.currentTimeMillis()-time);
154 881 mdecorde
        }
155 881 mdecorde
}