Merge pull request #35 from vadi2/master

Serialise -output as json if the filename ends on ".json"
This commit is contained in:
Grahame Grieve 2019-06-01 06:39:05 +10:00 committed by GitHub
commit 74b9488ffc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 41 additions and 34 deletions

View File

@ -9,9 +9,9 @@ package org.hl7.fhir.r5.validation;
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -23,27 +23,27 @@ package org.hl7.fhir.r5.validation;
Copyright (c) 2011+, HL7, Inc Copyright (c) 2011+, HL7, Inc
All rights reserved. All rights reserved.
Redistribution and use in source and binary forms, with or without modification, Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met: are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this * Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer. list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, * Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution. and/or other materials provided with the distribution.
* Neither the name of HL7 nor the names of its contributors may be used to * Neither the name of HL7 nor the names of its contributors may be used to
endorse or promote products derived from this software without specific endorse or promote products derived from this software without specific
prior written permission. prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE. POSSIBILITY OF SUCH DAMAGE.
*/ */
@ -62,8 +62,10 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import org.hl7.fhir.r5.conformance.ProfileComparer; import org.hl7.fhir.r5.conformance.ProfileComparer;
import org.hl7.fhir.r5.formats.IParser;
import org.hl7.fhir.r5.formats.IParser.OutputStyle; import org.hl7.fhir.r5.formats.IParser.OutputStyle;
import org.hl7.fhir.r5.formats.XmlParser; import org.hl7.fhir.r5.formats.XmlParser;
import org.hl7.fhir.r5.formats.JsonParser;
import org.hl7.fhir.r5.model.Bundle; import org.hl7.fhir.r5.model.Bundle;
import org.hl7.fhir.r5.model.Bundle.BundleEntryComponent; import org.hl7.fhir.r5.model.Bundle.BundleEntryComponent;
import org.hl7.fhir.r5.model.Constants; import org.hl7.fhir.r5.model.Constants;
@ -80,14 +82,14 @@ import org.hl7.fhir.utilities.cache.ToolsVersion;
import org.hl7.fhir.utilities.xhtml.XhtmlComposer; import org.hl7.fhir.utilities.xhtml.XhtmlComposer;
/** /**
* A executable class that will validate one or more FHIR resources against * A executable class that will validate one or more FHIR resources against
* the specification * the specification
* *
* todo: schema validation (w3c xml, json schema, shex?) * todo: schema validation (w3c xml, json schema, shex?)
* *
* if you want to host validation inside a process, skip this class, and look at * if you want to host validation inside a process, skip this class, and look at
* ValidationEngine * ValidationEngine
* *
* todo: find a gome for this: * todo: find a gome for this:
* @author Grahame * @author Grahame
@ -194,7 +196,7 @@ public class Validator {
System.out.println(" -snapshot"); System.out.println(" -snapshot");
System.out.println(""); System.out.println("");
System.out.println("-snapshot requires the parameters -defn, -txserver, -source, and -output. ig may be used to provide necessary base profiles"); System.out.println("-snapshot requires the parameters -defn, -txserver, -source, and -output. ig may be used to provide necessary base profiles");
} else if (hasParam(args, "-compare")) { } else if (hasParam(args, "-compare")) {
String dest = getParam(args, "-dest"); String dest = getParam(args, "-dest");
if (dest == null) if (dest == null)
System.out.println("no -dest parameter provided"); System.out.println("no -dest parameter provided");
@ -245,12 +247,12 @@ public class Validator {
} }
} }
} }
} else { } else {
System.out.print("Arguments:"); System.out.print("Arguments:");
for (String s : args) for (String s : args)
System.out.print(s.contains(" ") ? " \""+s+"\"" : " "+s); System.out.print(s.contains(" ") ? " \""+s+"\"" : " "+s);
System.out.println(); System.out.println();
String definitions = "hl7.fhir.core#current"; String definitions = "hl7.fhir.core#current";
String map = null; String map = null;
List<String> igs = new ArrayList<String>(); List<String> igs = new ArrayList<String>();
@ -299,7 +301,7 @@ public class Validator {
i++; i++;
if (i+1 == args.length) if (i+1 == args.length)
throw new Error("Specified -profile with @ without indicating profile location"); throw new Error("Specified -profile with @ without indicating profile location");
else else
locations.put(p, args[++i]); locations.put(p, args[++i]);
} }
} else if (args[i].equals("-questionnaire")) } else if (args[i].equals("-questionnaire"))
@ -360,7 +362,7 @@ public class Validator {
} }
if (sources.isEmpty()) if (sources.isEmpty())
throw new Exception("Must provide at least one source file"); throw new Exception("Must provide at least one source file");
// Comment this out because definitions filename doesn't necessarily contain version (and many not even be 14 characters long). Version gets spit out a couple of lines later after we've loaded the context // Comment this out because definitions filename doesn't necessarily contain version (and many not even be 14 characters long). Version gets spit out a couple of lines later after we've loaded the context
System.out.println(" .. connect to tx server @ "+txServer); System.out.println(" .. connect to tx server @ "+txServer);
System.out.println(" .. definitions from "+definitions); System.out.println(" .. definitions from "+definitions);
@ -377,7 +379,13 @@ public class Validator {
validator.setHintAboutNonMustSupport(hintAboutNonMustSupport); validator.setHintAboutNonMustSupport(hintAboutNonMustSupport);
validator.setAnyExtensionsAllowed(anyExtensionsAllowed); validator.setAnyExtensionsAllowed(anyExtensionsAllowed);
XmlParser x = new XmlParser(); IParser x;
if (output != null && output.endsWith(".json"))
x = new JsonParser();
else
x = new XmlParser();
x.setOutputStyle(OutputStyle.PRETTY);
if (mode == EngineMode.TRANSFORM) { if (mode == EngineMode.TRANSFORM) {
if (sources.size() > 1) if (sources.size() > 1)
throw new Exception("Can only have one source when doing a transform (found "+sources+")"); throw new Exception("Can only have one source when doing a transform (found "+sources+")");
@ -391,8 +399,7 @@ public class Validator {
System.out.println(" ...success"); System.out.println(" ...success");
if (output != null) { if (output != null) {
FileOutputStream s = new FileOutputStream(output); FileOutputStream s = new FileOutputStream(output);
x.setOutputStyle(OutputStyle.PRETTY); x.compose(s, r);
x.compose(s, r, true);
s.close(); s.close();
} }
} catch (Exception e) { } catch (Exception e) {
@ -434,7 +441,7 @@ public class Validator {
displayOO((OperationOutcome)r); displayOO((OperationOutcome)r);
} else { } else {
FileOutputStream s = new FileOutputStream(output); FileOutputStream s = new FileOutputStream(output);
x.compose(s, r, true); x.compose(s, r);
s.close(); s.close();
} }
} }
@ -496,7 +503,7 @@ public class Validator {
else else
info++; info++;
} }
System.out.println((error==0?"Success...":"*FAILURE* ")+ "validating "+file+": "+" error:"+Integer.toString(error)+" warn:"+Integer.toString(warn)+" info:"+Integer.toString(info)); System.out.println((error==0?"Success...":"*FAILURE* ")+ "validating "+file+": "+" error:"+Integer.toString(error)+" warn:"+Integer.toString(warn)+" info:"+Integer.toString(info));
for (OperationOutcomeIssueComponent issue : oo.getIssue()) { for (OperationOutcomeIssueComponent issue : oo.getIssue()) {
System.out.println(getIssueSummary(issue)); System.out.println(getIssueSummary(issue));
@ -507,7 +514,7 @@ public class Validator {
private static String getIssueSummary(OperationOutcomeIssueComponent issue) { private static String getIssueSummary(OperationOutcomeIssueComponent issue) {
return " " + issue.getSeverity().getDisplay() + " @ " + issue.getLocation().get(0).asStringValue() + " : " + issue.getDetails().getText(); return " " + issue.getSeverity().getDisplay() + " @ " + issue.getLocation().get(0).asStringValue() + " : " + issue.getDetails().getText();
} }
private static boolean hasParam(String[] args, String param) { private static boolean hasParam(String[] args, String param) {
for (String a : args) for (String a : args)