private static void testSearchForPatients() { List<IBaseResource> resources = new IncludesExamples().searchForPatients(); // Create a bundle with both FhirContext ctx = FhirContext.forDstu2(); Dstu2BundleFactory bf = new Dstu2BundleFactory(ctx); bf.initializeBundleFromResourceList(null, resources, "http://example.com/base", "http://example.com/base/Patient", 1, BundleTypeEnum.SEARCHSET); IBaseResource b = bf.getResourceBundle(); // Encode the bundle String encoded = ctx.newXmlParser().setPrettyPrint(true).encodeResourceToString(b); System.out.println(encoded); }
private org.hl7.fhir.instance.model.Resource toDstu2(IBaseResource theResponseResource) { if (theResponseResource instanceof IResource) { return (org.hl7.fhir.instance.model.Resource) myCtxDstu2Hl7Org.newJsonParser().parseResource(myCtxDstu2.newJsonParser().encodeResourceToString(theResponseResource)); } return (org.hl7.fhir.instance.model.Resource) theResponseResource; }
public void parserValidation() { // START SNIPPET: parserValidation FhirContext ctx = FhirContext.forDstu3(); // Create a parser and configure it to use the strict error handler IParser parser = ctx.newXmlParser(); parser.setParserErrorHandler(new StrictErrorHandler()); // This example resource is invalid, as Patient.active can not repeat String input = "<Patient><active value=\"true\"/><active value=\"false\"/></Patient>"; // The following will throw a DataFormatException because of the StrictErrorHandler parser.parseResource(Patient.class, input); // END SNIPPET: parserValidation }
public static byte[] encodeResource(IBaseResource theResource, ResourceEncodingEnum theEncoding, Set<String> theExcludeElements, FhirContext theContext) { byte[] bytes; IParser parser = theEncoding.newParser(theContext); parser.setDontEncodeElements(theExcludeElements); String encoded = parser.encodeResourceToString(theResource); switch (theEncoding) { case JSON: bytes = encoded.getBytes(Charsets.UTF_8); break; case JSONC: bytes = GZipUtil.compress(encoded); break; default: case DEL: bytes = new byte[0]; break; } ourLog.debug("Encoded {} chars of resource body as {} bytes", encoded.length(), bytes.length); return bytes; }
parsedRes = xmlParser.parseResource(new FileReader(nextFile)); } else if (nextFile.getAbsolutePath().toLowerCase().endsWith(".json")) { parsedRes = jsonParser.parseResource(new FileReader(nextFile)); } else { ourLog.info("Ignoring file: {}", nextFile.getName()); ValidationResult result = val.validateWithResult(input); IBaseOperationOutcome oo = result.toOperationOutcome(); ourLog.info("Result:\n{}", xmlParser.setPrettyPrint(true).encodeResourceToString(oo));
public IBaseResource getNewPayload(FhirContext theCtx) { if (myPayloadDecoded == null && isNotBlank(myPayload)) { myPayloadDecoded = theCtx.newJsonParser().parseResource(myPayload); } return myPayloadDecoded; }
@Override public String encode() { return theContext.newXmlParser().encodeResourceToString(theResource); }
private String encodeContents(Boolean thePrettyPrint, EncodingEnum encoding) { IParser parser; if (encoding == EncodingEnum.JSON) { parser = getContext().newJsonParser(); } else { parser = getContext().newXmlParser(); } if (thePrettyPrint != null) { parser.setPrettyPrint(thePrettyPrint); } if (myForceResourceId != null) { parser.setEncodeForceResourceId(myForceResourceId); } parser.setOmitResourceId(myOmitResourceId); if (myResources != null) { IVersionSpecificBundleFactory bundleFactory = getContext().newBundleFactory(); bundleFactory.initializeBundleFromResourceList("", myResources, "", "", myResources.size(), myBundleType); IBaseResource bundle = bundleFactory.getResourceBundle(); if (bundle != null) { return parser.encodeResourceToString(bundle); } IBaseResource bundleRes = bundleFactory.getResourceBundle(); return parser.encodeResourceToString(bundleRes); } else if (myContents != null) { return myContents; } else { return parser.encodeResourceToString(myResource); } }
p.setPrettyPrint(true); EncodingEnum encoding = p.getEncoding(); String encoded = p.encodeResourceToString(theResource);
protected void configureParser(IParser parser) { if (ObjectHelper.isNotEmpty(getServerBaseUrl())) { parser.setServerBaseUrl(getServerBaseUrl()); parser.setDontEncodeElements(getDontEncodeElements()); parser.setDontStripVersionsFromReferencesAtPaths(getDontStripVersionsFromReferencesAtPaths()); parser.setEncodeElements(getEncodeElements()); parser.setEncodeElementsAppliesToResourceTypes(getEncodeElementsAppliesToResourceTypes()); parser.setEncodeForceResourceId(getForceResourceId()); parser.setPreferTypes(getPreferTypes()); parser.setParserErrorHandler(getParserErrorHandler()); parser.setOverrideResourceIdWithBundleEntryFullUrl(getOverrideResourceIdWithBundleEntryFullUrl()); parser.setStripVersionsFromReferences(getStripVersionsFromReferences()); parser.setSummaryMode(isSummaryMode()); parser.setOmitResourceId(isOmitResourceId()); parser.setPrettyPrint(isPrettyPrint()); parser.setEncodeElementsAppliesToChildResourcesOnly(isEncodeElementsAppliesToChildResourcesOnly());
@Override public T invokeClient(String theResponseMimeType, InputStream theResponseInputStream, int theResponseStatusCode, Map<String, List<String>> theHeaders) throws BaseServerResponseException { EncodingEnum respType = EncodingEnum.forContentType(theResponseMimeType); if (respType == null) { if (myAllowHtmlResponse && theResponseMimeType.toLowerCase().contains(Constants.CT_HTML) && myReturnType != null) { return readHtmlResponse(theResponseInputStream); } throw NonFhirResponseException.newInstance(theResponseStatusCode, theResponseMimeType, theResponseInputStream); } IParser parser = respType.newParser(getFhirContext()); parser.setServerBaseUrl(getUrlBase()); if (myPreferResponseTypes != null) { parser.setPreferTypes(myPreferResponseTypes); } T retVal = parser.parseResource(myReturnType, theResponseInputStream); MethodUtil.parseClientRequestResourceHeaders(myId, theHeaders, retVal); return retVal; }
public static void main(String[] args) throws Exception { FhirContext ctx = FhirContext.forDstu2(); String fileName = "src/main/resources/vs/dstu2/all-valuesets-bundle.xml"; FileReader fr = new FileReader(fileName); Bundle b = ctx.newXmlParser().parseResource(Bundle.class, fr); for (Entry nextEntry : b.getEntry()) { BaseResource nextRes = (BaseResource) nextEntry.getResource(); nextRes.setText(new NarrativeDt()); } File f = new File(fileName); OutputStreamWriter fw = new OutputStreamWriter(new FileOutputStream(f, false), "UTF-8"); ctx.newXmlParser().encodeResourceToWriter(b, fw); fw.close(); ourLog.info("Fixed {} valuesets", b.getEntry().size()); }
parser.setPrettyPrint(prettyPrint); parser.setServerBaseUrl(theRequestDetails.getFhirServerBase()); parser.setEncodeElements(Collections.singleton("Bundle.total")); } else if (summaryMode.contains(SummaryEnum.TEXT) && summaryMode.size() == 1) { parser.setEncodeElements(TEXT_ENCODE_ELEMENTS); } else { parser.setSuppressNarratives(summaryMode.contains(SummaryEnum.DATA)); parser.setSummaryMode(summaryMode.contains(SummaryEnum.TRUE)); parser.setEncodeElementsAppliesToChildResourcesOnly(true); parser.setEncodeElements(newElements); parser.setEncodeElementsAppliesToResourceTypes(elementsAppliesTo);
@Override public void marshal(AuditMessage auditMessage, Writer writer, boolean pretty) throws IOException { getParser(fhirContext) .setPrettyPrint(pretty) .encodeResourceToWriter(translate(auditMessage), writer); }
parser.setServerBaseUrl(theRequest.getFhirServerBase()); T retVal; try { if (theResourceType != null) { retVal = parser.parseResource(theResourceType, requestReader); } else { retVal = (T) parser.parseResource(requestReader);
public Object returnResponse(ServletRequestDetails theRequest, ParseAction<?> outcome, int operationStatus, boolean allowPrefer, MethodOutcome response, String resourceName) throws IOException { HttpServletResponse servletResponse = theRequest.getServletResponse(); servletResponse.setStatus(operationStatus); servletResponse.setCharacterEncoding(Constants.CHARSET_NAME_UTF8); addHeadersToResponse(servletResponse); if (allowPrefer) { addContentLocationHeaders(theRequest, servletResponse, response, resourceName); } Writer writer; if (outcome != null) { ResponseEncoding encoding = RestfulServerUtils.determineResponseEncodingWithDefault(theRequest); servletResponse.setContentType(encoding.getResourceContentType()); writer = servletResponse.getWriter(); IParser parser = encoding.getEncoding().newParser(getFhirContext()); parser.setPrettyPrint(RestfulServerUtils.prettyPrintResponse(this, theRequest)); outcome.execute(parser, writer); } else { servletResponse.setContentType(Constants.CT_TEXT_WITH_UTF8); writer = servletResponse.getWriter(); } return writer; }
@Override public void marshal(Exchange exchange, Object o, OutputStream outputStream) throws Exception { IBaseResource iBaseResource; if (!(o instanceof IBaseResource)) { iBaseResource = exchange.getContext().getTypeConverter().mandatoryConvertTo(IBaseResource.class, exchange, o); } else { iBaseResource = (IBaseResource) o; } IParser parser = getFhirContext().newXmlParser(); configureParser(parser); parser.encodeResourceToWriter(iBaseResource, new OutputStreamWriter(outputStream)); if (isContentTypeHeader()) { exchange.getMessage().setHeader(Exchange.CONTENT_TYPE, parser.getEncoding().getResourceContentTypeNonLegacy()); } }
FhirVersionEnum forVersion = theResource.getStructureFhirVersionEnum(); IParser parser = getNewParser(theServer.getFhirContext(), forVersion, theRequestDetails); parser.encodeResourceToWriter(theResource, writer);
@SuppressWarnings("unused") public static void main(String[] args) throws DataFormatException, IOException { //START SNIPPET: patientUse MyPatient patient = new MyPatient(); patient.setPetName(new StringDt("Fido")); patient.getImportantDates().add(new DateTimeDt("2010-01-02")); patient.getImportantDates().add(new DateTimeDt("2014-01-26T11:11:11")); patient.addName().addFamily("Smith").addGiven("John").addGiven("Quincy").addSuffix("Jr"); IParser p = FhirContext.forDstu2().newXmlParser().setPrettyPrint(true); String messageString = p.encodeResourceToString(patient); System.out.println(messageString); //END SNIPPET: patientUse //START SNIPPET: patientParse IParser parser = FhirContext.forDstu2().newXmlParser(); MyPatient newPatient = parser.parseResource(MyPatient.class, messageString); //END SNIPPET: patientParse { FhirContext ctx2 = FhirContext.forDstu2(); RuntimeResourceDefinition def = ctx2.getResourceDefinition(patient); System.out.println(ctx2.newXmlParser().setPrettyPrint(true).encodeResourceToString(def.toProfile())); } }
public IBaseResource getPayload(FhirContext theCtx) { IBaseResource retVal = myPayload; if (retVal == null && isNotBlank(myPayloadString)) { retVal = theCtx.newJsonParser().parseResource(myPayloadString); myPayload = retVal; } return retVal; }