Statistics
| Revision:

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

History | View | Annotate | Download (8.4 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
                        return read(parser);
48
                } catch (XMLStreamException ex) {
49
                        System.out.println(ex);
50
                }catch (IOException ex) {
51
                        System.out.println("IOException while parsing ");
52
                }
53
        }
54

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

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

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

    
191
                                                if (level == null)
192
                                                        level = "1";
193

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

    
214
        public Set<String> getAlignements()
215
        {
216
                return links.keySet();
217
        }
218

    
219
        public ArrayList<String> getTargets(String alignement)
220
        {
221
                return links.get(alignement);
222
        }
223

    
224
        public String getStructure(String alignement)
225
        {
226
                return structs.get(alignement);
227
        }
228

    
229
        public int getLevel(String alignement)
230
        {
231
                return levels.get(alignement);
232
        }
233

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

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