001package org.hl7.fhir.convertors.misc;
002
003import java.io.File;
004import java.io.FileInputStream;
005import java.io.FileOutputStream;
006import java.io.IOException;
007import java.util.Date;
008
009import javax.annotation.Nonnull;
010
011/*
012  Copyright (c) 2011+, HL7, Inc.
013  All rights reserved.
014  
015  Redistribution and use in source and binary forms, with or without modification, 
016  are permitted provided that the following conditions are met:
017    
018   * Redistributions of source code must retain the above copyright notice, this 
019     list of conditions and the following disclaimer.
020   * Redistributions in binary form must reproduce the above copyright notice, 
021     this list of conditions and the following disclaimer in the documentation 
022     and/or other materials provided with the distribution.
023   * Neither the name of HL7 nor the names of its contributors may be used to 
024     endorse or promote products derived from this software without specific 
025     prior written permission.
026  
027  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
028  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
029  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
030  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
031  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
032  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
033  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
034  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
035  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
036  POSSIBILITY OF SUCH DAMAGE.
037  
038 */
039
040
041import org.hl7.fhir.convertors.advisors.impl.BaseAdvisor_10_30;
042import org.hl7.fhir.convertors.factory.VersionConvertorFactory_10_30;
043import org.hl7.fhir.dstu3.formats.IParser.OutputStyle;
044import org.hl7.fhir.dstu3.model.Bundle;
045import org.hl7.fhir.dstu3.model.Bundle.BundleEntryComponent;
046import org.hl7.fhir.dstu3.model.Bundle.BundleType;
047import org.hl7.fhir.dstu3.model.CodeSystem;
048import org.hl7.fhir.dstu3.model.ValueSet;
049import org.hl7.fhir.exceptions.FHIRException;
050import org.hl7.fhir.utilities.FhirPublication;
051import org.hl7.fhir.utilities.Utilities;
052
053public class IGPackConverter102 extends BaseAdvisor_10_30 {
054
055  private final Bundle cslist = new Bundle();
056
057  public static void main(String[] args) throws Exception {
058    new IGPackConverter102().process();
059  }
060
061  private void process() throws IOException, FHIRException {
062    initCSList();
063    for (String s : new File(Utilities.path("[tmp]", "igpack2")).list()) {
064      if (s.endsWith(".xml") && !s.startsWith("z-") && !Utilities.existsInList(s, "expansions.xml", "v3-codesystems.xml", "v2-tables.xml")) {
065        System.out.println("process " + s);
066        org.hl7.fhir.dstu2.formats.XmlParser xp = new org.hl7.fhir.dstu2.formats.XmlParser();
067        org.hl7.fhir.dstu2.model.Resource r10 = xp.parse(new FileInputStream(Utilities.path("[tmp]", "igpack2\\" + s)));
068        org.hl7.fhir.dstu3.model.Resource r17 = VersionConvertorFactory_10_30.convertResource(r10, this);
069        org.hl7.fhir.dstu3.formats.XmlParser xc = new org.hl7.fhir.dstu3.formats.XmlParser();
070        xc.setOutputStyle(OutputStyle.PRETTY);
071        xc.compose(new FileOutputStream(Utilities.path("[tmp]", "igpack2\\" + s)), r17);
072      }
073    }
074    System.out.println("save codesystems");
075    org.hl7.fhir.dstu3.formats.XmlParser xc = new org.hl7.fhir.dstu3.formats.XmlParser();
076    xc.setOutputStyle(OutputStyle.PRETTY);
077    xc.compose(new FileOutputStream(Utilities.path("[tmp]", "igpack2\\codesystems.xml")), cslist);
078    System.out.println("done");
079  }
080
081  private void initCSList() {
082    cslist.setId("codesystems");
083    cslist.setType(BundleType.COLLECTION);
084    cslist.getMeta().setLastUpdated(new Date());
085  }
086
087  @Override
088  public boolean ignoreEntry(@Nonnull BundleEntryComponent src, @Nonnull FhirPublication publication) {
089    return false;
090  }
091
092
093  @Override
094  public void handleCodeSystem(@Nonnull CodeSystem tgtcs, @Nonnull ValueSet vs) {
095    cslist.addEntry().setFullUrl(tgtcs.getUrl()).setResource(tgtcs);
096  }
097
098  @Override
099  public CodeSystem getCodeSystem(@Nonnull ValueSet src) {
100    return null;
101  }
102
103}