Révision 2937

tmp/org.txm.utils/src/org/txm/utils/i18n/LangFormater.java (revision 2937)
88 88

  
89 89
		if (lang.startsWith("fr") || lang.startsWith("FR")) //$NON-NLS-1$ //$NON-NLS-2$
90 90
			return (FRNoSpaceBefore);
91
		else if (lang.startsWith("fro") || lang.startsWith("FRO")) //$NON-NLS-1$ //$NON-NLS-2$
92
			return (FRONoSpaceBefore);
91 93
		else if (lang.startsWith("en") || lang.startsWith("EN") //$NON-NLS-1$ //$NON-NLS-2$
92 94
				|| lang.startsWith("us") || lang.startsWith("US")) //$NON-NLS-1$ //$NON-NLS-2$
93 95
			return (ENNoSpaceBefore);
......
106 108

  
107 109
		if (lang.startsWith("fr") || lang.startsWith("FR")) //$NON-NLS-1$ //$NON-NLS-2$
108 110
			return (FRNoSpaceAfter);
111
		else if (lang.startsWith("fro") || lang.startsWith("FRO")) //$NON-NLS-1$ //$NON-NLS-2$
112
			return (FRONoSpaceAfter);
109 113
		else if (lang.startsWith("en") || lang.startsWith("EN") //$NON-NLS-1$ //$NON-NLS-2$
110 114
				|| lang.startsWith("us") || lang.startsWith("US")) //$NON-NLS-1$ //$NON-NLS-2$
111 115
			return (ENNoSpaceAfter);
tmp/org.txm.analec.rcp/src/visuAnalec/fichiers/FichiersGlozz.java (revision 2937)
20 20
 * @author Bernard
21 21
 */
22 22
public abstract class FichiersGlozz {
23
  public static void convertirCorpusGlozz(final Corpus corpus) {
24
    File repGlozz;
25
    final File repJava;
26
    if ((repGlozz = Fichiers.choisirRepertoire("Choix du répertoire Glozz"))==null)
27
      return;
28
    if ((repJava = Fichiers.choisirRepertoire("Choix du répertoire Java"))==null)
29
      return;
30
    if (!repJava.exists()) {
31
      if (!GMessages.confirmationAction("Le répertoire spécifié n'existe pas."
32
              +" Voulez-vous le créer ?")) return;
33
      if (!repJava.mkdir()) {
34
        GMessages.impossibiliteAction("Echec de la création du répertoire");
35
        return;
36
      }
37
    }
38
    final File[] fics = Fichiers.listerFichiers(repGlozz, Fichiers.TypeFichier.GLOZZ_CORPUS);
39
    final GMessages.GProgression progression =
40
            new GMessages.GProgression(fics.length, "Conversion", "fichiers");
41
    Thread traitement = new Thread() {
42
      @Override
43
      public void run() {
44
        try {
45
        for (int i = 0; i<fics.length; i++) {
46
          if (progression.isCanceled()) break;
47
          progression.setNbTraites(i+1);
48
          File fichierCorpus = fics[i];
49
          File fichierAnnot = Fichiers.trouverFichier(TypeFichier.GLOZZ_ANNOT, fichierCorpus);
50
          if (fichierAnnot==null) continue;
51
          importer(fichierCorpus, fichierAnnot, corpus);
52
          FichiersJava.enregistrerCorpus(corpus, Fichiers.creerFichier(repJava, TypeFichier.DOC_ANALEC, fichierCorpus));
53
//          corpus.fermer();
54
        }
55
        corpus.fermer();
56
        } catch (Throwable ex) {
57
          GMessages.erreurFatale(ex);
58
        }
59
      }
60
    };
61
    traitement.start();
62
  }
63

  
23
	
24
	public static void convertirCorpusGlozz(final Corpus corpus) {
25
		File repGlozz;
26
		final File repJava;
27
		if ((repGlozz = Fichiers.choisirRepertoire("Choix du répertoire Glozz")) == null)
28
			return;
29
		if ((repJava = Fichiers.choisirRepertoire("Choix du répertoire Java")) == null)
30
			return;
31
		if (!repJava.exists()) {
32
			if (!GMessages.confirmationAction("Le répertoire spécifié n'existe pas."
33
					+ " Voulez-vous le créer ?")) return;
34
			if (!repJava.mkdir()) {
35
				GMessages.impossibiliteAction("Echec de la création du répertoire");
36
				return;
37
			}
38
		}
39
		final File[] fics = Fichiers.listerFichiers(repGlozz, Fichiers.TypeFichier.GLOZZ_CORPUS);
40
		final GMessages.GProgression progression = new GMessages.GProgression(fics.length, "Conversion", "fichiers");
41
		Thread traitement = new Thread() {
42
			
43
			@Override
44
			public void run() {
45
				try {
46
					for (int i = 0; i < fics.length; i++) {
47
						if (progression.isCanceled()) break;
48
						progression.setNbTraites(i + 1);
49
						File fichierCorpus = fics[i];
50
						File fichierAnnot = Fichiers.trouverFichier(TypeFichier.GLOZZ_ANNOT, fichierCorpus);
51
						if (fichierAnnot == null) continue;
52
						importer(fichierCorpus, fichierAnnot, corpus);
53
						FichiersJava.enregistrerCorpus(corpus, Fichiers.creerFichier(repJava, TypeFichier.DOC_ANALEC, fichierCorpus));
54
						// corpus.fermer();
55
					}
56
					corpus.fermer();
57
				}
58
				catch (Throwable ex) {
59
					GMessages.erreurFatale(ex);
60
				}
61
			}
62
		};
63
		traitement.start();
64
	}
65
	
64 66
	public static boolean importerGlozz(Corpus corpus, File fichierCorpus, File fichierAnnot) {
65 67
		if (!importer(fichierCorpus, fichierAnnot, corpus)) {
66 68
			return false;
......
78 80
		File fichierCorpus;
79 81
		File fichierAnnot;
80 82
		if ((fichierCorpus = Fichiers.choisirFichier(Commande.OUVRIR,
81
				"Choix du fichier corpus", Fichiers.TypeFichier.GLOZZ_CORPUS))==null)
83
				"Choix du fichier corpus", Fichiers.TypeFichier.GLOZZ_CORPUS)) == null)
82 84
			return false;
83 85
		if ((fichierAnnot = Fichiers.choisirFichier(Commande.OUVRIR,
84
				"Choix du fichier annotations", Fichiers.TypeFichier.GLOZZ_ANNOT))==null)
86
				"Choix du fichier annotations", Fichiers.TypeFichier.GLOZZ_ANNOT)) == null)
85 87
			return false;
86 88
		
87 89
		return importerGlozz(corpus, fichierCorpus, fichierAnnot);
88 90
	}
89 91
	
90
  
91
  private static boolean importer(File fichierCorpus, File fichierAnnot, Corpus corpus) {
92
    BufferedReader text = null;
93
    BufferedInputStream annot = null;
94
    try {
95
      text = new BufferedReader(new InputStreamReader(new FileInputStream(fichierCorpus), "UTF-8"));
96
      annot = new BufferedInputStream(new FileInputStream(fichierAnnot));
97
      corpus.setTexte(text.readLine());
98
      SAXParserFactory pf = (SAXParserFactory) SAXParserFactory.newInstance();
99
      SAXParser sp = pf.newSAXParser();
100
      sp.parse(annot, new GlozzHandler(corpus));
101
    } catch (IOException ex) {
102
      corpus.fermer();
103
      JOptionPane.showMessageDialog(null, ex, "Erreur de lecture de fichier", JOptionPane.ERROR_MESSAGE);
104
      return false;
105
    } catch (ParserConfigurationException ex) {
106
      corpus.fermer();
107
      JOptionPane.showMessageDialog(null, ex, "Erreur de format de fichier", JOptionPane.ERROR_MESSAGE);
108
      return false;
109
    } catch (SAXException ex) {
110
      corpus.fermer();
111
      JOptionPane.showMessageDialog(null, ex, "Erreur de format de fichier", JOptionPane.ERROR_MESSAGE);
112
      return false;
113
    } finally {
114
      try {
115
        if (text!=null) text.close();
116
        if (annot!=null) annot.close();
117
      } catch (IOException ex) {
118
        JOptionPane.showMessageDialog(null, ex, "Erreur de fermeture du fichier lu", JOptionPane.ERROR_MESSAGE);
119
      }
120
    }
121
    return true;
122
  }
