Statistics
| Revision:

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

History | View | Annotate | Download (8.5 kB)

1 1000 mdecorde
package org.txm.scripts.importer
2 881 mdecorde
3 881 mdecorde
import javax.xml.stream.*;
4 881 mdecorde
import java.net.URL;
5 881 mdecorde
/**
6 881 mdecorde
 * Read the import.xml file
7 881 mdecorde
 * @author mdecorde
8 881 mdecorde
 */
9 881 mdecorde
class ReadImportParameters {
10 881 mdecorde
        HashMap<String, ArrayList<String>> links = new HashMap<String, ArrayList<String>>();;
11 881 mdecorde
        HashMap<String, String> structs = new HashMap<String, String>();
12 881 mdecorde
        HashMap<String, Integer> levels = new HashMap<String, Integer>();
13 881 mdecorde
14 881 mdecorde
        HashMap<String, HashMap<String, String>> metadatas = [:];
15 881 mdecorde
        HashMap<String, HashMap<String, String>> pattributes = [:];
16 881 mdecorde
        HashMap<String, HashMap<String, String>> sattributes = [:];
17 881 mdecorde
18 881 mdecorde
        HashMap<String, String> biblios = [:];
19 881 mdecorde
20 881 mdecorde
        HashMap<String, String> uis = [:];
21 881 mdecorde
22 881 mdecorde
        HashMap<String, ArrayList<HashMap<String, String>>> editions = [:];
23 881 mdecorde
24 881 mdecorde
        String basename;
25 881 mdecorde
        String encoding;
26 881 mdecorde
        String language;
27 881 mdecorde
        String rootDir;
28 881 mdecorde
        String script;
29 881 mdecorde
        String xslt;
30 881 mdecorde
        String author;
31 881 mdecorde
        String description;
32 881 mdecorde
33 881 mdecorde
        boolean debug;
34 881 mdecorde
35 881 mdecorde
        public ReadImportParameters()
36 881 mdecorde
        {
37 881 mdecorde
38 881 mdecorde
        }
39 881 mdecorde
40 881 mdecorde
        public boolean read(File infile)
41 881 mdecorde
        {
42 881 mdecorde
                try {
43 881 mdecorde
                        def url = infile.toURI().toURL();
44 881 mdecorde
                        def inputData = url.openStream();
45 881 mdecorde
                        def factory = XMLInputFactory.newInstance();
46 881 mdecorde
                        def parser = factory.createXMLStreamReader(inputData);
47 1688 mdecorde
                        boolean ret = read(parser);
48 1688 mdecorde
                        if (parser != null) parser.close();
49 1688 mdecorde
                        if (inputData != null) inputData.close();
50 1688 mdecorde
                        return ret;
51 881 mdecorde
                } catch (XMLStreamException ex) {
52 881 mdecorde
                        System.out.println(ex);
53 881 mdecorde
                }catch (IOException ex) {
54 881 mdecorde
                        System.out.println("IOException while parsing ");
55 881 mdecorde
                }
56 1688 mdecorde
57 881 mdecorde
        }
58 881 mdecorde
59 881 mdecorde
        public boolean read(def parser)
60 881 mdecorde
        {
61 881 mdecorde
                boolean startAlignInfos = false;
62 881 mdecorde
                boolean startEditions = false;
63 881 mdecorde
                boolean startMetadatas = false;
64 881 mdecorde
                boolean startPAttributes = false;
65 881 mdecorde
                boolean startSAttributes = false;
66 881 mdecorde
                boolean startTokenizer = false;
67 881 mdecorde
68 881 mdecorde
                for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next())
69 881 mdecorde
                {
70 881 mdecorde
                        switch (event) {
71 881 mdecorde
72 881 mdecorde
                                case XMLStreamConstants.END_ELEMENT:
73 881 mdecorde
                                        String localname = parser.getLocalName();
74 881 mdecorde
                                        if(localname == "linkGrp")
75 881 mdecorde
                                        {
76 881 mdecorde
                                                startAlignInfos = false;
77 881 mdecorde
                                        }
78 881 mdecorde
                                        else if(localname == "metadatas")
79 881 mdecorde
                                        {
80 881 mdecorde
                                                startMetadatas = false;
81 881 mdecorde
                                        }
82 881 mdecorde
                                        else if(localname == "editions")
83 881 mdecorde
                                        {
84 881 mdecorde
                                                startEditions = false;
85 881 mdecorde
                                        }
86 881 mdecorde
                                        else if(localname == "pattributes")
87 881 mdecorde
                                        {
88 881 mdecorde
                                                startPAttributes = false;
89 881 mdecorde
                                        }
90 881 mdecorde
                                        else if(localname == "sattributes")
91 881 mdecorde
                                        {
92 881 mdecorde
                                                startSAttributes = false;
93 881 mdecorde
                                        }
94 881 mdecorde
                                        else if(localname == "tokenizer")
95 881 mdecorde
                                        {
96 881 mdecorde
                                                startSAttributes = false;
97 881 mdecorde
                                        }
98 881 mdecorde
                                        break;
99 881 mdecorde
100 881 mdecorde
                                case XMLStreamConstants.START_ELEMENT:
101 881 mdecorde
                                        String localname = parser.getLocalName();
102 881 mdecorde
                                        if (localname == "import")
103 881 mdecorde
                                        {
104 881 mdecorde
                                                basename = parser.getAttributeValue(null, "name");
105 881 mdecorde
                                                encoding = parser.getAttributeValue(null, "encoding");
106 881 mdecorde
                                                language = parser.getAttributeValue(null, "lang");
107 881 mdecorde
                                                rootDir = parser.getAttributeValue(null, "rootDir");
108 881 mdecorde
                                                script = parser.getAttributeValue(null, "script");
109 881 mdecorde
                                                xslt = parser.getAttributeValue(null, "xslt");
110 881 mdecorde
                                                author = parser.getAttributeValue(null, "author")
111 881 mdecorde
                                                description = parser.getAttributeValue(null, "desc");
112 881 mdecorde
113 881 mdecorde
                                                boolean debug;
114 881 mdecorde
                                        }
115 881 mdecorde
                                        else if(localname == "editions")
116 881 mdecorde
                                        {
117 881 mdecorde
                                                startEditions = true;
118 881 mdecorde
                                        }
119 881 mdecorde
                                        else if(localname == "edition")
120 881 mdecorde
                                        {
121 881 mdecorde
                                                String corpus = parser.getAttributeValue(null, "corpus")
122 881 mdecorde
                                                if(!editions.containsKey(corpus))
123 881 mdecorde
                                                        editions.put(corpus, []);
124 881 mdecorde
                                                def list = editions.get(corpus);
125 881 mdecorde
                                                def map = [:]
126 881 mdecorde
                                                list.add(map)
127 881 mdecorde
128 881 mdecorde
                                                map.put("name", parser.getAttributeValue(null, "name"))
129 881 mdecorde
                                                map.put("mode", parser.getAttributeValue(null, "mode"))
130 881 mdecorde
                                                map.put("script", parser.getAttributeValue(null, "script"))
131 881 mdecorde
                                        }
132 881 mdecorde
                                        else if (localname == "metadatas") {
133 881 mdecorde
                                                startMetadatas = true;
134 881 mdecorde
                                        }
135 881 mdecorde
                                        else if (localname == "metadata" && startMetadatas) {
136 881 mdecorde
                                                metadatas.put(parser.getAttributeValue(null, "id"),
137 881 mdecorde
                                                        ["id":parser.getAttributeValue(null, "id"),
138 881 mdecorde
                                                        "shortname":parser.getAttributeValue(null, "shortname"),
139 881 mdecorde
                                                        "longname":parser.getAttributeValue(null, "longname"),
140 881 mdecorde
                                                        "type":parser.getAttributeValue(null, "type"),
141 881 mdecorde
                                                        "selection":parser.getAttributeValue(null, "selection"),
142 881 mdecorde
                                                        "partition":parser.getAttributeValue(null, "partition"),
143 881 mdecorde
                                                        "display":parser.getAttributeValue(null, "display"),])
144 881 mdecorde
                                        }
145 881 mdecorde
                                        else if (localname == "pattributes") {
146 881 mdecorde
                                                startPAttributes = true;
147 881 mdecorde
                                        } else if(localname == "pattribute" && startPAttributes) {
148 881 mdecorde
                                                pattributes.put(parser.getAttributeValue(null, "id"),
149 881 mdecorde
                                                        ["id":parser.getAttributeValue(null, "id"),
150 881 mdecorde
                                                        "shortname":parser.getAttributeValue(null, "shortname"),
151 881 mdecorde
                                                        "longname":parser.getAttributeValue(null, "longname"),
152 881 mdecorde
                                                        "type":parser.getAttributeValue(null, "type"),
153 881 mdecorde
                                                        "renderer":parser.getAttributeValue(null, "renderer"),
154 881 mdecorde
                                                        "tooltip":parser.getAttributeValue(null, "tooltip"),
155 881 mdecorde
                                                        "display":parser.getAttributeValue(null, "display"),
156 881 mdecorde
                                                        "min":parser.getAttributeValue(null, "min"),
157 881 mdecorde
                                                        "max":parser.getAttributeValue(null, "max"),
158 881 mdecorde
                                                        "inputFormat":parser.getAttributeValue(null, "inputFormat"),
159 881 mdecorde
                                                        "outputFormat":parser.getAttributeValue(null, "outputFormat"),
160 881 mdecorde
                                                        "position":parser.getAttributeValue(null, "position"),
161 881 mdecorde
                                                        "sortBy":parser.getAttributeValue(null, "sortBy")])
162 881 mdecorde
                                        }
163 881 mdecorde
                                        else if (localname == "sattributes") {
164 881 mdecorde
                                                startSAttributes = true;
165 881 mdecorde
                                        }
166 881 mdecorde
                                        else if (localname == "sattribute" && startSAttributes) {
167 881 mdecorde
                                                sattributes.put(parser.getAttributeValue(null, "id"),
168 881 mdecorde
                                                        ["id":parser.getAttributeValue(null, "id"),
169 881 mdecorde
                                                        "shortname":parser.getAttributeValue(null, "shortname"),
170 881 mdecorde
                                                        "longname":parser.getAttributeValue(null, "longname"),
171 881 mdecorde
                                                        "type":parser.getAttributeValue(null, "type"),
172 881 mdecorde
                                                        "renderer":parser.getAttributeValue(null, "renderer"),
173 881 mdecorde
                                                        "tooltip":parser.getAttributeValue(null, "tooltip"),
174 881 mdecorde
                                                        "pattern":parser.getAttributeValue(null, "renderer"),
175 881 mdecorde
                                                        "import":parser.getAttributeValue(null, "import"),
176 881 mdecorde
                                                        "min":parser.getAttributeValue(null, "min"),
177 881 mdecorde
                                                        "max":parser.getAttributeValue(null, "max"),
178 881 mdecorde
                                                        "mandatory":parser.getAttributeValue(null, "mandatory"),
179 881 mdecorde
                                                        "order":parser.getAttributeValue(null, "order"),
180 881 mdecorde
                                                        "inputFormat":parser.getAttributeValue(null, "inputFormat"),
181 881 mdecorde
                                                        "outputFormat":parser.getAttributeValue(null, "outputFormat"),])
182 881 mdecorde
                                        } else if (localname == "link" && startAlignInfos) {
183 881 mdecorde
                                                String target = parser.getAttributeValue(null, "target");
184 881 mdecorde
                                                String struct = parser.getAttributeValue(null, "alignElement");
185 881 mdecorde
                                                String level = parser.getAttributeValue(null, "alignLevel")
186 881 mdecorde
                                                ArrayList<String> corpora = target.split("#");
187 881 mdecorde
                                                for (int i = 0 ; i < corpora.size() ; i++) {
188 881 mdecorde
                                                        corpora[i] = corpora[i].trim()
189 881 mdecorde
                                                        if (corpora[i].length() == 0) {
190 881 mdecorde
                                                                corpora.remove(i)
191 881 mdecorde
                                                                i--
192 881 mdecorde
                                                        }
193 881 mdecorde
                                                }
194 881 mdecorde
195 881 mdecorde
                                                if (level == null)
196 881 mdecorde
                                                        level = "1";
197 881 mdecorde
198 881 mdecorde
                                                if (corpora.size() == 0) {
199 881 mdecorde
                                                        println "no corpus aligned: "+parser.getLocation()
200 881 mdecorde
                                                        return false;
201 881 mdecorde
                                                }
202 881 mdecorde
                                                if (struct == null) {
203 881 mdecorde
                                                        println "a sturcture is missing: "+parser.getLocation()
204 881 mdecorde
                                                        return false;
205 881 mdecorde
                                                }
206 881 mdecorde
                                                links.put(target, corpora)
207 881 mdecorde
                                                structs.put(target, struct)
208 881 mdecorde
                                                levels.put(target, level.toInteger())
209 881 mdecorde
                                        } else if(localname == "linkGrp" && parser.getAttributeValue(null, "type") == "align") {
210 881 mdecorde
                                                startAlignInfos = true;
211 881 mdecorde
                                        } else if(localname == "tokenizer") {
212 881 mdecorde
                                                startSAttributes = true;
213 881 mdecorde
                                        }
214 881 mdecorde
                        }
215 881 mdecorde
                }
216 881 mdecorde
        }
