Statistics
| Revision:

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

History | View | Annotate | Download (8.5 kB)

1
package org.txm.scripts.importer
2

    
3
import javax.xml.stream.*;
4
import java.net.URL;
5
/**
6
 * Read the import.xml file
7
 * @author mdecorde
8
 */
9
class ReadImportParameters {
10
        HashMap<String, ArrayList<String>> links = new HashMap<String, ArrayList<String>>();;
11
        HashMap<String, String> structs = new HashMap<String, String>();
12
        HashMap<String, Integer> levels = new HashMap<String, Integer>();
13
        
14
        HashMap<String, HashMap<String, String>> metadatas = [:];
15
        HashMap<String, HashMap<String, String>> pattributes = [:];
16
        HashMap<String, HashMap<String, String>> sattributes = [:];
17
        
18
        HashMap<String, String> biblios = [:];
19
        
20
        HashMap<String, String> uis = [:];
21
        
22
        HashMap<String, ArrayList<HashMap<String, String>>> editions = [:];
23
        
24
        String basename;
25
        String encoding;
26
        String language;
27
        String rootDir;
28
        String script;
29
        String xslt;
30
        String author;
31
        String description;
32
        
33
        boolean debug;
34

    
35
        public ReadImportParameters()
36
        {
37

    
38
        }
39

    
40
        public boolean read(File infile)
41
        {
42
                try {
43
                        def url = infile.toURI().toURL();
44
                        def inputData = url.openStream();
45
                        def factory = XMLInputFactory.newInstance();
46
                        def parser = factory.createXMLStreamReader(inputData);
47
                        boolean ret = read(parser);
48
                        if (parser != null) parser.close();
49
                        if (inputData != null) inputData.close();
50
                        return ret;
51
                } catch (XMLStreamException ex) {
52
                        System.out.println(ex);
53
                }catch (IOException ex) {
54
                        System.out.println("IOException while parsing ");
55
                }
56
                
57
        }
58

    
59
        public boolean read(def parser)
60
        {
61
                boolean startAlignInfos = false;
62
                boolean startEditions = false;
63
                boolean startMetadatas = false;
64
                boolean startPAttributes = false;
65
                boolean startSAttributes = false;
66
                boolean startTokenizer = false;
67
                
68
                for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next())
69
                {
70
                        switch (event) {
71

    
72
                                case XMLStreamConstants.END_ELEMENT:
73
                                        String localname = parser.getLocalName();
74
                                        if(localname == "linkGrp")
75
                                        {
76
                                                startAlignInfos = false;
77
                                        }
78
                                        else if(localname == "metadatas")
79
                                        {
80
                                                startMetadatas = false;
81
                                        }
82
                                        else if(localname == "editions")
83
                                        {
84
                                                startEditions = false;
85
                                        }
86
                                        else if(localname == "pattributes")
87
                                        {
88
                                                startPAttributes = false;
89
                                        }
90
                                        else if(localname == "sattributes")
91
                                        {
92
                                                startSAttributes = false;
93
                                        }
94
                                        else if(localname == "tokenizer")
95
                                        {
96
                                                startSAttributes = false;
97
                                        }
98
                                        break;
99

    
100
                                case XMLStreamConstants.START_ELEMENT:
101
                                        String localname = parser.getLocalName();
102
                                        if (localname == "import")
103
                                        {
104
                                                basename = parser.getAttributeValue(null, "name");
105
                                                encoding = parser.getAttributeValue(null, "encoding");
106
                                                language = parser.getAttributeValue(null, "lang");
107
                                                rootDir = parser.getAttributeValue(null, "rootDir");
108
                                                script = parser.getAttributeValue(null, "script");
109
                                                xslt = parser.getAttributeValue(null, "xslt");
110
                                                author = parser.getAttributeValue(null, "author")
111
                                                description = parser.getAttributeValue(null, "desc");
112
                                                
113
                                                boolean debug;
114
                                        }
115
                                        else if(localname == "editions")
116
                                        {
117
                                                startEditions = true;
118
                                        }
119
                                        else if(localname == "edition")
120
                                        {
121
                                                String corpus = parser.getAttributeValue(null, "corpus")
122
                                                if(!editions.containsKey(corpus))
123
                                                        editions.put(corpus, []);
124
                                                def list = editions.get(corpus);
125
                                                def map = [:]
126
                                                list.add(map)
127
                                                
128
                                                map.put("name", parser.getAttributeValue(null, "name"))
129
                                                map.put("mode", parser.getAttributeValue(null, "mode"))
130
                                                map.put("script", parser.getAttributeValue(null, "script"))
131
                                        }
132
                                        else if (localname == "metadatas") {
133
                                                startMetadatas = true;
134
                                        }
135
                                        else if (localname == "metadata" && startMetadatas) {
136
                                                metadatas.put(parser.getAttributeValue(null, "id"), 
137
                                                        ["id":parser.getAttributeValue(null, "id"),
138
                                                        "shortname":parser.getAttributeValue(null, "shortname"),
139
                                                        "longname":parser.getAttributeValue(null, "longname"),
140
                                                        "type":parser.getAttributeValue(null, "type"),
141
                                                        "selection":parser.getAttributeValue(null, "selection"),
142
                                                        "partition":parser.getAttributeValue(null, "partition"),
143
                                                        "display":parser.getAttributeValue(null, "display"),])
144
                                        }
145
                                        else if (localname == "pattributes") {
146
                                                startPAttributes = true;
147
                                        } else if(localname == "pattribute" && startPAttributes) {
148
                                                pattributes.put(parser.getAttributeValue(null, "id"), 
149
                                                        ["id":parser.getAttributeValue(null, "id"),
150
                                                        "shortname":parser.getAttributeValue(null, "shortname"),
151
                                                        "longname":parser.getAttributeValue(null, "longname"),
152
                                                        "type":parser.getAttributeValue(null, "type"),
153
                                                        "renderer":parser.getAttributeValue(null, "renderer"),
154
                                                        "tooltip":parser.getAttributeValue(null, "tooltip"),
155
                                                        "display":parser.getAttributeValue(null, "display"),
156
                                                        "min":parser.getAttributeValue(null, "min"),
157
                                                        "max":parser.getAttributeValue(null, "max"),
158
                                                        "inputFormat":parser.getAttributeValue(null, "inputFormat"),
159
                                                        "outputFormat":parser.getAttributeValue(null, "outputFormat"),
160
                                                        "position":parser.getAttributeValue(null, "position"),
161
                                                        "sortBy":parser.getAttributeValue(null, "sortBy")])
162
                                        }
163
                                        else if (localname == "sattributes") {
164
                                                startSAttributes = true;
165
                                        }
166
                                        else if (localname == "sattribute" && startSAttributes) {
167
                                                sattributes.put(parser.getAttributeValue(null, "id"), 
168
                                                        ["id":parser.getAttributeValue(null, "id"),
169
                                                        "shortname":parser.getAttributeValue(null, "shortname"),
170
                                                        "longname":parser.getAttributeValue(null, "longname"),
171
                                                        "type":parser.getAttributeValue(null, "type"),
172
                                                        "renderer":parser.getAttributeValue(null, "renderer"),
173
                                                        "tooltip":parser.getAttributeValue(null, "tooltip"),
174
                                                        "pattern":parser.getAttributeValue(null, "renderer"),
175
                                                        "import":parser.getAttributeValue(null, "import"),
176
                                                        "min":parser.getAttributeValue(null, "min"),
177
                                                        "max":parser.getAttributeValue(null, "max"),
178
                                                        "mandatory":parser.getAttributeValue(null, "mandatory"),
179
                                                        "order":parser.getAttributeValue(null, "order"),
180
                                                        "inputFormat":parser.getAttributeValue(null, "inputFormat"),
181
                                                        "outputFormat":parser.getAttributeValue(null, "outputFormat"),])
182
                                        } else if (localname == "link" && startAlignInfos) {
183
                                                String target = parser.getAttributeValue(null, "target");
184
                                                String struct = parser.getAttributeValue(null, "alignElement");
185
                                                String level = parser.getAttributeValue(null, "alignLevel")
186
                                                ArrayList<String> corpora = target.split("#");
187
                                                for (int i = 0 ; i < corpora.size() ; i++) {
188
                                                        corpora[i] = corpora[i].trim()
189
                                                        if (corpora[i].length() == 0) {
190
                                                                corpora.remove(i)
191
                                                                i--
192
                                                        }
193
                                                }
194

    
195
                                                if (level == null)
196
                                                        level = "1";
197

    
198
                                                if (corpora.size() == 0) {
199
                                                        println "no corpus aligned: "+parser.getLocation()
200
                                                        return false;
201
                                                }
202
                                                if (struct == null) {
203
                                                        println "a sturcture is missing: "+parser.getLocation()
204
                                                        return false;
205
                                                }
206
                                                links.put(target, corpora)
207
                                                structs.put(target, struct)
208
                                                levels.put(target, level.toInteger())
209
                                        } else if(localname == "linkGrp" && parser.getAttributeValue(null, "type") == "align") {
210
                                                startAlignInfos = true;
211
                                        } else if(localname == "tokenizer") {
212
                                                startSAttributes = true;
213
                                        }
214
                        }
215
                }
216
        }
