001package org.hl7.fhir.dstu3.elementmodel;
002
003/*
004  Copyright (c) 2011+, HL7, Inc.
005  All rights reserved.
006  
007  Redistribution and use in source and binary forms, with or without modification, 
008  are permitted provided that the following conditions are met:
009    
010   * Redistributions of source code must retain the above copyright notice, this 
011     list of conditions and the following disclaimer.
012   * Redistributions in binary form must reproduce the above copyright notice, 
013     this list of conditions and the following disclaimer in the documentation 
014     and/or other materials provided with the distribution.
015   * Neither the name of HL7 nor the names of its contributors may be used to 
016     endorse or promote products derived from this software without specific 
017     prior written permission.
018  
019  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
020  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
021  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
022  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
023  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
024  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
025  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
026  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
027  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
028  POSSIBILITY OF SUCH DAMAGE.
029  
030 */
031
032
033
034import java.io.File;
035import java.io.FileInputStream;
036import java.io.FileOutputStream;
037import java.util.Map.Entry;
038
039import org.hl7.fhir.dstu3.context.IWorkerContext;
040import org.hl7.fhir.dstu3.context.SimpleWorkerContext;
041import org.hl7.fhir.dstu3.elementmodel.Manager.FhirFormat;
042import org.hl7.fhir.dstu3.formats.IParser.OutputStyle;
043import org.hl7.fhir.utilities.TextFile;
044import org.hl7.fhir.utilities.Utilities;
045
046import com.google.gson.JsonArray;
047import com.google.gson.JsonElement;
048import com.google.gson.JsonObject;
049
050public class Tester {
051
052        public static void main(String[] args) throws Exception {
053                IWorkerContext context = SimpleWorkerContext.fromPack(Utilities.path("C:\\work\\org.hl7.fhir\\build\\publish", "validation-min.xml.zip"));
054                int t = 0;
055                int ok = 0;
056                for (String f : new File("C:\\work\\org.hl7.fhir\\build\\publish").list()) {
057                        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()) {
058//                              if (f.equals("account-questionnaire.xml")) {
059                                System.out.print("convert "+f);
060//                              Manager.convert(context, new FileInputStream("C:\\work\\org.hl7.fhir\\build\\publish\\"+f), FhirFormat.XML, 
061//                                              new FileOutputStream("C:\\work\\org.hl7.fhir\\build\\publish\\"+Utilities.changeFileExt(f, ".mm.json")), FhirFormat.JSON, OutputStyle.PRETTY);
062//                              String src = normalise(TextFile.fileToString("C:\\work\\org.hl7.fhir\\build\\publish\\"+Utilities.changeFileExt(f, ".mm.json")));
063//                              String tgt = normalise(TextFile.fileToString("C:\\work\\org.hl7.fhir\\build\\publish\\"+Utilities.changeFileExt(f, ".json")));
064                                Element e = Manager.parse(context, new FileInputStream("C:\\work\\org.hl7.fhir\\build\\publish\\"+f), FhirFormat.XML);
065                                Manager.compose(context, e, new FileOutputStream("C:\\work\\org.hl7.fhir\\build\\publish\\"+Utilities.changeFileExt(f, ".mm.ttl")), FhirFormat.TURTLE, OutputStyle.PRETTY, null);
066        Manager.compose(context, e, new FileOutputStream("C:\\temp\\resource.xml"), FhirFormat.XML, OutputStyle.PRETTY, null);
067                                String src = TextFile.fileToString("C:\\work\\org.hl7.fhir\\build\\publish\\"+Utilities.changeFileExt(f, ".mm.ttl"));
068                                String tgt = TextFile.fileToString("C:\\work\\org.hl7.fhir\\build\\publish\\"+Utilities.changeFileExt(f, ".ttl"));
069                                t++;
070                                if (src.equals(tgt)) {
071                                        System.out.println(".. ok");
072                                        ok++;
073                                } else
074                                        System.out.println(".. fail");
075                                }
076//                      }
077                }
078                System.out.println("done - "+Integer.toString(t)+" files, "+Integer.toString(ok)+" ok");
079        }
080
081        private static com.google.gson.JsonParser  parser = new com.google.gson.JsonParser();
082        
083        private static String normalise(String s) {
084                JsonObject json = parser.parse(s).getAsJsonObject();
085                JsonElement txt = json.get("text");
086                if (txt != null) {
087                        if (((JsonObject) txt).has("div"))
088                        ((JsonObject) txt).remove("div");
089                }
090                removeComments(json);
091          return json.toString();
092        }
093
094        private static void removeComments(JsonArray arr) {
095          for (JsonElement i : arr) {
096                if (i instanceof JsonObject)
097                        removeComments((JsonObject) i);
098                if (i instanceof JsonArray) 
099                        removeComments((JsonArray) i);
100          }
101        }
102        
103        private static void removeComments(JsonObject json) {
104                if (json.has("fhir_comments"))
105                        json.remove("fhir_comments");
106          for (Entry<String, JsonElement> p : json.entrySet()) {
107                if (p.getValue() instanceof JsonObject) 
108                        removeComments((JsonObject) p.getValue());
109                if (p.getValue() instanceof JsonArray) 
110                        removeComments((JsonArray) p.getValue());
111          }
112        
113        }
114
115}