My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#!/usr/bin/perl

#####################################################################
#
# simplexml2meta.pl
#
# Konvertierung eines flachen XML-Formates in des OpenBib
# Einlade-Metaformat
#
# Dieses File ist (C) 2012 Oliver Flimm <flimm@openbib.org>
#
# Dieses Programm ist freie Software. Sie koennen es unter
# den Bedingungen der GNU General Public License, wie von der
# Free Software Foundation herausgegeben, weitergeben und/oder
# modifizieren, entweder unter Version 2 der Lizenz oder (wenn
# Sie es wuenschen) jeder spaeteren Version.
#
# Die Veroeffentlichung dieses Programms erfolgt in der
# Hoffnung, dass es Ihnen von Nutzen sein wird, aber OHNE JEDE
# GEWAEHRLEISTUNG - sogar ohne die implizite Gewaehrleistung
# der MARKTREIFE oder der EIGNUNG FUER EINEN BESTIMMTEN ZWECK.
# Details finden Sie in der GNU General Public License.
#
# Sie sollten eine Kopie der GNU General Public License zusammen
# mit diesem Programm erhalten haben. Falls nicht, schreiben Sie
# an die Free Software Foundation, Inc., 675 Mass Ave, Cambridge,
# MA 02139, USA.
#
#####################################################################

use 5.008001;

use utf8;
use strict;
use warnings;

use Encode 'decode';
use Getopt::Long;
use XML::Twig::XPath;
use XML::Simple;

use YAML::Syck;

use OpenBib::Config;
use OpenBib::Conv::Common::Util;

my ($inputfile,$configfile);

&GetOptions(
"inputfile=s" => \$inputfile,
"configfile=s" => \$configfile,
);

if (!$inputfile || !$configfile){
print << "HELP";
simplexml2meta.pl - Aufrufsyntax

simplexml2meta.pl --inputfile=xxx --configfile=yyy.yml
HELP
exit;
}

# Ininitalisierung mit Config-Parametern
my $convconfig = YAML::Syck::LoadFile($configfile);

open (TIT, ">:utf8","unload.TIT");
open (AUT, ">:utf8","unload.PER");
open (KOR, ">:utf8","unload.KOE");
open (NOTATION,">:utf8","unload.SYS");
open (SWT, ">:utf8","unload.SWD");
open (MEX, ">:utf8","unload.MEX");

my $twig= XML::Twig::XPath->new(
TwigHandlers => {
"$convconfig->{recordselector}" => \&parse_record
}
);

our $counter = 0;

$twig->safe_parsefile($inputfile);

print STDERR "All $counter records converted\n";

close(TIT);
close(AUT);
close(KOR);
close(NOTATION);
close(SWT);
close(MEX);