217 881 mdecorde
218 881 mdecorde
        public Set<String> getAlignements()
219 881 mdecorde
        {
220 881 mdecorde
                return links.keySet();
221 881 mdecorde
        }
222 881 mdecorde
223 881 mdecorde
        public ArrayList<String> getTargets(String alignement)
224 881 mdecorde
        {
225 881 mdecorde
                return links.get(alignement);
226 881 mdecorde
        }
227 881 mdecorde
228 881 mdecorde
        public String getStructure(String alignement)
229 881 mdecorde
        {
230 881 mdecorde
                return structs.get(alignement);
231 881 mdecorde
        }
232 881 mdecorde
233 881 mdecorde
        public int getLevel(String alignement)
234 881 mdecorde
        {
235 881 mdecorde
                return levels.get(alignement);
236 881 mdecorde
        }
237 881 mdecorde
238 881 mdecorde
        public void dump()
239 881 mdecorde
        {
240 881 mdecorde
                println "LINKS"
241 881 mdecorde
                for(String link : links.keySet())
242 881 mdecorde
                {
243 881 mdecorde
                        println link
244 881 mdecorde
                        println " corpus: "+links.get(link)
245 881 mdecorde
                        println " struct: "+structs.get(link)
246 881 mdecorde
                        println " level: "+levels.get(link)
247 881 mdecorde
                }
248 881 mdecorde
249 881 mdecorde
                println "PROPERTIES"
250 2242 mdecorde
                println "metadata $metadatas"
251 881 mdecorde
                println " pattributes $pattributes"
252 881 mdecorde
                println " sattributes $sattributes"
253 881 mdecorde
254 881 mdecorde
                println " biblios $biblios"
255 881 mdecorde
256 881 mdecorde
                println "uis $uis"
257 881 mdecorde
258 881 mdecorde
                println " editions $editions"
259 881 mdecorde
260 881 mdecorde
                println "HEADER"
261 881 mdecorde
                println " basename $basename"
262 881 mdecorde
                println " encoding $encoding"
263 881 mdecorde
                println " language $language"
264 881 mdecorde
                println " rootDir $rootDir"
265 881 mdecorde
                println " script $script"
266 881 mdecorde
                println " xslt $xslt"
267 881 mdecorde
                println " author $author"
268 881 mdecorde
                println " description $description"
269 881 mdecorde
                println "DEBUG: $debug"
270 881 mdecorde
        }
271 881 mdecorde
272 881 mdecorde
        public static void main(String[] args)
273 881 mdecorde
        {
274 881 mdecorde
                String userhome = System.getProperty("user.home");
275 881 mdecorde
                File alignfile = new File(userhome, "xml/xmltxmpara/align.xml")
276 881 mdecorde
                ReadImportParameters parameters = new ReadImportParameters();
277 881 mdecorde
                parameters.read(alignfile);
278 881 mdecorde
                parameters.dump();
279 881 mdecorde
        }
280 881 mdecorde
}