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 org.hl7.fhir.utilities.filesystem.ManagedFileAccess;
036import java.io.FileInputStream;
037import java.io.FileOutputStream;
038import java.util.Map.Entry;
039
040import org.hl7.fhir.dstu3.context.IWorkerContext;
041import org.hl7.fhir.dstu3.context.SimpleWorkerContext;
042import org.hl7.fhir.dstu3.elementmodel.Manager.FhirFormat;
043import org.hl7.fhir.dstu3.formats.IParser.OutputStyle;
044import org.hl7.fhir.utilities.FileUtilities;
045import org.hl7.fhir.utilities.Utilities;
046
047import com.google.gson.JsonArray;
048import com.google.gson.JsonElement;
049import com.google.gson.JsonObject;
050
051@Deprecated
052@SuppressWarnings("checkstyle:systemout")
053public class Tester {
054
055        public static void main(String[] args) throws Exception {
056                IWorkerContext context = SimpleWorkerContext.fromPack(Utilities.path("C:\\work\\org.hl7.fhir\\build\\publish", "validation-min.xml.zip"));
057                int t = 0;
058                int ok = 0;
059                for (String f : ManagedFileAccess.file("C:\\work\\org.hl7.fhir\\build\\publish").list()) {
060                        if (f.endsWith(".xml") && !f.endsWith(".canonical.xml") && !f.contains("profile") && !f.contains("questionnaire") && ManagedFileAccess.file("C:\\work\\org.hl7.fhir\\build\\publish\\"+FileUtilities.changeFileExt(f, ".ttl")).exists()) {
061//                              if (f.equals("account-questionnaire.xml")) {
062                                System.out.print("convert "+f);
063//                              Manager.convert(context, ManagedFileAccess.inStream("C:\\work\\org.hl7.fhir\\build\\publish\\"+f), FhirFormat.XML, 
064//                                              ManagedFileAccess.outStream("C:\\work\\org.hl7.fhir\\build\\publish\\"+Utilities.changeFileExt(f, ".mm.json")), FhirFormat.JSON, OutputStyle.PRETTY);
065//                              String src = normalise(FileUtilities.fileToString("C:\\work\\org.hl7.fhir\\build\\publish\\"+Utilities.changeFileExt(f, ".mm.json")));
066//                              String tgt = normalise(FileUtilities.fileToString("C:\\work\\org.hl7.fhir\\build\\publish\\"+Utilities.changeFileExt(f, ".json")));
067                                Element e = Manager.parse(context, ManagedFileAccess.inStream("C:\\work\\org.hl7.fhir\\build\\publish\\"+f), FhirFormat.XML);
068                                Manager.compose(context, e, ManagedFileAccess.outStream("C:\\work\\org.hl7.fhir\\build\\publish\\"+FileUtilities.changeFileExt(f, ".mm.ttl")), FhirFormat.TURTLE, OutputStyle.PRETTY, null);
069        Manager.compose(context, e, ManagedFileAccess.outStream(Utilities.path("[tmp]", "resource.xml")), FhirFormat.XML, OutputStyle.PRETTY, null);
070                                String src = FileUtilities.fileToString("C:\\work\\org.hl7.fhir\\build\\publish\\"+FileUtilities.changeFileExt(f, ".mm.ttl"));
071                                String tgt = FileUtilities.fileToString("C:\\work\\org.hl7.fhir\\build\\publish\\"+FileUtilities.changeFileExt(f, ".ttl"));
072                                t++;
073                                if (src.equals(tgt)) {
074                                        System.out.println(".. ok");
075                                        ok++;
076                                } else
077                                        System.out.println(".. fail");
078                                }
079//                      }
080                }
081                System.out.println("done - "+Integer.toString(t)+" files, "+Integer.toString(ok)+" ok");
082        }
083
084        private static com.google.gson.JsonParser  parser = new com.google.gson.JsonParser();
085        
086        private static String normalise(String s) {
087                JsonObject json = parser.parse(s).getAsJsonObject();
088                JsonElement txt = json.get("text");
089                if (txt != null) {
090                        if (((JsonObject) txt).has("div"))
091                        ((JsonObject) txt).remove("div");
092                }
093                removeComments(json);
094          return json.toString();
095        }
096
097        private static void removeComments(JsonArray arr) {
098          for (JsonElement i : arr) {
099                if (i instanceof JsonObject)
100                        removeComments((JsonObject) i);
101                if (i instanceof JsonArray) 
102                        removeComments((JsonArray) i);
103          }
104        }
105        
106        private static void removeComments(JsonObject json) {
107                if (json.has("fhir_comments"))
108                        json.remove("fhir_comments");
109          for (Entry<String, JsonElement> p : json.entrySet()) {
110                if (p.getValue() instanceof JsonObject) 
111                        removeComments((JsonObject) p.getValue());
112                if (p.getValue() instanceof JsonArray) 
113                        removeComments((JsonArray) p.getValue());
114          }
115        
116        }
117
118}