File: bins_edit

package info (click to toggle)
bins 1.1.27-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,212 kB
  • ctags: 553
  • sloc: perl: 4,756; xml: 1,768; sh: 428; makefile: 149
file content (436 lines) | stat: -rwxr-xr-x 13,662 bytes parent folder | download
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
#!/usr/bin/perl -w

#    bins_edit for BINS Photo Album version 1.1.27
#    Copyright (C) 2001-2004 Jrme Sautret (Jerome@Sautret.org)
#
#    $Id: bins_edit,v 1.21 2004/10/24 13:19:16 jerome Exp $
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; see the file COPYING.  If not, write to
#    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
#    Boston, MA 02111-1307, USA.

#    Type "bins_edit -h" on command line for usage information.

use strict;

use Getopt::Long;
use IO::File;
use UNIVERSAL qw(isa);

# XML parsing & writing
use XML::Grove;
use XML::Grove::Builder;
use XML::Grove::Path;
use XML::Grove::PerlSAX;
use XML::Parser::PerlSAX;
#use XML::Handler::XMLWriter;
use XML::Handler::YAWriter;
use Text::Iconv;
use HTML::Entities;

my $verbose = 1;
my $html=0;

my $localEncoding;
$localEncoding = `locale charmap`;
if ($? != 0 ) {
  $localEncoding = "LATIN1";
}
else {
if (! $localEncoding or ($localEncoding eq "ANSI_X3.4-1968")) {
  chop($localEncoding);
  # ANSI is unspeakably primitive, promote it.
  $localEncoding = "LATIN1";
  print "Forcing encoding to $localEncoding\n" if ($verbose >=2);
}
}
my $converter = Text::Iconv->new($localEncoding, "UTF-8");

# decode HTML entites which doesn't exist in XML
sub decodeEntites{
  my $s = shift;

  my %entities = (
		 AElig	=> '',	# capital AE diphthong (ligature)
		 Aacute	=> '',	# capital A, acute accent
		 Acirc	=> '',	# capital A, circumflex accent
		 Agrave	=> '',	# capital A, grave accent
		 Aring	=> '',	# capital A, ring
		 Atilde	=> '',	# capital A, tilde
		 Auml	=> '',	# capital A, dieresis or umlaut mark
		 Ccedil	=> '',	# capital C, cedilla
		 ETH	=> '',	# capital Eth, Icelandic
		 Eacute	=> '',	# capital E, acute accent
		 Ecirc	=> '',	# capital E, circumflex accent
		 Egrave	=> '',	# capital E, grave accent
		 Euml	=> '',	# capital E, dieresis or umlaut mark
		 Iacute	=> '',	# capital I, acute accent
		 Icirc	=> '',	# capital I, circumflex accent
		 Igrave	=> '',	# capital I, grave accent
		 Iuml	=> '',	# capital I, dieresis or umlaut mark
		 Ntilde	=> '',	# capital N, tilde
		 Oacute	=> '',	# capital O, acute accent
		 Ocirc	=> '',	# capital O, circumflex accent
		 Ograve	=> '',	# capital O, grave accent
		 Oslash	=> '',	# capital O, slash
		 Otilde	=> '',	# capital O, tilde
		 Ouml	=> '',	# capital O, dieresis or umlaut mark
		 THORN	=> '',	# capital THORN, Icelandic
		 Uacute	=> '',	# capital U, acute accent
		 Ucirc	=> '',	# capital U, circumflex accent
		 Ugrave	=> '',	# capital U, grave accent
		 Uuml	=> '',	# capital U, dieresis or umlaut mark
		 Yacute	=> '',	# capital Y, acute accent
		 aacute	=> '',	# small a, acute accent
		 acirc	=> '',	# small a, circumflex accent
		 aelig	=> '',	# small ae diphthong (ligature)
		 agrave	=> '',	# small a, grave accent
		 aring	=> '',	# small a, ring
		 atilde	=> '',	# small a, tilde
		 auml	=> '',	# small a, dieresis or umlaut mark
		 ccedil	=> '',	# small c, cedilla
		 eacute	=> '',	# small e, acute accent
		 ecirc	=> '',	# small e, circumflex accent
		 egrave	=> '',	# small e, grave accent
		 eth	=> '',	# small eth, Icelandic
		 euml	=> '',	# small e, dieresis or umlaut mark
		 iacute	=> '',	# small i, acute accent
		 icirc	=> '',	# small i, circumflex accent
		 igrave	=> '',	# small i, grave accent
		 iuml	=> '',	# small i, dieresis or umlaut mark
		 ntilde	=> '',	# small n, tilde
		 oacute	=> '',	# small o, acute accent
		 ocirc	=> '',	# small o, circumflex accent
		 ograve	=> '',	# small o, grave accent
		 oslash	=> '',	# small o, slash
		 otilde	=> '',	# small o, tilde
		 ouml	=> '',	# small o, dieresis or umlaut mark
		 szlig	=> '',	# small sharp s, German (sz ligature)
		 thorn	=> '',	# small thorn, Icelandic
		 uacute	=> '',	# small u, acute accent
		 ucirc	=> '',	# small u, circumflex accent
		 ugrave	=> '',	# small u, grave accent
		 uuml	=> '',	# small u, dieresis or umlaut mark
		 yacute	=> '',	# small y, acute accent
		 yuml	=> '',	# small y, dieresis or umlaut mark

		 # Some extra Latin 1 chars that are listed in the HTML3.2 draft (21-May-96)
		 copy   => '',	# copyright sign
		 reg    => '',	# registered sign
		 nbsp   => "\240", # non breaking space

		 # Additional ISO-8859/1 entities listed in rfc1866 (section 14)
		 iexcl  => '',
		 cent   => '',
		 pound  => '',
		 curren => '',
		 yen    => '',
		 brvbar => '',
		 sect   => '',
		 uml    => '',
		 ordf   => '',
		 laquo  => '',
		 'not'   => '', # not is a keyword in perl
		 shy    => '',
		 macr   => '',
		 deg    => '',
		 plusmn => '',
		 sup1   => '',
		 sup2   => '',
		 sup3   => '',
		 acute  => '',
		 micro  => '',
		 para   => '',
		 middot => '',
		 cedil  => '',
		 ordm   => '',
		 raquo  => '',
		 frac14 => '',
		 frac12 => '',
		 frac34 => '',
		 iquest => '',
		 'times' => '', # times is a keyword in perl
		 divide => '',
		);

  while (my($entity, $char) = each(%entities)) {
    $s =~ s/\&$entity\;/$char/g;
  }
  return $s;
}

