Minimize validation resources

This commit is contained in:
James 2017-08-13 14:38:51 -04:00
parent f6868cce5c
commit 12f89a423a
26 changed files with 574293 additions and 1161904 deletions

View File

@ -0,0 +1,232 @@
package ca.uhn.fhir.jpa.z;
import ca.uhn.fhir.context.BaseRuntimeChildDefinition;
import ca.uhn.fhir.context.BaseRuntimeElementCompositeDefinition;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.model.api.IResource;
import ca.uhn.fhir.model.dstu2.resource.Bundle;
import ca.uhn.fhir.model.dstu2.resource.Bundle.Entry;
import ca.uhn.fhir.parser.IParser;
import ca.uhn.fhir.rest.api.EncodingEnum;
import ca.uhn.fhir.util.BundleUtil;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.core.joran.util.ConfigurationWatchListUtil;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.hl7.fhir.instance.model.api.IBaseBundle;
import org.hl7.fhir.instance.model.api.IBaseResource;
import java.io.*;
import java.net.URL;
import java.util.Collection;
public class ResourceMinimizerMojo {
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(ResourceMinimizerMojo.class);
private String fhirVersion;
private long myByteCount;
private FhirContext myCtx;
private int myFileCount;
private File targetDirectory;
public void execute() throws Exception {
ourLog.info("Starting resource minimizer");
if (myCtx != null) {
// nothing
} else if ("DSTU2".equals(fhirVersion)) {
myCtx = FhirContext.forDstu2();
} else if ("HL7ORG_DSTU2".equals(fhirVersion)) {
myCtx = FhirContext.forDstu2Hl7Org();
} else if ("DSTU2_1".equals(fhirVersion)) {
myCtx = FhirContext.forDstu2_1();
} else if ("DSTU3".equals(fhirVersion)) {
myCtx = FhirContext.forDstu3();
} else if ("R4".equals(fhirVersion)) {
myCtx = FhirContext.forR4();
} else {
throw new Exception("Unknown version: " + fhirVersion);
}
ourLog.info("Looking for files in directory: {}", targetDirectory.getAbsolutePath());
Collection<File> files = FileUtils.listFiles(targetDirectory, new String[]{"xml", "json"}, true);
for (File nextFile : files) {
ourLog.debug("Checking file: {}", nextFile);
String inputString;
try {
inputString = IOUtils.toString(new FileInputStream(nextFile), "UTF-8");
} catch (IOException e) {
throw new Exception("Failed to read file: " + nextFile, e);
}
IParser parser = EncodingEnum.detectEncoding(inputString).newParser(myCtx);
IBaseResource input = parser.parseResource(inputString);
if (input instanceof IResource) {
((IResource) input).getText().getDiv().setValueAsString((String) null);
((IResource) input).getText().getStatus().setValueAsString((String) null);
if (input instanceof Bundle) {
for (Entry nextEntry : ((Bundle) input).getEntry()) {
if (nextEntry.getResource() != null) {
nextEntry.getResource().getText().getDiv().setValueAsString((String) null);
nextEntry.getResource().getText().getStatus().setValueAsString((String) null);
}
}
}
} else {
minimizeResource((IBaseResource) input);
}
String outputString = parser.setPrettyPrint(true).encodeResourceToString(input);
StringBuilder b = new StringBuilder();
for (String nextLine : outputString.split("\\n")) {
int i;
for (i = 0; i < nextLine.length(); i++) {
if (nextLine.charAt(i) != ' ') {
break;
}
}
b.append(StringUtils.leftPad("", i / 3, ' '));
b.append(nextLine.substring(i));
b.append("\n");
}
outputString = b.toString();
if (!inputString.equals(outputString)) {
ourLog.info("Trimming contents of resource: {} - From {} to {}", nextFile, FileUtils.byteCountToDisplaySize(inputString.length()), FileUtils.byteCountToDisplaySize(outputString.length()));
myByteCount += (inputString.length() - outputString.length());
myFileCount++;
try {
String f = nextFile.getAbsolutePath();
Writer w = new OutputStreamWriter(new FileOutputStream(f, false), "UTF-8");
w = new BufferedWriter(w);
w.append(outputString);
w.close();
} catch (IOException e) {
throw new Exception("Failed to write " + nextFile, e);
}
}
}
}
private void minimizeResource(IBaseResource theInput) {
if (theInput instanceof IBaseBundle) {
for (IBaseResource next : BundleUtil.toListOfResources(myCtx, (IBaseBundle) theInput)) {
minimizeResource(next);
}
}
BaseRuntimeElementCompositeDefinition<?> element = (BaseRuntimeElementCompositeDefinition) myCtx.getElementDefinition(theInput.getClass());
BaseRuntimeChildDefinition textElement = element.getChildByName("text");
if (textElement != null) {
textElement.getMutator().setValue(theInput, null);
}
}
public long getByteCount() {
return myByteCount;
}
public int getFileCount() {
return myFileCount;
}
public static void main(String[] args) throws Exception {
FhirContext ctxDstu2 = FhirContext.forDstu2();
// FhirContext ctxDstu2_1 = FhirContext.forDstu2_1();
FhirContext ctxDstu3 = FhirContext.forDstu3();
FhirContext ctxR4 = FhirContext.forR4();
LoggerContext loggerContext = ((ch.qos.logback.classic.Logger) ourLog).getLoggerContext();
URL mainURL = ConfigurationWatchListUtil.getMainWatchURL(loggerContext);
System.out.println(mainURL);
// or even
ourLog.info("Logback used '{}' as the configuration file.", mainURL);
int fileCount = 0;
long byteCount = 0;
ResourceMinimizerMojo m = new ResourceMinimizerMojo();
m.myCtx = ctxDstu2;
m.targetDirectory = new File("./hapi-tinder-plugin/src/main/resources/vs/dstu2");
m.fhirVersion = "DSTU2";
m.execute();
byteCount += m.getByteCount();
fileCount += m.getFileCount();
m = new ResourceMinimizerMojo();
m.myCtx = ctxDstu2;
m.targetDirectory = new File("./hapi-fhir-validation-resources-dstu2/src/main/resources/org/hl7/fhir/instance/model/valueset");
m.fhirVersion = "DSTU2";
m.execute();
byteCount += m.getByteCount();
fileCount += m.getFileCount();
m = new ResourceMinimizerMojo();
m.myCtx = ctxDstu2;
m.targetDirectory = new File("./hapi-fhir-validation-resources-dstu2/src/main/resources/org/hl7/fhir/instance/model/profile");
m.fhirVersion = "DSTU2";
m.execute();
byteCount += m.getByteCount();
fileCount += m.getFileCount();
m = new ResourceMinimizerMojo();
m.myCtx = ctxDstu3;
m.targetDirectory = new File("./hapi-fhir-validation-resources-dstu3/src/main/resources/org/hl7/fhir/dstu3/model/profile");
m.fhirVersion = "DSTU3";
m.execute();
byteCount += m.getByteCount();
fileCount += m.getFileCount();
m = new ResourceMinimizerMojo();
m.myCtx = ctxDstu3;
m.targetDirectory = new File("./hapi-fhir-validation-resources-dstu3/src/main/resources/org/hl7/fhir/dstu3/model/valueset");
m.fhirVersion = "DSTU3";
m.execute();
byteCount += m.getByteCount();
// m = new ResourceMinimizerMojo();
// m.myCtx = ctxDstu2_1;
// m.targetDirectory = new File("./hapi-fhir-validation-resources-dstu2.1/src/main/resources/org/hl7/fhir/dstu2016may/model/profile");
// m.fhirVersion = "DSTU2_1";
// m.execute();
// byteCount += m.getByteCount();
// fileCount += m.getFileCount();
//
// m = new ResourceMinimizerMojo();
// m.myCtx = ctxDstu2_1;
// m.targetDirectory = new File("./hapi-fhir-validation-resources-dstu2.1/src/main/resources/org/hl7/fhir/dstu2016may/model/valueset");
// m.fhirVersion = "DSTU2_1";
// m.execute();
// byteCount += m.getByteCount();
// fileCount += m.getFileCount();
m = new ResourceMinimizerMojo();
m.myCtx = ctxR4;
m.targetDirectory = new File("./hapi-fhir-validation-resources-r4/src/main/resources/org/hl7/fhir/r4/model/profile");
m.fhirVersion = "R4";
m.execute();
byteCount += m.getByteCount();
fileCount += m.getFileCount();
m = new ResourceMinimizerMojo();
m.myCtx = ctxR4;
m.targetDirectory = new File("./hapi-fhir-validation-resources-r4/src/main/resources/org/hl7/fhir/r4/model/valueset");
m.fhirVersion = "R4";
m.execute();
byteCount += m.getByteCount();
fileCount += m.getFileCount();
ourLog.info("Trimmed {} files", fileCount);
ourLog.info("Trimmed {} bytes", FileUtils.byteCountToDisplaySize(byteCount));
}
}

