/[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 29 by dpavlin, Sun Feb 23 07:08:54 2003 UTC revision 42 by dpavlin, Sat Mar 15 21:48: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 33  getopts('d:m:qs', \%opts); Line 34  getopts('d:m:qs', \%opts);
34    
35  my $db_dir;  my $db_dir;
36    
 #die "usage: $0 -d [database_dir] -m [database1,database2] " if (! %opts);  
   
 #print Dumper($config->{indexer});  
 #print "-" x 70,"\n";  
   
37  Text::Iconv->raise_error(1);     # Conversion errors raise exceptions  Text::Iconv->raise_error(1);     # Conversion errors raise exceptions
38    
39  my $isis_codepage;  # this is encoding of all files on disk, including import_xml/*.xml file and
40  my $index_codepage;  # filter/*.pm files! It will be used to store strings in perl internally!
41  my $cludge_codepage = Text::Iconv->new('UTF8','ISO8859-1');  my $codepage = 'ISO-8859-2';
42  my $xml_codepage;  
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  my $XML_CHARSET = 'UTF8';  # decode isis import codepage
50    my $isis2cp;
51    
52    # outgoing xml must be in UTF-8
53    my $cp2utf = Text::Iconv->new($codepage,'UTF-8');
54    
55  sub isis2xml {  sub isis2xml {
56    
# Line 63  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            # 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          foreach my $field (keys %{$config->{indexer}}) {                  $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 = "";                  my $line_delimiter;
88    
89                  foreach my $x (@{$config->{indexer}->{$field}->{isis}}) {                  my ($swish,$display);
90    
91                          my $format = $x->{content};                  foreach my $x (@{$config->{indexer}->{$field}->{isis}}) {
                         my $delimiter = $x->{delimiter} || ' ';  
92    
93                          # FIX: this is ugly, UGLY, cludge string is returned                          my $format = x($x->{content});
94                          # in UTF8 encoding , but as if source charset                          my $delimiter = x($x->{delimiter}) || ' ';
                         # is ISO8859-1 and not some other. This break other  
                         # character encodings, so we convert it first  
                         # back to ISO8859-1  
                         $format = $xml_codepage->convert($format);  
                         $delimiter = $xml_codepage->convert($delimiter) if ($delimiter);  
95    
96                          my $isis_i = 0;         # isis repeatable offset                          my $isis_i = 0;         # isis repeatable offset
97    
# Line 91  sub isis2xml { Line 99  sub isis2xml {
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                          $display_data .= $line_delimiter if ($display_data && $display_data !~ /$line_delimiter$/);                          # what will separate last line from this one?
104                            if ($display_data && $x->{append} && $x->{append} eq "1") {
105                                    $line_delimiter = ' ';
106                            } elsif ($display_data) {
107                                    $line_delimiter = '<br/>';
108                            }
109    
110                          my ($swish,$display) = (1,1);                          # init vars so that we go into while...
111                            ($swish,$display) = (1,1);
112    
113                          while ($swish || $display) {                          while ($swish || $display) {
114                                  ($swish,$display) = parse_format($format,$row,$isis_i++);                                  ($swish,$display) = parse_format($format,$row,$isis_i++,$isis2cp);
 #print STDERR "s: $swish\nd: $display\n" if ($swish);  
   
 #print STDERR "swish: $swish<-- display: $display<--\n";  
115    
116                                  # filter="name" ; filter this field through                                  # filter="name" ; filter this field through
117                                  # filter/[name].pm                                  # filter/[name].pm
# Line 112  sub isis2xml { Line 122  sub isis2xml {
122                                  # type="swish" ; field for swish                                  # type="swish" ; field for swish
123                                  if ($s && $swish) {                                  if ($s && $swish) {
124                                          if ($filter) {                                          if ($filter) {
 print STDERR "using filter '$filter'\n";  
125                                                  no strict 'refs';                                                  no strict 'refs';
126                                                  $swish_data .= join(" ",&$filter($swish));                                                  $swish_data .= join(" ",&$filter($swish));
127    print STDERR "#### $swish_data\n";
128                                          } else {                                          } else {
129                                                  $swish_data .= $swish;                                                  $swish_data .= $swish;
130                                          }                                          }
# Line 122  print STDERR "using filter '$filter'\n"; Line 132  print STDERR "using filter '$filter'\n";
132    
133                                  # type="display" ; field for display                                  # type="display" ; field for display
134                                  if ($d && $display) {                                  if ($d && $display) {
135                                            if ($line_delimiter && $display_data) {
136                                                    $display_data .= $line_delimiter;
137                                                    undef $line_delimiter;
138                                            }
139                                          if ($filter) {                                          if ($filter) {
140                                                  no strict 'refs';                                                  no strict 'refs';
141                                                  $display_data .= join($delimiter,&$filter($display));                                                  $display_data .= join($delimiter,&$filter($display));
# Line 136  print STDERR "using filter '$filter'\n"; Line 150  print STDERR "using filter '$filter'\n";
150                                                                                                    
151                                  # type="index" ; insert into index                                  # type="index" ; insert into index
152                                  if ($i && $display) {                                  if ($i && $display) {
153                                          my $index_data = $index_codepage->convert($display) || $display;                                          my $index_data = $display;
154                                          if ($filter) {                                          if ($filter) {
155                                                  no strict 'refs';                                                  no strict 'refs';
156                                                  foreach my $d (&$filter($index_data)) {                                                  foreach my $d (&$filter($index_data)) {
# Line 147  print STDERR "using filter '$filter'\n"; Line 161  print STDERR "using filter '$filter'\n";
161                                          }                                          }
162                                  }                                  }
163                          }                          }
                         if ($x->{append}) {  
                                 $line_delimiter = ' ';  
                         } else {  
                                 $line_delimiter = '<br/>';  
                         }  
164                  }                  }
165    
166    
 #print STDERR "s_d: $swish_data\nd_d: $display_data\n" if ($swish_data);  
167                  if ($display_data) {                  if ($display_data) {
168    
                         # remove last <br>  
                         $display_data =~ s/$line_delimiter$//;  
   
                         $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);  
   
169                          if ($field eq "headline") {                          if ($field eq "headline") {
170                                  $xml .= xmlify("headline", $display_data);                                  $xml .= xmlify("headline", $display_data);
171                          } else {                          } else {
# Line 181  print STDERR "using filter '$filter'\n"; Line 182  print STDERR "using filter '$filter'\n";
182                                          print STDERR "WARNING: field '$field' doesn't have 'name' attribute!";                                          print STDERR "WARNING: field '$field' doesn't have 'name' attribute!";
183                                  }                                  }
184                                  if ($field_name) {                                  if ($field_name) {
185                                          $html .= $xml_codepage->convert($field_name);                                          $html .= x($field_name);
 #                                       $html .= "-->".$field_name."<--";  
186                                  }                                  }
187                                  $html .= $display_data."###\n";                                  $html .= $display_data."###\n";
188                          }                          }
189                  }                  }
190                  if ($swish_data) {                  if ($swish_data) {
                         my $i = Text::Iconv->new($config->{isis_codepage},'ISO8859-2');  
191                          # remove extra spaces                          # remove extra spaces
192                          $swish_data =~ s/ +/ /g;                          $swish_data =~ s/ +/ /g;
193                          $swish_data =~ s/ +$//g;                          $swish_data =~ s/ +$//g;
194    
195                          $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));  
196                  }                  }
197    
198    
# Line 208  print STDERR "using filter '$filter'\n"; Line 204  print STDERR "using filter '$filter'\n";
204          }          }
205                    
206          if ($xml) {          if ($xml) {
 #print STDERR "x: $xml\n";  
207                  $xml .= $add_xml if ($add_xml);                  $xml .= $add_xml if ($add_xml);
208                  return "<xml>\n$xml</xml>\n";                  return "<xml>\n$xml</xml>\n";
209          } else {          } else {
# Line 222  my $cfg = new Config::IniFiles( -file => Line 217  my $cfg = new Config::IniFiles( -file =>
217    
218  foreach my $database ($cfg->Sections) {  foreach my $database ($cfg->Sections) {
219    
220          my $isis_db = $cfg -> val($database, 'isis_db');          my $isis_db = $cfg -> val($database, 'isis_db') || die "$database doesn't have 'isis_db' defined!";
221          my $type = $cfg -> val($database, 'type');          my $type = $cfg -> val($database, 'type') || die "$database doesn't have 'type' defined";
222          my $add_xml = $cfg -> val($database, 'xml');          my $add_xml = $cfg -> val($database, 'xml');    # optional
223    
         # read configuration for this type  
224          $config=XMLin("./import_xml/$type.xml", forcearray => [ 'isis' ], forcecontent => 1);          $config=XMLin("./import_xml/$type.xml", forcearray => [ 'isis' ], forcecontent => 1);
225          $isis_codepage = Text::Iconv->new($config->{isis_codepage},$XML_CHARSET);  
226          $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'),$XML_CHARSET);  
227    
228          my $db = OpenIsis::open( $isis_db );          my $db = OpenIsis::open( $isis_db );
229          if (0) {          if (0) {
# Line 253  foreach my $database ($cfg->Sections) { Line 246  foreach my $database ($cfg->Sections) {
246          for (my $row_id = 1; $row_id <= $max_rowid; $row_id++ ) {          for (my $row_id = 1; $row_id <= $max_rowid; $row_id++ ) {
247                  my $row = OpenIsis::read( $db, $row_id );                  my $row = OpenIsis::read( $db, $row_id );
248                  if ($row && $row->{mfn}) {                  if ($row && $row->{mfn}) {
 #print STDERR "mfn: ",$row->{mfn},"\n";  
249                          # output current process indicator                          # output current process indicator
250                          my $p = int($row->{mfn} * 100 / $max_rowid);                          my $p = int($row->{mfn} * 100 / $max_rowid);
251                          if ($p != $last_p) {                          if ($p != $last_p) {
# Line 261  foreach my $database ($cfg->Sections) { Line 253  foreach my $database ($cfg->Sections) {
253                                  $last_p = $p;                                  $last_p = $p;
254                          }                          }
255    
256                          if (my $xml = isis2xml($row,$add_xml)) {                          if (my $xml = $cp2utf->convert(isis2xml($row,$add_xml))) {
257  #print STDERR "--ret-->$xml\n";                                  use bytes;      # as opposed to chars
258                                  print "Path-Name: $path#".int($row->{mfn})."\n";                                  print "Path-Name: $path#".int($row->{mfn})."\n";
259                                  print "Content-Length: ".(length($xml)+1)."\n";                                  print "Content-Length: ".(length($xml)+1)."\n";
260                                  print "Document-Type: XML\n\n$xml\n";                                  print "Document-Type: XML\n\n$xml\n";

Legend:
Removed from v.29  
changed lines
  Added in v.42

  ViewVC Help
Powered by ViewVC 1.1.26