Statistics
| Revision:

root / tmp / org.txm.groovy.core / src / groovy / org / txm / importer / ReadImportParameters.groovy @ 966

History | View | Annotate | Download (8.4 kB)

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