View File

@ -65,8 +65,8 @@
<min value="0"/>
<max value="1"/>
<!-- Note: primitive values do not have an assigned type
e.g. this is compiler magic
XML and JSON types provided by extension -->
e.g. this is compiler magic
XML and JSON types provided by extension -->
<type>
<code>
<extension url="http://hl7.org/fhir/StructureDefinition/structuredefinition-json-type">
@ -99,8 +99,8 @@
<min value="0"/>
<max value="1"/>
<!-- Note: primitive values do not have an assigned type
e.g. this is compiler magic
XML and JSON types provided by extension -->
e.g. this is compiler magic
XML and JSON types provided by extension -->
<type>
<code>
<extension url="http://hl7.org/fhir/StructureDefinition/structuredefinition-json-type">

View File

@ -64,8 +64,8 @@
<min value="0"/>
<max value="1"/>
<!-- Note: primitive values do not have an assigned type
e.g. this is compiler magic
XML and JSON types provided by extension -->
e.g. this is compiler magic
XML and JSON types provided by extension -->
<type>
<code>
<extension url="http://hl7.org/fhir/StructureDefinition/structuredefinition-json-type">
@ -97,8 +97,8 @@
<min value="0"/>
<max value="1"/>
<!-- Note: primitive values do not have an assigned type
e.g. this is compiler magic
XML and JSON types provided by extension -->
e.g. this is compiler magic
XML and JSON types provided by extension -->
<type>
<code>
<extension url="http://hl7.org/fhir/StructureDefinition/structuredefinition-json-type">

