use of com.evolveum.midpoint.util.exception.SchemaException in project midpoint by Evolveum.
the class PrismUnmarshaller method parseReferenceValueAsCompositeObject.
private PrismReferenceValue parseReferenceValueAsCompositeObject(XNode node, PrismReferenceDefinition definition, ParsingContext pc) throws SchemaException {
if (!(node instanceof MapXNode)) {
throw new IllegalArgumentException("Cannot parse reference composite object from " + node);
}
MapXNode map = (MapXNode) node;
QName targetTypeName = definition.getTargetTypeName();
PrismObjectDefinition<Objectable> objectDefinition = null;
if (map.getTypeQName() != null) {
objectDefinition = getSchemaRegistry().findObjectDefinitionByType(map.getTypeQName());
}
if (objectDefinition == null && targetTypeName != null) {
objectDefinition = getSchemaRegistry().findObjectDefinitionByType(targetTypeName);
}
if (objectDefinition == null) {
throw new SchemaException("No object definition for composite object in reference element " + definition.getCompositeObjectElementName());
}
PrismObject<Objectable> compositeObject;
try {
compositeObject = parseObject(map, objectDefinition, pc);
} catch (SchemaException e) {
throw new SchemaException(e.getMessage() + " while parsing composite object in reference element " + definition.getCompositeObjectElementName(), e);
}
PrismReferenceValue refVal = new PrismReferenceValue();
setReferenceObject(refVal, compositeObject);
((PrismReferenceDefinitionImpl) definition).setComposite(true);
return refVal;
}
use of com.evolveum.midpoint.util.exception.SchemaException in project midpoint by Evolveum.
the class AbstractJsonLexicalProcessor method parseFromStart.
@NotNull
private RootXNode parseFromStart(JsonParser unconfiguredParser, ParsingContext parsingContext) throws SchemaException {
JsonParsingContext ctx = null;
try {
JsonParser parser = configureParser(unconfiguredParser);
parser.nextToken();
if (parser.currentToken() == null) {
throw new SchemaException("Nothing to parse: the input is empty.");
}
ctx = new JsonParsingContext(parser, parsingContext);
XNode xnode = parseValue(ctx);
if (!(xnode instanceof MapXNode) || ((MapXNode) xnode).size() != 1) {
throw new SchemaException("Expected MapXNode with a single key; got " + xnode + " instead. At " + getPositionSuffix(ctx));
}
processDefaultNamespaces(xnode, null, ctx);
processSchemaNodes(xnode);
Entry<QName, XNode> entry = ((MapXNode) xnode).entrySet().iterator().next();
RootXNode root = new RootXNode(entry.getKey(), entry.getValue());
if (entry.getValue() != null) {
// TODO - ok ????
root.setTypeQName(entry.getValue().getTypeQName());
}
return root;
} catch (IOException e) {
throw new SchemaException("Cannot parse JSON/YAML object: " + e.getMessage() + (ctx != null ? " At: " + getPositionSuffix(ctx) : ""), e);
}
}
use of com.evolveum.midpoint.util.exception.SchemaException in project midpoint by Evolveum.
the class AbstractJsonLexicalProcessor method write.
@NotNull
@Override
public String write(@NotNull RootXNode root, SerializationContext prismSerializationContext) throws SchemaException {
StringWriter out = new StringWriter();
try (JsonGenerator generator = createJacksonGenerator(out)) {
JsonSerializationContext ctx = new JsonSerializationContext(generator, prismSerializationContext);
// TODO default namespace
serialize(root.toMapXNode(), ctx, false);
} catch (IOException ex) {
throw new SchemaException("Error during serializing to JSON/YAML: " + ex.getMessage(), ex);
}
return out.toString();
}
use of com.evolveum.midpoint.util.exception.SchemaException in project midpoint by Evolveum.
the class DomLexicalWriter method serializeInternal.
@NotNull
private Element serializeInternal(@NotNull RootXNode rootxnode, Element parentElement) throws SchemaException {
QName rootElementName = rootxnode.getRootElementName();
Element topElement = createElement(rootElementName, parentElement);
if (parentElement != null) {
parentElement.appendChild(topElement);
}
QName typeQName = rootxnode.getTypeQName();
if (typeQName != null && rootxnode.isExplicitTypeDeclaration()) {
DOMUtil.setXsiType(topElement, setQNamePrefixExplicitIfNeeded(typeQName));
}
XNode subnode = rootxnode.getSubnode();
if (subnode instanceof PrimitiveXNode) {
serializePrimitiveElementOrAttribute((PrimitiveXNode) subnode, topElement, rootElementName, false);
return DOMUtil.getFirstChildElement(topElement);
}
if (subnode instanceof MapXNode) {
// at this point we can put frequently used namespaces (e.g. c, t, q, ri) into the document to eliminate their use
// on many places inside the doc (MID-2198)
DOMUtil.setNamespaceDeclarations(topElement, getNamespacePrefixMapper().getNamespacesDeclaredByDefault());
serializeMap((MapXNode) subnode, topElement);
} else if (subnode.isHeterogeneousList()) {
DOMUtil.setNamespaceDeclarations(topElement, getNamespacePrefixMapper().getNamespacesDeclaredByDefault());
serializeExplicitList((ListXNode) subnode, topElement);
} else {
throw new SchemaException("Sub-root xnode is not a map nor an explicit list, cannot serialize to XML (it is " + subnode + ")");
}
addDefaultNamespaceDeclaration(topElement);
return topElement;
}
use of com.evolveum.midpoint.util.exception.SchemaException in project midpoint by Evolveum.
the class AbstractJsonLexicalProcessor method processSchemaNodes.
// Schema nodes can be detected only after namespaces are resolved.
// We simply convert primitive nodes to schema ones.
private void processSchemaNodes(XNode xnode) throws SchemaException, IOException {
if (xnode instanceof MapXNode) {
MapXNode map = (MapXNode) xnode;
XNode schemaNode = null;
for (Entry<QName, XNode> entry : map.entrySet()) {
QName fieldName = entry.getKey();
XNode subnode = entry.getValue();
if (DOMUtil.XSD_SCHEMA_ELEMENT.equals(fieldName)) {
schemaNode = subnode;
} else {
processSchemaNodes(subnode);
}
}
if (schemaNode != null) {
if (schemaNode instanceof PrimitiveXNode) {
PrimitiveXNode<?> primitiveXNode = (PrimitiveXNode<?>) schemaNode;
if (primitiveXNode.isParsed()) {
throw new SchemaException("Cannot convert from PrimitiveXNode to SchemaXNode: node is already parsed: " + primitiveXNode);
}
SchemaXNode schemaXNode = new SchemaXNode();
map.replace(DOMUtil.XSD_SCHEMA_ELEMENT, schemaXNode);
schemaXNode.setSchemaElement(((JsonValueParser) primitiveXNode.getValueParser()).asDomElement());
} else {
throw new SchemaException("Cannot convert 'schema' field to SchemaXNode: not a PrimitiveNode but " + schemaNode);
}
}
} else if (xnode instanceof ListXNode) {
for (XNode item : (ListXNode) xnode) {
processSchemaNodes(item);
}
}
}
Aggregations