本文整理汇总了Java中ca.uhn.fhir.parser.IParser.setParserErrorHandler方法的典型用法代码示例。如果您正苦于以下问题:Java IParser.setParserErrorHandler方法的具体用法?Java IParser.setParserErrorHandler怎么用?Java IParser.setParserErrorHandler使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ca.uhn.fhir.parser.IParser
的用法示例。
在下文中一共展示了IParser.setParserErrorHandler方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import ca.uhn.fhir.parser.IParser; //导入方法依赖的package包/类
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);
}
示例2: parserValidation
import ca.uhn.fhir.parser.IParser; //导入方法依赖的package包/类
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
}
示例3: testOutOfBoundsDate
import ca.uhn.fhir.parser.IParser; //导入方法依赖的package包/类
/**
* See issue #50
*/
@Test()
public void testOutOfBoundsDate() {
Patient p = new Patient();
p.setBirthDateElement(new DateType("2000-12-31"));
// Put in an invalid date
IParser parser = ourCtx.newXmlParser();
parser.setParserErrorHandler(new StrictErrorHandler());
String encoded = parser.setPrettyPrint(true).encodeResourceToString(p).replace("2000-12-31", "2000-15-31");
ourLog.info(encoded);
assertThat(encoded, StringContains.containsString("2000-15-31"));
ValidationResult result = ourCtx.newValidator().validateWithResult(encoded);
String resultString = parser.setPrettyPrint(true).encodeResourceToString(result.toOperationOutcome());
ourLog.info(resultString);
assertEquals(2, ((OperationOutcome)result.toOperationOutcome()).getIssue().size());
assertThat(resultString, StringContains.containsString("cvc-pattern-valid"));
try {
parser.parseResource(encoded);
fail();
} catch (DataFormatException e) {
assertEquals("DataFormatException at [[row,col {unknown-source}]: [2,4]]: Invalid attribute value \"2000-15-31\": Invalid date/time format: \"2000-15-31\"", e.getMessage());
}
}
示例4: forText
import ca.uhn.fhir.parser.IParser; //导入方法依赖的package包/类
public static IValidationContext<IBaseResource> forText(final FhirContext theContext, final String theResourceBody) {
ObjectUtil.requireNonNull(theContext, "theContext can not be null");
ObjectUtil.requireNotEmpty(theResourceBody, "theResourceBody can not be null or empty");
return new BaseValidationContext<IBaseResource>(theContext) {
private EncodingEnum myEncoding;
private IBaseResource myParsed;
@Override
public IBaseResource getResource() {
if (myParsed == null) {
IParser parser = getResourceAsStringEncoding().newParser(getFhirContext());
LenientErrorHandler errorHandler = new LenientErrorHandler();
errorHandler.setErrorOnInvalidValue(false);
parser.setParserErrorHandler(errorHandler);
myParsed = parser.parseResource(getResourceAsString());
}
return myParsed;
}
@Override
public String getResourceAsString() {
return theResourceBody;
}
@Override
public EncodingEnum getResourceAsStringEncoding() {
if (myEncoding == null) {
myEncoding = EncodingEnum.detectEncodingNoDefault(theResourceBody);
if (myEncoding == null) {
throw new InvalidRequestException(theContext.getLocalizer().getMessage(ValidationContext.class, "unableToDetermineEncoding"));
}
}
return myEncoding;
}
};
}
示例5: testOutOfBoundsDate
import ca.uhn.fhir.parser.IParser; //导入方法依赖的package包/类
/**
* See issue #50
*/
@Test()
public void testOutOfBoundsDate() {
Patient p = new Patient();
p.setBirthDate(new DateTimeDt("2000-12-31"));
// Put in an invalid date
IParser parser = ourCtx.newXmlParser();
parser.setParserErrorHandler(new StrictErrorHandler());
String encoded = parser.setPrettyPrint(true).encodeResourceToString(p).replace("2000-12-31", "2000-15-31");
ourLog.info(encoded);
assertThat(encoded, StringContains.containsString("2000-15-31"));
FhirValidator validator = ourCtx.newValidator();
ValidationResult result = validator.validateWithResult(encoded);
String resultString = parser.setPrettyPrint(true).encodeResourceToString(result.toOperationOutcome());
ourLog.info(resultString);
assertEquals(2, ((OperationOutcome)result.toOperationOutcome()).getIssue().size());
assertThat(resultString, StringContains.containsString("cvc-datatype-valid.1.2.3"));
try {
parser.parseResource(encoded);
fail();
} catch (DataFormatException e) {
assertEquals("DataFormatException at [[row,col {unknown-source}]: [2,4]]: Invalid attribute value \"2000-15-31\": Invalid date/time format: \"2000-15-31\"", e.getMessage());
}
}
示例6: testJsonLikeSimpleResourceEncode
import ca.uhn.fhir.parser.IParser; //导入方法依赖的package包/类
@Test
public void testJsonLikeSimpleResourceEncode() throws IOException {
String xmlString = IOUtils.toString(JsonParser.class.getResourceAsStream("/example-patient-general.xml"), Charset.forName("UTF-8"));
IParser parser = ourCtx.newXmlParser();
parser.setParserErrorHandler(new StrictErrorHandler());
Patient obs = parser.parseResource(Patient.class, xmlString);
IJsonLikeParser jsonLikeParser = (IJsonLikeParser)ourCtx.newJsonParser();
StringWriter stringWriter = new StringWriter();
JsonLikeStructure jsonLikeStructure = new GsonStructure();
JsonLikeWriter jsonLikeWriter = jsonLikeStructure.getJsonLikeWriter(stringWriter);
jsonLikeParser.encodeResourceToJsonLikeWriter(obs, jsonLikeWriter);
String encoded = stringWriter.toString();
ourLog.info(encoded);
String jsonString = IOUtils.toString(JsonParser.class.getResourceAsStream("/example-patient-general.json"), Charset.forName("UTF-8"));
JSON expected = JSONSerializer.toJSON(jsonString);
JSON actual = JSONSerializer.toJSON(encoded.trim());
// The encoded escapes quote marks using XML escaping instead of JSON escaping, which is probably nicer anyhow...
String exp = expected.toString().replace("\\\"Jim\\\"", ""Jim"");
String act = actual.toString();
ourLog.info("Expected: {}", exp);
ourLog.info("Actual : {}", act);
assertEquals("\nExpected: " + exp + "\nActual : " + act, exp, act);
}
示例7: testOutOfBoundsDate
import ca.uhn.fhir.parser.IParser; //导入方法依赖的package包/类
/**
* See issue #50
*/
@Test()
public void testOutOfBoundsDate() {
Patient p = new Patient();
p.setBirthDateElement(new DateType("2000-12-31"));
// Put in an invalid date
IParser parser = ourCtx.newXmlParser();
parser.setParserErrorHandler(new StrictErrorHandler());
String encoded = parser.setPrettyPrint(true).encodeResourceToString(p).replace("2000-12-31", "2000-15-31");
ourLog.info(encoded);
assertThat(encoded, StringContains.containsString("2000-15-31"));
ValidationResult result = ourCtx.newValidator().validateWithResult(encoded);
String resultString = parser.setPrettyPrint(true).encodeResourceToString(result.toOperationOutcome());
ourLog.info(resultString);
assertEquals(2, ((OperationOutcome) result.toOperationOutcome()).getIssue().size());
assertThat(resultString, StringContains.containsString("cvc-pattern-valid"));
try {
parser.parseResource(encoded);
fail();
} catch (DataFormatException e) {
assertEquals("DataFormatException at [[row,col {unknown-source}]: [2,4]]: Invalid attribute value \"2000-15-31\": Invalid date/time format: \"2000-15-31\"", e.getMessage());
}
}