/[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 21 by dpavlin, Sun Feb 23 00:00:51 2003 UTC revision 44 by dpavlin, Sat Mar 22 22:51:48 2003 UTC
# Line 8  use XML::Simple; Line 8  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  use Text::Iconv;  use Text::Iconv;
10  use Config::IniFiles;  use Config::IniFiles;
11    use Encode;
12    
13  $|=1;  $|=1;
14    
# Line 31  my %opts; Line 32  my %opts;
32    
33  getopts('d:m:qs', \%opts);  getopts('d:m:qs', \%opts);
34    
35  my $db_dir;  my $path;       # this is name of database
36    
37  #die "usage: $0 -d [database_dir] -m [database1,database2] " if (! %opts);  Text::Iconv->raise_error(1);     # Conversion errors raise exceptions
38    
39  #print Dumper($config->{indexer});  # this is encoding of all files on disk, including import_xml/*.xml file and
40  #print "-" x 70,"\n";  # filter/*.pm files! It will be used to store strings in perl internally!
41    my $codepage = 'ISO-8859-2';
42    
43    my $utf2cp = Text::Iconv->new('UTF-8',$codepage);
44    # this function will convert data from XML files to local encoding
45    sub x {
46            return $utf2cp->convert($_[0]);
47    }
48    
49  Text::Iconv->raise_error(1);     # Conversion errors raise exceptions  # decode isis import codepage
50    my $isis2cp;
51    
52  #my $isis_codepage = Text::Iconv->new($config->{isis_codepage},'UTF8');  # outgoing xml must be in UTF-8
53  #my $index_codepage = Text::Iconv->new($config->{isis_codepage},$config->{index_codepage});  my $cp2utf = Text::Iconv->new($codepage,'UTF-8');
 my $isis_codepage;  
 my $index_codepage;  
 my $cludge_codepage = Text::Iconv->new('UTF8','ISO8859-1');  
 my $xml_codepage;  
54    
55  sub isis2xml {  sub isis2xml {
56    
# Line 62  sub isis2xml { Line 67  sub isis2xml {
67    
68          my %field_usage;        # counter for usage of each field          my %field_usage;        # counter for usage of each field
69    
70          foreach my $field (keys %{$config->{indexer}}) {          # sort subrouting using order="" attribute
71            sub by_order {
72                    return 0 if (! $config->{indexer}->{$a}->{order});
73                    return 0 if (! $config->{indexer}->{$b}->{order});
74    
75                    return $config->{indexer}->{$a}->{order} <=>
76                            $config->{indexer}->{$b}->{order} ;
77            }
78    
79            foreach my $field (sort by_order keys %{$config->{indexer}}) {
80    
81                    $field=x($field);
82    
83                  $field_usage{$field}++;                  $field_usage{$field}++;
84    
85                  my $swish_data = "";                  my $swish_data = "";
86                  my $display_data = "";                  my $display_data = "";
87                    my $line_delimiter;
88    
89                    my ($swish,$display);
90    
91                  foreach my $x (@{$config->{indexer}->{$field}->{isis}}) {                  foreach my $x (@{$config->{indexer}->{$field}->{isis}}) {
92    
93                          my $format = $x->{content};                          my $format = x($x->{content});
94                            my $delimiter = x($x->{delimiter}) || ' ';
95    
96                            my $isis_i = 0;         # isis repeatable offset
97    
98                          my ($s,$d,$i) = (1,1,0);        # swish, display default                          my ($s,$d,$i) = (1,1,0);        # swish, display default
99                          $s = 0 if (lc($x->{type}) eq "display");                          $s = 0 if (lc($x->{type}) eq "display");
100                          $d = 0 if (lc($x->{type}) eq "swish");                          $d = 0 if (lc($x->{type}) eq "swish");
101                          ($s,$d,$i) = (0,0,1) if (lc($x->{type}) eq "index");                          ($s,$d,$i) = (0,0,1) if (lc($x->{type}) eq "index");
 #print STDERR "## s: $s d: $d i: $i ## $format ##\n";    
102    
103                          # FIX: this is ugly, UGLY, cludge string is returned                          # what will separate last line from this one?
104                          # in UTF8 encoding , but as if source charset                          if ($display_data && $x->{append} && $x->{append} eq "1") {
105                          # is ISO8859-1 and not some other. This break other                                  $line_delimiter = ' ';
106                          # character encodings, so we convert it first                          } elsif ($display_data) {
107                          # back to ISO8859-1                                  $line_delimiter = '<br/>';
                         $format = $cludge_codepage->convert($format);  
   
                         my ($swish,$display) = parse_format($format,$row);  
 #print STDERR "s: $swish\nd: $display\n" if ($swish);  
   
 #print STDERR "swish: $swish<-- display: $display<--\n";  
   
                         # filter="name" ; filter this field through  
                         # filter/[name].pm  
                         my $filter;  
                         if ($x->{filter}) {  
                                 $filter = "filter/".$x->{filter}.".pm";  
                                 require $filter;  
108                          }                          }
109                          # type="swish" ; field for swish  
110                          if ($s && $swish) {                          # init vars so that we go into while...
111                            ($swish,$display) = (1,1);
112    
113                            while ($swish || $display) {
114                                    ($swish,$display) = parse_format($format,$row,$isis_i++,$isis2cp);
115    
116                                    # filter="name" ; filter this field through
117                                    # filter/[name].pm
118                                    my $filter = $x->{filter};
119                                  if ($filter) {                                  if ($filter) {
120                                          $swish_data .= join(" ",&filter($swish));                                          require "filter/".$filter.".pm";
121                                  } else {                                  }
122                                          $swish_data .= $swish if ($s && $swish);                                  # type="swish" ; field for swish
123                                    if ($s && $swish) {
124                                            if ($filter) {
125                                                    no strict 'refs';
126                                                    $swish_data .= join(" ",&$filter($swish));
127                                            } else {
128                                                    $swish_data .= $swish;
129                                            }
130                                  }                                  }
                         }  
131    
132                          # type="display" ; field for display                                  # type="display" ; field for display
133                          if ($d && $display) {                                  if ($d && $display) {
134                                  if ($filter) {                                          if ($line_delimiter && $display_data) {
135                                          $display_data .= join(" ",&filter($display));                                                  $display_data .= $line_delimiter;
136                                  } else {                                                  undef $line_delimiter;
137                                          $display_data .= $display;                                          }
138                                            if ($filter) {
139                                                    no strict 'refs';
140                                                    $display_data .= join($delimiter,&$filter($display));
141                                            } else {
142                                                    if ($display_data) {
143                                                            $display_data .= $delimiter.$display;
144                                                    } else {
145                                                            $display_data .= $display;
146                                                    }
147                                            }
148                                  }                                  }
149                          }                                                  
150                                                                            # type="index" ; insert into index
151                          # type="index" ; insert into index                                  if ($i && $display) {
152                          if ($i && $display) {                                          my $index_data = $display;
153                                  my $index_data = $index_codepage->convert($display) || $display;                                          if ($filter) {
154                                  if ($filter) {                                                  no strict 'refs';
155                                          foreach my $d (&filter($index_data)) {                                                  foreach my $d (&$filter($index_data)) {
156                                                  $index->insert($field, $d, $db_dir);                                                          $index->insert($field, $d, $path);
157                                                    }
158                                            } else {
159                                                    $index->insert($field, $index_data, $path);
160                                          }                                          }
                                 } else {  
                                         $index->insert($field, $index_data, $db_dir);  
161                                  }                                  }
162                          }                          }
163                  }                  }
164    
165    
 #print STDERR "s_d: $swish_data\nd_d: $display_data\n" if ($swish_data);  
166                  if ($display_data) {                  if ($display_data) {
                         $display_data = $isis_codepage->convert($display_data) || die "Can't convert '$display_data' !";  
                         # FIX: this is removed and replaced by html tag.  
                         #$xml .= xmlify($field."_display", $display_data);  
167    
168                          if ($field eq "headline") {                          if ($field eq "headline") {
169                                  $xml .= xmlify("headline", $display_data);                                  $xml .= xmlify("headline", $display_data);
# Line 151  sub isis2xml { Line 181  sub isis2xml {
181                                          print STDERR "WARNING: field '$field' doesn't have 'name' attribute!";                                          print STDERR "WARNING: field '$field' doesn't have 'name' attribute!";
182                                  }                                  }
183                                  if ($field_name) {                                  if ($field_name) {
184                                          $html .= $xml_codepage->convert($field_name);                                          $html .= x($field_name);
185                                  }                                  }
186                                  $html .= $display_data."###\n";                                  $html .= $display_data."###\n";
187                          }                          }
188                  }                  }
189                  if ($swish_data) {                  if ($swish_data) {
                         my $i = Text::Iconv->new($config->{isis_codepage},'ISO8859-2');  
190                          # remove extra spaces                          # remove extra spaces
191                          $swish_data =~ s/ +/ /g;                          $swish_data =~ s/ +/ /g;
192                          $swish_data =~ s/ +$//g;                          $swish_data =~ s/ +$//g;
193    
194                          $swish_data = $i->convert($swish_data);                          $xml .= xmlify($field."_swish", unac_string($codepage,$swish_data));
                         $xml .= xmlify($field."_swish",unac_string('ISO8859-2',$swish_data));  
                         #$swish_data = $isis_codepage->convert($swish_data)."##" || $swish_data;  
                         #$xml .= xmlify($field."_swish",unac_string($config->{isis_codepage},$swish_data));  
195                  }                  }
196    
197    
# Line 177  sub isis2xml { Line 203  sub isis2xml {
203          }          }
204                    
205          if ($xml) {          if ($xml) {
 #print STDERR "x: $xml\n";  
206                  $xml .= $add_xml if ($add_xml);                  $xml .= $add_xml if ($add_xml);
207                  return "<xml>\n$xml</xml>\n";                  return "<xml>\n$xml</xml>\n";
208          } else {          } else {
# Line 191  my $cfg = new Config::IniFiles( -file => Line 216  my $cfg = new Config::IniFiles( -file =>
216    
217  foreach my $database ($cfg->Sections) {  foreach my $database ($cfg->Sections) {
218    
219          my $isis_db = $cfg -> val($database, 'isis_db');          my $isis_db = $cfg -> val($database, 'isis_db') || die "$database doesn't have 'isis_db' defined!";
220          my $type = $cfg -> val($database, 'type');          my $type = $cfg -> val($database, 'type') || die "$database doesn't have 'type' defined";
221          my $add_xml = $cfg -> val($database, 'xml');          my $add_xml = $cfg -> val($database, 'xml');    # optional
222    
         # read configuration for this type  
223          $config=XMLin("./import_xml/$type.xml", forcearray => [ 'isis' ], forcecontent => 1);          $config=XMLin("./import_xml/$type.xml", forcearray => [ 'isis' ], forcecontent => 1);
224          $isis_codepage = Text::Iconv->new($config->{isis_codepage},'UTF8');  
225          $index_codepage = Text::Iconv->new($config->{isis_codepage},$config->{index_codepage});          $isis2cp = Text::Iconv->new($config->{isis_codepage},$codepage);
         $xml_codepage = Text::Iconv->new($cfg->val($database,'xml_codepage'),'UTF8');  
226    
227          my $db = OpenIsis::open( $isis_db );          my $db = OpenIsis::open( $isis_db );
228          if (0) {          if (0) {
# Line 222  foreach my $database ($cfg->Sections) { Line 245  foreach my $database ($cfg->Sections) {
245          for (my $row_id = 1; $row_id <= $max_rowid; $row_id++ ) {          for (my $row_id = 1; $row_id <= $max_rowid; $row_id++ ) {
246                  my $row = OpenIsis::read( $db, $row_id );                  my $row = OpenIsis::read( $db, $row_id );
247                  if ($row && $row->{mfn}) {                  if ($row && $row->{mfn}) {
 #print STDERR "mfn: ",$row->{mfn},"\n";  
248                          # output current process indicator                          # output current process indicator
249                          my $p = int($row->{mfn} * 100 / $max_rowid);                          my $p = int($row->{mfn} * 100 / $max_rowid);
250                          if ($p != $last_p) {                          if ($p != $last_p) {
# Line 230  foreach my $database ($cfg->Sections) { Line 252  foreach my $database ($cfg->Sections) {
252                                  $last_p = $p;                                  $last_p = $p;
253                          }                          }
254    
255                            my $swishpath = $path."#".int($row->{mfn});
256    
257                          if (my $xml = isis2xml($row,$add_xml)) {                          if (my $xml = isis2xml($row,$add_xml)) {
258  #print STDERR "--ret-->$xml\n";                                  $xml = $cp2utf->convert($xml);
259                                  print "Path-Name: $path#".int($row->{mfn})."\n";                                  use bytes;      # as opposed to chars
260                                    print "Path-Name: $swishpath\n";
261                                  print "Content-Length: ".(length($xml)+1)."\n";                                  print "Content-Length: ".(length($xml)+1)."\n";
262                                  print "Document-Type: XML\n\n$xml\n";                                  print "Document-Type: XML\n\n$xml\n";
263                          }                          }

Legend:
Removed from v.21  
changed lines
  Added in v.44

  ViewVC Help
Powered by ViewVC 1.1.26