sub charac_indent{
  my $n = shift(@_);
  my $s="\n";
  for (1..$n){
    $s .= "   ";
  }
  return XML::Grove::Characters->new ( Data => $s );
}

sub setField{
  my $field = shift(@_);  # field to add or modify
  my $value = shift(@_);  # value to set to field
  my $fileType  = shift(@_);  # type of file (iamge or album)
  my $document  = shift(@_);  # XML document as a Grove

  if (! $html) {
    $value = encode_entities($value, '\00-\31<&"');
  }

  my $characters =
    XML::Grove::Characters->new( Data =>
				 decodeEntites($value));
  #my $characters = XML::Grove::Characters->new ( Data => $value );

  my $fieldName;
  my $fieldValue;
  foreach my $element
    (@{$document->at_path('/'.$fileType.'/description')->{Contents}}) {
      if (isa($element, 'XML::Grove::Element') && $element->{Name} eq "field") {
	$fieldName = $element->{Attributes}{'name'};
	$fieldValue = "";
	if ($fieldName eq $field) {
	  print "  Modifying field '$fieldName' to '$value'... "
	    if ($verbose >= 3);
	  @{$element->{Contents}} = ( charac_indent(3),
				      $characters,
				      charac_indent(2));
	  print "OK.\n" if ($verbose >= 3);
	  return;
	}
      }
    }

  print "  Adding field '$field' with value '$value'... " if ($verbose >= 2);
  my $element = XML::Grove::Element->new ( Name => 'field',
					   Contents => [charac_indent(3),
							$characters,
							charac_indent(2)],
					   Attributes => {"name" => $field});
  push @{$document->at_path('/'.$fileType.'/description')->{Contents}},
    (charac_indent(2), $element, charac_indent(1));

  print "OK.\n" if ($verbose >= 2);
}

sub setFields{
  my $file   = shift(@_);
  my $fields = shift(@_);
  my $album  = shift(@_);  # type of file (0 if image or 1 if album)
  my $document;

  my $fileType;
  if ($album) {
    $fileType = "album";
  } else{
    $fileType = "image";
  }

  if (-e $file) {
    # Get XML document as a Grove
    print " Reading file '$file'... " if ($verbose >= 2);
    my $grove_builder = XML::Grove::Builder->new;
    my $parser = XML::Parser::PerlSAX->new ( Handler => $grove_builder );
    $document = $parser->parse ( Source => { SystemId => $file } );
    print "OK.\n" if ($verbose >= 2);
  } else {
    print " Creating file '$file'... " if ($verbose >= 2);
    my @elements;
    push @elements, (charac_indent(1),
		     XML::Grove::Element->new ( Name => 'description',
						Contents => [charac_indent(1)]),
		     charac_indent(1),
		     XML::Grove::Element->new ( Name => 'bins',
						Contents => [charac_indent(1)]),
		    );
    if (!$album) {
      push @elements, ( charac_indent(1),
			XML::Grove::Element->new ( Name => 'exif',
						   Contents =>
						   [charac_indent(1)]),
		      );
    }
    push @elements, charac_indent(0);
    my $element =
      XML::Grove::Element->new ( Name => $fileType,
				 Contents => \@elements);
    $document = XML::Grove::Document->new ( Contents => [ $element ] );
    print "OK.\n" if ($verbose >= 3);
  }

  my $fieldName;
  my $fieldValue;
  while ( ($fieldName, $fieldValue) = each(%$fields) ) {
    if (defined $fieldValue) {
      setField($fieldName, $fieldValue, $fileType, $document);
    }
  }

  print " Writing file '$file'... " if ($verbose >= 2);
  # Write the Grove to the desc file
  my $fileHandler = new IO::File;
  open($fileHandler, '>', $file)
    or die("Cannot open file $file to write Exif tag ($!)");
  binmode($fileHandler, ":utf8") if $^V ge v5.8.0;

  my $my_handler = new XML::Handler::YAWriter( 'Output' => $fileHandler,
					 #      'Escape' => {
					#		    '--'  => '&mdash;',
							    #'&'   => '&amp;',
					#		   },
					       'Encoding' => "UTF-8",
					     );
#  my $my_handler = XML::Handler::XMLWriter->new( Output => $fileHandler,
#  						 Newlines => 0);
  $document->parse(DocumentHandler => $my_handler);
  close ($fileHandler) || bail ("can't close $file ($!)");
  print "OK.\n" if ($verbose >= 2);
}