217

    
218
        public Set<String> getAlignements()
219
        {
220
                return links.keySet();
221
        }
222

    
223
        public ArrayList<String> getTargets(String alignement)
224
        {
225
                return links.get(alignement);
226
        }
227

    
228
        public String getStructure(String alignement)
229
        {
230
                return structs.get(alignement);
231
        }
232

    
233
        public int getLevel(String alignement)
234
        {
235
                return levels.get(alignement);
236
        }
237

    
238
        public void dump()
239
        {
240
                println "LINKS"
241
                for(String link : links.keySet())
242
                {
243
                        println link
244
                        println " corpus: "+links.get(link)
245
                        println " struct: "+structs.get(link)
246
                        println " level: "+levels.get(link)
247
                }
248
                
249
                println "PROPERTIES"
250
                println "metadatas $metadatas"
251
                println " pattributes $pattributes"
252
                println " sattributes $sattributes"
253
                
254
                println " biblios $biblios"
255
                
256
                println "uis $uis"
257
                
258
                println " editions $editions"
259
                
260
                println "HEADER"
261
                println " basename $basename"
262
                println " encoding $encoding"
263
                println " language $language"
264
                println " rootDir $rootDir"
265
                println " script $script"
266
                println " xslt $xslt"
267
                println " author $author"
268
                println " description $description"
269
                println "DEBUG: $debug"
270
        }
271

    
272
        public static void main(String[] args)
273
        {
274
                String userhome = System.getProperty("user.home");
275
                File alignfile = new File(userhome, "xml/xmltxmpara/align.xml")
276
                ReadImportParameters parameters = new ReadImportParameters();
277
                parameters.read(alignfile);
278
                parameters.dump();
279
        }
280
}
281