View File

@ -64,8 +64,8 @@
<max value="1"/>
</base>
<!-- Note: primitive values do not have an assigned type
e.g. this is compiler magic
XML and JSON types provided by extension -->
e.g. this is compiler magic
XML and JSON types provided by extension -->
<type>
<extension url="http://hl7.org/fhir/StructureDefinition/structuredefinition-regex">
<valueString value="[^\s]+([\s][^\s]+)*"/>
@ -100,8 +100,8 @@
<min value="0"/>
<max value="1"/>
<!-- Note: primitive values do not have an assigned type
e.g. this is compiler magic
XML and JSON types provided by extension -->
e.g. this is compiler magic
XML and JSON types provided by extension -->
<type>
<extension url="http://hl7.org/fhir/StructureDefinition/structuredefinition-regex">
<valueString value="[^\s]+([\s][^\s]+)*"/>

View File

@ -64,8 +64,8 @@
<min value="0"/>
<max value="1"/>
<!-- Note: primitive values do not have an assigned type
e.g. this is compiler magic
XML and JSON types provided by extension -->
e.g. this is compiler magic
XML and JSON types provided by extension -->
<type>
<extension url="http://hl7.org/fhir/StructureDefinition/structuredefinition-regex">
<valueString value="-?[0-9]{4}(-(0[1-9]|1[0-2])(-(0[0-9]|[1-2][0-9]|3[0-1]))?)?"/>
@ -100,8 +100,8 @@
<min value="0"/>
<max value="1"/>
<!-- Note: primitive values do not have an assigned type
e.g. this is compiler magic
XML and JSON types provided by extension -->
e.g. this is compiler magic
XML and JSON types provided by extension -->
<type>
<extension url="http://hl7.org/fhir/StructureDefinition/structuredefinition-regex">
<valueString value="-?[0-9]{4}(-(0[1-9]|1[0-2])(-(0[0-9]|[1-2][0-9]|3[0-1]))?)?"/>

View File

@ -64,8 +64,8 @@
<min value="0"/>
<max value="1"/>
<!-- Note: primitive values do not have an assigned type
e.g. this is compiler magic
XML and JSON types provided by extension -->
e.g. this is compiler magic
XML and JSON types provided by extension -->
<type>
<extension url="http://hl7.org/fhir/StructureDefinition/structuredefinition-regex">
<valueString value="-?[0-9]{4}(-(0[1-9]|1[0-2])(-(0[0-9]|[1-2][0-9]|3[0-1])(T([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9](\.[0-9]+)?(Z|(\+|-)((0[0-9]|1[0-3]):[0-5][0-9]|14:00)))?)?)?"/>
@ -100,8 +100,8 @@
<min value="0"/>
<max value="1"/>
<!-- Note: primitive values do not have an assigned type
e.g. this is compiler magic
XML and JSON types provided by extension -->
e.g. this is compiler magic
XML and JSON types provided by extension -->
<type>
<extension url="http://hl7.org/fhir/StructureDefinition/structuredefinition-regex">
<valueString value="-?[0-9]{4}(-(0[1-9]|1[0-2])(-(0[0-9]|[1-2][0-9]|3[0-1])(T([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9](\.[0-9]+)?(Z|(\+|-)((0[0-9]|1[0-3]):[0-5][0-9]|14:00)))?)?)?"/>