92
	
93
	private static boolean importer(File fichierCorpus, File fichierAnnot, Corpus corpus) {
94
		BufferedReader text = null;
95
		BufferedInputStream annot = null;
96
		try {
97
			text = new BufferedReader(new InputStreamReader(new FileInputStream(fichierCorpus), "UTF-8"));
98
			annot = new BufferedInputStream(new FileInputStream(fichierAnnot));
99
			corpus.setTexte(text.readLine());
100
			SAXParserFactory pf = SAXParserFactory.newInstance();
101
			SAXParser sp = pf.newSAXParser();
102
			sp.parse(annot, new GlozzHandler(fichierAnnot, corpus));
103
		}
104
		catch (IOException ex) {
105
			corpus.fermer();
106
			JOptionPane.showMessageDialog(null, ex, "Erreur de lecture de fichier", JOptionPane.ERROR_MESSAGE);
107
			return false;
108
		}
109
		catch (ParserConfigurationException ex) {
110
			corpus.fermer();
111
			JOptionPane.showMessageDialog(null, ex, "Erreur de format de fichier", JOptionPane.ERROR_MESSAGE);
112
			return false;
113
		}
114
		catch (SAXException ex) {
115
			corpus.fermer();
116
			JOptionPane.showMessageDialog(null, ex, "Erreur de format de fichier", JOptionPane.ERROR_MESSAGE);
117
			return false;
118
		}
119
		finally {
120
			try {
121
				if (text != null) text.close();
122
				if (annot != null) annot.close();
123
			}
124
			catch (IOException ex) {
125
				JOptionPane.showMessageDialog(null, ex, "Erreur de fermeture du fichier lu", JOptionPane.ERROR_MESSAGE);
126
			}
127
		}
128
		return true;
129
	}
130
	
123 131
	public static boolean importerModeleGlozz(Corpus corpus, File fichierModele) {
124 132
		BufferedInputStream modele = null;
125 133
		
126 134
		try {
127 135
			modele = new BufferedInputStream(new FileInputStream(fichierModele));
128
			SAXParserFactory pf = (SAXParserFactory) SAXParserFactory.newInstance();
136
			SAXParserFactory pf = SAXParserFactory.newInstance();
129 137
			SAXParser sp = pf.newSAXParser();
130 138
			Structure structureLue = new Structure();
131
			sp.parse(modele, new ModeleGlozzHandler(structureLue));
139
			sp.parse(modele, new ModeleGlozzHandler(fichierModele, structureLue));
132 140
			corpus.fusionnerStructureLue(structureLue);
133 141
			corpus.newCorpusLu();
134
		} catch (IOException ex) {
142
		}
143
		catch (IOException ex) {
135 144
			JOptionPane.showMessageDialog(null, ex, "Erreur de lecture de fichier", JOptionPane.ERROR_MESSAGE);
136 145
			return false;
137
		} catch (ParserConfigurationException ex) {
146
		}
147
		catch (ParserConfigurationException ex) {
138 148
			JOptionPane.showMessageDialog(null, ex, "Erreur de format de fichier", JOptionPane.ERROR_MESSAGE);
139 149
			return false;
140
		} catch (SAXException ex) {
150
		}
151
		catch (SAXException ex) {
141 152
			JOptionPane.showMessageDialog(null, ex, "Erreur de format de fichier", JOptionPane.ERROR_MESSAGE);
142 153
			return false;
143
		} finally {
154
		}
155
		finally {
144 156
			try {
145
				if (modele!=null) modele.close();
146
			} catch (IOException ex) {
157
				if (modele != null) modele.close();
158
			}
159
			catch (IOException ex) {
147 160
				JOptionPane.showMessageDialog(null, ex, "Erreur de fermeture du fichier lu", JOptionPane.ERROR_MESSAGE);
148 161
			}
149 162
		}
......
153 166
	public static boolean importerModeleGlozz(Corpus corpus) {
154 167
		File fichierModele;
155 168
		if ((fichierModele = Fichiers.choisirFichier(Commande.OUVRIR,
156
				"Choix du fichier modèle", Fichiers.TypeFichier.GLOZZ_MODELE))==null)
169
				"Choix du fichier modèle", Fichiers.TypeFichier.GLOZZ_MODELE)) == null)
157 170
			return false;
158 171
		
159 172
		return importerModeleGlozz(corpus, fichierModele);
160 173
	}
161
  public static boolean exporterGlozz(Corpus corpus) {
162
    PrintWriter text = null;
163
    PrintWriter annot = null;
164
    PrintWriter model = null;
165
    File fichierCorpus;
166
    File fichierAnnot;
167
    File fichierModel;
168
    if ((fichierCorpus = Fichiers.choisirFichier(Commande.ENREGISTRER,
169
            "Nom du fichier corpus", Fichiers.TypeFichier.GLOZZ_CORPUS))==null) return false;
170
    if ((fichierAnnot = Fichiers.choisirFichier(Commande.ENREGISTRER,
171
            "Nom du fichier annotations", Fichiers.TypeFichier.GLOZZ_ANNOT))==null)
172
      return false;
173
    try {
174
      text = new PrintWriter(fichierCorpus, "UTF-8");
175
      annot = new PrintWriter(fichierAnnot, "UTF-8");
176
      text.print(corpus.getTexte());
177
      GlozzPrinter gp = new GlozzPrinter(corpus, annot);
178
      gp.ecrireAnnot();
179
    } catch (FileNotFoundException ex) {
180
      JOptionPane.showMessageDialog(null, ex, "Erreur d'écriture de fichier", JOptionPane.ERROR_MESSAGE);
181
      return false;
182
    } catch (UnsupportedEncodingException ex) {
183
      JOptionPane.showMessageDialog(null, ex, "Erreur d'écriture de fichier", JOptionPane.ERROR_MESSAGE);
184
      return false;
185
    } finally {
186
      if (text!=null) text.close();
187
      if (annot!=null) annot.close();
188
    }
189
    if ((fichierModel = Fichiers.choisirFichier(Commande.ENREGISTRER,
190
            "Exporter aussi le modèle ?", Fichiers.TypeFichier.GLOZZ_MODELE))==null)
191
      return true;
192
    try {
193
      model = new PrintWriter(fichierModel, "UTF-8");
194
      ModelGlozzPrinter gp = new ModelGlozzPrinter(corpus.getStructure(), model);
195
      gp.ecrireModel();
196
    } catch (FileNotFoundException ex) {
197
      JOptionPane.showMessageDialog(null, ex, "Erreur d'écriture de fichier", JOptionPane.ERROR_MESSAGE);
198
    } catch (UnsupportedEncodingException ex) {
199
      JOptionPane.showMessageDialog(null, ex, "Erreur d'écriture de fichier", JOptionPane.ERROR_MESSAGE);
200
    } finally {
201
      if (model!=null) model.close();
202
    }
203
    return true;
204
  }
205
  
206
  public static boolean exporterModeleGlozz(Structure structure) {
207
    PrintWriter model = null;
208
    File fichierModel;
209
    if ((fichierModel = Fichiers.choisirFichier(Commande.ENREGISTRER,
210
            "Nom du fichier modèle ?", Fichiers.TypeFichier.GLOZZ_MODELE))==null)
211
      return false;
212
    try {
213
      model = new PrintWriter(fichierModel, "UTF-8");
214
      ModelGlozzPrinter gp = new ModelGlozzPrinter(structure, model);
215
      gp.ecrireModel();
216
    } catch (Exception ex) {
217
      JOptionPane.showMessageDialog(null, ex, "Erreur d'écriture de fichier", JOptionPane.ERROR_MESSAGE);
218
      return false;
219
    } finally {
220
      if (model!=null) model.close();
221
    }
222
    return true;
223
  }
