JAL-3420 JAL-3394 Using install4j8 template with file associations. Attempts unix...
[jalview.git] / utils / install4j / auto_file_associations-i4j8.pl
1 #!/usr/bin/env perl
2
3 use strict;
4
5 my $i4jversion = 8;
6 if ($ARGV[0] eq "-v") {
7   shift @ARGV;
8   $i4jversion = shift @ARGV;
9   die("-v i4jversion must be an integer [probably 7 or 8]") unless $i4jversion =~ m/^\d+$/;
10 }
11
12 my $fileformats = $ARGV[0];
13 $fileformats = "../../src/jalview/io/FileFormat.java" unless $fileformats;
14
15 # default mimetype will be text/x-$shortname
16 # TODO: find an actual extension for mat, see JAL-Xxxxx for outstanding issues too
17 # TODO: look up standard mime type used for BLASTfmt matrices, etc
18 my $mimetypes = {
19   rnaml => "application/rnaml+xml",
20   biojson => "application/x-jalview-biojson+json",
21   jnet => "application/x-jalview-jnet+text",
22   features => "application/x-jalview-features+text",
23   scorematrix => "application/x-jalview-scorematrix+text",
24   pdb => "chemical/x-pdb",
25   mmcif => "chemical/x-cif",
26   mmcif2 => "chemical/x-mcif",
27   jalview => "application/x-jalview+xml+zip",
28   jvl => "application/x-jalview-jvl+text",
29   annotations => "application/x-jalview-annotations+text",
30 };
31
32 my @dontaddshortname = qw(features json);
33 my @dontaddextension = qw(html xml json jar mfa fastq);
34 my $add_associations = {
35   biojson => {shortname=>"biojson",name=>"BioJSON",extensions=>["biojson"]},
36   gff2 => {shortname=>"gff2",name=>"Generic Features Format v2",extensions=>["gff2"]},
37   gff3 => {shortname=>"gff3",name=>"Generic Features Format v3",extensions=>["gff3"]},
38   features => {shortname=>"features",name=>"Jalview Features",extensions=>["features","jvfeatures"]},
39   annotations => {shortname=>"annotations",name=>"Jalview Annotations",extensions=>["annotations","jvannotations"]},
40   mmcif2 => {shortname=>"mmcif2",name=>"mmCIF",extensions=>["mcif","mmcif"]},
41   jvl => {shortname=>"jvl",name=>"Jalview Version Locator",extensions=>["jvl"],iconfile=>"Jalview-Version-Locator"},
42   jnet => {shortname=>"jnet",name=>"JnetFile",extensions=>["concise","jnet"]},
43   scorematrix => {shortname=>"scorematrix",name=>"Substitution Matrix",extensions=>["mat"]},
44 };
45 my $add_extensions = {
46   blc => ["blc"],
47 };
48 my @put_first = qw(jalview jvl);
49
50 my $v = ($i4jversion >= 8)?$i4jversion:"";
51 my $i4jtemplatefile = "file_associations_template-install4j${v}.xml";
52 my $i4jtemplate;
53 open(IT,"<$i4jtemplatefile") or dir("Could not open '$i4jtemplatefile' for reading");
54 while(<IT>){
55   $i4jtemplate .= $_;
56 }
57 close(IT);
58 my $i4jauto;
59
60 my $i4jautofile = $i4jtemplatefile;
61 $i4jautofile =~ s/template/auto$1/;
62
63 open(IA,">$i4jautofile") or die ("Could not open '$i4jautofile' for writing");
64
65 open(IN, "<$fileformats") or die ("Could not open '$fileformats' for reading");
66 my $id = 10000;
67 my $file_associations = {};
68 while(my $line = <IN>) {
69   $line =~ s/\s+/ /g;
70   $line =~ s/(^ | $)//g;
71   if ($line =~ m/^(\w+) ?\( ?"([^"]*)" ?, ?"([^"]*)" ?, ?(true|false) ?, ?(true|false) ?\)$/i) {
72     my $shortname = lc($1);
73     next if (grep($_ eq $shortname, @dontaddshortname));
74     my $name = $2;
75     my $extensions = $3;
76     $extensions =~ s/\s+//g;
77     my @possextensions = split(m/,/,$extensions);
78     my @extensions;
79     my $addext = $add_extensions->{$shortname};
80     if (ref($addext) eq "ARRAY") {
81       push(@possextensions, @$addext);
82     }
83     for my $possext (@possextensions) {
84       next if grep($_ eq $possext, @extensions);
85       next if grep($_ eq $possext, @dontaddextension);
86       push(@extensions,$possext);
87     }
88     next unless scalar(@extensions);
89     $file_associations->{$shortname} = {
90       shortname => $shortname,
91       name => $name,
92       extensions => \@extensions
93     };
94     warn("Adding file association for $shortname\n");
95   }
96 }
97 close(IN);
98
99 my %all_associations = (%$file_associations, %$add_associations);
100
101 for my $shortname (@put_first, sort keys %all_associations) {
102   my $a = $all_associations{$shortname};
103   if (ref($a) ne "HASH") {
104     next;
105   }
106
107   my $name = $a->{name};
108   my $extensions = $a->{extensions};
109   my $mimetype = $mimetypes->{$shortname};
110   $mimetype = "text/x-$shortname" unless $mimetype;
111
112   my $iconfile = $a->{iconfile};
113   $iconfile = "Jalview-File" unless $iconfile;
114
115   my @extensions = @$extensions;
116   #warn("LINE: $line\nFound extensions '".join("', '", @extensions)."' for $name Files ($shortname)'n");
117
118   my $i4jentry = $i4jtemplate;
119
120   $i4jentry =~ s/\$\$NAME\$\$/$name/g;
121   $i4jentry =~ s/\$\$SHORTNAME\$\$/$shortname/g;
122   $i4jentry =~ s/\$\$MIMETYPE\$\$/$mimetype/g;
123   $i4jentry =~ s/\$\$ICONFILE\$\$/$iconfile/g;
124
125   for my $ext (@extensions) {
126     my $i4jextentry = $i4jentry;
127     $i4jextentry =~ s/\$\$EXTENSION\$\$/$ext/g;
128     $i4jextentry =~ s/\$\$ID\$\$/$id/g;
129     $id++;
130
131     print IA $i4jextentry;
132   }
133
134   delete $all_associations{$shortname};
135 }
136
137 close(IA);