View File

@ -65,8 +65,8 @@
<min value="0"/>
<max value="1"/>
<!-- Note: primitive values do not have an assigned type
e.g. this is compiler magic
XML and JSON types provided by extension -->
e.g. this is compiler magic
XML and JSON types provided by extension -->
<type>
<extension url="http://hl7.org/fhir/StructureDefinition/structuredefinition-regex">
<valueString value="-?([0]|([1-9][0-9]*))(\.[0-9]+)?"/>
@ -102,8 +102,8 @@
<min value="0"/>
<max value="1"/>
<!-- Note: primitive values do not have an assigned type
e.g. this is compiler magic
XML and JSON types provided by extension -->
e.g. this is compiler magic
XML and JSON types provided by extension -->
<type>
<extension url="http://hl7.org/fhir/StructureDefinition/structuredefinition-regex">
<valueString value="-?([0]|([1-9][0-9]*))(\.[0-9]+)?"/>

View File

@ -65,8 +65,8 @@
<max value="1"/>
</base>
<!-- Note: primitive values do not have an assigned type
e.g. this is compiler magic
XML and JSON types provided by extension -->
e.g. this is compiler magic
XML and JSON types provided by extension -->
<type>
<extension url="http://hl7.org/fhir/StructureDefinition/structuredefinition-regex">
<valueString value="[A-Za-z0-9\-\.]{1,64}"/>
@ -102,8 +102,8 @@
<min value="0"/>
<max value="1"/>
<!-- Note: primitive values do not have an assigned type
e.g. this is compiler magic
XML and JSON types provided by extension -->
e.g. this is compiler magic
XML and JSON types provided by extension -->
<type>
<extension url="http://hl7.org/fhir/StructureDefinition/structuredefinition-regex">
<valueString value="[A-Za-z0-9\-\.]{1,64}"/>

View File

@ -65,8 +65,8 @@
<min value="0"/>
<max value="1"/>
<!-- Note: primitive values do not have an assigned type
e.g. this is compiler magic
XML and JSON types provided by extension -->
e.g. this is compiler magic
XML and JSON types provided by extension -->
<type>
<code>
<extension url="http://hl7.org/fhir/StructureDefinition/structuredefinition-json-type">
@ -99,8 +99,8 @@
<min value="0"/>
<max value="1"/>
<!-- Note: primitive values do not have an assigned type
e.g. this is compiler magic
XML and JSON types provided by extension -->
e.g. this is compiler magic
XML and JSON types provided by extension -->
<type>
<code>
<extension url="http://hl7.org/fhir/StructureDefinition/structuredefinition-json-type">

View File

@ -65,8 +65,8 @@
<min value="0"/>
<max value="1"/>
<!-- Note: primitive values do not have an assigned type
e.g. this is compiler magic
XML and JSON types provided by extension -->
e.g. this is compiler magic
XML and JSON types provided by extension -->
<type>
<extension url="http://hl7.org/fhir/StructureDefinition/structuredefinition-regex">
<valueString value="-?([0]|([1-9][0-9]*))"/>
@ -104,8 +104,8 @@
<min value="0"/>
<max value="1"/>
<!-- Note: primitive values do not have an assigned type
e.g. this is compiler magic
XML and JSON types provided by extension -->
e.g. this is compiler magic
XML and JSON types provided by extension -->
<type>
<extension url="http://hl7.org/fhir/StructureDefinition/structuredefinition-regex">
<valueString value="-?([0]|([1-9][0-9]*))"/>

View File