224

  
225
  static class GlozzHandler extends DefaultHandler {
226
    Corpus corpus;
227
    Balise etatCourant = null;
228
    Element elementCourant = null;
229
    String champCourant = null;
230
    String valCourant = null;
231
    HashMap<String, Element> elements = new HashMap<String, Element>();
232
    ArrayList<Relation> relations = new ArrayList<Relation>();
233
    HashMap<Relation, ArrayList<String>> idTermes = new HashMap<Relation, ArrayList<String>>();
234
    ArrayList<Schema> schemas = new ArrayList<Schema>();
235
    HashMap<Schema, ArrayList<String>> idContenus = new HashMap<Schema, ArrayList<String>>();
236

  
237
    enum Balise {
238
      ANNOTATIONS, UNIT, RELATION, SCHEMA, METADATA, AUTHOR, CREATIONDATE,
239
      LASTMODIFIER, LASTMODIFICATIONDATE, CHARACTERISATION, TYPE, FEATURESET,
240
      FEATURE, POSITIONING, START, END, SINGLEPOSITION, TERM,
241
      EMBEDDEDUNIT, EMBEDDEDRELATION, EMBEDDEDSCHEMA,
242
      FLAG, COMMENT
243
    };
244
    static Balise balise(String nomBalise) {
245
      return Balise.valueOf(nomBalise.replaceAll("\\W", "").toUpperCase());
246
    }
247
    GlozzHandler(Corpus corpus) {
248
      super();
249
      this.corpus = corpus;
250
    }
251
    @Override
252
    public void startDocument() {
253
      corpus.clearAnnot();
254
    }
255
    @Override
256
    public void endDocument() throws SAXNotSupportedException {
257
      for (Relation rel : relations) {
258
        ArrayList<String> idterms = idTermes.get(rel);
259
        if (idterms.size()!=2) {
260
          throw new SAXNotSupportedException("Une relation de type "+rel.getType()+" n'a pas deux termes");
261
        }
262
        if (elements.get(idterms.get(0))==null) {
263
          throw new SAXNotSupportedException("Terme inconnu id = "+idterms.get(0)
264
                  +" pour une relation de type "+rel.getType());
265
        }
266
        rel.setElt1(elements.get(idterms.get(0)));
267
        if (elements.get(idterms.get(1))==null) {
268
          throw new SAXNotSupportedException("Terme inconnu id = "+idterms.get(1)
269
                  +" pour une relation de type "+rel.getType());
270
        }
271
        rel.setElt2(elements.get(idterms.get(1)));
272
        corpus.addRelationLue(rel);
273
      }
274
      for (Schema sch : schemas) {
275
        for (String id : idContenus.get(sch)) {
276
          if (elements.get(id)==null) {
277
            throw new SAXNotSupportedException("Contenu inconnu id = "+id
278
                    +" pour un schéma de type "+sch.getType());
279
          }
280
          sch.ajouter(elements.get(id));
281
        }
282
        corpus.addSchemaLu(sch);
283
      }
284
      corpus.verifierFinParagraphesTypesUnites(new String[]{
285
                "title", "subTitle", "sectionTitle", "subSectionTitle",
286
                "subSubSectionTitle", "subSubSubSectionTitle", "quote", "listItem"});
287
      corpus.trierFinParagraphes();
288
      corpus.verifierFinParagrapheFinTexte();
289
    }
290
    @Override
291
    public void startElement(String uri, String local, String nomBalise, Attributes attributs) throws SAXException {
292
      if (etatCourant==Balise.FLAG) return;
293
      switch (balise(nomBalise)) {
294
        case FLAG:
295
          etatCourant = Balise.FLAG;
296
          break;
297
        case UNIT:
298
          elementCourant = new Unite();
299
          elements.put(attributs.getValue("id"), elementCourant);
300
          break;
301
        case RELATION:
302
          elementCourant = new Relation();
303
          elements.put(attributs.getValue("id"), elementCourant);
304
          idTermes.put((Relation) elementCourant, new ArrayList<String>());
305
          break;
306
        case SCHEMA:
307
          elementCourant = new Schema();
308
          elements.put(attributs.getValue("id"), elementCourant);
309
          idContenus.put((Schema) elementCourant, new ArrayList<String>());
310
          break;
311
        case TYPE:
312
          etatCourant = Balise.TYPE;
313
          valCourant = "";
314
          break;
315
        case FEATURE:
316
          etatCourant = Balise.FEATURE;
317
          champCourant = attributs.getValue("name");
318
          valCourant = "";
319
          break;
320
        case START:
321
          etatCourant = Balise.START;
322
          break;
323
        case END:
324
          etatCourant = Balise.END;
325
          break;
326
        case SINGLEPOSITION:
327
          if (elementCourant.getClass()!=Unite.class) {
328
            throw new SAXNotSupportedException("La balise singlePosition ne peut être utilisée que pour des unités");
329
          }
330
          if (elementCourant.getType().equals("paragraph")) {
331
            if (etatCourant==Balise.END) {
332
              corpus.addFinParagraphe(Integer.parseInt(attributs.getValue("index")));
333
            }
334
          } else {
335
            switch (etatCourant) {
336
              case START:
337
                ((Unite) elementCourant).setDeb(Integer.parseInt(attributs.getValue("index")));
338
                break;
339
              case END:
340
                ((Unite) elementCourant).setFin(Integer.parseInt(attributs.getValue("index")));
341
                break;
342
            }
343
          }
344
          break;
345
        case TERM:
346
          if (elementCourant.getClass()!=Relation.class)
347
            throw new SAXNotSupportedException("La balise term ne peut être utilisée que pour des relations");
348
          idTermes.get((Relation) elementCourant).add(attributs.getValue("id"));
349
          break;
350

  
351
        case EMBEDDEDUNIT:
352
        case EMBEDDEDRELATION:
353
        case EMBEDDEDSCHEMA:
354
          if (elementCourant.getClass()!=Schema.class)
355
            throw new SAXNotSupportedException(
356
                    "Les balises embedded ne peuvent être utilisées que pour des schémas");
357
          idContenus.get((Schema) elementCourant).add(attributs.getValue("id"));
358
          break;
359
      }
360
    }
361
    @Override
362
    public void endElement(String uri, String local, String nomBalise) throws SAXException {
363
      if (etatCourant==Balise.FLAG) {
364
        if (balise(nomBalise)==Balise.FLAG) etatCourant = null;
365
        return;
366
      }
367
      String type = "";
368
      switch (balise(nomBalise)) {
369
        case UNIT:
370
          type = elementCourant.getType();
371
          if (type==null||type.isEmpty())
372
            throw new SAXNotSupportedException("Pas trouvé de type pour cette unité");
373
          if (!type.equals("paragraph")) {
374
            corpus.addUniteLue((Unite) elementCourant);
375
            elementCourant = null;
376
          }
377
          break;
378
        case RELATION:
379
          type = elementCourant.getType();
380
          if (type==null||type.isEmpty())
381
            throw new SAXNotSupportedException("Pas trouvé de type pour cette relation");
382
          relations.add((Relation) elementCourant);
383
          elementCourant = null;
384
          break;
385
        case SCHEMA:
386
          type = elementCourant.getType();
387
          if (type==null||type.isEmpty())
388
            throw new SAXNotSupportedException("Pas trouvé de type pour ce shéma");
389
          schemas.add((Schema) elementCourant);
390
          elementCourant = null;
391
          break;
392
        case TYPE:
393
          elementCourant.setType(valCourant);
394
          valCourant = null;
395
          etatCourant = null;
396
          break;
397
        case FEATURE:
398
          elementCourant.putProp(champCourant, valCourant);
399
          etatCourant = null;
400
          champCourant = null;
401
          valCourant = null;
402
          break;
403
        case START:
404
          etatCourant = null;
405
          break;
406
        case END:
407
          etatCourant = null;
408
          break;
409
      }
410
    }
411
    @Override
412
    public void characters(char[] ch, int start, int taille) throws SAXException {
413
      if (etatCourant==null||!EnumSet.of(Balise.TYPE, Balise.FEATURE).contains(etatCourant)) {
414
        return;
415
      }
416
      valCourant += new String(ch, start, taille);
417
    }
418
  }
