001package org.hl7.fhir.convertors.misc.xver;
002
003import java.io.FileInputStream;
004import java.io.FileOutputStream;
005import java.io.IOException;
006
007import org.hl7.fhir.convertors.factory.VersionConvertorFactory_10_30;
008import org.hl7.fhir.convertors.factory.VersionConvertorFactory_10_40;
009import org.hl7.fhir.convertors.factory.VersionConvertorFactory_10_50;
010import org.hl7.fhir.convertors.factory.VersionConvertorFactory_14_30;
011import org.hl7.fhir.convertors.factory.VersionConvertorFactory_14_40;
012import org.hl7.fhir.convertors.factory.VersionConvertorFactory_14_50;
013import org.hl7.fhir.convertors.factory.VersionConvertorFactory_30_40;
014import org.hl7.fhir.convertors.factory.VersionConvertorFactory_30_50;
015import org.hl7.fhir.convertors.factory.VersionConvertorFactory_40_50;
016import org.hl7.fhir.utilities.Utilities;
017import org.hl7.fhir.utilities.VersionUtilities;
018import org.hl7.fhir.utilities.filesystem.ManagedFileAccess;
019import org.hl7.fhir.utilities.json.model.JsonArray;
020import org.hl7.fhir.utilities.json.model.JsonObject;
021import org.hl7.fhir.utilities.json.parser.JsonParser;
022import org.hl7.fhir.utilities.npm.NpmPackage;
023import org.hl7.fhir.utilities.npm.NpmPackage.NpmPackageFolder;
024
025
026public class CorePackageVersionConvertor {
027
028  public interface IContentConvertor {
029    byte[] convert(byte[] cnt) throws IOException;
030  }
031
032  public class BaseConvertor {
033    protected String version;
034
035    public BaseConvertor(String version) {
036      super();
037      this.version = version;
038    }
039  }
040
041  public class ContentConverter3to4 extends BaseConvertor implements IContentConvertor {
042
043    public ContentConverter3to4(String version) {
044      super(version);
045    }
046
047    @Override
048    public byte[] convert(byte[] cnt) throws IOException {
049      org.hl7.fhir.dstu3.model.Resource r3 = new org.hl7.fhir.dstu3.formats.JsonParser().parse(cnt);
050      if (r3 instanceof org.hl7.fhir.dstu3.model.MetadataResource) {
051        org.hl7.fhir.dstu3.model.MetadataResource cr = (org.hl7.fhir.dstu3.model.MetadataResource) r3;
052        if (!cr.hasVersion()) {
053          cr.setVersion(version);          
054        }
055      }
056      org.hl7.fhir.r4.model.Resource r4 = VersionConvertorFactory_30_40.convertResource(r3);
057      return new org.hl7.fhir.r4.formats.JsonParser().composeBytes(r4);
058    }
059  }
060  public class ContentConverter2to4 extends BaseConvertor implements IContentConvertor {
061
062    public ContentConverter2to4(String version) {
063      super(version);
064    }
065
066    @Override
067    public byte[] convert(byte[] cnt) throws IOException {
068      org.hl7.fhir.dstu2.model.Resource r2 = new org.hl7.fhir.dstu2.formats.JsonParser().parse(cnt);      
069      org.hl7.fhir.r4.model.Resource r4 = VersionConvertorFactory_10_40.convertResource(r2);
070      if (r4 instanceof org.hl7.fhir.r4.model.MetadataResource) {
071        org.hl7.fhir.r4.model.MetadataResource cr = (org.hl7.fhir.r4.model.MetadataResource) r4;
072        if (!cr.hasVersion()) {
073          cr.setVersion(version);          
074        }
075      }
076      return new org.hl7.fhir.r4.formats.JsonParser().composeBytes(r4);
077    }
078  }
079  public class ContentConverter2Bto4 extends BaseConvertor implements IContentConvertor {
080
081    public ContentConverter2Bto4(String version) {
082      super(version);
083    }
084
085    @Override
086    public byte[] convert(byte[] cnt) throws IOException {
087      org.hl7.fhir.dstu2016may.model.Resource r2 = new org.hl7.fhir.dstu2016may.formats.JsonParser().parse(cnt);      
088      org.hl7.fhir.r4.model.Resource r4 = VersionConvertorFactory_14_40.convertResource(r2);
089      if (r4 instanceof org.hl7.fhir.r4.model.MetadataResource) {
090        org.hl7.fhir.r4.model.MetadataResource cr = (org.hl7.fhir.r4.model.MetadataResource) r4;
091        if (!cr.hasVersion()) {
092          cr.setVersion(version);          
093        }
094      }
095      return new org.hl7.fhir.r4.formats.JsonParser().composeBytes(r4);
096    }
097  }
098  public class ContentConverter5to4 extends BaseConvertor implements IContentConvertor {
099    public ContentConverter5to4(String version) {
100      super(version);
101    }
102
103    @Override
104    public byte[] convert(byte[] cnt) throws IOException {
105      org.hl7.fhir.r5.model.Resource r5 = new org.hl7.fhir.r5.formats.JsonParser().parse(cnt);
106      if (r5 instanceof org.hl7.fhir.r5.model.MetadataResource) {
107        org.hl7.fhir.r5.model.MetadataResource cr = (org.hl7.fhir.r5.model.MetadataResource) r5;
108        if (!cr.hasVersion()) {
109          cr.setVersion(version);          
110        }
111      }
112      org.hl7.fhir.r4.model.Resource r4 = VersionConvertorFactory_40_50.convertResource(r5);
113      return new org.hl7.fhir.r4.formats.JsonParser().composeBytes(r4);
114    }
115  }
116 
117  public class ContentConverter4to3 extends BaseConvertor implements IContentConvertor {
118
119    public ContentConverter4to3(String version) {
120      super(version);
121    }
122
123    @Override
124    public byte[] convert(byte[] cnt) throws IOException {
125      org.hl7.fhir.r4.model.Resource r4 = new org.hl7.fhir.r4.formats.JsonParser().parse(cnt);
126      if (r4 instanceof org.hl7.fhir.r4.model.MessageDefinition) {
127        return null;
128      }
129      if (r4 instanceof org.hl7.fhir.r4.model.GraphDefinition) {
130        return null;
131      }
132      if (r4 instanceof org.hl7.fhir.r4.model.TerminologyCapabilities) {
133        return null;
134      }
135      
136      if (r4 instanceof org.hl7.fhir.r4.model.MetadataResource) {
137        org.hl7.fhir.r4.model.MetadataResource cr = (org.hl7.fhir.r4.model.MetadataResource) r4;
138        if (!cr.hasVersion()) {
139          cr.setVersion(version);          
140        }
141      }
142      org.hl7.fhir.dstu3.model.Resource r3 = VersionConvertorFactory_30_40.convertResource(r4);
143      return new org.hl7.fhir.dstu3.formats.JsonParser().composeBytes(r3);
144    }
145  }
146  public class ContentConverter2to3 extends BaseConvertor implements IContentConvertor {
147
148    public ContentConverter2to3(String version) {
149      super(version);
150    }
151
152    @Override
153    public byte[] convert(byte[] cnt) throws IOException {
154      org.hl7.fhir.dstu2.model.Resource r2 = new org.hl7.fhir.dstu2.formats.JsonParser().parse(cnt);      
155      org.hl7.fhir.dstu3.model.Resource r3 = VersionConvertorFactory_10_30.convertResource(r2);
156      if (r3 instanceof org.hl7.fhir.dstu3.model.MetadataResource) {
157        org.hl7.fhir.dstu3.model.MetadataResource cr = (org.hl7.fhir.dstu3.model.MetadataResource) r3;
158        if (!cr.hasVersion()) {
159          cr.setVersion(version);          
160        }
161      }
162      return new org.hl7.fhir.dstu3.formats.JsonParser().composeBytes(r3);
163    }
164  }
165  public class ContentConverter2Bto3 extends BaseConvertor implements IContentConvertor {
166
167    public ContentConverter2Bto3(String version) {
168      super(version);
169    }
170
171    @Override
172    public byte[] convert(byte[] cnt) throws IOException {
173      org.hl7.fhir.dstu2016may.model.Resource r2 = new org.hl7.fhir.dstu2016may.formats.JsonParser().parse(cnt);  
174      if (r2 instanceof org.hl7.fhir.dstu2016may.model.StructureMap) {
175        return null;
176      }
177      org.hl7.fhir.dstu3.model.Resource r3 = VersionConvertorFactory_14_30.convertResource(r2);
178      if (r3 instanceof org.hl7.fhir.dstu3.model.MetadataResource) {
179        org.hl7.fhir.dstu3.model.MetadataResource cr = (org.hl7.fhir.dstu3.model.MetadataResource) r3;
180        if (!cr.hasVersion()) {
181          cr.setVersion(version);          
182        }
183      }
184      return new org.hl7.fhir.dstu3.formats.JsonParser().composeBytes(r3);
185    }
186  }
187  public class ContentConverter5to3 extends BaseConvertor implements IContentConvertor {
188    public ContentConverter5to3(String version) {
189      super(version);
190    }
191
192    @Override
193    public byte[] convert(byte[] cnt) throws IOException {
194      org.hl7.fhir.r5.model.Resource r5 = new org.hl7.fhir.r5.formats.JsonParser().parse(cnt);
195      if (r5 instanceof org.hl7.fhir.r5.model.TerminologyCapabilities) {
196        return null;
197      }
198      
199      if (r5 instanceof org.hl7.fhir.r5.model.MetadataResource) {
200        org.hl7.fhir.r5.model.MetadataResource cr = (org.hl7.fhir.r5.model.MetadataResource) r5;
201        if (!cr.hasVersion()) {
202          cr.setVersion(version);          
203        }
204      }
205      org.hl7.fhir.dstu3.model.Resource r3 = VersionConvertorFactory_30_50.convertResource(r5);
206      return new org.hl7.fhir.dstu3.formats.JsonParser().composeBytes(r3);
207    }
208  }
209 
210  public class ContentConverter3to5 extends BaseConvertor implements IContentConvertor {
211
212    public ContentConverter3to5(String version) {
213      super(version);
214    }
215
216    @Override
217    public byte[] convert(byte[] cnt) throws IOException {
218      org.hl7.fhir.dstu3.model.Resource r3 = new org.hl7.fhir.dstu3.formats.JsonParser().parse(cnt);
219      if (r3 instanceof org.hl7.fhir.dstu3.model.MetadataResource) {
220        org.hl7.fhir.dstu3.model.MetadataResource cr = (org.hl7.fhir.dstu3.model.MetadataResource) r3;
221        if (!cr.hasVersion()) {
222          cr.setVersion(version);          
223        }
224      }
225      org.hl7.fhir.r5.model.Resource r5 = VersionConvertorFactory_30_50.convertResource(r3);
226      return new org.hl7.fhir.r5.formats.JsonParser().composeBytes(r5);
227    }
228  }
229  public class ContentConverter2to5 extends BaseConvertor implements IContentConvertor {
230
231    public ContentConverter2to5(String version) {
232      super(version);
233    }
234
235    @Override
236    public byte[] convert(byte[] cnt) throws IOException {
237      org.hl7.fhir.dstu2.model.Resource r2 = new org.hl7.fhir.dstu2.formats.JsonParser().parse(cnt);      
238      org.hl7.fhir.r5.model.Resource r5 = VersionConvertorFactory_10_50.convertResource(r2);
239      if (r5 instanceof org.hl7.fhir.r5.model.MetadataResource) {
240        org.hl7.fhir.r5.model.MetadataResource cr = (org.hl7.fhir.r5.model.MetadataResource) r5;
241        if (!cr.hasVersion()) {
242          cr.setVersion(version);          
243        }
244      }
245      return new org.hl7.fhir.r5.formats.JsonParser().composeBytes(r5);
246    }
247  }
248  public class ContentConverter2Bto5 extends BaseConvertor implements IContentConvertor {
249
250    public ContentConverter2Bto5(String version) {
251      super(version);
252    }
253
254    @Override
255    public byte[] convert(byte[] cnt) throws IOException {
256      org.hl7.fhir.dstu2016may.model.Resource r2 = new org.hl7.fhir.dstu2016may.formats.JsonParser().parse(cnt);      
257      org.hl7.fhir.r5.model.Resource r5 = VersionConvertorFactory_14_50.convertResource(r2);
258      if (r5 instanceof org.hl7.fhir.r5.model.MetadataResource) {
259        org.hl7.fhir.r5.model.MetadataResource cr = (org.hl7.fhir.r5.model.MetadataResource) r5;
260        if (!cr.hasVersion()) {
261          cr.setVersion(version);          
262        }
263      }
264      return new org.hl7.fhir.r5.formats.JsonParser().composeBytes(r5);
265    }
266  }
267  public class ContentConverter4to5 extends BaseConvertor implements IContentConvertor {
268    public ContentConverter4to5(String version) {
269      super(version);
270    }
271
272    @Override
273    public byte[] convert(byte[] cnt) throws IOException {
274      org.hl7.fhir.r4.model.Resource r4 = new org.hl7.fhir.r4.formats.JsonParser().parse(cnt);
275      if (r4 instanceof org.hl7.fhir.r4.model.MetadataResource) {
276        org.hl7.fhir.r4.model.MetadataResource cr = (org.hl7.fhir.r4.model.MetadataResource) r4;
277        if (!cr.hasVersion()) {
278          cr.setVersion(version);          
279        }
280      }
281      org.hl7.fhir.r5.model.Resource r5 = VersionConvertorFactory_40_50.convertResource(r4);
282      return new org.hl7.fhir.r5.formats.JsonParser().composeBytes(r5);
283    }
284  }
285
286  public class ContentConverter5to2 extends BaseConvertor implements IContentConvertor {
287
288    public ContentConverter5to2(String version) {
289      super(version);
290    }
291
292    @Override
293    public byte[] convert(byte[] cnt) throws IOException {
294      org.hl7.fhir.r5.model.Resource r5 = new org.hl7.fhir.r5.formats.JsonParser().parse(cnt);      
295      if (r5 instanceof org.hl7.fhir.r5.model.MetadataResource) {
296        org.hl7.fhir.r5.model.MetadataResource cr = (org.hl7.fhir.r5.model.MetadataResource) r5;
297        if (!cr.hasVersion()) {
298          cr.setVersion(version);          
299        }
300      }
301      org.hl7.fhir.dstu2.model.Resource r2 = VersionConvertorFactory_10_50.convertResource(r5);
302      return new org.hl7.fhir.dstu2.formats.JsonParser().composeBytes(r2);
303    }
304  }
305
306  public class ContentConverter5to2B extends BaseConvertor implements IContentConvertor {
307
308    public ContentConverter5to2B(String version) {
309      super(version);
310    }
311
312    @Override
313    public byte[] convert(byte[] cnt) throws IOException {
314      org.hl7.fhir.r5.model.Resource r5 = new org.hl7.fhir.r5.formats.JsonParser().parse(cnt);      
315      if (r5 instanceof org.hl7.fhir.r5.model.MessageDefinition) {
316        return null;
317      }
318      if (r5 instanceof org.hl7.fhir.r5.model.GraphDefinition) {
319        return null;
320      }
321      if (r5 instanceof org.hl7.fhir.r5.model.TerminologyCapabilities) {
322        return null;
323      }
324      
325      if (r5 instanceof org.hl7.fhir.r5.model.MetadataResource) {
326        org.hl7.fhir.r5.model.MetadataResource cr = (org.hl7.fhir.r5.model.MetadataResource) r5;
327        if (!cr.hasVersion()) {
328          cr.setVersion(version);          
329        }
330      }
331      org.hl7.fhir.dstu2016may.model.Resource r2 = VersionConvertorFactory_14_50.convertResource(r5);
332      return new org.hl7.fhir.dstu2016may.formats.JsonParser().composeBytes(r2);
333    }
334  }
335
336  public class ContentConverter4to2 extends BaseConvertor implements IContentConvertor {
337
338    public ContentConverter4to2(String version) {
339      super(version);
340    }
341
342    @Override
343    public byte[] convert(byte[] cnt) throws IOException {
344      org.hl7.fhir.r4.model.Resource r4 = new org.hl7.fhir.r4.formats.JsonParser().parse(cnt);      
345      if (r4 instanceof org.hl7.fhir.r4.model.MetadataResource) {
346        org.hl7.fhir.r4.model.MetadataResource cr = (org.hl7.fhir.r4.model.MetadataResource) r4;
347        if (!cr.hasVersion()) {
348          cr.setVersion(version);          
349        }
350      }
351      org.hl7.fhir.dstu2.model.Resource r2 = VersionConvertorFactory_10_40.convertResource(r4);
352      return new org.hl7.fhir.dstu2.formats.JsonParser().composeBytes(r2);
353    }
354  }
355
356  public class ContentConverter4to2B extends BaseConvertor implements IContentConvertor {
357
358    public ContentConverter4to2B(String version) {
359      super(version);
360    }
361
362    @Override
363    public byte[] convert(byte[] cnt) throws IOException {
364      org.hl7.fhir.r4.model.Resource r4 = new org.hl7.fhir.r4.formats.JsonParser().parse(cnt);  
365      if (r4 instanceof org.hl7.fhir.r4.model.MessageDefinition) {
366        return null;
367      }
368      if (r4 instanceof org.hl7.fhir.r4.model.GraphDefinition) {
369        return null;
370      }
371      if (r4 instanceof org.hl7.fhir.r4.model.TerminologyCapabilities) {
372        return null;
373      }
374      if (r4 instanceof org.hl7.fhir.r4.model.MetadataResource) {
375        org.hl7.fhir.r4.model.MetadataResource cr = (org.hl7.fhir.r4.model.MetadataResource) r4;
376        if (!cr.hasVersion()) {
377          cr.setVersion(version);          
378        }
379      }
380      org.hl7.fhir.dstu2016may.model.Resource r2 = VersionConvertorFactory_14_40.convertResource(r4);
381      return new org.hl7.fhir.dstu2016may.formats.JsonParser().composeBytes(r2);
382    }
383  }
384
385  public class ContentConverter3to2 extends BaseConvertor implements IContentConvertor {
386
387    public ContentConverter3to2(String version) {
388      super(version);
389    }
390
391    @Override
392    public byte[] convert(byte[] cnt) throws IOException {
393      org.hl7.fhir.dstu3.model.Resource r3 = new org.hl7.fhir.dstu3.formats.JsonParser().parse(cnt);      
394      if (r3 instanceof org.hl7.fhir.dstu3.model.MetadataResource) {
395        org.hl7.fhir.dstu3.model.MetadataResource cr = (org.hl7.fhir.dstu3.model.MetadataResource) r3;
396        if (!cr.hasVersion()) {
397          cr.setVersion(version);          
398        }
399      }
400      org.hl7.fhir.dstu2.model.Resource r2 = VersionConvertorFactory_10_30.convertResource(r3);
401      return new org.hl7.fhir.dstu2.formats.JsonParser().composeBytes(r2);
402    }
403  }
404
405  public class ContentConverter3to2B extends BaseConvertor implements IContentConvertor {
406
407    public ContentConverter3to2B(String version) {
408      super(version);
409    }
410
411    @Override
412    public byte[] convert(byte[] cnt) throws IOException {
413      org.hl7.fhir.dstu3.model.Resource r3 = new org.hl7.fhir.dstu3.formats.JsonParser().parse(cnt);      
414      if (r3 instanceof org.hl7.fhir.dstu3.model.StructureMap) {
415        return null;
416      }
417
418      if (r3 instanceof org.hl7.fhir.dstu3.model.MetadataResource) {
419        org.hl7.fhir.dstu3.model.MetadataResource cr = (org.hl7.fhir.dstu3.model.MetadataResource) r3;
420        if (!cr.hasVersion()) {
421          cr.setVersion(version);          
422        }
423      }
424      org.hl7.fhir.dstu2016may.model.Resource r2 = VersionConvertorFactory_14_30.convertResource(r3);
425      return new org.hl7.fhir.dstu2016may.formats.JsonParser().composeBytes(r2);
426    }
427  }
428
429  public class ContentConverter4to4 extends BaseConvertor implements IContentConvertor {
430
431    public ContentConverter4to4(String version) {
432      super(version);
433    }
434
435    @Override
436    public byte[] convert(byte[] cnt) throws IOException {
437      org.hl7.fhir.r4.model.Resource r4 = new org.hl7.fhir.r4.formats.JsonParser().parse(cnt);      
438      if (r4 instanceof org.hl7.fhir.r4.model.MetadataResource) {
439        org.hl7.fhir.r4.model.MetadataResource cr = (org.hl7.fhir.r4.model.MetadataResource) r4;
440        if (!cr.hasVersion()) {
441          cr.setVersion(version);          
442        }
443      }
444      return new org.hl7.fhir.r4.formats.JsonParser().composeBytes(r4);
445    }
446  }
447
448
449  public static void main(String[] args) throws Exception {
450    new CorePackageVersionConvertor().execute(Utilities.path(args[0], "hl7.fhir.r2b.core.tgz"),  "3.0.1");
451    new CorePackageVersionConvertor().execute(Utilities.path(args[0], "hl7.fhir.r2b.core.tgz"),  "4.0.1");
452    new CorePackageVersionConvertor().execute(Utilities.path(args[0], "hl7.fhir.r2b.core.tgz"),  "4.3.0");
453    new CorePackageVersionConvertor().execute(Utilities.path(args[0], "hl7.fhir.r2b.core.tgz"),  "5.0.0");
454
455    new CorePackageVersionConvertor().execute(Utilities.path(args[0], "hl7.fhir.r3.core.tgz"),  "1.4.0");
456    new CorePackageVersionConvertor().execute(Utilities.path(args[0], "hl7.fhir.r3.core.tgz"),  "4.0.1");
457    new CorePackageVersionConvertor().execute(Utilities.path(args[0], "hl7.fhir.r3.core.tgz"),  "4.3.0");
458    new CorePackageVersionConvertor().execute(Utilities.path(args[0], "hl7.fhir.r3.core.tgz"),  "5.0.0");
459
460    new CorePackageVersionConvertor().execute(Utilities.path(args[0], "hl7.fhir.r4.core.tgz"),  "1.4.0");
461    new CorePackageVersionConvertor().execute(Utilities.path(args[0], "hl7.fhir.r4.core.tgz"),  "3.0.1");
462    new CorePackageVersionConvertor().execute(Utilities.path(args[0], "hl7.fhir.r4.core.tgz"),  "4.3.0");
463    new CorePackageVersionConvertor().execute(Utilities.path(args[0], "hl7.fhir.r4.core.tgz"),  "5.0.0");
464
465    new CorePackageVersionConvertor().execute(Utilities.path(args[0], "hl7.fhir.r4b.core.tgz"),  "1.4.0");
466    new CorePackageVersionConvertor().execute(Utilities.path(args[0], "hl7.fhir.r4b.core.tgz"),  "3.0.1");
467    new CorePackageVersionConvertor().execute(Utilities.path(args[0], "hl7.fhir.r4b.core.tgz"),  "4.0.1");
468    new CorePackageVersionConvertor().execute(Utilities.path(args[0], "hl7.fhir.r4b.core.tgz"),  "5.0.0");
469
470    new CorePackageVersionConvertor().execute(Utilities.path(args[0], "hl7.fhir.r5.core.tgz"),  "1.4.0");
471    new CorePackageVersionConvertor().execute(Utilities.path(args[0], "hl7.fhir.r5.core.tgz"),  "3.0.1");
472    new CorePackageVersionConvertor().execute(Utilities.path(args[0], "hl7.fhir.r5.core.tgz"),  "4.0.1");
473    new CorePackageVersionConvertor().execute(Utilities.path(args[0], "hl7.fhir.r5.core.tgz"),  "4.3.0");
474
475  }
476
477  public void execute(String packageSource, String versionTarget) throws Exception {
478    // open the existing package 
479    // chose the version converter 
480    // build a new package
481    System.out.println("Convert "+packageSource+" to "+versionTarget);
482    
483    NpmPackage src = NpmPackage.fromPackage(ManagedFileAccess.inStream(packageSource));
484    IContentConvertor conv = contentConvertorFactory(src.fhirVersion(), versionTarget);
485    
486    NpmPackage dst = NpmPackage.empty();
487    
488    for (NpmPackageFolder folder : src.getFolders().values()) {
489      for (String s : folder.getContent().keySet()) {
490        byte[] cnt = folder.getContent().get(s);
491        if (s.endsWith(".json")) {
492          if (s.equals("package.json")) {
493           cnt = convertPackage(cnt, versionTarget); 
494          } else {
495            String sJ = new String(cnt);
496            if (sJ.contains("\"resourceType\"") && !s.equals(".index.json") && !s.endsWith(".schema.json")) {
497              try {
498                cnt = conv.convert(cnt);
499              } catch (Exception e) {
500                throw new Exception("Error processing "+folder.getFolderName()+"/"+s+": "+e.getMessage(), e);
501              }
502            } else {
503              // nothing
504            }
505          }        
506          if (cnt != null) {
507            dst.addFile(folder.getFolderName(), s, cnt, null);
508          }
509        } else {
510          dst.addFile(folder.getFolderName(), s, cnt, null);
511        }
512      }
513    }
514    dst.save(ManagedFileAccess.outStream(Utilities.changeFileExt(packageSource, ".as."+VersionUtilities.getNameForVersion(versionTarget).toLowerCase()+".tgz")));
515  }
516
517  private IContentConvertor contentConvertorFactory(String fhirVersion, String versionTarget) throws Exception {
518    if (VersionUtilities.isR3Ver(fhirVersion)) {
519      if (VersionUtilities.isR4Ver(versionTarget) || VersionUtilities.isR4BVer(versionTarget)) {
520        return new ContentConverter3to4(fhirVersion);
521      } else if (VersionUtilities.isR5Plus(versionTarget)) {
522        return new ContentConverter3to5(fhirVersion);
523      } else if (VersionUtilities.isR2Ver(versionTarget)) {
524        return new ContentConverter3to2(fhirVersion);
525      } else if (VersionUtilities.isR2BVer(versionTarget)) {
526        return new ContentConverter3to2B(fhirVersion);
527      }
528    } else if (VersionUtilities.isR2Ver(fhirVersion)) {
529      if (VersionUtilities.isR4Ver(versionTarget) || VersionUtilities.isR4BVer(versionTarget)) {
530        return new ContentConverter2to4(fhirVersion);
531      } else if (VersionUtilities.isR5Plus(versionTarget)) {
532        return new ContentConverter2to5(fhirVersion);
533      } else if (VersionUtilities.isR3Ver(versionTarget)) {
534        return new ContentConverter2to3(fhirVersion);
535      }
536    } else if (VersionUtilities.isR2BVer(fhirVersion)) {
537      if (VersionUtilities.isR4Ver(versionTarget) || VersionUtilities.isR4BVer(versionTarget)) {
538        return new ContentConverter2Bto4(fhirVersion);
539      } else if (VersionUtilities.isR5Plus(versionTarget)) {
540        return new ContentConverter2Bto5(fhirVersion);
541      } else if (VersionUtilities.isR3Ver(versionTarget)) {
542        return new ContentConverter2Bto3(fhirVersion);
543      }
544    } else if (VersionUtilities.isR4Ver(fhirVersion)) {
545      if (VersionUtilities.isR3Ver(versionTarget)) {
546        return new ContentConverter4to3(fhirVersion);
547      } else if (VersionUtilities.isR5Plus(versionTarget)) {
548        return new ContentConverter4to5(fhirVersion);
549      } else if (VersionUtilities.isR2Ver(versionTarget)) {
550        return new ContentConverter4to2(fhirVersion);
551      } else if (VersionUtilities.isR4BVer(versionTarget)) {
552        return new ContentConverter4to4(fhirVersion);
553      } else if (VersionUtilities.isR2BVer(versionTarget)) {
554        return new ContentConverter4to2B(fhirVersion);
555      }
556    } else if (VersionUtilities.isR4BVer(fhirVersion)) {
557      if (VersionUtilities.isR3Ver(versionTarget)) {
558        return new ContentConverter4to3(fhirVersion);
559      } else if (VersionUtilities.isR5Plus(versionTarget)) {
560        return new ContentConverter4to5(fhirVersion);
561      } else if (VersionUtilities.isR4Ver(versionTarget)) {
562        return new ContentConverter4to4(fhirVersion);
563      } else if (VersionUtilities.isR2Ver(versionTarget)) {
564        return new ContentConverter4to2(fhirVersion);
565      } else if (VersionUtilities.isR2BVer(versionTarget)) {
566        return new ContentConverter4to2B(fhirVersion);
567      }
568    } else     if (VersionUtilities.isR5Plus(fhirVersion)) {
569      if (VersionUtilities.isR4Ver(versionTarget) || VersionUtilities.isR4BVer(versionTarget)) {
570        return new ContentConverter5to4(fhirVersion);
571      } else if (VersionUtilities.isR3Ver(versionTarget)) {
572        return new ContentConverter5to3(fhirVersion);
573      } else if (VersionUtilities.isR2Ver(versionTarget)) {
574        return new ContentConverter5to2(fhirVersion);
575      } else if (VersionUtilities.isR2BVer(versionTarget)) {
576        return new ContentConverter5to2B(fhirVersion);
577      }
578    }
579    throw new Exception("Unable to convert from "+fhirVersion+" to "+versionTarget);
580  }
581
582  private byte[] convertPackage(byte[] cnt, String version) throws IOException {
583    JsonObject json = JsonParser.parseObject(cnt);
584    json.remove("fhir-version-list");
585    JsonArray vl = new JsonArray();
586    json.add("fhirVersions", vl);
587    vl.add(version);
588    json.add("name", json.asString("name")+".as."+VersionUtilities.getNameForVersion(version).toLowerCase());
589    json.add("title", json.asString("title")+" (as Version "+VersionUtilities.getNameForVersion(version).toLowerCase()+")");
590    return JsonParser.composeBytes(json);
591  }
592}