@ -65,8 +65,8 @@
<max value="1"/>
</base>
<!-- Note: primitive values do not have an assigned type
e.g. this is compiler magic
XML and JSON types provided by extension -->
e.g. this is compiler magic
XML and JSON types provided by extension -->
<type>
<code>
<extension url="http://hl7.org/fhir/StructureDefinition/structuredefinition-json-type">
@ -99,8 +99,8 @@
<min value="0"/>
<max value="1"/>
<!-- Note: primitive values do not have an assigned type
e.g. this is compiler magic
XML and JSON types provided by extension -->
e.g. this is compiler magic
XML and JSON types provided by extension -->
<type>
<code>
<extension url="http://hl7.org/fhir/StructureDefinition/structuredefinition-json-type">

View File

@ -65,8 +65,8 @@
<max value="1"/>
</base>
<!-- Note: primitive values do not have an assigned type
e.g. this is compiler magic
XML and JSON types provided by extension -->
e.g. this is compiler magic
XML and JSON types provided by extension -->
<type>
<extension url="http://hl7.org/fhir/StructureDefinition/structuredefinition-regex">
<valueString value="urn:oid:(0|[1-9][0-9]*)(\.(0|[1-9][0-9]*))*"/>
@ -102,8 +102,8 @@
<min value="0"/>
<max value="1"/>
<!-- Note: primitive values do not have an assigned type
e.g. this is compiler magic
XML and JSON types provided by extension -->
e.g. this is compiler magic
XML and JSON types provided by extension -->
<type>
<extension url="http://hl7.org/fhir/StructureDefinition/structuredefinition-regex">
<valueString value="urn:oid:(0|[1-9][0-9]*)(\.(0|[1-9][0-9]*))*"/>

View File

@ -64,8 +64,8 @@
<max value="1"/>
</base>
<!-- Note: primitive values do not have an assigned type
e.g. this is compiler magic
XML and JSON types provided by extension -->
e.g. this is compiler magic
XML and JSON types provided by extension -->
<type>
<extension url="http://hl7.org/fhir/StructureDefinition/structuredefinition-regex">
<valueString value="[1-9][0-9]*"/>
@ -100,8 +100,8 @@
<min value="0"/>
<max value="1"/>
<!-- Note: primitive values do not have an assigned type
e.g. this is compiler magic
XML and JSON types provided by extension -->
e.g. this is compiler magic
XML and JSON types provided by extension -->
<type>
<extension url="http://hl7.org/fhir/StructureDefinition/structuredefinition-regex">
<valueString value="[1-9][0-9]*"/>

View File

@ -65,8 +65,8 @@
<min value="0"/>
<max value="1"/>
<!-- Note: primitive values do not have an assigned type
e.g. this is compiler magic
XML and JSON types provided by extension -->
e.g. this is compiler magic
XML and JSON types provided by extension -->
<type>
<code>
<extension url="http://hl7.org/fhir/StructureDefinition/structuredefinition-json-type">
@ -100,8 +100,8 @@
<min value="0"/>
<max value="1"/>
<!-- Note: primitive values do not have an assigned type
e.g. this is compiler magic
XML and JSON types provided by extension -->
e.g. this is compiler magic
XML and JSON types provided by extension -->
<type>
<code>
<extension url="http://hl7.org/fhir/StructureDefinition/structuredefinition-json-type">

View File

@ -64,8 +64,8 @@
<min value="0"/>
<max value="1"/>
<!-- Note: primitive values do not have an assigned type
e.g. this is compiler magic
XML and JSON types provided by extension -->
e.g. this is compiler magic
XML and JSON types provided by extension -->
<type>
<extension url="http://hl7.org/fhir/StructureDefinition/structuredefinition-regex">
<valueString value="([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9](\.[0-9]+)?"/>
@ -100,8 +100,8 @@
<min value="0"/>
<max value="1"/>
<!-- Note: primitive values do not have an assigned type
e.g. this is compiler magic
XML and JSON types provided by extension -->
e.g. this is compiler magic
XML and JSON types provided by extension -->
<type>
<extension url="http://hl7.org/fhir/StructureDefinition/structuredefinition-regex">
<valueString value="([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9](\.[0-9]+)?"/>

View File

@ -64,8 +64,8 @@
<max value="1"/>
</base>
<!-- Note: primitive values do not have an assigned type
e.g. this is compiler magic
XML and JSON types provided by extension -->
e.g. this is compiler magic
XML and JSON types provided by extension -->
<type>
<extension url="http://hl7.org/fhir/StructureDefinition/structuredefinition-regex">
<valueString value="[0]|([1-9][0-9]*)"/>
@ -100,8 +100,8 @@
<min value="0"/>
<max value="1"/>
<!-- Note: primitive values do not have an assigned type
e.g. this is compiler magic
XML and JSON types provided by extension -->
e.g. this is compiler magic
XML and JSON types provided by extension -->
<type>
<extension url="http://hl7.org/fhir/StructureDefinition/structuredefinition-regex">
<valueString value="[0]|([1-9][0-9]*)"/>