sub copyleft{
print "\nbins_edit for BINS Photo Album 1.1.27 (http://bins.sautret.org/)\n";
print "Copyright  2001-2004 Jrme Sautret (Jerome\@Sautret.org)\n";
print "This is free software with ABSOLUTELY NO WARRANTY.\n";
print "See COPYING file for details.\n\n";
}

sub usage{
  my $exit=shift; # should we exit after usage information ?
  copyleft();

  print <<EoF ;
bins_edit is a script to set fields in XML pictures description files for BINS.

usage:
bins_edit [-a|--album] [-m|--html]
          [-t|--title title] [-e|--event event] [-l|--location location]
          [-p|--people people] [-y|--date date] [-d|--description description]
	  [--longdesc longDescription] [--shortdesc shortDesription]
          [--sample pictureFileName]
          [-g|--generic tag=value]
          [-h|--help] [-v|--verbose] [-q|--quiet] file [files...]

EoF

  if ($exit){
    print "Type bins_edit --help for complete help.\n";
    exit ($exit);
  }
}


sub help{
  usage(0);
  print <<EoF ;
Options:
  -t, --title, -e, --event event, -l, --location,
  -p, --people, -y, --date, -d, --description :
                   these switchs are used to set a value to a picture 
                   desciption field.
  -t, --title, --longdesc, --shortdesc, --sample :
                   these switchs are used to set a value to an album
                   desciption field (with --album option)
  -a, --album :    edit album description.
                   (default is editing image description)
                   In this case, the file parameter must be the
                   source directory of the album.
                   Only the --title, --longdesc, --shortdesc and --sample
                   switchs have sense with this option.
  -m, --html :     input value will be considering as HTML code, thus,
                   no HTML encoding will be done.
  -v, --verbose :  this switch can appear several times to increase
                   verbosity level.
  -q, --quiet :    suppress output

If filenames have no .xml suffix, it is added, so you can directly give
picture names on the command line.
Spaces and other special characters (even newlines) can be used in values
given as parameters as long as they are enclosed between quotes.

Examples:
Set the title of the Image.jpg file to "My picture":
bins_edit -t "My picture" Image.jpg

Set the title and location of all JPEG pictures in the directory:
bins_edit --title Holiday --location Paris *.jpg

Use of HTML values:
bins_edit --html --description '<b>BINS</b> is cool' file.jpg

Set the title short description and sample image of the album 
in the current directory (note the dot as final parameter):
bins_edit -a -t "My Album" --sample image.jpg --shortdesc "This is my album" .

EoF

  exit 1;
}


sub main{
  my %values;
  my $album = 0;  # 1 if it a album description file


  # process args
  Getopt::Long::Configure("bundling");
  GetOptions('t|title:s'       => \$values{title},
	     'e|event:s'       => \$values{event},
	     'l|location:s'    => \$values{location},
	     'p|people:s'      => \$values{people},
	     'y|date:s'        => \$values{date},
	     'd|description:s' => \$values{description},
	     'longdesc:s'      => \$values{longdesc},
	     'shortdesc:s'     => \$values{shortdesc},
	     'sample:s'        => \$values{sampleimage},
	     'g|generic=s%'    => \%values,
	     'm|html'          => \$html,
	     'a|album'         => \$album,
	     'v|verbose+'      => \$verbose,
	     'q|quiet'         => sub { $verbose = 0 },
	     'h|help'          => sub { help() },
	     'copyright'       => sub { copyleft() },
	    )
    or usage(1);

  my @files;
  if ($#ARGV < 0) {
    if ($album) {
      @files = (".");
    } else {
      print "No files specified.\n";
      usage(1)
    }
  } else {
    @files = @ARGV;
  }

  copyleft() if ($verbose >=2);

  foreach my $file (@files) {
    if ($album) {
      $file .= "/album.xml";
    }
    if ($file !~ m/.xml$/) {
      $file .= ".xml";
    }
    print "Processing file '$file'... " if ($verbose >= 1);
    print "\n" if ($verbose >= 2);
    setFields($file, \%values, $album);
    print "OK.\n" if ($verbose == 1);
  }
}

main();