sub parse_record {
my($t, $titset)= @_;

my @ids= $titset->get_xpath($convconfig->{uniqueidfield});

print TIT "0000:".$ids[0]->first_child()->text()."\n";

foreach my $kateg (keys %{$convconfig->{title}}){

my @elements = $titset->get_xpath($kateg);

my @parts = ();

foreach my $element (@elements){
next unless (defined $element->first_child());
my $content = konv($element->first_child()->text());

if ($convconfig->{filter}{$kateg}{filter_junk}){
$content = filter_junk($content);
}

if ($convconfig->{filter}{$kateg}{filter_newline2br}){
$content = filter_newline2br($content);
}

if ($convconfig->{filter}{$kateg}{filter_match}){
$content = filter_match($content,$convconfig->{filter}{$kateg}{filter_match});
}

if ($convconfig->{filter}{$kateg}{filter_add_year}){
my $new_content = filter_match($content,$convconfig->{filter}{$kateg}{filter_add_year}{regexp});
print TIT $convconfig->{filter}{$kateg}{filter_add_year}{category}.$new_content."\n";
}

if ($content){
$content=decode($convconfig->{encoding},$content) if ($convconfig->{encoding});

if (exists $convconfig->{category_split_chars}{$kateg} && $content=~/$convconfig->{category_split_chars}{$kateg}/){
@parts = split($convconfig->{category_split_chars}{$kateg},$content);
}
else {
push @parts, $content;
}
}
}

foreach my $part (@parts){
print TIT $convconfig->{title}{$kateg}.$part."\n";
}
}

# Autoren abarbeiten Anfang
foreach my $kateg (keys %{$convconfig->{pers}}){
my @elements = $titset->get_xpath($kateg);

my @parts = ();

foreach my $element (@elements){
next unless (defined $element->first_child());
my $content = konv($element->first_child()->text());

if ($convconfig->{filter}{$kateg}{filter_junk}){
$content = filter_junk($content);
}

if ($convconfig->{filter}{$kateg}{filter_newline2br}){
$content = filter_newline2br($content);
}

if ($convconfig->{filter}{$kateg}{filter_match}){
$content = filter_match($content,$convconfig->{filter}{$kateg}{filter_match});
}

if ($content){
$content=decode($convconfig->{encoding},$content) if ($convconfig->{encoding});

if (exists $convconfig->{category_split_chars}{$kateg} && $content=~/$convconfig->{category_split_chars}{$kateg}/){
@parts = split($convconfig->{category_split_chars}{$kateg},$content);
}
else {
push @parts, $content;
}
}
}

foreach my $part (@parts){
my $autidn=OpenBib::Conv::Common::Util::get_autidn($part);

if ($autidn > 0){
print AUT "0000:$autidn\n";
print AUT "0001:$part\n";
print AUT "9999:\n";
}
else {
$autidn=(-1)*$autidn;
}

print TIT $convconfig->{pers}{$kateg}."IDN: $autidn\n";
}
# Autoren abarbeiten Ende
}

# Koerperschaften abarbeiten Anfang
foreach my $kateg (keys %{$convconfig->{corp}}){
my @elements = $titset->get_xpath($kateg);

my @parts = ();

foreach my $element (@elements){
next unless (defined $element->first_child());
my $content = konv($element->first_child()->text());

if ($convconfig->{filter}{$kateg}{filter_junk}){
$content = filter_junk($content);
}

if ($convconfig->{filter}{$kateg}{filter_newline2br}){
$content = filter_newline2br($content);
}

if ($convconfig->{filter}{$kateg}{filter_match}){
$content = filter_match($content,$convconfig->{filter}{$kateg}{filter_match});
}

if ($content){
$content=decode($convconfig->{encoding},$content) if ($convconfig->{encoding});

if (exists $convconfig->{category_split_chars}{$kateg} && $content=~/$convconfig->{category_split_chars}{$kateg}/){
@parts = split($convconfig->{category_split_chars}{$kateg},$content);
}
else {
push @parts, $content;
}
}
}

foreach my $part (@parts){
my $koridn=OpenBib::Conv::Common::Util::get_koridn($part);

if ($koridn > 0){
print KOR "0000:$koridn\n";
print KOR "0001:$part\n";
print KOR "9999:\n";
}
else {
$koridn=(-1)*$koridn;
}

print TIT $convconfig->{corp}{$kateg}."IDN: $koridn\n";
}
}
# Koerperschaften abarbeiten Ende

# Notationen abarbeiten Anfang
foreach my $kateg (keys %{$convconfig->{sys}}){
my @elements = $titset->get_xpath($kateg);

my @parts = ();

foreach my $element (@elements){
next unless (defined $element->first_child());
my $content = konv($element->first_child()->text());

if ($convconfig->{filter}{$kateg}{filter_junk}){
$content = filter_junk($content);
}

if ($convconfig->{filter}{$kateg}{filter_newline2br}){
$content = filter_newline2br($content);
}

if ($convconfig->{filter}{$kateg}{filter_match}){
$content = filter_match($content,$convconfig->{filter}{$kateg}{filter_match});
}

if ($content){
$content=decode($convconfig->{encoding},$content) if ($convconfig->{encoding});

if (exists $convconfig->{category_split_chars}{$kateg} && $content=~/$convconfig->{category_split_chars}{$kateg}/){
@parts = split($convconfig->{category_split_chars}{$kateg},$content);
}
else {
push @parts, $content;
}
}
}

foreach my $part (@parts){
my $notidn=OpenBib::Conv::Common::Util::get_notidn($part);

if ($notidn > 0){
print NOTATION "0000:$notidn\n";
print NOTATION "0001:$part\n";
print NOTATION "9999:\n";
}
else {
$notidn=(-1)*$notidn;
}
print TIT $convconfig->{sys}{$kateg}."IDN: $notidn\n";
}
}
# Notationen abarbeiten Ende

# Schlagworte abarbeiten Anfang
foreach my $kateg (keys %{$convconfig->{subj}}){
my @elements = $titset->get_xpath($kateg);

my @parts = ();

foreach my $element (@elements){
next unless (defined $element->first_child());
my $content = konv($element->first_child()->text());

if ($convconfig->{filter}{$kateg}{filter_junk}){
$content = filter_junk($content);
}

if ($convconfig->{filter}{$kateg}{filter_newline2br}){
$content = filter_newline2br($content);
}

if ($convconfig->{filter}{$kateg}{filter_match}){
$content = filter_match($content,$convconfig->{filter}{$kateg}{filter_match});
}

if ($content){
$content=decode($convconfig->{encoding},$content) if ($convconfig->{encoding});

if (exists $convconfig->{category_split_chars}{$kateg} && $content=~/$convconfig->{category_split_chars}{$kateg}/){
@parts = split($convconfig->{category_split_chars}{$kateg},$content);
}
else {
push @parts, $content;
}
}
}

foreach my $part (@parts){
my $swtidn=OpenBib::Conv::Common::Util::get_swtidn($part);

if ($swtidn > 0){
print SWT "0000:$swtidn\n";
print SWT "0001:$part\n";
print SWT "9999:\n";
}
else {
$swtidn=(-1)*$swtidn;
}
print TIT $convconfig->{subj}{$kateg}."IDN: $swtidn\n";
}
}
# Schlagworte abarbeiten Ende

print TIT "9999:\n";

$counter++;

if ($counter % 1000 == 0){
print STDERR "$counter records converted\n";
}

# Release memory of processed tree
# up to here
$t->purge();
}

sub konv {
my ($content)=@_;

# $content=~s/\&/&amp;/g;
$content=~s/>/&gt;/g;
$content=~s/</&lt;/g;

return $content;
}

# Filter

sub filter_junk {
my ($content) = @_;

$content=~s/\W/ /g;
$content=~s/\s+/ /g;
$content=~s/\s\D\s/ /g;


return $content;
}

sub filter_newline2br {
my ($content) = @_;

$content=~s/\n/<br\/>/g;

return $content;
}

sub filter_match {
my ($content,$regexp) = @_;

my ($match)=$content=~m/($regexp)/g;

return $match;
}

Change log

r5738 by fl...@sigtrap.de on Jan 12, 2012   Diff
- Erweiterung um Konverter fuer generische
XML-Dateien, bei denen die Zuordnung zu
den Kategorien des Metaformats via XPath
vorgenommen
  wird.

  Beispielkonfiguration: PaperC XML-Dump
Go to: 
Project members, sign in to write a code review

Older revisions

All revisions of this file

File info

Size: 12120 bytes, 396 lines

File properties

svn:executable
*
Powered by Google Project Hosting