Statistics
| Revision:

root / tmp / org.txm.core / src / groovy / org / txm / importer / doc / StylesToCSS.groovy @ 187

History | View | Annotate | Download (6.1 kB)

1
package org.txm.importer.doc
2

    
3
class StylesToCSS {
4

    
5
        File cssFile, styleFile, contentFile;
6
        def rootStyles, rootContent;
7

    
8
        def styles = [:]
9
        def translatedStyles = [:]
10
        
11
        def beforebreaks = []
12
        def afterbreaks = []
13
        def parentStyles = [:]
14

    
15
        public StylesToCSS(File unzipDir) {
16
                this.styleFile= new File(unzipDir, "styles.xml");
17
                this.contentFile= new File(unzipDir, "content.xml");
18
                rootStyles = new XmlSlurper().parse(styleFile);
19
                rootContent = new XmlSlurper().parse(contentFile);
20
        }
21

    
22
        public boolean process(File css) {
23
                //println "StylesToCSS: "+css
24
                this.cssFile = cssFile;
25
                
26
                //INDEXING STYLES BY ".NAME_FAMILY"
27
                // for each style of styles.xml
28
                for( def style : rootStyles.styles.style) {
29
                        String family = ""+style.@family;
30
                        String name = ""+style.@name
31
                        if ("paragraph".equals(family)) {
32
                                styles[name+"_"+family] = style
33
                        } else if ("text".equals(family)) {
34
                                if (!name.startsWith("WW8Num"))
35
                                        styles[name+"_"+family] = style
36
                        } else if ("graphic".equals(family)) {
37
                                styles[name+"_"+family] = style
38
                        }
39
                }
40
                
41
                // for each automatic style of content.xml
42
                for( def style : rootContent['automatic-styles'].style) {
43
                        //println "style: "+style.@name
44
                        String family = ""+style.@family;
45
                        String name = ""+style.@name
46
                        String parent = ""+style['@parent-style-name'] // get parent style name
47
                        String breakpage = ""; // get soft page break
48
                        for (def pproperty : style['paragraph-properties']) {
49
                                //println " paragraph-properties: "+pproperty['@break-before']
50
                                //println " paragraph-properties: "+pproperty['@break-after']
51
                                if ("page" == pproperty['@break-before'].toString())
52
                                        beforebreaks << name
53
                                else if ("page" == pproperty['@break-after'].toString())
54
                                        afterbreaks << name
55
                        }
56
                        
57
                        parentStyles[name] = parent
58
                        
59
                        //println "auto $name $family"
60
                        if ("paragraph".equals(family)) {
61
                                styles[name+"_"+family] = style
62
                        } else if ("text".equals(family)) {
63
                                if (!name.startsWith("WW8Num"))
64
                                        styles[name+"_"+family] = style
65
                        } else if ("graphic".equals(family)) {
66
                                styles[name+"_"+family] = style
67
                        }
68
                }
69
                
70
                // for each default style of styles.xml
71
                for( def style : rootStyles.styles['default-style']) {
72
                        String family = ""+style.@family;
73
                        String name = ""+style.@name
74
                        if ("paragraph".equals(family)) {
75
                                styles[name+"_"+family] = style
76
                        }
77
                }
78
                //println styles.keySet()
79
                
80
                //BUILDING CSS
81
                // build styles.xml css
82
                for( def style : rootStyles.styles.style) {
83
                        String name = ""+style.@name
84
                        if (!name.startsWith("WW8Num")) buildStyle(style);
85
                }
86
                
87
                // for each default style of styles.xml
88
                for( def style : rootStyles.styles['default-style']) {
89
                        String family = ""+style.@family;
90
                        String name = ""+style.@name
91
                        if ("paragraph".equals(family)) {
92
                                buildStyle(style)
93
                        }
94
                }
95
                
96
                // build content.xml css
97
                for( def style : rootContent['automatic-styles'].style) {
98
                        String name = ""+style.@name
99
                        if (!name.startsWith("WW8Num")) buildStyle(style);
100
                }
101
                
102
                def writer = css.newPrintWriter("UTF-8")
103
                def keys = new ArrayList<String>(translatedStyles.keySet());
104
                keys.sort()
105
                //println keys
106
                for (String name : keys) {
107
                        if (name.equals("")) {
108
                                writer.println("body{\n")
109
                        } else {
110
                                writer.println(".$name{")
111
                        }
112
                        def props = translatedStyles[name]
113
                        for (def key : props.keySet())
114
                                writer.println(""+key+":"+props[key]+";")
115
                        writer.println("}\n")
116
                }
117
                writer.close()
118

    
119
                return true;
120
        }
121
        
122
        private void buildDefaultStyle(def style) {
123
                
124
        }
125

    
126
        private void buildStyle (def style) {
127
                
128
                StringBuffer str = new StringBuffer();
129
                String name = style.@name
130
                //if (name.length() == 1) name = ".body"
131
                if (translatedStyles.containsKey(name)) return
132
                def props = [:];
133
                
134
                String family = ""+style.@family
135
                String parentname = style['@parent-style-name']
136
                if ( parentname != null && parentname.length() > 0) {
137
                        String fullparentname = parentname+"_"+family
138
                        //println "build parent: "+parentname+" for $name"
139
                        buildStyle(styles[fullparentname]); // build its parent if any
140
                        def parentprops = translatedStyles[parentname]
141
                        for (def key : parentprops.keySet())
142
                                props[key] = parentprops[key]
143
                }
144

    
145
                //writer.println(".$name {")
146
                for (def property : style['paragraph-properties']) {
147
                        for (def attr : property.attributes()) {
148
                                //println attr.getClass()
149
                                switch (attr.getKey()) {
150
                                        case "text-align": props[attr.getKey()] = attr.getValue(); break;
151
                                        case "margin": props[attr.getKey()] = attr.getValue(); break;
152
                                        case "margin-left": props[attr.getKey()] = attr.getValue(); break;
153
                                        case "margin-right": props[attr.getKey()] = attr.getValue(); break;
154
                                        case "margin-top": props[attr.getKey()] = attr.getValue(); break;
155
                                        case "margin-bottom": props[attr.getKey()] = attr.getValue(); break;
156
                                        case "padding": props[attr.getKey()] = attr.getValue(); break;
157
                                        case "padding-left": props[attr.getKey()] = attr.getValue(); break;
158
                                        case "padding-right": props[attr.getKey()] = attr.getValue(); break;
159
                                        case "padding-top": props[attr.getKey()] = attr.getValue(); break;
160
                                        case "padding-bottom": props[attr.getKey()] = attr.getValue(); break;
161
                                        //case "writing-mode": println(attr); break;
162
                                }
163
                        }
164
                        break;
165
                }
166

    
167
                for (def property : style['text-properties']) {
168
                        //println property.attributes()
169
                        for (def attr : property.attributes()) {
170
                                switch (attr.getKey()) {
171
                                        //case "fo:language": println(attr); break;
172
                                        case "font-size": props[attr.getKey()] = attr.getValue(); break;
173
                                        case "font-name": props["font-family"] = "\""+attr.getValue()+"\""; break;
174
                                        case "font-style": props[attr.getKey()] = attr.getValue(); break;
175
                                        case "font-weight": props[attr.getKey()] = attr.getValue(); break;
176
                                        //case "text-position": str.append(""+attr+"\n"); break;
177
                                        case "color": props[attr.getKey()] = attr.getValue(); break;
178
                                }
179
                        }
180
                        break;
181
                }
182
                                
183
                translatedStyles[name] = props;
184
                //println "builded $name"
185
        }
186

    
187
        static main(args) {
188
                File unzipDir = new File("/home/mdecorde/xml/txmrefman/TEI/files-Manuel de Référence TXM 0.6_FR.odt.xml")
189
                File cssFile = new File("/home/mdecorde/xml/txmrefman/TEI/files-Manuel de Référence TXM 0.6_FR.odt.xml", "style.css")
190

    
191
                StylesToCSS converter = new StylesToCSS(unzipDir);
192
                println converter.process(cssFile);
193
        }
194
}