/[webpac]/trunk/all2xml.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/all2xml.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 5 by dpavlin, Sat Jan 11 06:14:48 2003 UTC revision 10 by dpavlin, Thu Jan 16 17:35:54 2003 UTC
# Line 6  use Getopt::Std; Line 6  use Getopt::Std;
6  use Data::Dumper;  use Data::Dumper;
7  use XML::Simple;  use XML::Simple;
8  use Text::Unaccent 1.02;        # 1.01 won't compile on my platform,  use Text::Unaccent 1.02;        # 1.01 won't compile on my platform,
9  require Unicode::Map8;  use Text::Iconv;
10    
11  my $config=XMLin(undef, forcearray => [ 'isis' ], forcecontent => 1);  
12    $|=1;
13    
14    my $config;
15    
16    $config=XMLin(undef, forcearray => [ 'isis' ], forcecontent => 1);
17    
18    use index_DBI;  # there is no other, right now ;-)
19    
20    my $index = new index_DBI();    # open index
21    
22  my %opts;  my %opts;
23    
24  getopts('d:m:q', \%opts);  # usage:
25    #       -d directory name
26    #       -m multiple directories
27    #       -q quiet
28    #       -s run swish
29    
30    getopts('d:m:qs', \%opts);
31    
32  my $db_dir = $opts{d} || "ps";  # FIX  my $db_dir = $opts{d} || "ps";  # FIX
33    
# Line 21  my $db_dir = $opts{d} || "ps"; # FIX Line 36  my $db_dir = $opts{d} || "ps"; # FIX
36  #print Dumper($config->{indexer});  #print Dumper($config->{indexer});
37  #print "-" x 70,"\n";  #print "-" x 70,"\n";
38    
39  # how to convert isis code page to UTF8?  Text::Iconv->raise_error(1);     # Conversion errors raise exceptions
40  my $isis_map = Unicode::Map8->new($config->{isis_codepage}) || die;  
41    my $isis_codepage = Text::Iconv->new($config->{isis_codepage},'UTF8');
42    my $index_codepage = Text::Iconv->new($config->{isis_codepage},$config->{index_codepage});
43    my $cludge_codepage = Text::Iconv->new('UTF8','ISO8859-1');
44    
45  sub isis2xml {  sub isis2xml {
46    
47            use xmlify;
48    
49          my $row = shift @_;          my $row = shift @_;
50    
51          my $xml;          my $xml;
52            $xml .= xmlify('db_dir',$db_dir);       # FIX remove?
53    
54            use parse_format;
55    
         sub isis_sf {  
                 my $row = shift @_;  
                 my $isis_id = shift @_;  
                 my $subfield = shift @_;  
                 if ($row->{$isis_id}->[0]) {  
                         my $sf = OpenIsis::subfields($row->{$isis_id}->[0]);  
                         if (! defined $subfield || length($subfield) == 0) {  
                                 # subfield list undef, empty or no defined subfields for this record  
                                 my $all_sf = $row->{$isis_id}->[0];  
                                 $all_sf =~ s/\^./ /g;   nuke definirions  
                                 return $all_sf;  
                         } elsif ($sf->{$subfield}) {  
                                 return $sf->{$subfield};  
                         }  
                 }  
         }  
56    
57          foreach my $field (keys %{$config->{indexer}}) {          foreach my $field (keys %{$config->{indexer}}) {
58    
59                    my $swish_data = "";
60                  my $display_data = "";                  my $display_data = "";
61                  my $index_data = "";                  my $index_data = "";
62    
63                  foreach my $x (@{$config->{indexer}->{$field}->{isis}}) {                  foreach my $x (@{$config->{indexer}->{$field}->{isis}}) {
64    
                         my $display_tmp = "";  
                         my $index_tmp = "";  
   
65                          my $format = $x->{content};                          my $format = $x->{content};
66                          my $i = 1;      # index only                          my ($s,$d,$i) = (1,1,0);        # swish, display default
67                          my $d = 1;      # display only                          $s = 0 if (lc($x->{type}) eq "display");
68                          $i = 0 if (lc($x->{type}) eq "display");                          $d = 0 if (lc($x->{type}) eq "swish");
69                          $d = 0 if (lc($x->{type}) eq "index");                          ($s,$d,$i) = (0,0,1) if (lc($x->{type}) eq "index");
70  #print "## i: $i d: $d ## $format ##";    #print STDERR "## s: $s d: $d i: $i ## $format ##\n";  
71                          # parse format  
72                          my $prefix = "";                          $format = $cludge_codepage->convert($format);
73                          if ($format =~ s/^([^\d]+)//) {                          my ($swish,$display) = parse_format($format,$row);
74                                  $prefix = $1;  #print STDERR "s: $swish\nd: $display\n" if ($swish);
75                          }  
76                          while ($format) {  #print STDERR "swish: $swish<-- display: $display<--\n";
77                                  if ($format =~ s/^(\d\d\d)(\w?)//) {                          # FIX: this is ugly, UGLY, cludge: OpenIsis return
78                                          my $isis_tmp = isis_sf($row,$1,$2);                          # UTF8 encoding of strings, but as if source charset
79                                          if ($isis_tmp) {                          # is ISO8859-1 and not some other. This breaks our
80  #                                               $display_tmp .= $prefix . "/$1/$2/".$isis_tmp if ($d);                          # isis character encoding, so we convert it first
81                                                  $display_tmp .= $prefix . $isis_tmp if ($d);                          # back to ISO8859-1 (which can actually be different
82                                                  $index_tmp .= $isis_tmp." " if ($i);                          # encoding in isis)
83  #print " $isis_tmp <--\n";  
84                                          }                          $swish_data .= $swish if ($s && $swish);
85                                          $prefix = "";                          $display_data .= $display if ($d && $display);
86                                  } elsif ($format =~ s/^([^\d]+)//) {                          $index_data .= $display if ($i && $display);
87                                          $prefix = $1;                  }
                                 } else {  
                                         print STDERR "WARNING: unparsed format '$format'\n";  
                                         last;  
                                 };  
                         }  
                         # add suffix  
                         $display_tmp .= $prefix if ($display_tmp);  
88    
89  #                       $display_data .= $display_tmp if ($display_tmp ne "");  #print STDERR "s_d: $swish_data\nd_d: $display_data\n" if ($swish_data);
90  #                       $index_data .= $index_tmp if ($index_tmp ne "");                  if ($display_data) {
91                          $display_data .= $display_tmp;                          $display_data = $isis_codepage->convert($display_data)."##" || $display_data;
92                          $index_data .= $index_tmp;                          $xml .= xmlify($field."_display", $display_data);
93                    }
94                    if ($swish_data) {
95                            my $i = Text::Iconv->new($config->{isis_codepage},'ISO8859-2');
96                            $swish_data = $i->convert($swish_data);
97                            $xml .= xmlify($field."_swish",unac_string('ISO8859-2',$swish_data));
98                            #$swish_data = $isis_codepage->convert($swish_data)."##" || $swish_data;
99                            #$xml .= xmlify($field."_swish",unac_string($config->{isis_codepage},$swish_data));
100                    }
101    
102                    # index
103                    if ($index_data && $index_data ne "") {
104                            $index_data = $index_codepage->convert($index_data) || $index_data;
105                            $index->insert($field, $index_data, $db_dir);
106                  }                  }
107  #print "--display:$display_data\n--index:$index_data\n";  
                 $xml->{$field}->{display} .= $isis_map->tou($display_data)->utf8 if ($display_data);  
                 $xml->{$field}->{index} .= unac_string($config->{isis_codepage},$index_data) if ($index_data);  
           
108          }          }
109          if ($xml) {          if ($xml) {
110                  return XMLout($xml, rootname => 'xml', noattr => 1 );  #print STDERR "x: $xml\n";
111                    return "<xml>\n$xml</xml>\n";
112          } else {          } else {
113                  return;                  return;
114          }          }
# Line 152  foreach my $isis_db (@isis_dbs) { Line 160  foreach my $isis_db (@isis_dbs) {
160    
161          print STDERR "Reading database: $isis_db [$max_rowid rows]\n";          print STDERR "Reading database: $isis_db [$max_rowid rows]\n";
162    
163            my $path = $isis_db;
164            $path =~ s#$config->{isis_data}/*##g;
165    
166          my $last_p = 0;          my $last_p = 0;
167    
168  #       { my $row_id = 1;  #       { my $row_id = 4514;
169  # FIX  # FIX
170          for (my $row_id = 1; $row_id <= $max_rowid; $row_id++ ) {          for (my $row_id = 1; $row_id <= $max_rowid; $row_id++ ) {
171                  my $row = OpenIsis::read( $db, $row_id );                  my $row = OpenIsis::read( $db, $row_id );
172                  if ($row && $row->{mfn}) {                  if ($row && $row->{mfn}) {
173    #print STDERR "mfn: ",$row->{mfn},"\n";
174                          # output current process indicator                          # output current process indicator
175                          my $p = int($row->{mfn} * 100 / $max_rowid);                          my $p = int($row->{mfn} * 100 / $max_rowid);
176                          if ($p != $last_p) {                          if ($p != $last_p) {
# Line 168  foreach my $isis_db (@isis_dbs) { Line 179  foreach my $isis_db (@isis_dbs) {
179                          }                          }
180    
181                          if (my $xml = isis2xml($row)) {                          if (my $xml = isis2xml($row)) {
182                                  print "Path-Name: $isis_db#".$row->{mfn}."\n";  #print STDERR "--ret-->$xml\n";
183                                    print "Path-Name: $path#".int($row->{mfn})."\n";
184                                  print "Content-Length: ".(length($xml)+1)."\n";                                  print "Content-Length: ".(length($xml)+1)."\n";
185                                  print "Document-Type: XML\n\n$xml\n";                                  print "Document-Type: XML\n\n$xml\n";
186                          }                          }
# Line 177  foreach my $isis_db (@isis_dbs) { Line 189  foreach my $isis_db (@isis_dbs) {
189          print STDERR "\n";          print STDERR "\n";
190  }  }
191    
192    # call this to commit index
193    $index->close;
194    
195  1;  1;
196  __END__  __END__

Legend:
Removed from v.5  
changed lines
  Added in v.10

  ViewVC Help
Powered by ViewVC 1.1.26