419

  
420
  static class ModeleGlozzHandler extends DefaultHandler {
421
    Structure structure;
422
    boolean listeValeurs = false;
423
    Class<? extends Element> classeCourante = Unite.class;  // par défaut : balise <units> sous-entendue 
424
    String typeCourant = null;
425
    String champCourant = null;
426
    String valeurCourante = null;
427

  
428
    enum Balise {
429
      ANNOTATIONMODEL, UNITS, RELATIONS, SCHEMAS, TYPE, FEATURESET,
430
      FEATURE, POSSIBLEVALUES, VALUE
431
    };
432
    static Balise balise(String nomBalise) throws SAXNotRecognizedException {
433
      try {
434
        return Balise.valueOf(nomBalise.replaceAll("\\W", "").toUpperCase());
435
      } catch (Exception e) {
436
        throw new SAXNotRecognizedException("Balise non reconnue : "+nomBalise);
437
      }
438
    }
439
    ModeleGlozzHandler(Structure structure) {
440
      super();
441
      this.structure = structure;
442
    }
443
    @Override
444
    public void startDocument() {
445
    }
446
    @Override
447
    public void endDocument() throws SAXNotSupportedException {
448
    }
449
    @Override
450
    public void startElement(String uri, String local, String nomBalise, Attributes attributs) throws SAXException {
451
      String valdef = null;
452
      switch (balise(nomBalise)) {
453
        case UNITS:
454
          classeCourante = Unite.class;
455
          break;
456
        case RELATIONS:
457
          classeCourante = Relation.class;
458
          break;
459
        case SCHEMAS:
460
          classeCourante = Schema.class;
461
          break;
462
        case TYPE:
463
          typeCourant = attributs.getValue("name");
464
          if (typeCourant==null||typeCourant.isEmpty())
465
            throw new SAXNotSupportedException("Nom de type absent");
466
          structure.ajouterType(classeCourante, typeCourant);
467
          break;
468
        case FEATURE:
469
          if (typeCourant==null||typeCourant.isEmpty())
470
            throw new SAXNotSupportedException("Nom de type absent");
471
          champCourant = attributs.getValue("name");
472
          if (champCourant==null||champCourant.isEmpty())
473
            throw new SAXNotSupportedException("Nom de type absent");
474
          structure.ajouterProp(classeCourante, typeCourant, champCourant);
475
          break;
476
        case POSSIBLEVALUES:
477
          if (typeCourant==null||typeCourant.isEmpty()||champCourant==null||champCourant.isEmpty())
478
            throw new SAXNotSupportedException("Nom de type ou de feature absent");
479
          listeValeurs = true;
480
          valdef = attributs.getValue("default");
481
          if (valdef!=null&&!valdef.isEmpty())
482
            structure.modifierValParDefaut(classeCourante, typeCourant, champCourant, valdef);
483
          break;
484
        case VALUE:
485
          if (listeValeurs) valeurCourante = "";
486
          valdef = attributs.getValue("default");
487
          if (valdef!=null&&!valdef.isEmpty()) {
488
            if (typeCourant==null||typeCourant.isEmpty()||champCourant==null||champCourant.isEmpty())
489
              throw new SAXNotSupportedException("Nom de type ou de feature absent");
490
            structure.ajouterVal(classeCourante, typeCourant, champCourant, valdef);
491
            structure.modifierValParDefaut(classeCourante, typeCourant, champCourant, valdef);
492
          }
493
          break;
494
      }
495
    }
496
    @Override
497
    public void endElement(String uri, String local, String nomBalise) throws SAXException {
498
      switch (balise(nomBalise)) {
499
        case POSSIBLEVALUES:
500
          listeValeurs = false;
501
          break;
502
        case VALUE:
503
          if (listeValeurs&&valeurCourante!=null&&!valeurCourante.isEmpty())
504
            structure.ajouterVal(classeCourante, typeCourant, champCourant, valeurCourante);
505
          break;
506
      }
507
    }
508
    @Override
509
    public void characters(char[] ch, int start, int taille) throws SAXException {
510
      if (listeValeurs) {
511
        valeurCourante += new String(ch, start, taille);
512
      }
513
    }
514
  }
515

  
516
  static class GlozzPrinter {
517
    Corpus corpus;
518
    PrintWriter annot;
519
    HashMap<Element, String> idDates = new HashMap<Element, String>();
520
    String idAuteur = "analec0.2";
521
    String idDateParagraphes = "0";
522
    String idParagraphes = idAuteur+"_"+idDateParagraphes;
523
    GlozzPrinter(Corpus corpus, PrintWriter annot) {
524
      this.corpus = corpus;
525
      this.annot = annot;
526
      attribueIdDates();
527
    }
528
    void attribueIdDates() {
529
      long idDate = (new Date()).getTime();
530
      for (Unite unite : corpus.getToutesUnites()) {
531
        idDates.put(unite, Long.toString(idDate++));
532
      }
533
      for (Relation relation : corpus.getToutesRelations()) {
534
        idDates.put(relation, Long.toString(idDate++));
535
      }
536
      for (Schema schema : corpus.getTousSchemas()) {
537
        idDates.put(schema, Long.toString(idDate++));
538
      }
539
    }
540
    String getId(Element elt) {
541
      return idAuteur+"_"+idDates.get(elt);
542
    }
543
    void ecrireAnnot() {
544
      annot.println("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>");
545
      annot.println("<annotations>");
546
      ecrireFinParagraphes();
547
      ecrireUnites();
548
      ecrireRelations();
549
      ecrireSchemas();
550
      annot.println("</annotations>");
551
    }
552
    private void ecrireFinParagraphes() {
553
      int deb = 0;
554
      for (int fin : corpus.getFinParagraphes()) {
555
        ecrireUnite("paragraph", idAuteur, idDateParagraphes, idParagraphes, new HashMap<String, String>(), deb, fin);
556
        deb = fin;
557
      }
558
    }
559
    private void ecrireUnites() {
560
      for (Unite unit : corpus.getToutesUnites()) {
561
        ecrireUnite(unit.getType(), idAuteur, idDates.get(unit), getId(unit), unit.getProps(), unit.getDeb(), unit.getFin());
562
      }
563
    }
564
    private void ecrireRelations() {
565
      for (Relation rel : corpus.getToutesRelations()) {
566
        ecrireRelation(rel.getType(), idAuteur, idDates.get(rel), getId(rel), rel.getProps(), rel.getElt1(), rel.getElt2());
567
      }
568
    }
569
    private void ecrireSchemas() {
570
      for (Schema sch : corpus.getTousSchemas()) {
571
        ecrireSchema(sch.getType(), idAuteur, idDates.get(sch), getId(sch), sch.getProps(), sch.getContenu());
572
      }
573
    }
574
    private void ecrireUnite(String type, String idAuteur, String idDate, String id, HashMap<String, String> props, int deb, int fin) {
575
      annot.println("<unit id=\""+id+"\">");
576
      ecrireMetadata(idAuteur, idDate);
577
      ecrireProps(type, props);
578
      ecrirePos(deb, fin);
579
      annot.println("</unit>");
580
    }
581
    private void ecrireRelation(String type, String idAuteur, String idDate, String id, HashMap<String, String> props, Element elt1, Element elt2) {
582
      annot.println("<relation id=\""+id+"\">");
583
      ecrireMetadata(idAuteur, idDate);
584
      ecrireProps(type, props);
585
      ecrireRel(elt1, elt2);
586
      annot.println("</relation>");
587
    }
588
    private void ecrireSchema(String type, String idAuteur, String idDate, String id, HashMap<String, String> props, HashSet<Element> elts) {
589
      annot.println("<schema id=\""+id+"\">");
590
      ecrireMetadata(idAuteur, idDate);
591
      ecrireProps(type, props);
592
      ecrireSch(elts);
593
      annot.println("</schema>");
594
    }
595
    private void ecrireMetadata(String idAuteur, String id) {
596
      annot.println("<metadata>");
597
      annot.println("<author>"+idAuteur+"</author>");
598
      annot.println("<creation-date>"+id+"</creation-date>");
599
      annot.println("</metadata>");
600
    }
601
    private void ecrireProps(String type, HashMap<String, String> props) {
602
      annot.println("<characterisation>");
603
      annot.println("<type>"+encodeXMLTexte(type)+"</type>");
604
      if (props.isEmpty()) {
605
        annot.println("<featureSet/>");
606
      } else {
607
        annot.println("<featureSet>");
608
        for (String prop : props.keySet()) {
609
          if (props.get(prop)!=null) {
610
            annot.println("<feature name =\""+encodeXMLTexte(prop)+"\">"
611
                    +encodeXMLTexte(props.get(prop))+"</feature>");
612
          }
613
        }
614
        annot.println("</featureSet>");
615

  
616
      }
617
      annot.println("</characterisation>");
618
    }
619
    private void ecrirePos(int deb, int fin) {
620
      annot.println("<positioning>");
621
      annot.println("<start>");
622
      annot.println("<singlePosition index = \""+deb+"\"/>");
623
      annot.println("</start>");
624
      annot.println("<end>");
625
      annot.println("<singlePosition index = \""+fin+"\"/>");
626
      annot.println("</end>");
627
      annot.println("</positioning>");
628
    }
629
    private void ecrireRel(Element elt1, Element elt2) {
630
      annot.println("<positioning>");
631
      annot.println("<term id = \""+getId(elt1)+"\"/>");
632
      annot.println("<term id = \""+getId(elt2)+"\"/>");
633
      annot.println("</positioning>");
634
    }
635
    private void ecrireSch(HashSet<Element> elts) {
636
      annot.println("<positioning>");
637
      for (Element elt : elts) {
638
        Class classe = elt.getClass();
639
        if (classe==Unite.class) {
640
          annot.print("<embedded-unit ");
641
        }
642
        if (classe==Relation.class) {
643
          annot.print("<embedded-relation ");
644
        }
645
        if (classe==Schema.class) {
646
          annot.print("<embedded-schema ");
647
        }
648
        annot.println("id = \""+getId(elt)+"\"/>");
649
      }
650
      annot.println("</positioning>");
651
    }
652
  }
