Statistics
| Revision:

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

History | View | Annotate | Download (3.2 kB)

1
package org.txm.sw
2

    
3
import java.io.File;
4

    
5
import org.txm.utils.xml.DomUtils;
6
import org.w3c.dom.Element;
7
import java.io.File;
8

    
9
import org.w3c.dom.Document;
10

    
11
import javax.xml.parsers.*;
12
import javax.xml.xpath.*;
13
import javax.xml.transform.*;
14
import javax.xml.transform.dom.DOMSource;
15
import javax.xml.transform.stream.StreamResult;
16
import org.txm.importer.graal.PersonalNamespaceContext;
17

    
18
class RemoveTag {
19

    
20
        public static boolean xpath(File xmlfile, String xpath)
21
        {
22
                if (!xmlfile.exists()) {
23
                        println "Error: $xmlfile does not exists"
24
                }
25

    
26
                if (!(xmlfile.canRead() && xmlfile.canWrite())) {
27
                        println "Error: $xmlfile is not readable or writable"
28
                }
29

    
30
                DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
31
                domFactory.setNamespaceAware(true); // never forget this!
32
                domFactory.setXIncludeAware(true);
33
                DocumentBuilder builder = domFactory.newDocumentBuilder();
34
                Document doc = builder.parse(xmlfile);
35

    
36
                XPathFactory xpathfactory = XPathFactory.newInstance();
37
                def _xpath = xpathfactory.newXPath();
38
                _xpath.setNamespaceContext(new PersonalNamespaceContext());
39
                def expr = _xpath.compile(xpath);
40
                def nodes = expr.evaluate(doc, XPathConstants.NODESET);
41

    
42
                for(Element node : nodes) {
43
                        def parent = node.getParentNode();
44
                        if (parent != null)
45
                                parent.removeChild(node)
46
                }
47
                return DomUtils.save(doc, xmlfile)
48
        }
49

    
50
        public static boolean xpath(File xmlfile, def xpaths)
51
        {
52
                if (!xmlfile.exists()) {
53
                        println "Error: $xmlfile does not exists"
54
                }
55

    
56
                if (!(xmlfile.canRead() && xmlfile.canWrite())) {
57
                        println "Error: $xmlfile is not readable or writable"
58
                }
59

    
60
                DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
61
                domFactory.setNamespaceAware(true); // never forget this!
62
                domFactory.setXIncludeAware(true);
63
                DocumentBuilder builder = domFactory.newDocumentBuilder();
64
                Document doc = builder.parse(xmlfile);
65
                XPathFactory xpathfactory = XPathFactory.newInstance();
66

    
67
                for (String xpath : xpaths) {
68
                        def _xpath = xpathfactory.newXPath();
69
                        _xpath.setNamespaceContext(new PersonalNamespaceContext());
70
                        def expr = _xpath.compile(xpath);
71
                        def nodes = expr.evaluate(doc, XPathConstants.NODESET);
72

    
73
                        for (Element node : nodes) {
74
                                def parent = node.getParentNode();
75
                                if (parent != null)
76
                                        parent.removeChild(node)
77
                        }
78
                }
79
                return DomUtils.save(doc, xmlfile)
80

    
81
        }
82
        public static void main(String[] args) {
83
                File dir = new File("/home/mdecorde/Bureau/matrice/témoignages")
84
                File infile = new File(dir, "tei2.xml")
85
                File outfile = new File(dir, "tei2-out.xml")
86

    
87
                def doc = DomUtils.load(infile);
88
                def addList = doc.getDocumentElement().getElementsByTagName("add")
89
                println "nb <add> to delete: "+addList.getLength()
90
                for(int i = 0 ; i < addList.getLength() ; i++) {
91
                        Element node = addList.item(i);
92
                        String text = node.getTextContent().trim();
93
                        Element parent = node.getParentNode();
94
                        parent.insertBefore(doc.createTextNode(text), node)
95
                        parent.removeChild(node);
96
                        i--
97
                }
98

    
99
                def delList = doc.getDocumentElement().getElementsByTagName("del")
100
                println "nb <del> to delete: "+delList.getLength()
101
                for(int i = 0 ; i < delList.getLength() ; i++) {
102
                        Element node = delList.item(i);
103
                        node.getParentNode().removeChild(node);
104
                        i--
105
                }
106

    
107
                DomUtils.save(doc, outfile);
108
        }
109
}