96aee95e5bbde478c288aa12d6cc5b50e62d21ff
[jalview.git] / utils / install4j / auto_file_associations-i4j10.pl
1 #!/usr/bin/env perl
2
3 use strict;
4
5 my $i4jversion = 10;
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-mmcif",
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 # mfa and fastq not properly supported in Jalview (but enough to be in the application extensions)
34 # ENA flatfile extension txt is too generic, as well as html xml json jar
35 my @dontaddextension = qw(txt html xml json jar mfa fastq);
36 my $add_associations = {
37   biojson => {shortname=>"biojson",name=>"BioJSON",extensions=>["biojson"]},
38   gff2 => {shortname=>"gff2",name=>"Generic Features Format v2",extensions=>["gff2"]},
39   gff3 => {shortname=>"gff3",name=>"Generic Features Format v3",extensions=>["gff3"]},
40   features => {shortname=>"features",name=>"Jalview Features",extensions=>["features","jvfeatures"]},
41   annotations => {shortname=>"annotations",name=>"Jalview Annotations",extensions=>["annotations","jvannotations"]},
42   mmcif => {shortname=>"mmcif",name=>"CIF",extensions=>["cif"]},
43   mmcif2 => {shortname=>"mmcif2",name=>"mmCIF",extensions=>["mcif","mmcif"]},
44   jvl => {shortname=>"jvl",name=>"Jalview Launch",extensions=>["jvl"],iconfile=>"jvl_file"},
45   jnet => {shortname=>"jnet",name=>"JnetFile",extensions=>["concise","jnet"]},
46   scorematrix => {shortname=>"scorematrix",name=>"Substitution Matrix",extensions=>["mat"]},
47 };
48 my $add_extensions = {
49   blc => ["blc"],
50 };
51 my @put_first = qw(jalview jvl);
52 my @owner = @put_first;
53
54 my @non_primary = qw(mmcif mmcif2 pdb txt);
55
56 my $v = ($i4jversion >= 8)?$i4jversion:"";
57 my $i4jtemplatefile = "file_associations_template-install4j${v}.xml";
58 my $i4jtemplate;
59 my $mactemplatefile = "file_associations_template-Info_plist.xml";
60 my $mactemplate;
61
62 open(MT,"<$mactemplatefile") or die("Could not open '$mactemplatefile' for reading");
63 while(<MT>){
64   $mactemplate .= $_;
65 }
66 close(MT);
67 open(IT,"<$i4jtemplatefile") or die("Could not open '$i4jtemplatefile' for reading");
68 while(<IT>){
69   $i4jtemplate .= $_;
70 }
71 close(IT);
72 my $macauto;
73 my $i4jauto;
74
75 my $macautofile = $mactemplatefile;
76 $macautofile =~ s/template/auto$1/;
77
78 my $i4jautofile = $i4jtemplatefile;
79 $i4jautofile =~ s/template/auto$1/;
80
81 for my $key (sort keys %$add_associations) {
82   my $a = $add_associations->{$key};
83   warn("Known file association for $a->{shortname} (".join(",",@{$a->{extensions}}).")\n");
84 }
85
86 open(MA,">$macautofile") or die ("Could not open '$macautofile' for writing");
87 print MA "<key>CFBundleDocumentTypes</key>\n<array>\n\n";
88
89 open(IA,">$i4jautofile") or die ("Could not open '$i4jautofile' for writing");
90
91 open(IN, "<$fileformats") or die ("Could not open '$fileformats' for reading");
92 my $id = 10000;
93 my $file_associations = {};
94 while(my $line = <IN>) {
95   $line =~ s/\s+/ /g;
96   $line =~ s/(^ | $)//g;
97   if ($line =~ m/^(\w+) ?\( ?"([^"]*)" ?, ?"([^"]*)" ?, ?(true|false) ?, ?(true|false) ?\)$/i) {
98     my $shortname = lc($1);
99     next if (grep($_ eq $shortname, @dontaddshortname));
100     my $name = $2;
101     my $extensions = $3;
102     $extensions =~ s/\s+//g;
103     my @possextensions = map(lc($_),split(m/,/,$extensions));
104     my @extensions;
105     my $addext = $add_extensions->{$shortname};
106     if (ref($addext) eq "ARRAY") {
107       push(@possextensions, @$addext);
108     }
109     for my $possext (@possextensions) {
110       next if grep($_ eq $possext, @extensions);
111       next if grep($_ eq $possext, @dontaddextension);
112       push(@extensions,$possext);
113     }
114     next unless scalar(@extensions);
115     $file_associations->{$shortname} = {
116       shortname => $shortname,
117       name => $name,
118       extensions => \@extensions
119     };
120     warn("Reading file association for $shortname (".join(",",@extensions).")\n");
121   }
122 }
123 close(IN);
124
125 my %all_associations = (%$file_associations, %$add_associations);
126
127 my @ordered = (@put_first, @non_primary);
128 for my $key (sort keys %all_associations) {
129   next if grep($_ eq $key, @ordered);
130   push(@ordered, $key);
131 }
132 my $num = $#ordered + 1;
133
134 warn("--\n");
135
136 my $i4jcount = 0;
137 for my $shortname (@ordered) {
138   my $a = $all_associations{$shortname};
139   next if (ref($a) ne "HASH");
140
141   my $name = $a->{name};
142   my $extensions = $a->{extensions};
143   my $mimetype = $mimetypes->{$shortname};
144   $mimetype = "application/x-$shortname+txt" unless $mimetype;
145
146   my $iconfile = $a->{iconfile};
147   $iconfile = "Jalview-File" unless $iconfile;
148
149   my $owner = grep($_ eq $shortname, @owner);
150   my $primary = (! grep($_ eq $shortname, @non_primary));
151   my $primarystring = $primary?"true":"false";
152   #my $role = $owner?"Owner":($primary?"Editor":"Viewer");
153   my $role = $primary?"Editor":"Viewer";
154   my $rank = $owner?"Owner":($primary?"Default":"Alternate");
155
156   my @extensions = @$extensions;
157
158   my $xname = xml_escape($name);
159   my $xmimetype = xml_escape($mimetype);
160   my $xshortname = xml_escape($shortname);
161   my $xiconfile = xml_escape($iconfile);
162   my $xrole = xml_escape($role);
163   my $xROLE = xml_escape(uc($role));
164   my $xrank = xml_escape($rank);
165   my $xprimarystring = xml_escape($primarystring);
166
167   my $macentry = $mactemplate;
168   $macentry =~ s/\$\$NAME\$\$/$xname/g;
169   $macentry =~ s/\$\$SHORTNAME\$\$/$xshortname/g;
170   $macentry =~ s/\$\$MIMETYPE\$\$/$xmimetype/g;
171   $macentry =~ s/\$\$ICONFILE\$\$/$xiconfile/g;
172   $macentry =~ s/\$\$ROLE\$\$/$xrole/g;
173   $macentry =~ s/\$\$RANK\$\$/$xrank/g;
174   $macentry =~ s/\$\$PRIMARY\$\$/$xprimarystring/g;
175   while ($macentry =~ m/\$\$([^\$]*)EXTENSIONS([^\$]*)\$\$/) {
176     my $pre = $1;
177     my $post = $2;
178     my $macextensions;
179     for my $ext (@extensions) {
180       my $xext = xml_escape($ext);
181       $macextensions .= $pre.$xext.$post;
182     }
183     $macentry =~ s/\$\$${pre}EXTENSIONS${post}\$\$/$macextensions/g;
184   }
185   print MA $macentry;
186
187   my $i4jentry = $i4jtemplate;
188   $i4jentry =~ s/\$\$NAME\$\$/$xname/g;
189   $i4jentry =~ s/\$\$SHORTNAME\$\$/$xshortname/g;
190   $i4jentry =~ s/\$\$MIMETYPE\$\$/$xmimetype/g;
191   $i4jentry =~ s/\$\$ICONFILE\$\$/$xiconfile/g;
192   $i4jentry =~ s/\$\$PRIMARY\$\$/$xprimarystring/g;
193   $i4jentry =~ s/\$\$MACASSOCIATIONROLE\$\$/$xROLE/g;
194
195   my $ext = join(",",sort(@extensions));
196   my $xdisplayext = xml_escape(join(", ", map(".$_",sort(@extensions))));
197   my $progresspercent = int(($i4jcount/$num)*100);
198   $progresspercent = 100 if $progresspercent > 100;
199   $i4jcount++;
200   my $xext = xml_escape($ext);
201   my $addunixextension = "true";
202
203   $i4jentry =~ s/\$\$ADDUNIXEXTENSION\$\$/$addunixextension/g;
204   $i4jentry =~ s/\$\$EXTENSION\$\$/$xext/g;
205   $i4jentry =~ s/\$\$DISPLAYEXTENSION\$\$/$xdisplayext/g;
206   $i4jentry =~ s/\$\$PROGRESSPERCENT\$\$/$progresspercent/g;
207   $i4jentry =~ s/\$\$ID\$\$/$id/g;
208   $id++;
209   $i4jentry =~ s/\$\$ID1\$\$/$id/g;
210   $id++;
211   $i4jentry =~ s/\$\$ID2\$\$/$id/g;
212   $id++;
213
214   print IA $i4jentry;
215
216   delete $all_associations{$shortname};
217   warn("Writing entry for $name (".join(",",@$extensions).": $mimetype)\n");
218 }
219
220 close(IA);
221 print MA "</array>\n";
222 close(MA);
223
224 sub xml_escape {
225   my $x = shift;
226   # stolen from Pod::Simple::XMLOutStream in base distro
227   $x =~ s/([^-\n\t !\#\$\%\(\)\*\+,\.\~\/\:\;=\?\@\[\\\]\^_\`\{\|\}a-zA-Z0-9])/'&#'.(ord($1)).';'/eg;
228   return $x;
229 }