653

  
654
  public static class ModelGlozzPrinter {
655
    Structure structure;
656
    PrintWriter model;
657
    public ModelGlozzPrinter(Structure structure, PrintWriter model) {
658
      this.structure = structure;
659
      this.model = model;
660
    }
661
    public void ecrireModel() {
662
      model.println("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>");
663
      model.println("<annotationModel>");
664
      ecrireUnites();
665
      ecrireRelations();
666
      ecrireSchemas();
667
      model.println("</annotationModel>");
668
    }
669
    private void ecrireUnites() {
670
      model.println("<units>");
671
      for (String type : structure.getTypes(Unite.class))
672
        ecrireType(Unite.class, type);
673
      model.println("</units>");
674
    }
675
    private void ecrireRelations() {
676
      model.println("<relations>");
677
      for (String type : structure.getTypes(Relation.class))
678
        ecrireType(Relation.class, type);
679
      model.println("</relations>");
680
    }
681
    private void ecrireSchemas() {
682
      model.println("<schemas>");
683
      for (String type : structure.getTypes(Schema.class))
684
        ecrireType(Schema.class, type);
685
      model.println("</schemas>");
686
    }
687
    private void ecrireType(Class<? extends Element> classe, String type) {
688
      model.print("<type name=\""+encodeXMLTexte(type)+"\"");
689
      if (classe==Relation.class) model.print(" oriented=\"true\"");
690
      model.println(">");
691
      model.println("<featureSet>");
692
      for (String prop : structure.getNomsProps(classe, type))
693
        ecrireProp(classe, type, prop);
694
      model.println("</featureSet>");
695
      model.println("</type>");
696
    }
697
    private void ecrireProp(Class<? extends Element> classe, String type, String prop) {
698
      model.println("<feature name=\""+encodeXMLTexte(prop)+"\">");
699
      if (structure.getValeursProp(classe, type, prop).isEmpty()) {
700
        model.println("<value type=\"free\" default=\"\" />");
701
      } else {
702
        model.println("<possibleValues default = \""
703
                +structure.getValeurParDefaut(classe, type, prop)+"\">");
704
        for (String val : structure.getValeursProp(classe, type, prop))
705
          model.println("<value>"+encodeXMLTexte(val)+"</value>");
706
        model.println("</possibleValues>");
707
      }
708
      model.println("</feature>");
709

  
710
    }
711
  }
712
  static String encodeXMLTexte(String ch) {
713
//    return ch;
714
    return ch.replaceAll("&", "&amp;").replaceAll("<", "&lt;").
715
            replaceAll(">", "&gt;").replaceAll("\"", "&quot;").
716
            replaceAll("\'", "&apos;");
717
  }
174
	
175
	public static boolean exporterGlozz(Corpus corpus) {
176
		PrintWriter text = null;
177
		PrintWriter annot = null;
178
		PrintWriter model = null;
179
		File fichierCorpus;
180
		File fichierAnnot;
181
		File fichierModel;
182
		if ((fichierCorpus = Fichiers.choisirFichier(Commande.ENREGISTRER,
183
				"Nom du fichier corpus", Fichiers.TypeFichier.GLOZZ_CORPUS)) == null) return false;
184
		if ((fichierAnnot = Fichiers.choisirFichier(Commande.ENREGISTRER,
185
				"Nom du fichier annotations", Fichiers.TypeFichier.GLOZZ_ANNOT)) == null)
186
			return false;
187
		try {
188
			text = new PrintWriter(fichierCorpus, "UTF-8");
189
			annot = new PrintWriter(fichierAnnot, "UTF-8");
190
			text.print(corpus.getTexte());
191
			GlozzPrinter gp = new GlozzPrinter(corpus, annot);
192
			gp.ecrireAnnot();
193
		}
194
		catch (FileNotFoundException ex) {
195
			JOptionPane.showMessageDialog(null, ex, "Erreur d'écriture de fichier", JOptionPane.ERROR_MESSAGE);
196
			return false;
197
		}
198
		catch (UnsupportedEncodingException ex) {
199
			JOptionPane.showMessageDialog(null, ex, "Erreur d'écriture de fichier", JOptionPane.ERROR_MESSAGE);
200
			return false;
201
		}
202
		finally {
203
			if (text != null) text.close();
204
			if (annot != null) annot.close();
205
		}
206
		if ((fichierModel = Fichiers.choisirFichier(Commande.ENREGISTRER,
207
				"Exporter aussi le modèle ?", Fichiers.TypeFichier.GLOZZ_MODELE)) == null)
208
			return true;
209
		try {
210
			model = new PrintWriter(fichierModel, "UTF-8");
211
			ModelGlozzPrinter gp = new ModelGlozzPrinter(corpus.getStructure(), model);
212
			gp.ecrireModel();
213
		}
214
		catch (FileNotFoundException ex) {
215
			JOptionPane.showMessageDialog(null, ex, "Erreur d'écriture de fichier", JOptionPane.ERROR_MESSAGE);
216
		}
217
		catch (UnsupportedEncodingException ex) {
218
			JOptionPane.showMessageDialog(null, ex, "Erreur d'écriture de fichier", JOptionPane.ERROR_MESSAGE);
219
		}
220
		finally {
221
			if (model != null) model.close();
222
		}
223
		return true;
224
	}
225
	
226
	public static boolean exporterModeleGlozz(Structure structure) {
227
		PrintWriter model = null;
228
		File fichierModel;
229
		if ((fichierModel = Fichiers.choisirFichier(Commande.ENREGISTRER,
230
				"Nom du fichier modèle ?", Fichiers.TypeFichier.GLOZZ_MODELE)) == null)
231
			return false;
232
		try {
233
			model = new PrintWriter(fichierModel, "UTF-8");
234
			ModelGlozzPrinter gp = new ModelGlozzPrinter(structure, model);
235
			gp.ecrireModel();
236
		}
237
		catch (Exception ex) {
238
			JOptionPane.showMessageDialog(null, ex, "Erreur d'écriture de fichier", JOptionPane.ERROR_MESSAGE);
239
			return false;
240
		}
241
		finally {
242
			if (model != null) model.close();
243
		}
244
		return true;
245
	}
