Search in sources :

Example 16 with PrimitiveXNode

use of com.evolveum.midpoint.prism.xnode.PrimitiveXNode in project midpoint by Evolveum.

the class TestXmlSerialization method testHandlingInvalidChars.

@Test
public void testHandlingInvalidChars() throws Exception {
    final String TEST_NAME = "testHandlingInvalidChars";
    displayTestTitle(TEST_NAME);
    // GIVEN
    PrismContext prismContext = PrismTestUtil.getPrismContext();
    // WHEN
    PrimitiveXNode<String> valOkNode = new PrimitiveXNode<>("abcdef");
    PrimitiveXNode<String> valWrongNode = new PrimitiveXNode<>("abc\1def");
    // THEN
    final DomLexicalProcessor domLexicalProcessor = ((PrismContextImpl) prismContext).getParserDom();
    String ok = domLexicalProcessor.write(valOkNode, new QName("ok"), null);
    System.out.println("correct value serialized to: " + ok);
    // todo make this less brittle with regards to serialization style
    assertEquals("Wrong serialization", "<ok>abcdef</ok>", ok.trim());
    try {
        String wrong = domLexicalProcessor.write(valWrongNode, new QName("wrong"), null);
        System.out.println("wrong value serialized to: " + wrong);
        assert false : "Wrong value serialization had to fail but it didn't!";
    } catch (RuntimeException e) {
        System.out.println("wrong value was not serialized (as expected): " + e);
        assertTrue(e.getMessage().contains("Invalid character"), "Didn't get expected error message");
    }
}
Also used : DomLexicalProcessor(com.evolveum.midpoint.prism.lex.dom.DomLexicalProcessor) PrimitiveXNode(com.evolveum.midpoint.prism.xnode.PrimitiveXNode) QName(javax.xml.namespace.QName) Test(org.testng.annotations.Test)

Example 17 with PrimitiveXNode

use of com.evolveum.midpoint.prism.xnode.PrimitiveXNode in project midpoint by Evolveum.

the class JaxbDomHack method toAny.

/**
	 * Serializes prism value to JAXB "any" format as returned by JAXB getAny() methods.
	 */
public Object toAny(PrismValue value) throws SchemaException {
    if (value == null) {
        return null;
    }
    Itemable parent = value.getParent();
    if (parent == null) {
        throw new IllegalStateException("Couldn't convert parent-less prism value to xsd:any: " + value);
    }
    QName elementName = parent.getElementName();
    if (value instanceof PrismPropertyValue) {
        PrismPropertyValue<Object> pval = (PrismPropertyValue) value;
        if (pval.isRaw() && parent.getDefinition() == null) {
            XNode rawElement = pval.getRawElement();
            if (rawElement instanceof MapXNode) {
                return domParser.serializeXMapToElement((MapXNode) rawElement, elementName);
            } else if (rawElement instanceof PrimitiveXNode<?>) {
                PrimitiveXNode<?> xprim = (PrimitiveXNode<?>) rawElement;
                String stringValue = xprim.getStringValue();
                Element element = DOMUtil.createElement(DOMUtil.getDocument(), elementName);
                element.setTextContent(stringValue);
                DOMUtil.setNamespaceDeclarations(element, xprim.getRelevantNamespaceDeclarations());
                return element;
            } else {
                throw new IllegalArgumentException("Cannot convert raw element " + rawElement + " to xsd:any");
            }
        } else {
            Object realValue = pval.getValue();
            if (XmlTypeConverter.canConvert(realValue.getClass())) {
                // Always record xsi:type. This is FIXME, but should work OK for now (until we put definition into deltas)
                return XmlTypeConverter.toXsdElement(realValue, elementName, DOMUtil.getDocument(), true);
            } else {
                return wrapIfNeeded(realValue, elementName);
            }
        }
    } else if (value instanceof PrismReferenceValue) {
        return prismContext.domSerializer().serialize(value, elementName);
    } else if (value instanceof PrismContainerValue<?>) {
        PrismContainerValue<?> pval = (PrismContainerValue<?>) value;
        if (pval.getParent().getCompileTimeClass() == null) {
            // We need to convert it to DOM
            return prismContext.domSerializer().serialize(pval, elementName);
        } else {
            return wrapIfNeeded(pval.asContainerable(), elementName);
        }
    } else {
        throw new IllegalArgumentException("Unknown type " + value);
    }
}
Also used : PrimitiveXNode(com.evolveum.midpoint.prism.xnode.PrimitiveXNode) QName(javax.xml.namespace.QName) XNode(com.evolveum.midpoint.prism.xnode.XNode) PrimitiveXNode(com.evolveum.midpoint.prism.xnode.PrimitiveXNode) MapXNode(com.evolveum.midpoint.prism.xnode.MapXNode) JAXBElement(javax.xml.bind.JAXBElement) Element(org.w3c.dom.Element) MapXNode(com.evolveum.midpoint.prism.xnode.MapXNode)

