use of com.evolveum.midpoint.prism.xnode.MapXNode 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);
}
}
}
use of com.evolveum.midpoint.prism.xnode.MapXNode in project midpoint by Evolveum.
the class AbstractJsonLexicalProcessor method parseJsonObject.
/**
* Normally returns a MapXNode. However, JSON primitives/lists can be simulated by two-member object (@type + @value); in these cases we return respective XNode.
*/
@NotNull
private XNode parseJsonObject(JsonParsingContext ctx) throws SchemaException, IOException {
Validate.notNull(ctx.parser.currentToken());
QName typeName = null;
QNameUtil.QNameInfo elementNameInfo = null;
Object tid = ctx.parser.getTypeId();
if (tid != null) {
typeName = tagToTypeName(tid, ctx);
}
final MapXNode map = new MapXNode();
XNode wrappedValue = null;
boolean defaultNamespaceDefined = false;
QNameUtil.QNameInfo currentFieldNameInfo = null;
for (; ; ) {
JsonToken token = ctx.parser.nextToken();
if (token == null) {
ctx.prismParsingContext.warnOrThrow(LOGGER, "Unexpected end of data while parsing a map structure at " + getPositionSuffix(ctx));
break;
} else if (token == JsonToken.END_OBJECT) {
break;
} else if (token == JsonToken.FIELD_NAME) {
String newFieldName = ctx.parser.getCurrentName();
if (currentFieldNameInfo != null) {
ctx.prismParsingContext.warnOrThrow(LOGGER, "Two field names in succession: " + currentFieldNameInfo + " and " + newFieldName);
}
currentFieldNameInfo = QNameUtil.uriToQNameInfo(newFieldName, true);
} else {
XNode valueXNode = parseValue(ctx);
assert currentFieldNameInfo != null;
if (isSpecial(currentFieldNameInfo.name)) {
if (isNamespaceDeclaration(currentFieldNameInfo.name)) {
if (defaultNamespaceDefined) {
ctx.prismParsingContext.warnOrThrow(LOGGER, "Default namespace defined more than once at " + getPositionSuffix(ctx));
}
ctx.defaultNamespaces.put(map, getStringValue(valueXNode, currentFieldNameInfo, ctx));
defaultNamespaceDefined = true;
} else if (isTypeDeclaration(currentFieldNameInfo.name)) {
if (typeName != null) {
ctx.prismParsingContext.warnOrThrow(LOGGER, "Value type defined more than once at " + getPositionSuffix(ctx));
}
typeName = QNameUtil.uriToQName(getStringValue(valueXNode, currentFieldNameInfo, ctx), true);
} else if (isElementDeclaration(currentFieldNameInfo.name)) {
if (elementNameInfo != null) {
ctx.prismParsingContext.warnOrThrow(LOGGER, "Element name defined more than once at " + getPositionSuffix(ctx));
}
elementNameInfo = QNameUtil.uriToQNameInfo(getStringValue(valueXNode, currentFieldNameInfo, ctx), true);
} else if (isValue(currentFieldNameInfo.name)) {
if (wrappedValue != null) {
ctx.prismParsingContext.warnOrThrow(LOGGER, "Value ('" + PROP_VALUE + "') defined more than once at " + getPositionSuffix(ctx));
}
wrappedValue = valueXNode;
}
} else {
Map.Entry<QName, XNode> entry = map.putReturningEntry(currentFieldNameInfo.name, valueXNode);
if (currentFieldNameInfo.explicitEmptyNamespace) {
ctx.noNamespaceEntries.put(entry, null);
}
}
currentFieldNameInfo = null;
}
}
// Return either map or primitive value (in case of @type/@value)
XNode rv;
if (wrappedValue != null) {
if (!map.isEmpty()) {
ctx.prismParsingContext.warnOrThrow(LOGGER, "Both '" + PROP_VALUE + "' and regular content present at " + getPositionSuffix(ctx));
rv = map;
} else {
rv = wrappedValue;
}
} else {
rv = map;
}
if (typeName != null) {
if (wrappedValue != null && wrappedValue.getTypeQName() != null && !wrappedValue.getTypeQName().equals(typeName)) {
ctx.prismParsingContext.warnOrThrow(LOGGER, "Conflicting type names for '" + PROP_VALUE + "' (" + wrappedValue.getTypeQName() + ") and regular content (" + typeName + ") present at " + getPositionSuffix(ctx));
}
rv.setTypeQName(typeName);
rv.setExplicitTypeDeclaration(true);
}
if (elementNameInfo != null) {
if (wrappedValue != null && wrappedValue.getElementName() != null) {
boolean wrappedValueElementNoNamespace = ctx.noNamespaceElementNames.containsKey(wrappedValue);
if (!wrappedValue.getElementName().equals(elementNameInfo.name) || wrappedValueElementNoNamespace != elementNameInfo.explicitEmptyNamespace) {
ctx.prismParsingContext.warnOrThrow(LOGGER, "Conflicting element names for '" + PROP_VALUE + "' (" + wrappedValue.getElementName() + "; no NS=" + wrappedValueElementNoNamespace + ") and regular content (" + elementNameInfo.name + "; no NS=" + elementNameInfo.explicitEmptyNamespace + ") present at " + getPositionSuffix(ctx));
}
}
rv.setElementName(elementNameInfo.name);
if (elementNameInfo.explicitEmptyNamespace) {
ctx.noNamespaceElementNames.put(rv, null);
}
}
return rv;
}
use of com.evolveum.midpoint.prism.xnode.MapXNode in project midpoint by Evolveum.
the class SearchFilterType method parseFromXNode.
public void parseFromXNode(XNode xnode, PrismContext prismContext) throws SchemaException {
if (xnode == null || xnode.isEmpty()) {
this.filterClauseXNode = null;
this.description = null;
} else {
if (!(xnode instanceof MapXNode)) {
throw new SchemaException("Cannot parse filter from " + xnode);
}
MapXNode xmap = (MapXNode) xnode;
XNode xdesc = xmap.get(SearchFilterType.F_DESCRIPTION);
if (xdesc != null) {
if (xdesc instanceof PrimitiveXNode<?>) {
String desc = ((PrimitiveXNode<String>) xdesc).getParsedValue(DOMUtil.XSD_STRING, String.class);
setDescription(desc);
} else {
throw new SchemaException("Description must have a primitive value");
}
}
MapXNode xfilter = new MapXNode();
for (Entry<QName, XNode> entry : xmap.entrySet()) {
if (!QNameUtil.match(entry.getKey(), SearchFilterType.F_DESCRIPTION) && !QNameUtil.match(entry.getKey(), new QName("condition"))) {
xfilter.put(entry.getKey(), entry.getValue());
}
}
if (xfilter.size() > 1) {
throw new SchemaException("Filter clause has more than one item: " + xfilter);
}
this.filterClauseXNode = xfilter;
QueryConvertor.parseFilterPreliminarily(xfilter, prismContext);
}
}
use of com.evolveum.midpoint.prism.xnode.MapXNode in project midpoint by Evolveum.
the class SearchFilterType method getFilterClauseAsRootXNode.
public RootXNode getFilterClauseAsRootXNode() throws SchemaException {
MapXNode clause = getFilterClauseXNode();
if (clause == null) {
return null;
}
Entry<QName, XNode> singleEntry = clause.getSingleSubEntry("getFilterClauseAsRootXNode");
if (singleEntry == null) {
return null;
}
return new RootXNode(singleEntry.getKey(), singleEntry.getValue());
}
use of com.evolveum.midpoint.prism.xnode.MapXNode in project midpoint by Evolveum.
the class PrismUtil method serializeExpression.
// TODO: Unify the two serializeExpression() methods
public static MapXNode serializeExpression(ExpressionWrapper expressionWrapper, PrismSerializer<RootXNode> xnodeSerializer) throws SchemaException {
MapXNode xmap = new MapXNode();
Object expressionObject = expressionWrapper.getExpression();
if (expressionObject == null) {
return xmap;
}
RootXNode xroot = xnodeSerializer.serializeAnyData(expressionObject, expressionWrapper.getElementName());
if (xroot == null) {
return xmap;
}
xmap.merge(expressionWrapper.getElementName(), xroot.getSubnode());
return xmap;
}
Aggregations