use of com.evolveum.midpoint.prism.xnode.PrimitiveXNode in project midpoint by Evolveum.
the class XNodeProcessorUtil method parseProtectedType.
public static <T> void parseProtectedType(ProtectedDataType<T> protectedType, MapXNode xmap, PrismContext prismContext, ParsingContext pc) throws SchemaException {
RootXNode xEncryptedData = xmap.getEntryAsRoot(ProtectedDataType.F_ENCRYPTED_DATA);
if (xEncryptedData != null) {
if (!(xEncryptedData.getSubnode() instanceof MapXNode)) {
throw new SchemaException("Cannot parse encryptedData from " + xEncryptedData);
}
EncryptedDataType encryptedDataType = prismContext.parserFor(xEncryptedData).context(pc).parseRealValue(EncryptedDataType.class);
protectedType.setEncryptedData(encryptedDataType);
} else {
// Check for legacy EncryptedData
RootXNode xLegacyEncryptedData = xmap.getEntryAsRoot(ProtectedDataType.F_XML_ENC_ENCRYPTED_DATA);
if (xLegacyEncryptedData != null) {
if (!(xLegacyEncryptedData.getSubnode() instanceof MapXNode)) {
throw new SchemaException("Cannot parse EncryptedData from " + xEncryptedData);
}
RootXNode xConvertedEncryptedData = (RootXNode) xLegacyEncryptedData.cloneTransformKeys(in -> {
String elementName = StringUtils.uncapitalize(in.getLocalPart());
if (elementName.equals("type")) {
return null;
}
return new QName(null, elementName);
});
EncryptedDataType encryptedDataType = prismContext.parserFor(xConvertedEncryptedData).context(pc).parseRealValue(EncryptedDataType.class);
protectedType.setEncryptedData(encryptedDataType);
if (protectedType instanceof ProtectedStringType) {
transformEncryptedValue(protectedType, prismContext);
}
}
}
RootXNode xHashedData = xmap.getEntryAsRoot(ProtectedDataType.F_HASHED_DATA);
if (xHashedData != null) {
if (!(xHashedData.getSubnode() instanceof MapXNode)) {
throw new SchemaException("Cannot parse hashedData from " + xHashedData);
}
HashedDataType hashedDataType = prismContext.parserFor(xHashedData).context(pc).parseRealValue(HashedDataType.class);
protectedType.setHashedData(hashedDataType);
}
// protected data empty..check for clear value
if (protectedType.isEmpty()) {
XNode xClearValue = xmap.get(ProtectedDataType.F_CLEAR_VALUE);
if (xClearValue == null) {
//TODO: try to use common namespace (only to be compatible with previous versions)
//FIXME maybe add some warning, info...
xClearValue = xmap.get(new QName(ProtectedDataType.F_CLEAR_VALUE.getLocalPart()));
}
if (xClearValue == null) {
return;
}
if (!(xClearValue instanceof PrimitiveXNode)) {
//this is maybe not good..
throw new SchemaException("Cannot parse clear value from " + xClearValue);
}
// TODO: clearValue
T clearValue = (T) ((PrimitiveXNode) xClearValue).getParsedValue(DOMUtil.XSD_STRING, String.class);
protectedType.setClearValue(clearValue);
}
}
use of com.evolveum.midpoint.prism.xnode.PrimitiveXNode 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.PrimitiveXNode in project midpoint by Evolveum.
the class TestDomParser method testParseUserToXNode.
@Test
public void testParseUserToXNode() throws Exception {
final String TEST_NAME = "testParseUserToXNode";
displayTestTitle(TEST_NAME);
// GIVEN
DomLexicalProcessor parser = createParser();
// WHEN
XNode xnode = parser.read(getFile(USER_JACK_FILE_BASENAME), ParsingContext.createDefault());
// THEN
System.out.println("Parsed XNode:");
System.out.println(xnode.debugDump());
RootXNode root = getAssertXNode("root node", xnode, RootXNode.class);
MapXNode rootMap = getAssertXNode("root subnode", root.getSubnode(), MapXNode.class);
PrimitiveXNode<String> xname = getAssertXMapSubnode("root map", rootMap, UserType.F_NAME, PrimitiveXNode.class);
// TODO: assert value
ListXNode xass = getAssertXMapSubnode("root map", rootMap, UserType.F_ASSIGNMENT, ListXNode.class);
assertEquals("assignment size", 2, xass.size());
// TODO: asserts
MapXNode xextension = getAssertXMapSubnode("root map", rootMap, UserType.F_EXTENSION, MapXNode.class);
}
use of com.evolveum.midpoint.prism.xnode.PrimitiveXNode in project midpoint by Evolveum.
the class DomLexicalWriter method serializeSubnode.
private void serializeSubnode(XNode xsubnode, Element parentElement, QName elementName) throws SchemaException {
if (xsubnode == null) {
return;
}
if (xsubnode instanceof RootXNode) {
Element element = createElement(elementName, parentElement);
appendCommentIfPresent(element, xsubnode);
parentElement.appendChild(element);
serializeSubnode(((RootXNode) xsubnode).getSubnode(), element, ((RootXNode) xsubnode).getRootElementName());
} else if (xsubnode instanceof MapXNode) {
Element element = createElement(elementName, parentElement);
appendCommentIfPresent(element, xsubnode);
if (xsubnode.isExplicitTypeDeclaration() && xsubnode.getTypeQName() != null) {
DOMUtil.setXsiType(element, setQNamePrefixExplicitIfNeeded(xsubnode.getTypeQName()));
}
parentElement.appendChild(element);
serializeMap((MapXNode) xsubnode, element);
} else if (xsubnode instanceof PrimitiveXNode<?>) {
PrimitiveXNode<?> xprim = (PrimitiveXNode<?>) xsubnode;
if (xprim.isAttribute()) {
serializePrimitiveElementOrAttribute(xprim, parentElement, elementName, true);
} else {
serializePrimitiveElementOrAttribute(xprim, parentElement, elementName, false);
}
} else if (xsubnode instanceof ListXNode) {
ListXNode xlist = (ListXNode) xsubnode;
if (xlist.isHeterogeneousList()) {
Element element = createElement(elementName, parentElement);
serializeExplicitList(xlist, element);
parentElement.appendChild(element);
} else {
for (XNode xsubsubnode : xlist) {
serializeSubnode(xsubsubnode, parentElement, elementName);
}
}
} else if (xsubnode instanceof SchemaXNode) {
serializeSchema((SchemaXNode) xsubnode, parentElement);
} else {
throw new IllegalArgumentException("Unknown subnode " + xsubnode);
}
}
use of com.evolveum.midpoint.prism.xnode.PrimitiveXNode 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;
}
Aggregations