246
	
247
	static class GlozzHandler extends DefaultHandler {
248
		
249
		Corpus corpus;
250
		
251
		Balise etatCourant = null;
252
		
253
		Element elementCourant = null;
254
		
255
		String champCourant = null;
256
		
257
		String valCourant = null;
258
		
259
		HashMap<String, Element> elements = new HashMap<>();
260
		
261
		ArrayList<Relation> relations = new ArrayList<>();
262
		
263
		HashMap<Relation, ArrayList<String>> idTermes = new HashMap<>();
264
		
265
		ArrayList<Schema> schemas = new ArrayList<>();
266
		
267
		HashMap<Schema, ArrayList<String>> idContenus = new HashMap<>();
268
		
269
		File parsedFile;
270
		
271
		enum Balise {
272
			ANNOTATIONS, UNIT, RELATION, SCHEMA, METADATA, AUTHOR, CREATIONDATE, LASTMODIFIER, LASTMODIFICATIONDATE, CHARACTERISATION, TYPE, FEATURESET, FEATURE, POSITIONING, START, END, SINGLEPOSITION, TERM, EMBEDDEDUNIT, EMBEDDEDRELATION, EMBEDDEDSCHEMA, FLAG, COMMENT
273
			// , POSSIBLEVALUES, VALUE
274
		};
275
		
276
		Balise balise(String nomBalise) {
277
			String s = nomBalise.replaceAll("\\W", "").toUpperCase();
278
			try {
279
				return Balise.valueOf(s);
280
			}
281
			catch (java.lang.IllegalArgumentException e) {
282
				System.out.println("Error: unknown tag '" + nomBalise + "' in " + parsedFile);
283
				throw e;
284
			}
285
		}
286
		
287
		GlozzHandler(File parsedFile, Corpus corpus) {
288
			super();
289
			this.corpus = corpus;
290
			this.parsedFile = parsedFile;
291
		}
292
		
293
		@Override
294
		public void startDocument() {
295
			corpus.clearAnnot();
296
		}
297
		
298
		@Override
299
		public void endDocument() throws SAXNotSupportedException {
300
			for (Relation rel : relations) {
301
				ArrayList<String> idterms = idTermes.get(rel);
302
				if (idterms.size() != 2) {
303
					throw new SAXNotSupportedException("Une relation de type " + rel.getType() + " n'a pas deux termes");
304
				}
305
				if (elements.get(idterms.get(0)) == null) {
306
					throw new SAXNotSupportedException("Terme inconnu id = " + idterms.get(0)
307
							+ " pour une relation de type " + rel.getType());
308
				}
309
				rel.setElt1(elements.get(idterms.get(0)));
310
				if (elements.get(idterms.get(1)) == null) {
311
					throw new SAXNotSupportedException("Terme inconnu id = " + idterms.get(1)
312
							+ " pour une relation de type " + rel.getType());
313
				}
314
				rel.setElt2(elements.get(idterms.get(1)));
315
				corpus.addRelationLue(rel);
316
			}
317
			for (Schema sch : schemas) {
318
				for (String id : idContenus.get(sch)) {
319
					if (elements.get(id) == null) {
320
						throw new SAXNotSupportedException("Contenu inconnu id = " + id
321
								+ " pour un schéma de type " + sch.getType());
322
					}
323
					sch.ajouter(elements.get(id));
324
				}
325
				corpus.addSchemaLu(sch);
326
			}
327
			corpus.verifierFinParagraphesTypesUnites(new String[] {
328
					"title", "subTitle", "sectionTitle", "subSectionTitle",
329
					"subSubSectionTitle", "subSubSubSectionTitle", "quote", "listItem" });
330
			corpus.trierFinParagraphes();
331
			corpus.verifierFinParagrapheFinTexte();
332
		}
333
		
334
		@Override
335
		public void startElement(String uri, String local, String nomBalise, Attributes attributs) throws SAXException {
336
			if (etatCourant == Balise.FLAG) return;
337
			switch (balise(nomBalise)) {
338
				case FLAG:
339
					etatCourant = Balise.FLAG;
340
					break;
341
				case UNIT:
342
					elementCourant = new Unite();
343
					elements.put(attributs.getValue("id"), elementCourant);
344
					break;
345
				case RELATION:
346
					elementCourant = new Relation();
347
					elements.put(attributs.getValue("id"), elementCourant);
348
					idTermes.put((Relation) elementCourant, new ArrayList<String>());
349
					break;
350
				case SCHEMA:
351
					elementCourant = new Schema();
352
					elements.put(attributs.getValue("id"), elementCourant);
353
					idContenus.put((Schema) elementCourant, new ArrayList<String>());
354
					break;
355
				case TYPE:
356
					etatCourant = Balise.TYPE;
357
					valCourant = "";
358
					break;
359
				case FEATURE:
360
					etatCourant = Balise.FEATURE;
361
					champCourant = attributs.getValue("name");
362
					valCourant = "";
363
					break;
364
				case START:
365
					etatCourant = Balise.START;
366
					break;
367
				case END:
368
					etatCourant = Balise.END;
369
					break;
370
				case SINGLEPOSITION:
371
					if (elementCourant.getClass() != Unite.class) {
372
						throw new SAXNotSupportedException("La balise singlePosition ne peut être utilisée que pour des unités");
373
					}
374
					if (elementCourant.getType().equals("paragraph")) {
375
						if (etatCourant == Balise.END) {
376
							corpus.addFinParagraphe(Integer.parseInt(attributs.getValue("index")));
377
						}
378
					}
379
					else {
380
						switch (etatCourant) {
381
							case START:
382
								((Unite) elementCourant).setDeb(Integer.parseInt(attributs.getValue("index")));
383
								break;
384
							case END:
385
								((Unite) elementCourant).setFin(Integer.parseInt(attributs.getValue("index")));
386
								break;
387
						}
388
					}
389
					break;
390
				case TERM:
391
					if (elementCourant.getClass() != Relation.class)
392
						throw new SAXNotSupportedException("La balise term ne peut être utilisée que pour des relations");
393
					idTermes.get(elementCourant).add(attributs.getValue("id"));
394
					break;
395
				
396
				case EMBEDDEDUNIT:
397
				case EMBEDDEDRELATION:
398
				case EMBEDDEDSCHEMA:
399
					if (elementCourant.getClass() != Schema.class)
400
						throw new SAXNotSupportedException(
401
								"Les balises embedded ne peuvent être utilisées que pour des schémas");
402
					idContenus.get(elementCourant).add(attributs.getValue("id"));
403
					break;
404
				default:
405
					// System.out.println("Warning: not supported '" + nomBalise + "' tag while parsing file: " + parsedFile);
406
			}
407
		}
408
		
409
		@Override
410
		public void endElement(String uri, String local, String nomBalise) throws SAXException {
411
			if (etatCourant == Balise.FLAG) {
412
				if (balise(nomBalise) == Balise.FLAG) etatCourant = null;
413
				return;
414
			}
415
			String type = "";
416
			switch (balise(nomBalise)) {
417
				case UNIT:
418
					type = elementCourant.getType();
419
					if (type == null || type.isEmpty())
420
						throw new SAXNotSupportedException("Pas trouvé de type pour cette unité");
421
					if (!type.equals("paragraph")) {
422
						corpus.addUniteLue((Unite) elementCourant);
423
						elementCourant = null;
424
					}
425
					break;
426
				case RELATION:
427
					type = elementCourant.getType();
428
					if (type == null || type.isEmpty())
429
						throw new SAXNotSupportedException("Pas trouvé de type pour cette relation");
430
					relations.add((Relation) elementCourant);
431
					elementCourant = null;
432
					break;
433
				case SCHEMA:
434
					type = elementCourant.getType();
435
					if (type == null || type.isEmpty())
436
						throw new SAXNotSupportedException("Pas trouvé de type pour ce shéma");
437
					schemas.add((Schema) elementCourant);
438
					elementCourant = null;
439
					break;
440
				case TYPE:
441
					elementCourant.setType(valCourant);
442
					valCourant = null;
443
					etatCourant = null;
444
					break;
445
				case FEATURE:
446
					elementCourant.putProp(champCourant, valCourant);
447
					etatCourant = null;
448
					champCourant = null;
449
					valCourant = null;
450
					break;
451
				case START:
452
					etatCourant = null;
453
					break;
454
				case END:
455
					etatCourant = null;
456
					break;
457
				default:
458
					// System.out.println("Warning: not supported '" + nomBalise + "' tag while parsing: " + parsedFile);
459
			}
460
		}
461
		
462
		@Override
463
		public void characters(char[] ch, int start, int taille) throws SAXException {
464
			if (etatCourant == null || !EnumSet.of(Balise.TYPE, Balise.FEATURE).contains(etatCourant)) {
465
				return;
466
			}
467
			valCourant += new String(ch, start, taille);
468
		}
469
	}
