use of org.ehrbase.serialisation.exception.UnmarshalException in project openEHR_SDK by ehrbase.
the class FlatJsonUnmarshaller method unmarshal.
/**
* Unmarshal flat Json to Composition
*
* @param flat the flat Json
* @param introspect the introspect belonging to the template
* @return
*/
public Composition unmarshal(String flat, WebTemplate introspect) {
Set<String> consumedPath;
Map<String, String> currentValues;
consumedPath = new HashSet<>();
try {
currentValues = new HashMap<>();
for (Iterator<Map.Entry<String, JsonNode>> it = OBJECT_MAPPER.readTree(flat).fields(); it.hasNext(); ) {
Map.Entry<String, JsonNode> e = it.next();
currentValues.put(e.getKey(), e.getValue().toString());
}
Composition generate = WebTemplateSkeletonBuilder.build(introspect, false);
StdToCompositionWalker walker = new StdToCompositionWalker();
DefaultValues defaultValues = new DefaultValues(currentValues);
// put default for the defaults
if (!defaultValues.containsDefaultValue(DefaultValuePath.TIME)) {
defaultValues.addDefaultValue(DefaultValuePath.TIME, OffsetDateTime.now());
}
if (!defaultValues.containsDefaultValue(DefaultValuePath.SETTING)) {
defaultValues.addDefaultValue(DefaultValuePath.SETTING, Setting.OTHER_CARE);
}
String templateId = generate.getArchetypeDetails().getTemplateId().getValue();
walker.walk(generate, currentValues.entrySet().stream().collect(Collectors.toMap(e1 -> new FlatPathDto(e1.getKey()), Map.Entry::getValue)), introspect, defaultValues, templateId);
consumedPath = walker.getConsumedPaths();
if (!CollectionUtils.isEmpty(getUnconsumed(consumedPath, currentValues))) {
throw new UnmarshalException(String.format("Could not consume Parts %s", getUnconsumed(consumedPath, currentValues)));
}
return generate;
} catch (JsonProcessingException e) {
throw new UnmarshalException(e.getMessage(), e);
}
}
use of org.ehrbase.serialisation.exception.UnmarshalException in project openEHR_SDK by ehrbase.
the class CanonicalXML method unmarshal.
@Override
public <T extends RMObject> T unmarshal(String value, Class<T> clazz) {
T composition;
try {
Unmarshaller unmarshaller = JAXBUtil.getArchieJAXBContext().createUnmarshaller();
// Set the parent XMLReader on the XMLFilter
SAXParserFactory spf = SAXParserFactory.newInstance();
// disable external entities
spf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE);
spf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
spf.setNamespaceAware(true);
spf.setValidating(false);
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
XMLFilter filter = new NamespaceFilter();
filter.setParent(xr);
UnmarshallerHandler unmarshallerHandler = unmarshaller.getUnmarshallerHandler();
filter.setContentHandler(unmarshallerHandler);
filter.parse(new InputSource(IOUtils.toInputStream(value, UTF_8)));
composition = (T) unmarshallerHandler.getResult();
} catch (JAXBException | ParserConfigurationException | SAXException | IOException e) {
throw new UnmarshalException(e.getMessage(), e);
}
return composition;
}
use of org.ehrbase.serialisation.exception.UnmarshalException in project openEHR_SDK by ehrbase.
the class StdToCompositionWalker method handleRaw.
private void handleRaw(Context<Map<FlatPathDto, String>> context) {
ObjectMapper om = JacksonUtil.getObjectMapper();
try {
Map.Entry<FlatPathDto, String> current = context.getObjectDeque().peek().entrySet().stream().findAny().orElseThrow();
RMObject newRmObject = new CanonicalJson().unmarshal(om.readValue(current.getValue(), String.class).replace("\"@class\"", "\"_type\""), RMObject.class);
// Replace old skeleton
replaceRmObject(context, newRmObject);
consumedPaths.add(current.getKey().format());
} catch (JsonProcessingException e) {
throw new UnmarshalException(e.getMessage());
}
}
Aggregations