root / tmp / org.txm.core / src / java / org / txm / scripts / importer / Factiva2XmlTxm.groovy @ 2473
History | View | Annotate | Download (8.3 kB)
1 |
/**
|
---|---|
2 |
* Main.
|
3 |
*
|
4 |
* @param args the args
|
5 |
*/
|
6 |
// script de recherche de contenus entre balises dans un fichier xml
|
7 |
|
8 |
/**
|
9 |
* @author Daniel Marin, Florent Bédécarrats
|
10 |
* @author Matthieu Decorde
|
11 |
* @copyright 2011 Daniel Marin & Florent Bédécarrats, for original Perl version
|
12 |
* @copyright 2011 ENS de Lyon, for translation in Groovy
|
13 |
* This software is licensed under the GNU GPL version 3.0 or later.
|
14 |
*/
|
15 |
package org.txm.scripts.importer;
|
16 |
|
17 |
import org.txm.importer.ValidateXml |
18 |
|
19 |
public run(File rootDir, String rep_final ) { |
20 |
|
21 |
// A RENSEIGNER #########################################
|
22 |
|
23 |
def fich_xml = rootDir.listFiles().grep(~/.*\.xml/) |
24 |
println "$rootDir : $fich_xml"
|
25 |
|
26 |
def ext = ".xml" |
27 |
//def rep_final = rootDir.getAbsolutePath()+"/out" // le nom du répertoire dans lequel on va créer les fichiers txm
|
28 |
|
29 |
List<String> Balises = ["<baseLanguage>","<date>","<sourceName>","<name>","<name>"] // balises XML à détecter |
30 |
List<String> FinBalises = ["</baseLanguage>","</date>","</sourceName>","</name>","</name>"] |
31 |
|
32 |
List<String> Sections = ["<ppsarticle>","<industry ","<region ","<headline>","<tailParagraphs>"] // Balises indiquant qu'à 'intérieur de la section se trouvent les infos (sur plusieurs lignes contrairement à @Balises) |
33 |
List<String> FinSections = ["</ppsarticle>","</industry>","</region>","</headline>","</tailParagraphs>"] |
34 |
|
35 |
def Headline = [] // le tableau dans lequel on va stocker le contenu de <headline> |
36 |
def TailPara = [] // le tableau dans lequel on va stocker le contenu de <tailParagraphs> |
37 |
|
38 |
def NomsMeta = ["language=","date=","source=","subject=","country="] // les noms qui interviennent dans les méta-données du fichier final |
39 |
// FIN A RENSEIGNER #####################################
|
40 |
|
41 |
def TestMeta = [0,0,0,0,0] // Tableau qui indique si les métadonnées sont disponibles dans le fichier |
42 |
def MetaData = [] //le tableau des metadonnées |
43 |
def metaData = "" // la chaine dans laquelle on va stocker les métadonnées |
44 |
String temp = "" |
45 |
def fich_out = ">" |
46 |
def nom_in = "" |
47 |
|
48 |
def new_fich=0 // variable booleenne qui indique si on doit créer un nouveau txm |
49 |
def industry=0 // variable booleenne qui indique qu'on est dans la section <industry> pour renseigner "subject" |
50 |
def premIndustry=0 // variable booleenne qui indique qu'on a passé le 1er <industry> pour renseigner "subject" |
51 |
def region=0 // variable booleenne qui indique qu'on est dans la section <region> pour renseigner "country" |
52 |
def premRegion=0 // variable booleenne qui indique qu'on a passé le 1er <region> pour renseigner "country" |
53 |
def copie=0 // variable booleenne qui indique si l'on doit copier le contenu de la ligne dans le fichier txm |
54 |
def copieSec3=0 // variable qui indique si on est à l'intérieur de la section 3 |
55 |
def copieSec4=0 // variable qui indique si on est à l'intérieur de la section 4 |
56 |
|
57 |
def num_fich=0 // le numéro du fichier traité |
58 |
def char_num_fich="" // pour numéroter les fichiers fich_001 pour garder l'ordre alphabétique |
59 |
|
60 |
def i = 1 |
61 |
|
62 |
println "Création du répertoire dans lequel on va mettre les fichiers finaux txm"
|
63 |
if (new File(rep_final).exists()) { |
64 |
println "Le répertoire $rep_final existe"
|
65 |
} |
66 |
else {
|
67 |
if (!new File(rep_final).mkdir()) { |
68 |
println "Echec de la création du répertoire: $rep_final"
|
69 |
return
|
70 |
} |
71 |
if (!new File(rep_final).canWrite()) { |
72 |
println "Le répertoire '$rep_final' final est read-only"
|
73 |
return
|
74 |
} |
75 |
} |
76 |
|
77 |
FileWriter ECRITURE
|
78 |
for (File fich_in : fich_xml) { |
79 |
if (fich_in.getName() == "import.xml") continue; |
80 |
println "Validate $fich_in...";
|
81 |
if (!ValidateXml.test(fich_in)) {
|
82 |
println "skip $fich_in"
|
83 |
continue;
|
84 |
} |
85 |
// open(LECTURE, fich_in) || die("erreur d'ouverture de $fich_in")
|
86 |
println "on parcourt l'ensemble de $fich_in"
|
87 |
def reader = new FileReader(fich_in) |
88 |
String line = reader.readLine()
|
89 |
while (line != null) |
90 |
{ |
91 |
// on stocke le contenu de la ligne (et on efface le retour chariot à la fin)
|
92 |
temp = line |
93 |
temp = temp.trim() |
94 |
temp = (temp =~ /^\s+/).replaceAll("") |
95 |
temp = (temp =~ /^\t/).replaceAll("") |
96 |
temp = temp.replaceAll("</paragraph>", "</paragraph><br/>") |
97 |
|
98 |
// On indique si on est dans la 1ère section de industry ou region pour prélever <name>
|
99 |
if ((temp =~ Sections[1]) && (premIndustry==0)){ |
100 |
industry=1
|
101 |
premIndustry=1
|
102 |
} |
103 |
else if (temp =~ FinSections[1]){ |
104 |
industry=0
|
105 |
} |
106 |
|
107 |
if ((temp =~ Sections[2]) && (premRegion==0)){ |
108 |
region = 1
|
109 |
premRegion = 1
|
110 |
} |
111 |
else if (temp =~ FinSections[2]){ |
112 |
region = 0
|
113 |
} |
114 |
|
115 |
// on regarde si la balise de création de nouveau fichier est présente
|
116 |
if (temp =~ Sections[0]){ |
117 |
new_fich=1
|
118 |
num_fich = num_fich+1
|
119 |
nom_in = fich_in.getName() |
120 |
nom_in = (nom_in =~ /$ext/).replaceAll("") |
121 |
// Petit test pour numéroter les fichiers en 001, 002 plutôt que 1, 2 (ordre alphabétique)
|
122 |
if (num_fich < 10){ |
123 |
char_num_fich = "00"+num_fich
|
124 |
} else if (num_fich<100) { |
125 |
char_num_fich = "0"+num_fich
|
126 |
} else {
|
127 |
char_num_fich = num_fich |
128 |
} |
129 |
fich_out = new File(rep_final, nom_in + "_" + char_num_fich + ext) |
130 |
//open(ECRITURE,"> $fich_out") || die("erreur d'ouverture de $fich_out ! $! \n")
|
131 |
println("new file : "+fich_out)
|
132 |
if (ECRITURE != null) ECRITURE.close() |
133 |
ECRITURE = new FileWriter(fich_out) |
134 |
} |
135 |
|
136 |
// Remplissage des métadonnées : ####################################################
|
137 |
// Language
|
138 |
if (temp =~ Balises[0]){ |
139 |
temp = (temp =~ Balises[0]).replaceAll("") |
140 |
temp = (temp =~ FinBalises[0]).replaceAll("") |
141 |
|
142 |
MetaData[0]=temp
|
143 |
print " language trouve : "+MetaData[0]+" \n" |
144 |
TestMeta[0]=1 |
145 |
|
146 |
} |
147 |
// date
|
148 |
if (temp =~ Balises[1]) { |
149 |
temp = (temp =~ Balises[1]).replaceAll("") |
150 |
temp = (temp =~ FinBalises[1]).replaceAll("") |
151 |
MetaData[1]=temp
|
152 |
print " date trouvee : "+MetaData[1]+" \n" |
153 |
TestMeta[1]=1 |
154 |
} |
155 |
// source
|
156 |
if (temp =~ Balises[2]){ |
157 |
temp = (temp =~ Balises[2]).replaceAll("") |
158 |
temp = (temp =~ FinBalises[2]).replaceAll("") |
159 |
MetaData[2]=temp
|
160 |
print " source trouvee : "+MetaData[2]+" \n" |
161 |
TestMeta[2]=1 |
162 |
} |
163 |
// subject (on doit être dans la 1ère section <industry>)
|
164 |
if ((temp =~ Balises[3]) && (industry==1)){ |
165 |
temp = (temp =~ Balises[3]).replaceAll("") |
166 |
temp = (temp =~ FinBalises[3]).replaceAll("") |
167 |
MetaData[3]=temp
|
168 |
industry=0
|
169 |
print " Sujet trouve : "+MetaData[3]+" \n" |
170 |
TestMeta[3]=1 |
171 |
} |
172 |
// country
|
173 |
if ((temp =~ Balises[4]) && (region==1)){ |
174 |
temp = (temp =~ Balises[4]).replaceAll("") |
175 |
temp = (temp =~ FinBalises[4]).replaceAll("") |
176 |
MetaData[4]=temp
|
177 |
region=0
|
178 |
print " Pays trouve : "+MetaData[4]+" \n" |
179 |
TestMeta[4]=1 |
180 |
|
181 |
// Toutes les méta-données ont été trouvées, on assemble la chaîne
|
182 |
metaData = "<article "
|
183 |
for (i=0;i<(NomsMeta.size()+1);i++){ |
184 |
if (TestMeta[i]==1){ |
185 |
metaData = metaData+NomsMeta[i]+"\""+MetaData[i]+"\" " |
186 |
} |
187 |
else if (TestMeta[i]==0){ |
188 |
metaData = metaData +NomsMeta[i]+"\"\" "
|
189 |
} |
190 |
} |
191 |
metaData = "$metaData>\n"
|
192 |
print " metaData : $metaData \n"
|
193 |
} |
194 |
// FIN Remplissage des métadonnées : #################################################
|
195 |
|
196 |
// On copie ou pas si on est ou pas dans <headline> ou <tailParagraphs>
|
197 |
if (temp =~ Sections[3]){ |
198 |
copieSec3=1
|
199 |
} |
200 |
if (temp =~ FinSections[3]){ |
201 |
Headline.add(temp) |
202 |
copieSec3=0
|
203 |
} |
204 |
if (temp =~ Sections[4]){ |
205 |
copieSec4=1
|
206 |
} |
207 |
if (temp =~ FinSections[4]){ |
208 |
TailPara.add(temp) |
209 |
copieSec4=0
|
210 |
} |
211 |
|
212 |
if (copieSec3 == 1){ |
213 |
Headline.add(temp) |
214 |
} |
215 |
if (copieSec4 == 1){ |
216 |
TailPara.add(temp) |
217 |
} |
218 |
|
219 |
if (temp =~ FinSections[0]){ |
220 |
ECRITURE.write("$metaData\n")
|
221 |
for (i=0;i<(Headline.size());i++){ |
222 |
if (i==0 || i==Headline){ |
223 |
ECRITURE.write(Headline[i].toString()+"\n")
|
224 |
} |
225 |
else{
|
226 |
ECRITURE.write("\t"+Headline[i]+"\n") |
227 |
} |
228 |
} |
229 |
for (i=0;i<(TailPara.size());i++){ |
230 |
if (i==0 || i==TailPara){ |
231 |
ECRITURE.write(TailPara[i].toString()+"\n")
|
232 |
} |
233 |
else{
|
234 |
ECRITURE.write("\t"+TailPara[i]+"\n") |
235 |
} |
236 |
} |
237 |
ECRITURE.write("</article>")
|
238 |
|
239 |
new_fich=0
|
240 |
industry=0
|
241 |
region=0
|
242 |
premIndustry=0
|
243 |
premRegion=0
|
244 |
MetaData=[]
|
245 |
TestMeta=[0,0,0,0,0] |
246 |
Headline=[]
|
247 |
TailPara=[]
|
248 |
|
249 |
ECRITURE.close() |
250 |
print "fichier $char_num_fich traite.\n"
|
251 |
} |
252 |
line = reader.readLine() |
253 |
} |
254 |
ECRITURE.close() |
255 |
reader.close() |
256 |
num_fich = 0
|
257 |
} |
258 |
} |
259 |
|
260 |
String userDir = System.getProperty("user.home"); |
261 |
File rootDir = new File(userDir, "xml/factiva") |
262 |
File outdir = new File(rootDir, "out2") |
263 |
run(rootDir, outdir.getAbsolutePath()); |