Statistics
| Revision:

root / tmp / org.txm.groovy.core / src / groovy / org / txm / importer / doc / FixBreaksAndAutomaticStyles.groovy @ 479

History | View | Annotate | Download (5.5 kB)

1
package org.txm.importer.doc
2

    
3
import org.txm.utils.io.IOUtils;
4
import org.txm.utils.NameSpaceXmlNodePrinter
5
import org.txm.utils.NameSpaceXmlNodePrinter.NamespaceContext
6

    
7
import groovy.xml.QName
8
import groovy.xml.StreamingMarkupBuilder
9

    
10
class FixBreaksAndAutomaticStyles {
11

    
12
        def nsText = new groovy.xml.Namespace("urn:oasis:names:tc:opendocument:xmlns:text:1.0", 'tei')
13
        def sytleAt;
14
        def pbAt;
15

    
16
        FixBreaksAndAutomaticStyles() {
17

    
18
        }
19
        def befores
20
        def afters
21
        def parents
22

    
23
        public boolean run(File xmlFile, def befores, def afters, def parents) {
24

    
25
                sytleAt = nsText.get("rend")
26
                pbAt = nsText.get("soft-page-break")
27

    
28

    
29
                def parser = new XmlParser(false, true)
30
                def root = parser.parse(xmlFile)
31

    
32
                this.befores = befores
33
                this.afters = afters
34
                this.parents = parents
35
                process(root, null)
36

    
37

    
38
                //println root.children()
39
                File tmp = new File(xmlFile.getParentFile(), "test.txt")
40
                def writer = IOUtils.getWriter(tmp, "UTF-8")
41
                writer.write('<?xml version="1.0" encoding="UTF-8"?>\n')
42
                def printer = new NameSpaceXmlNodePrinter(new PrintWriter(writer))
43
                //create namespace context
44
                NamespaceContext namespaceContext = new NamespaceContext(printer);
45
                namespaceContext.registerNamespacePrefix("office", "urn:oasis:names:tc:opendocument:xmlns:office:1.0");
46
                namespaceContext.registerNamespacePrefix("style", "urn:oasis:names:tc:opendocument:xmlns:style:1.0");
47
                namespaceContext.registerNamespacePrefix("text", "urn:oasis:names:tc:opendocument:xmlns:text:1.0");
48
                namespaceContext.registerNamespacePrefix("table", "urn:oasis:names:tc:opendocument:xmlns:table:1.0");
49
                namespaceContext.registerNamespacePrefix("draw", "urn:oasis:names:tc:opendocument:xmlns:drawing:1.0");
50
                namespaceContext.registerNamespacePrefix("fo", "urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0");
51
                namespaceContext.registerNamespacePrefix("xlink", "http://www.w3.org/1999/xlink");
52
                namespaceContext.registerNamespacePrefix("dc", "http://purl.org/dc/elements/1.1/");
53
                namespaceContext.registerNamespacePrefix("meta", "urn:oasis:names:tc:opendocument:xmlns:meta:1.0");
54
                namespaceContext.registerNamespacePrefix("number", "urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0");
55
                namespaceContext.registerNamespacePrefix("svg", "urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0");
56
                namespaceContext.registerNamespacePrefix("chart", "urn:oasis:names:tc:opendocument:xmlns:chart:1.0");
57
                namespaceContext.registerNamespacePrefix("dr3d", "urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0");
58
                namespaceContext.registerNamespacePrefix("math", "http://www.w3.org/1998/Math/MathML");
59
                namespaceContext.registerNamespacePrefix("form", "urn:oasis:names:tc:opendocument:xmlns:form:1.0");
60
                namespaceContext.registerNamespacePrefix("script", "urn:oasis:names:tc:opendocument:xmlns:script:1.0");
61
                namespaceContext.registerNamespacePrefix("ooo", "http://openoffice.org/2004/office");
62
                namespaceContext.registerNamespacePrefix("ooow", "http://openoffice.org/2004/writer");
63
                namespaceContext.registerNamespacePrefix("oooc", "http://openoffice.org/2004/calc");
64
                namespaceContext.registerNamespacePrefix("dom", "http://www.w3.org/2001/xml-events");
65
                namespaceContext.registerNamespacePrefix("xforms", "http://www.w3.org/2002/xforms");
66
                namespaceContext.registerNamespacePrefix("xsd", "http://www.w3.org/2001/XMLSchema");
67
                namespaceContext.registerNamespacePrefix("xsi", "http://www.w3.org/2001/XMLSchema-instance");
68
                namespaceContext.registerNamespacePrefix("rpt", "http://openoffice.org/2005/report");
69
                namespaceContext.registerNamespacePrefix("of", "urn:oasis:names:tc:opendocument:xmlns:of:1.2");
70
                namespaceContext.registerNamespacePrefix("xhtml", "http://www.w3.org/1999/xhtml");
71
                namespaceContext.registerNamespacePrefix("grddl", "http://www.w3.org/2003/g/data-view#");
72
                namespaceContext.registerNamespacePrefix("tableooo", "http://openoffice.org/2009/table");
73
                namespaceContext.registerNamespacePrefix("field", "urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0");
74
                namespaceContext.registerNamespacePrefix("formx", "urn:openoffice:names:experimental:ooxml-odf-interop:xmlns:form:1.0");
75
                namespaceContext.registerNamespacePrefix("css3t", "http://www.w3.org/TR/css3-text/");
76

    
77
                printer.setNamespaceAware(true);
78
                printer.print(root, namespaceContext)
79
                writer.close();
80

    
81
                if (xmlFile.delete()) tmp.renameTo(xmlFile);
82
                else println "FixBreaksAndAutomaticStyles: Error: could not replace "+xmlFile
83
        }
84

    
85
        public void process(def node, def parent) {
86
                if (!(node instanceof groovy.util.Node)) return;
87

    
88
                def list = new ArrayList(node.children())
89
                for( def child : list) {
90
                        process(child, node)
91
                }
92
                String name = node.name().getLocalPart()
93
                if ("p" == name || "div" == name) {
94
                        //println "P: "+node.attribute("rend")
95
                        String style = node.attribute("rend")
96
                        if (befores.contains(style)) {
97
                                def newChild = new Node(null, "pb")
98
                                newChild.parent = parent
99
                                def idx = parent.children().indexOf(node)
100
                                //println "b idx: $idx"
101
                                parent.children().add(idx, newChild)
102
                        } else if (afters.contains(style)) {
103
                                def newChild = new Node(null, "pb")
104
                                newChild.parent = parent
105
                                def idx = parent.children().indexOf(node)
106
                                //println "a idx: $idx"
107
                                parent.children().add(idx+1, newChild)
108
                        }
109
                        if (parents.containsKey(style)) {
110
                                //println "parent of "+style+ " = "+parents[style]
111
                                node.attributes()["rend"] = parents[style]
112
                        }
113
                }
114
        }
115

    
116
        public static void main(String[] args) {
117
                File xmlFile = new File("/home/mdecorde/xml/temoignages/content.xml")
118
                new FixBreaksAndAutomaticStyles().run(xmlFile, ["P1", "P2"], ["P3"], ["P1": "Standard", "P2":"Standard", "P3":"Standard"])
119
        }
120
}