Java Code Examples for ca.uhn.fhir.parser.IParser#setParserErrorHandler()

The following examples show how to use ca.uhn.fhir.parser.IParser#setParserErrorHandler() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ValidationSupportR4.java    From synthea with Apache License 2.0 6 votes vote down vote up
/**
 * Loads the structure definitions from the given directory.
 * @param rootDir the directory to load structure definitions from
 * @return a list of structure definitions
 * @throws Throwable when there is an error reading the structure definitions.
 */
private void loadFromDirectory(String rootDir) throws Throwable {

  IParser jsonParser = FhirContext.forR4().newJsonParser();
  jsonParser.setParserErrorHandler(new StrictErrorHandler());

  URL profilesFolder = ClassLoader.getSystemClassLoader().getResource(rootDir);
  Path path = Paths.get(profilesFolder.toURI());
  Files.walk(path, Integer.MAX_VALUE).filter(Files::isReadable).filter(Files::isRegularFile)
      .filter(p -> p.toString().endsWith(".json")).forEach(f -> {
        try {
          IBaseResource resource = jsonParser.parseResource(new FileReader(f.toFile()));
          handleResource(resource);
        } catch (FileNotFoundException e) {
          throw new RuntimeException(e);
        }
      });
}
 
Example 2
Source File: ValidationSupportSTU3.java    From synthea with Apache License 2.0 5 votes vote down vote up
/**
 * Loads the structure definitions from the given directory.
 * @param rootDir the directory to load structure definitions from
 * @return a list of structure definitions
 * @throws Throwable when there is an error reading the structure definitions.
 */
private void loadFromDirectory(String rootDir) throws Throwable {

  IParser jsonParser = FhirContext.forDstu3().newJsonParser();
  jsonParser.setParserErrorHandler(new StrictErrorHandler());

  URL profilesFolder = ClassLoader.getSystemClassLoader().getResource(rootDir);
  Path path = Paths.get(profilesFolder.toURI());
  Files.walk(path, Integer.MAX_VALUE).filter(Files::isReadable).filter(Files::isRegularFile)
      .filter(p -> p.toString().endsWith(".json")).forEach(f -> {
        try {
          IBaseResource resource = jsonParser.parseResource(new FileReader(f.toFile()));
          resources.add(resource);
          if (resource instanceof CodeSystem) {
            CodeSystem cs = (CodeSystem) resource;
            resourcesMap.put(cs.getUrl(), cs);
            codeSystemMap.put(cs.getUrl(), cs);
          } else if (resource instanceof ValueSet) {
            ValueSet vs = (ValueSet) resource;
            resourcesMap.put(vs.getUrl(), vs);
          } else if (resource instanceof StructureDefinition) {
            StructureDefinition sd = (StructureDefinition) resource;
            resourcesMap.put(sd.getUrl(), sd);
            definitions.add(sd);
            definitionsMap.put(sd.getUrl(), sd);
          }
        } catch (FileNotFoundException e) {
          throw new RuntimeException(e);
        }
      });
}
 
Example 3
Source File: Example35_ParserErrorHandler.java    From fhirstarters with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static void main(String[] args) {

      String input =
         "{" +
            "\"resourceType\": \"Patient\"," +
            "\"foo\": \"bar\"" +
            "}";

      FhirContext ctx = FhirContext.forDstu3();
      IParser parser = ctx.newJsonParser();
      parser.setParserErrorHandler(new StrictErrorHandler());
      IBaseResource output = parser.parseResource(input);
   }
 
Example 4
Source File: Example12_ParserErrorHandler.java    From fhirstarters with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static void main(String[] args) {
	String input = "<Encounter xmlns=\"http://hl7.org/fhir\"><AAAA value=\"foo\"/></Encounter>";

	IParser p = FhirContext.forDstu3().newXmlParser();
	
	// Parse with (default) lenient error handler
	p.setParserErrorHandler(new LenientErrorHandler());
	p.parseResource(input);

	// Parse with strict error handler
	p.setParserErrorHandler(new StrictErrorHandler());
	p.parseResource(input);
}