Statistics
| Revision:

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

History | View | Annotate | Download (3.2 kB)

1 321 mdecorde
package org.txm.sw
2 321 mdecorde
3 479 mdecorde
import java.io.File;
4 321 mdecorde
5 479 mdecorde
import org.txm.utils.xml.DomUtils;
6 479 mdecorde
import org.w3c.dom.Element;
7 479 mdecorde
import java.io.File;
8 321 mdecorde
9 479 mdecorde
import org.w3c.dom.Document;
10 479 mdecorde
11 479 mdecorde
import javax.xml.parsers.*;
12 479 mdecorde
import javax.xml.xpath.*;
13 479 mdecorde
import javax.xml.transform.*;
14 479 mdecorde
import javax.xml.transform.dom.DOMSource;
15 479 mdecorde
import javax.xml.transform.stream.StreamResult;
16 479 mdecorde
import org.txm.importer.graal.PersonalNamespaceContext;
17 479 mdecorde
18 321 mdecorde
class RemoveTag {
19 321 mdecorde
20 321 mdecorde
        public static boolean xpath(File xmlfile, String xpath)
21 321 mdecorde
        {
22 321 mdecorde
                if (!xmlfile.exists()) {
23 321 mdecorde
                        println "Error: $xmlfile does not exists"
24 321 mdecorde
                }
25 321 mdecorde
26 321 mdecorde
                if (!(xmlfile.canRead() && xmlfile.canWrite())) {
27 321 mdecorde
                        println "Error: $xmlfile is not readable or writable"
28 321 mdecorde
                }
29 321 mdecorde
30 321 mdecorde
                DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
31 321 mdecorde
                domFactory.setNamespaceAware(true); // never forget this!
32 479 mdecorde
                domFactory.setXIncludeAware(true);
33 321 mdecorde
                DocumentBuilder builder = domFactory.newDocumentBuilder();
34 321 mdecorde
                Document doc = builder.parse(xmlfile);
35 321 mdecorde
36 321 mdecorde
                XPathFactory xpathfactory = XPathFactory.newInstance();
37 321 mdecorde
                def _xpath = xpathfactory.newXPath();
38 321 mdecorde
                _xpath.setNamespaceContext(new PersonalNamespaceContext());
39 321 mdecorde
                def expr = _xpath.compile(xpath);
40 321 mdecorde
                def nodes = expr.evaluate(doc, XPathConstants.NODESET);
41 321 mdecorde
42 321 mdecorde
                for(Element node : nodes) {
43 321 mdecorde
                        def parent = node.getParentNode();
44 321 mdecorde
                        if (parent != null)
45 321 mdecorde
                                parent.removeChild(node)
46 321 mdecorde
                }
47 321 mdecorde
                return DomUtils.save(doc, xmlfile)
48 321 mdecorde
        }
49 321 mdecorde
50 321 mdecorde
        public static boolean xpath(File xmlfile, def xpaths)
51 321 mdecorde
        {
52 321 mdecorde
                if (!xmlfile.exists()) {
53 321 mdecorde
                        println "Error: $xmlfile does not exists"
54 321 mdecorde
                }
55 321 mdecorde
56 321 mdecorde
                if (!(xmlfile.canRead() && xmlfile.canWrite())) {
57 321 mdecorde
                        println "Error: $xmlfile is not readable or writable"
58 321 mdecorde
                }
59 321 mdecorde
60 321 mdecorde
                DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
61 321 mdecorde
                domFactory.setNamespaceAware(true); // never forget this!
62 479 mdecorde
                domFactory.setXIncludeAware(true);
63 321 mdecorde
                DocumentBuilder builder = domFactory.newDocumentBuilder();
64 321 mdecorde
                Document doc = builder.parse(xmlfile);
65 321 mdecorde
                XPathFactory xpathfactory = XPathFactory.newInstance();
66 321 mdecorde
67 321 mdecorde
                for (String xpath : xpaths) {
68 321 mdecorde
                        def _xpath = xpathfactory.newXPath();
69 321 mdecorde
                        _xpath.setNamespaceContext(new PersonalNamespaceContext());
70 321 mdecorde
                        def expr = _xpath.compile(xpath);
71 321 mdecorde
                        def nodes = expr.evaluate(doc, XPathConstants.NODESET);
72 321 mdecorde
73 321 mdecorde
                        for (Element node : nodes) {
74 321 mdecorde
                                def parent = node.getParentNode();
75 321 mdecorde
                                if (parent != null)
76 321 mdecorde
                                        parent.removeChild(node)
77 321 mdecorde
                        }
78 321 mdecorde
                }
79 321 mdecorde
                return DomUtils.save(doc, xmlfile)
80 321 mdecorde
81 321 mdecorde
        }
82 321 mdecorde
        public static void main(String[] args) {
83 321 mdecorde
                File dir = new File("/home/mdecorde/Bureau/matrice/témoignages")
84 321 mdecorde
                File infile = new File(dir, "tei2.xml")
85 321 mdecorde
                File outfile = new File(dir, "tei2-out.xml")
86 321 mdecorde
87 321 mdecorde
                def doc = DomUtils.load(infile);
88 321 mdecorde
                def addList = doc.getDocumentElement().getElementsByTagName("add")
89 321 mdecorde
                println "nb <add> to delete: "+addList.getLength()
90 321 mdecorde
                for(int i = 0 ; i < addList.getLength() ; i++) {
91 321 mdecorde
                        Element node = addList.item(i);
92 321 mdecorde
                        String text = node.getTextContent().trim();
93 321 mdecorde
                        Element parent = node.getParentNode();
94 321 mdecorde
                        parent.insertBefore(doc.createTextNode(text), node)
95 321 mdecorde
                        parent.removeChild(node);
96 321 mdecorde
                        i--
97 321 mdecorde
                }
98 321 mdecorde
99 321 mdecorde
                def delList = doc.getDocumentElement().getElementsByTagName("del")
100 321 mdecorde
                println "nb <del> to delete: "+delList.getLength()
101 321 mdecorde
                for(int i = 0 ; i < delList.getLength() ; i++) {
102 321 mdecorde
                        Element node = delList.item(i);
103 321 mdecorde
                        node.getParentNode().removeChild(node);
104 321 mdecorde
                        i--
105 321 mdecorde
                }
106 321 mdecorde
107 321 mdecorde
                DomUtils.save(doc, outfile);
108 321 mdecorde
        }
109 321 mdecorde
}