001package org.hl7.fhir.r4.elementmodel;
002
003/*-
004 * #%L
005 * org.hl7.fhir.r4
006 * %%
007 * Copyright (C) 2014 - 2019 Health Level 7
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 * 
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 * 
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023
024import java.io.File;
025import java.io.FileInputStream;
026import java.io.FileOutputStream;
027import java.util.Map.Entry;
028
029import org.hl7.fhir.r4.context.IWorkerContext;
030import org.hl7.fhir.r4.context.SimpleWorkerContext;
031import org.hl7.fhir.r4.elementmodel.Manager.FhirFormat;
032import org.hl7.fhir.r4.formats.IParser.OutputStyle;
033import org.hl7.fhir.utilities.TextFile;
034import org.hl7.fhir.utilities.Utilities;
035
036import com.google.gson.JsonArray;
037import com.google.gson.JsonElement;
038import com.google.gson.JsonObject;
039
040public class Tester {
041
042        public static void main(String[] args) throws Exception {
043                IWorkerContext context = SimpleWorkerContext.fromPack(Utilities.path("C:\\work\\org.hl7.fhir\\build\\publish", "validation-min.xml.zip"));
044                int t = 0;
045                int ok = 0;
046                for (String f : new File("C:\\work\\org.hl7.fhir\\build\\publish").list()) {
047                        if (f.endsWith(".xml") && !f.endsWith(".canonical.xml") && !f.contains("profile") && !f.contains("questionnaire") && new File("C:\\work\\org.hl7.fhir\\build\\publish\\"+Utilities.changeFileExt(f, ".ttl")).exists()) {
048//                              if (f.equals("account-questionnaire.xml")) {
049                                System.out.print("convert "+f);
050//                              Manager.convert(context, new FileInputStream("C:\\work\\org.hl7.fhir\\build\\publish\\"+f), FhirFormat.XML, 
051//                                              new FileOutputStream("C:\\work\\org.hl7.fhir\\build\\publish\\"+Utilities.changeFileExt(f, ".mm.json")), FhirFormat.JSON, OutputStyle.PRETTY);
052//                              String src = normalise(TextFile.fileToString("C:\\work\\org.hl7.fhir\\build\\publish\\"+Utilities.changeFileExt(f, ".mm.json")));
053//                              String tgt = normalise(TextFile.fileToString("C:\\work\\org.hl7.fhir\\build\\publish\\"+Utilities.changeFileExt(f, ".json")));
054                                Element e = Manager.parse(context, new FileInputStream("C:\\work\\org.hl7.fhir\\build\\publish\\"+f), FhirFormat.XML);
055                                Manager.compose(context, e, new FileOutputStream("C:\\work\\org.hl7.fhir\\build\\publish\\"+Utilities.changeFileExt(f, ".mm.ttl")), FhirFormat.TURTLE, OutputStyle.PRETTY, null);
056        Manager.compose(context, e, new FileOutputStream("C:\\temp\\resource.xml"), FhirFormat.XML, OutputStyle.PRETTY, null);
057                                String src = TextFile.fileToString("C:\\work\\org.hl7.fhir\\build\\publish\\"+Utilities.changeFileExt(f, ".mm.ttl"));
058                                String tgt = TextFile.fileToString("C:\\work\\org.hl7.fhir\\build\\publish\\"+Utilities.changeFileExt(f, ".ttl"));
059                                t++;
060                                if (src.equals(tgt)) {
061                                        System.out.println(".. ok");
062                                        ok++;
063                                } else
064                                        System.out.println(".. fail");
065                                }
066//                      }
067                }
068                System.out.println("done - "+Integer.toString(t)+" files, "+Integer.toString(ok)+" ok");
069        }
070
071        private static com.google.gson.JsonParser  parser = new com.google.gson.JsonParser();
072        
073        private static String normalise(String s) {
074                JsonObject json = parser.parse(s).getAsJsonObject();
075                JsonElement txt = json.get("text");
076                if (txt != null) {
077                        if (((JsonObject) txt).has("div"))
078                        ((JsonObject) txt).remove("div");
079                }
080                removeComments(json);
081          return json.toString();
082        }
083
084        private static void removeComments(JsonArray arr) {
085          for (JsonElement i : arr) {
086                if (i instanceof JsonObject)
087                        removeComments((JsonObject) i);
088                if (i instanceof JsonArray) 
089                        removeComments((JsonArray) i);
090          }
091        }
092        
093        private static void removeComments(JsonObject json) {
094                if (json.has("fhir_comments"))
095                        json.remove("fhir_comments");
096          for (Entry<String, JsonElement> p : json.entrySet()) {
097                if (p.getValue() instanceof JsonObject) 
098                        removeComments((JsonObject) p.getValue());
099                if (p.getValue() instanceof JsonArray) 
100                        removeComments((JsonArray) p.getValue());
101          }
102        
103        }
104
105}