470
	
471
	static class ModeleGlozzHandler extends DefaultHandler {
472
		
473
		Structure structure;
474
		
475
		boolean listeValeurs = false;
476
		
477
		Class<? extends Element> classeCourante = Unite.class;  // par défaut : balise <units> sous-entendue
478
		
479
		String typeCourant = null;
480
		
481
		String champCourant = null;
482
		
483
		String valeurCourante = null;
484
		
485
		File parsedFile;
486
		
487
		public enum Balise {
488
			ANNOTATIONMODEL, UNITS, RELATIONS, SCHEMAS, TYPE, FEATURESET, FEATURE, POSSIBLEVALUES, VALUE
489
		};
490
		
491
		static Balise balise(String nomBalise) throws SAXNotRecognizedException {
492
			try {
493
				return Balise.valueOf(nomBalise.replaceAll("\\W", "").toUpperCase());
494
			}
495
			catch (Exception e) {
496
				throw new SAXNotRecognizedException("Balise non reconnue : " + nomBalise);
497
			}
498
		}
499
		
500
		ModeleGlozzHandler(File parsedFile, Structure structure) {
501
			super();
502
			this.structure = structure;
503
			this.parsedFile = parsedFile;
504
		}
505
		
506
		@Override
507
		public void startDocument() {}
508
		
509
		@Override
510
		public void endDocument() throws SAXNotSupportedException {}
511
		
512
		@Override
513
		public void startElement(String uri, String local, String nomBalise, Attributes attributs) throws SAXException {
514
			String valdef = null;
515
			switch (balise(nomBalise)) {
516
				case UNITS:
517
					classeCourante = Unite.class;
518
					break;
519
				case RELATIONS:
520
					classeCourante = Relation.class;
521
					break;
522
				case SCHEMAS:
523
					classeCourante = Schema.class;
524
					break;
525
				case TYPE:
526
					typeCourant = attributs.getValue("name");
527
					if (typeCourant == null || typeCourant.isEmpty())
528
						throw new SAXNotSupportedException("Nom de type absent");
529
					structure.ajouterType(classeCourante, typeCourant);
530
					break;
531
				case FEATURE:
532
					if (typeCourant == null || typeCourant.isEmpty())
533
						throw new SAXNotSupportedException("Nom de type absent");
534
					champCourant = attributs.getValue("name");
535
					if (champCourant == null || champCourant.isEmpty())
536
						throw new SAXNotSupportedException("Nom de type absent");
537
					structure.ajouterProp(classeCourante, typeCourant, champCourant);
538
					break;
539
				case POSSIBLEVALUES:
540
					if (typeCourant == null || typeCourant.isEmpty() || champCourant == null || champCourant.isEmpty())
541
						throw new SAXNotSupportedException("Nom de type ou de feature absent");
542
					listeValeurs = true;
543
					valdef = attributs.getValue("default");
544
					if (valdef != null && !valdef.isEmpty())
545
						structure.modifierValParDefaut(classeCourante, typeCourant, champCourant, valdef);
546
					break;
547
				case VALUE:
548
					if (listeValeurs) valeurCourante = "";
549
					valdef = attributs.getValue("default");
550
					if (valdef != null && !valdef.isEmpty()) {
551
						if (typeCourant == null || typeCourant.isEmpty() || champCourant == null || champCourant.isEmpty())
552
							throw new SAXNotSupportedException("Nom de type ou de feature absent");
553
						structure.ajouterVal(classeCourante, typeCourant, champCourant, valdef);
554
						structure.modifierValParDefaut(classeCourante, typeCourant, champCourant, valdef);
555
					}
556
					break;
557
				default:
558
					// System.out.println("Warning: not supported '" + nomBalise + "' tag while parsing file: " + parsedFile);
559
			}
560
		}
561
		
562
		@Override
563
		public void endElement(String uri, String local, String nomBalise) throws SAXException {
564
			switch (balise(nomBalise)) {
565
				case POSSIBLEVALUES:
566
					listeValeurs = false;
567
					break;
568
				case VALUE:
569
					if (listeValeurs && valeurCourante != null && !valeurCourante.isEmpty())
570
						structure.ajouterVal(classeCourante, typeCourant, champCourant, valeurCourante);
571
					break;
572
				default:
573
					// System.out.println("Warning: not supported '" + nomBalise + "' tag while parsing file: " + parsedFile);
574
			}
575
		}
576
		
577
		@Override
578
		public void characters(char[] ch, int start, int taille) throws SAXException {
579
			if (listeValeurs) {
580
				valeurCourante += new String(ch, start, taille);
581
			}
582
		}
583
	}
584
	