View File

@ -65,8 +65,8 @@
<min value="0"/>
<max value="1"/>
<!-- Note: primitive values do not have an assigned type
e.g. this is compiler magic
XML and JSON types provided by extension -->
e.g. this is compiler magic
XML and JSON types provided by extension -->
<type>
<code>
<extension url="http://hl7.org/fhir/StructureDefinition/structuredefinition-json-type">
@ -99,8 +99,8 @@
<min value="0"/>
<max value="1"/>
<!-- Note: primitive values do not have an assigned type
e.g. this is compiler magic
XML and JSON types provided by extension -->
e.g. this is compiler magic
XML and JSON types provided by extension -->
<type>
<code>
<extension url="http://hl7.org/fhir/StructureDefinition/structuredefinition-json-type">

View File

@ -65,8 +65,8 @@
<max value="1"/>
</base>
<!-- Note: primitive values do not have an assigned type
e.g. this is compiler magic
XML and JSON types provided by extension -->
e.g. this is compiler magic
XML and JSON types provided by extension -->
<type>
<extension url="http://hl7.org/fhir/StructureDefinition/structuredefinition-regex">
<valueString value="urn:uuid:[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"/>
@ -102,8 +102,8 @@
<min value="0"/>
<max value="1"/>
<!-- Note: primitive values do not have an assigned type
e.g. this is compiler magic
XML and JSON types provided by extension -->
e.g. this is compiler magic
XML and JSON types provided by extension -->
<type>
<extension url="http://hl7.org/fhir/StructureDefinition/structuredefinition-regex">
<valueString value="urn:uuid:[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"/>

View File

@ -56749,14 +56749,14 @@
<contained>
<ConceptMap xmlns="http://hl7.org/fhir">
<id value="map"/>
<name value="NHIN / V3 Mapping"/>
<status value="draft"/>
<sourceReference>
<reference value="http://hl7.org/fhir/ValueSet/nhin-purposeofuse"/>
</sourceReference>
<targetReference>
<reference value="http://hl7.org/fhir/ValueSet/v3-PurposeOfUse"/>
</targetReference>
<name value="NHIN / V3 Mapping"/>
<status value="draft"/>
<group>
<source value="http://healthit.gov/nhin/purposeofuse"/>
<target value="http://hl7.org/fhir/v3/ActReason"/>

View File

