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");
}
}
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);
}
}
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;
}
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;
}
Aggregations