585
	static class GlozzPrinter {
586
		
587
		Corpus corpus;
588
		
589
		PrintWriter annot;
590
		
591
		HashMap<Element, String> idDates = new HashMap<>();
592
		
593
		String idAuteur = "analec0.2";
594
		
595
		String idDateParagraphes = "0";
596
		
597
		String idParagraphes = idAuteur + "_" + idDateParagraphes;
598
		
599
		GlozzPrinter(Corpus corpus, PrintWriter annot) {
600
			this.corpus = corpus;
601
			this.annot = annot;
602
			attribueIdDates();
603
		}
604
		
605
		void attribueIdDates() {
606
			long idDate = (new Date()).getTime();
607
			for (Unite unite : corpus.getToutesUnites()) {
608
				idDates.put(unite, Long.toString(idDate++));
609
			}
610
			for (Relation relation : corpus.getToutesRelations()) {
611
				idDates.put(relation, Long.toString(idDate++));
612
			}
613
			for (Schema schema : corpus.getTousSchemas()) {
614
				idDates.put(schema, Long.toString(idDate++));
615
			}
616
		}
617
		
618
		String getId(Element elt) {
619
			return idAuteur + "_" + idDates.get(elt);
620
		}
621
		
622
		void ecrireAnnot() {
623
			annot.println("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>");
624
			annot.println("<annotations>");
625
			ecrireFinParagraphes();
626
			ecrireUnites();
627
			ecrireRelations();
628
			ecrireSchemas();
629
			annot.println("</annotations>");
630
		}
631
		
632
		private void ecrireFinParagraphes() {
633
			int deb = 0;
634
			for (int fin : corpus.getFinParagraphes()) {
635
				ecrireUnite("paragraph", idAuteur, idDateParagraphes, idParagraphes, new HashMap<String, String>(), deb, fin);
636
				deb = fin;
637
			}
638
		}
639
		
640
		private void ecrireUnites() {
641
			for (Unite unit : corpus.getToutesUnites()) {
642
				ecrireUnite(unit.getType(), idAuteur, idDates.get(unit), getId(unit), unit.getProps(), unit.getDeb(), unit.getFin());
643
			}
644
		}
645
		
646
		private void ecrireRelations() {
647
			for (Relation rel : corpus.getToutesRelations()) {
648
				ecrireRelation(rel.getType(), idAuteur, idDates.get(rel), getId(rel), rel.getProps(), rel.getElt1(), rel.getElt2());
649
			}
650
		}
651
		
652
		private void ecrireSchemas() {
653
			for (Schema sch : corpus.getTousSchemas()) {
654
				ecrireSchema(sch.getType(), idAuteur, idDates.get(sch), getId(sch), sch.getProps(), sch.getContenu());
655
			}
656
		}
657
		
658
		private void ecrireUnite(String type, String idAuteur, String idDate, String id, HashMap<String, String> props, int deb, int fin) {
659
			annot.println("<unit id=\"" + id + "\">");
660
			ecrireMetadata(idAuteur, idDate);
661
			ecrireProps(type, props);
662
			ecrirePos(deb, fin);
663
			annot.println("</unit>");
664
		}
665
		
666
		private void ecrireRelation(String type, String idAuteur, String idDate, String id, HashMap<String, String> props, Element elt1, Element elt2) {
667
			annot.println("<relation id=\"" + id + "\">");
668
			ecrireMetadata(idAuteur, idDate);
669
			ecrireProps(type, props);
670
			ecrireRel(elt1, elt2);
671
			annot.println("</relation>");
672
		}
673
		
674
		private void ecrireSchema(String type, String idAuteur, String idDate, String id, HashMap<String, String> props, HashSet<Element> elts) {
675
			annot.println("<schema id=\"" + id + "\">");
676
			ecrireMetadata(idAuteur, idDate);
677
			ecrireProps(type, props);
678
			ecrireSch(elts);
679
			annot.println("</schema>");
680
		}
681
		
682
		private void ecrireMetadata(String idAuteur, String id) {
683
			annot.println("<metadata>");
684
			annot.println("<author>" + idAuteur + "</author>");
685
			annot.println("<creation-date>" + id + "</creation-date>");
686
			annot.println("</metadata>");
687
		}
688
		
689
		private void ecrireProps(String type, HashMap<String, String> props) {
690
			annot.println("<characterisation>");
691
			annot.println("<type>" + encodeXMLTexte(type) + "</type>");
692
			if (props.isEmpty()) {
693
				annot.println("<featureSet/>");
694
			}
695
			else {
696
				annot.println("<featureSet>");
697
				for (String prop : props.keySet()) {
698
					if (props.get(prop) != null) {
699
						annot.println("<feature name =\"" + encodeXMLTexte(prop) + "\">"
700
								+ encodeXMLTexte(props.get(prop)) + "</feature>");
701
					}
702
				}
703
				annot.println("</featureSet>");
704
				
705
			}
706
			annot.println("</characterisation>");
707
		}
708
		
709
		private void ecrirePos(int deb, int fin) {
710
			annot.println("<positioning>");
711
			annot.println("<start>");
712
			annot.println("<singlePosition index = \"" + deb + "\"/>");
713
			annot.println("</start>");
714
			annot.println("<end>");
715
			annot.println("<singlePosition index = \"" + fin + "\"/>");
716
			annot.println("</end>");
717
			annot.println("</positioning>");
718
		}
719
		
720
		private void ecrireRel(Element elt1, Element elt2) {
721
			annot.println("<positioning>");
722
			annot.println("<term id = \"" + getId(elt1) + "\"/>");
723
			annot.println("<term id = \"" + getId(elt2) + "\"/>");
724
			annot.println("</positioning>");
725
		}
726
		
727
		private void ecrireSch(HashSet<Element> elts) {
728
			annot.println("<positioning>");
729
			for (Element elt : elts) {
730
				Class classe = elt.getClass();
731
				if (classe == Unite.class) {
732
					annot.print("<embedded-unit ");
733
				}
734
				if (classe == Relation.class) {
735
					annot.print("<embedded-relation ");
736
				}
737
				if (classe == Schema.class) {
738
					annot.print("<embedded-schema ");
739
				}
740
				annot.println("id = \"" + getId(elt) + "\"/>");
741
			}
742
			annot.println("</positioning>");
743
		}
744
	}
745
	
746
	public static class ModelGlozzPrinter {
747
		
748
		Structure structure;
749
		
750
		PrintWriter model;
751
		
752
		public ModelGlozzPrinter(Structure structure, PrintWriter model) {
753
			this.structure = structure;
754
			this.model = model;
755
		}
756
		
757
		public void ecrireModel() {
758
			model.println("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>");
759
			model.println("<annotationModel>");
760
			ecrireUnites();
761
			ecrireRelations();
762
			ecrireSchemas();
763
			model.println("</annotationModel>");
764
		}
765
		
766
		private void ecrireUnites() {
767
			model.println("<units>");
768
			for (String type : structure.getTypes(Unite.class))
769
				ecrireType(Unite.class, type);
770
			model.println("</units>");
771
		}
772
		
773
		private void ecrireRelations() {
774
			model.println("<relations>");
775
			for (String type : structure.getTypes(Relation.class))
776
				ecrireType(Relation.class, type);
777
			model.println("</relations>");
778
		}
779
		
780
		private void ecrireSchemas() {
781
			model.println("<schemas>");
782
			for (String type : structure.getTypes(Schema.class))
783
				ecrireType(Schema.class, type);
784
			model.println("</schemas>");
785
		}
786
		
787
		private void ecrireType(Class<? extends Element> classe, String type) {
788
			model.print("<type name=\"" + encodeXMLTexte(type) + "\"");
789
			if (classe == Relation.class) model.print(" oriented=\"true\"");
790
			model.println(">");
791
			model.println("<featureSet>");
792
			for (String prop : structure.getNomsProps(classe, type))
793
				ecrireProp(classe, type, prop);
794
			model.println("</featureSet>");
795
			model.println("</type>");
796
		}
797
		
798
		private void ecrireProp(Class<? extends Element> classe, String type, String prop) {
799
			model.println("<feature name=\"" + encodeXMLTexte(prop) + "\">");
800
			if (structure.getValeursProp(classe, type, prop).isEmpty()) {
801
				model.println("<value type=\"free\" default=\"\" />");
802
			}
803
			else {
804
				model.println("<possibleValues default = \""
805
						+ structure.getValeurParDefaut(classe, type, prop) + "\">");
806
				for (String val : structure.getValeursProp(classe, type, prop))
807
					model.println("<value>" + encodeXMLTexte(val) + "</value>");
808
				model.println("</possibleValues>");
809
			}
810
			model.println("</feature>");
811
			
812
		}
813
	}
814
	
815
	static String encodeXMLTexte(String ch) {
816
		// return ch;
817
		return ch.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;").replaceAll("\"", "&quot;").replaceAll("\'", "&apos;");
818
	}
718 819
}
tmp/org.txm.analec.rcp/plugin.xml (revision 2937)
443 443
   </extension>
444 444
   <extension
445 445
         point="org.txm.searchengine.core.SearchEngine">
446
      <SearchEngine
447
            class="org.txm.annotation.urs.search.URSSearchEngine">
448
      </SearchEngine>
446 449
   </extension>
447 450
   <extension
448 451
         point="org.eclipse.core.runtime.preferences">
tmp/org.txm.tigersearch.rcp/src/org/txm/tigersearch/commands/ImportCONNLUAnnotations.java (revision 2937)
88 88
			return null;
89 89
		}
90 90
		
91
		ParametersDialog.open(this);
91
		if (!ParametersDialog.open(this)) {
92
			return null;
93
		}
92 94
		if (connluDirectory == null || !connluDirectory.exists() || !connluDirectory.isDirectory() || connluDirectory.listFiles().length == 0) {
93 95
			Log.warning("Error: connlu directory is empty: " + connluDirectory);
94 96
			return null;
......
158 160
			
159 161
			int nWords2 = 0;
160 162
			int nLine = 0;
163
			String sent_id = null;
164
			String newpar_id = null;
165
			String newdoc_id = null;
161 166
			while (line != null) {
162 167
				nLine++;
163
				if (line.startsWith("#") || line.length() == 0) {
168
				if (line.length() == 0) {
164 169
					line = reader.readLine();
165 170
					continue; // comment
166 171
				}
167 172
				
... Ce différentiel a été tronqué car il excède la taille maximale pouvant être affichée.

Formats disponibles : Unified diff