@ -4,6 +4,10 @@ import java.io.*;
import java.net.URL;
import java.util.Collection;
import ca.uhn.fhir.context.BaseRuntimeChildDefinition;
import ca.uhn.fhir.context.BaseRuntimeElementCompositeDefinition;
import ca.uhn.fhir.context.BaseRuntimeElementDefinition;
import ca.uhn.fhir.util.BundleUtil;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
@ -11,6 +15,7 @@ import org.apache.maven.plugin.*;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.hl7.fhir.instance.model.api.IBaseBundle;
import org.hl7.fhir.instance.model.api.IBaseResource;
import ca.uhn.fhir.context.FhirContext;
@ -51,6 +56,8 @@ public class ResourceMinimizerMojo extends AbstractMojo {
myCtx = FhirContext.forDstu2_1();
} else if ("DSTU3".equals(fhirVersion)) {
myCtx = FhirContext.forDstu3();
} else if ("R4".equals(fhirVersion)) {
myCtx = FhirContext.forR4();
} else {
throw new MojoFailureException("Unknown version: " + fhirVersion);
}
@ -82,30 +89,8 @@ public class ResourceMinimizerMojo extends AbstractMojo {
}
}
}
} else if (input instanceof org.hl7.fhir.dstu3.model.Bundle) {
for (org.hl7.fhir.dstu3.model.Bundle.BundleEntryComponent nextEntry : ((org.hl7.fhir.dstu3.model.Bundle) input).getEntry()) {
if (nextEntry.getResource() instanceof org.hl7.fhir.dstu3.model.DomainResource) {
((org.hl7.fhir.dstu3.model.DomainResource) nextEntry.getResource()).getText().getDiv().setValueAsString((String) null);
((org.hl7.fhir.dstu3.model.DomainResource) nextEntry.getResource()).getText().getStatusElement().setValueAsString((String) null);
}
}
} else if (input instanceof org.hl7.fhir.dstu2016may.model.Bundle) {
for (org.hl7.fhir.dstu2016may.model.Bundle.BundleEntryComponent nextEntry : ((org.hl7.fhir.dstu2016may.model.Bundle) input).getEntry()) {
if (nextEntry.getResource() instanceof org.hl7.fhir.dstu2016may.model.DomainResource) {
((org.hl7.fhir.dstu2016may.model.DomainResource) nextEntry.getResource()).getText().getDiv().setValueAsString((String) null);
((org.hl7.fhir.dstu2016may.model.DomainResource) nextEntry.getResource()).getText().getStatusElement().setValueAsString((String) null);
}
}
} else if (input instanceof org.hl7.fhir.dstu3.model.DomainResource) {
try {
((org.hl7.fhir.dstu3.model.DomainResource) input).getText().setDivAsString(null);
((org.hl7.fhir.dstu3.model.DomainResource) input).getText().getStatusElement().setValueAsString(null);
} catch (Exception e) {
ourLog.error("Failed to clear narrative", e);
}
} else {
ourLog.info("Ignoring type: " + input.getClass());
continue;
minimizeResource((IBaseResource)input);
}
String outputString = parser.setPrettyPrint(true).encodeResourceToString(input);
@ -143,6 +128,20 @@ public class ResourceMinimizerMojo extends AbstractMojo {
}
}
private void minimizeResource(IBaseResource theInput) {
if (theInput instanceof IBaseBundle) {
for (IBaseResource next : BundleUtil.toListOfResources(myCtx, (IBaseBundle) theInput)) {
minimizeResource(next);
}
}
BaseRuntimeElementCompositeDefinition<?> element = (BaseRuntimeElementCompositeDefinition) myCtx.getElementDefinition(theInput.getClass());
BaseRuntimeChildDefinition textElement = element.getChildByName("text");
if (textElement != null) {
textElement.getMutator().setValue(theInput, null);
}
}
public long getByteCount() {
return myByteCount;
}
@ -153,8 +152,9 @@ public class ResourceMinimizerMojo extends AbstractMojo {
public static void main(String[] args) throws Exception {
FhirContext ctxDstu2 = FhirContext.forDstu2();
// FhirContext ctxDstu2_1 = FhirContext.forDstu2_1();
// FhirContext ctxDstu3 = FhirContext.forDstu3();
FhirContext ctxDstu2_1 = FhirContext.forDstu2_1();
FhirContext ctxDstu3 = FhirContext.forDstu3();
FhirContext ctxR4 = FhirContext.forR4();
LoggerContext loggerContext = ((ch.qos.logback.classic.Logger) ourLog).getLoggerContext();
URL mainURL = ConfigurationWatchListUtil.getMainWatchURL(loggerContext);
@ -166,59 +166,76 @@ public class ResourceMinimizerMojo extends AbstractMojo {
long byteCount = 0;
ResourceMinimizerMojo m = new ResourceMinimizerMojo();
// m.myCtx = ctxDstu2;
// m.targetDirectory = new File("../hapi-tinder-plugin/src/main/resources/vs/dstu2");
// m.fhirVersion = "DSTU2";
// m.execute();
// byteCount += m.getByteCount();
// fileCount += m.getFileCount();
//
// m = new ResourceMinimizerMojo();
// m.myCtx = ctxDstu2;
// m.targetDirectory = new File("../hapi-fhir-validation-resources-dstu2/src/main/resources/org/hl7/fhir/instance/model/valueset");
// m.fhirVersion = "DSTU2";
// m.execute();
// byteCount += m.getByteCount();
// fileCount += m.getFileCount();
//
m = new ResourceMinimizerMojo();
m.myCtx = ctxDstu2;
m.targetDirectory = new File("../hapi-fhir-validation-resources-dstu2/src/main/resources/org/hl7/fhir/instance/model/profile");
m.targetDirectory = new File("./hapi-tinder-plugin/src/main/resources/vs/dstu2");
m.fhirVersion = "DSTU2";
m.execute();
byteCount += m.getByteCount();
fileCount += m.getFileCount();
// m = new ResourceMinimizerMojo();
// m.myCtx = ctxDstu3;
// m.targetDirectory = new File("../hapi-fhir-validation-resources-dstu3/src/main/resources/org/hl7/fhir/dstu3/model/profile");
// m.fhirVersion = "DSTU3";
// m.execute();
// byteCount += m.getByteCount();
// fileCount += m.getFileCount();
m = new ResourceMinimizerMojo();
m.myCtx = ctxDstu2;
m.targetDirectory = new File("./hapi-fhir-validation-resources-dstu2/src/main/resources/org/hl7/fhir/instance/model/valueset");
m.fhirVersion = "DSTU2";
m.execute();
byteCount += m.getByteCount();
fileCount += m.getFileCount();
// m = new ResourceMinimizerMojo();
// m.myCtx = ctxDstu3;
// m.targetDirectory = new File("../hapi-fhir-validation-resources-dstu3/src/main/resources/org/hl7/fhir/dstu3/model/valueset");
// m.fhirVersion = "DSTU3";
// m.execute();
// byteCount += m.getByteCount();
m = new ResourceMinimizerMojo();
m.myCtx = ctxDstu2;
m.targetDirectory = new File("./hapi-fhir-validation-resources-dstu2/src/main/resources/org/hl7/fhir/instance/model/profile");
m.fhirVersion = "DSTU2";
m.execute();
byteCount += m.getByteCount();
fileCount += m.getFileCount();
// m = new ResourceMinimizerMojo();
// m.myCtx = ctxDstu2_1;
// m.targetDirectory = new File("../hapi-fhir-validation-resources-dstu2.1/src/main/resources/org/hl7/fhir/dstu2016may/model/profile");
// m.fhirVersion = "DSTU2_1";
// m.execute();
// byteCount += m.getByteCount();
// fileCount += m.getFileCount();
//
// m = new ResourceMinimizerMojo();
// m.myCtx = ctxDstu2_1;
// m.targetDirectory = new File("../hapi-fhir-validation-resources-dstu2.1/src/main/resources/org/hl7/fhir/dstu2016may/model/valueset");
// m.fhirVersion = "DSTU2_1";
// m.execute();
// byteCount += m.getByteCount();
// fileCount += m.getFileCount();
m = new ResourceMinimizerMojo();
m.myCtx = ctxDstu3;
m.targetDirectory = new File("./hapi-fhir-validation-resources-dstu3/src/main/resources/org/hl7/fhir/dstu3/model/profile");
m.fhirVersion = "DSTU3";
m.execute();
byteCount += m.getByteCount();
fileCount += m.getFileCount();
m = new ResourceMinimizerMojo();
m.myCtx = ctxDstu3;
m.targetDirectory = new File("./hapi-fhir-validation-resources-dstu3/src/main/resources/org/hl7/fhir/dstu3/model/valueset");
m.fhirVersion = "DSTU3";
m.execute();
byteCount += m.getByteCount();
m = new ResourceMinimizerMojo();
m.myCtx = ctxDstu2_1;
m.targetDirectory = new File("./hapi-fhir-validation-resources-dstu2.1/src/main/resources/org/hl7/fhir/dstu2016may/model/profile");
m.fhirVersion = "DSTU2_1";
m.execute();
byteCount += m.getByteCount();
fileCount += m.getFileCount();
m = new ResourceMinimizerMojo();
m.myCtx = ctxDstu2_1;
m.targetDirectory = new File("./hapi-fhir-validation-resources-dstu2.1/src/main/resources/org/hl7/fhir/dstu2016may/model/valueset");
m.fhirVersion = "DSTU2_1";
m.execute();
byteCount += m.getByteCount();
fileCount += m.getFileCount();
m = new ResourceMinimizerMojo();
m.myCtx = ctxDstu2_1;
m.targetDirectory = new File("./hapi-fhir-validation-resources-r4/src/main/resources/org/hl7/fhir/r4/model/profile");
m.fhirVersion = "R4";
m.execute();
byteCount += m.getByteCount();
fileCount += m.getFileCount();
m = new ResourceMinimizerMojo();
m.myCtx = ctxDstu2_1;
m.targetDirectory = new File("./hapi-fhir-validation-resources-r4/src/main/resources/org/hl7/fhir/r4/model/valueset");
m.fhirVersion = "R4";
m.execute();
byteCount += m.getByteCount();
fileCount += m.getFileCount();
ourLog.info("Trimmed {} files", fileCount);
ourLog.info("Trimmed {} bytes", FileUtils.byteCountToDisplaySize(byteCount));