Search in sources :

Example 11 with MapXNode

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);
        }
    }
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) SchemaXNode(com.evolveum.midpoint.prism.xnode.SchemaXNode) PrimitiveXNode(com.evolveum.midpoint.prism.xnode.PrimitiveXNode) ListXNode(com.evolveum.midpoint.prism.xnode.ListXNode) QName(javax.xml.namespace.QName) SchemaXNode(com.evolveum.midpoint.prism.xnode.SchemaXNode) RootXNode(com.evolveum.midpoint.prism.xnode.RootXNode) XNode(com.evolveum.midpoint.prism.xnode.XNode) MapXNode(com.evolveum.midpoint.prism.xnode.MapXNode) PrimitiveXNode(com.evolveum.midpoint.prism.xnode.PrimitiveXNode) ListXNode(com.evolveum.midpoint.prism.xnode.ListXNode) MapXNode(com.evolveum.midpoint.prism.xnode.MapXNode)

Example 12 with MapXNode

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;
}
Also used : QName(javax.xml.namespace.QName) SchemaXNode(com.evolveum.midpoint.prism.xnode.SchemaXNode) RootXNode(com.evolveum.midpoint.prism.xnode.RootXNode) XNode(com.evolveum.midpoint.prism.xnode.XNode) MapXNode(com.evolveum.midpoint.prism.xnode.MapXNode) PrimitiveXNode(com.evolveum.midpoint.prism.xnode.PrimitiveXNode) ListXNode(com.evolveum.midpoint.prism.xnode.ListXNode) QNameUtil(com.evolveum.midpoint.util.QNameUtil) PolyString(com.evolveum.midpoint.prism.polystring.PolyString) MapXNode(com.evolveum.midpoint.prism.xnode.MapXNode) NotNull(org.jetbrains.annotations.NotNull)

Example 13 with MapXNode

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);
    }
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) PrimitiveXNode(com.evolveum.midpoint.prism.xnode.PrimitiveXNode) QName(javax.xml.namespace.QName) RootXNode(com.evolveum.midpoint.prism.xnode.RootXNode) MapXNode(com.evolveum.midpoint.prism.xnode.MapXNode) XNode(com.evolveum.midpoint.prism.xnode.XNode) PrimitiveXNode(com.evolveum.midpoint.prism.xnode.PrimitiveXNode) MapXNode(com.evolveum.midpoint.prism.xnode.MapXNode)

Example 14 with MapXNode

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());
}
Also used : QName(javax.xml.namespace.QName) RootXNode(com.evolveum.midpoint.prism.xnode.RootXNode) MapXNode(com.evolveum.midpoint.prism.xnode.MapXNode) XNode(com.evolveum.midpoint.prism.xnode.XNode) PrimitiveXNode(com.evolveum.midpoint.prism.xnode.PrimitiveXNode) RootXNode(com.evolveum.midpoint.prism.xnode.RootXNode) MapXNode(com.evolveum.midpoint.prism.xnode.MapXNode)

Example 15 with MapXNode

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;
}
Also used : RootXNode(com.evolveum.midpoint.prism.xnode.RootXNode) MapXNode(com.evolveum.midpoint.prism.xnode.MapXNode)

Aggregations

MapXNode (com.evolveum.midpoint.prism.xnode.MapXNode)42 QName (javax.xml.namespace.QName)24 RootXNode (com.evolveum.midpoint.prism.xnode.RootXNode)21 XNode (com.evolveum.midpoint.prism.xnode.XNode)18 PrimitiveXNode (com.evolveum.midpoint.prism.xnode.PrimitiveXNode)15 ListXNode (com.evolveum.midpoint.prism.xnode.ListXNode)13 Test (org.testng.annotations.Test)13 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)9 SchemaXNode (com.evolveum.midpoint.prism.xnode.SchemaXNode)8 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)8 SearchFilterType (com.evolveum.prism.xml.ns._public.query_3.SearchFilterType)7 ProtectedStringType (com.evolveum.prism.xml.ns._public.types_3.ProtectedStringType)7 Element (org.w3c.dom.Element)6 PrismContext (com.evolveum.midpoint.prism.PrismContext)4 ItemPath (com.evolveum.midpoint.prism.path.ItemPath)4 QueryType (com.evolveum.prism.xml.ns._public.query_3.QueryType)4 Protector (com.evolveum.midpoint.prism.crypto.Protector)3 EncryptedDataType (com.evolveum.prism.xml.ns._public.types_3.EncryptedDataType)3 NotNull (org.jetbrains.annotations.NotNull)3 TestProtector (com.evolveum.midpoint.prism.crypto.TestProtector)2