Example 18 with PrimitiveXNode

use of com.evolveum.midpoint.prism.xnode.PrimitiveXNode in project midpoint by Evolveum.

the class AbstractJsonLexicalProcessor method getStringValue.

private String getStringValue(XNode valueXNode, QNameUtil.QNameInfo currentFieldNameInfo, JsonParsingContext ctx) throws SchemaException {
    String stringValue;
    if (!(valueXNode instanceof PrimitiveXNode)) {
        ctx.prismParsingContext.warnOrThrow(LOGGER, "Value of '" + currentFieldNameInfo + "' attribute must be a primitive one. It is " + valueXNode + " instead. At " + getPositionSuffix(ctx));
        stringValue = "";
    } else {
        stringValue = ((PrimitiveXNode<?>) valueXNode).getStringValue();
    }
    return stringValue;
}
Also used : PrimitiveXNode(com.evolveum.midpoint.prism.xnode.PrimitiveXNode) PolyString(com.evolveum.midpoint.prism.polystring.PolyString)

Example 19 with PrimitiveXNode

use of com.evolveum.midpoint.prism.xnode.PrimitiveXNode in project midpoint by Evolveum.

the class ParamsTypeUtil method fromParamsType.

public static Map<String, Serializable> fromParamsType(ParamsType paramsType, PrismContext prismContext) throws SchemaException {
    if (paramsType != null) {
        Map<String, Serializable> params = new HashMap<String, Serializable>();
        Serializable realValue = null;
        for (EntryType entry : paramsType.getEntry()) {
            if (entry.getEntryValue() != null) {
                Serializable value = (Serializable) entry.getEntryValue().getValue();
                if (value instanceof RawType) {
                    XNode xnode = ((RawType) value).getXnode();
                    if (xnode instanceof PrimitiveXNode) {
                        realValue = ((PrimitiveXNode) xnode).getGuessedFormattedValue();
                    }
                } else {
                    realValue = value;
                }
            }
            params.put(entry.getKey(), (Serializable) (realValue));
        }
        return params;
    }
    return null;
}
Also used : Serializable(java.io.Serializable) EntryType(com.evolveum.midpoint.xml.ns._public.common.common_3.EntryType) PrimitiveXNode(com.evolveum.midpoint.prism.xnode.PrimitiveXNode) HashMap(java.util.HashMap) RootXNode(com.evolveum.midpoint.prism.xnode.RootXNode) XNode(com.evolveum.midpoint.prism.xnode.XNode) PrimitiveXNode(com.evolveum.midpoint.prism.xnode.PrimitiveXNode) RawType(com.evolveum.prism.xml.ns._public.types_3.RawType)

Aggregations

PrimitiveXNode (com.evolveum.midpoint.prism.xnode.PrimitiveXNode)19 QName (javax.xml.namespace.QName)13 MapXNode (com.evolveum.midpoint.prism.xnode.MapXNode)10 XNode (com.evolveum.midpoint.prism.xnode.XNode)9 RootXNode (com.evolveum.midpoint.prism.xnode.RootXNode)8 ListXNode (com.evolveum.midpoint.prism.xnode.ListXNode)5 RawType (com.evolveum.prism.xml.ns._public.types_3.RawType)5 Test (org.testng.annotations.Test)5 ItemPath (com.evolveum.midpoint.prism.path.ItemPath)4 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)4 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)4 ItemPathType (com.evolveum.prism.xml.ns._public.types_3.ItemPathType)4 SchemaXNode (com.evolveum.midpoint.prism.xnode.SchemaXNode)3 JAXBElement (javax.xml.bind.JAXBElement)3 Element (org.w3c.dom.Element)3 PrismContext (com.evolveum.midpoint.prism.PrismContext)2 DomLexicalProcessor (com.evolveum.midpoint.prism.lex.dom.DomLexicalProcessor)2 MappingType (com.evolveum.midpoint.xml.ns._public.common.common_3.MappingType)2 AbstractModelIntegrationTest (com.evolveum.midpoint.model.test.AbstractModelIntegrationTest)1 ParsingContext (com.evolveum.midpoint.prism.ParsingContext)1