use of com.evolveum.midpoint.prism.marshaller.XPathHolder in project midpoint by Evolveum.
the class DomLexicalWriter method serializePrimitiveElementOrAttribute.
private void serializePrimitiveElementOrAttribute(PrimitiveXNode<?> xprim, Element parentElement, QName elementOrAttributeName, boolean asAttribute) throws SchemaException {
QName typeQName = xprim.getTypeQName();
// TODO we should probably set typeQName when parsing the value...
if (typeQName == null && xprim.isParsed()) {
Object v = xprim.getValue();
if (v != null) {
typeQName = XsdTypeMapper.toXsdType(v.getClass());
}
}
if (typeQName == null) {
// this means that either xprim is unparsed or it is empty
if (com.evolveum.midpoint.prism.PrismContextImpl.isAllowSchemalessSerialization()) {
// We cannot correctly serialize without a type. But this is needed
// sometimes. So just default to string
String stringValue = xprim.getStringValue();
if (stringValue != null) {
if (asAttribute) {
DOMUtil.setAttributeValue(parentElement, elementOrAttributeName.getLocalPart(), stringValue);
DOMUtil.setNamespaceDeclarations(parentElement, xprim.getRelevantNamespaceDeclarations());
} else {
Element element;
try {
element = createElement(elementOrAttributeName, parentElement);
appendCommentIfPresent(element, xprim);
} catch (DOMException e) {
throw new DOMException(e.code, e.getMessage() + "; creating element " + elementOrAttributeName + " in element " + DOMUtil.getQName(parentElement));
}
parentElement.appendChild(element);
DOMUtil.setElementTextContent(element, stringValue);
DOMUtil.setNamespaceDeclarations(element, xprim.getRelevantNamespaceDeclarations());
}
}
return;
} else {
throw new IllegalStateException("No type for primitive element " + elementOrAttributeName + ", cannot serialize (schemaless serialization is disabled)");
}
}
if (StringUtils.isBlank(typeQName.getNamespaceURI())) {
typeQName = XsdTypeMapper.determineQNameWithNs(typeQName);
}
Element element = null;
if (ItemPathType.COMPLEX_TYPE.equals(typeQName)) {
//ItemPathType itemPathType = //ItemPathType.asItemPathType(xprim.getValue()); // TODO fix this hack
ItemPathType itemPathType = (ItemPathType) xprim.getValue();
if (itemPathType != null) {
if (asAttribute) {
throw new UnsupportedOperationException("Serializing ItemPath as an attribute is not supported yet");
}
XPathHolder holder = new XPathHolder(itemPathType.getItemPath());
element = holder.toElement(elementOrAttributeName, parentElement.getOwnerDocument());
parentElement.appendChild(element);
}
} else {
if (!asAttribute) {
try {
element = createElement(elementOrAttributeName, parentElement);
} catch (DOMException e) {
throw new DOMException(e.code, e.getMessage() + "; creating element " + elementOrAttributeName + " in element " + DOMUtil.getQName(parentElement));
}
appendCommentIfPresent(element, xprim);
parentElement.appendChild(element);
}
if (DOMUtil.XSD_QNAME.equals(typeQName)) {
QName value = (QName) xprim.getParsedValueWithoutRecording(DOMUtil.XSD_QNAME);
value = setQNamePrefixExplicitIfNeeded(value);
if (asAttribute) {
try {
DOMUtil.setQNameAttribute(parentElement, elementOrAttributeName.getLocalPart(), value);
} catch (DOMException e) {
throw new DOMException(e.code, e.getMessage() + "; setting attribute " + elementOrAttributeName.getLocalPart() + " in element " + DOMUtil.getQName(parentElement) + " to QName value " + value);
}
} else {
DOMUtil.setQNameValue(element, value);
}
} else {
// not ItemType nor QName
String value = xprim.getGuessedFormattedValue();
if (asAttribute) {
DOMUtil.setAttributeValue(parentElement, elementOrAttributeName.getLocalPart(), value);
} else {
DOMUtil.setElementTextContent(element, value);
}
}
}
if (!asAttribute && xprim.isExplicitTypeDeclaration()) {
DOMUtil.setXsiType(element, setQNamePrefixExplicitIfNeeded(typeQName));
}
}
use of com.evolveum.midpoint.prism.marshaller.XPathHolder in project midpoint by Evolveum.
the class ItemPathSerializer method serialize.
@Override
public void serialize(ItemPath value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
XPathHolder xpath = new XPathHolder(value);
String path = xpath.getXPathWithDeclarations(true);
jgen.writeObject(path);
}
use of com.evolveum.midpoint.prism.marshaller.XPathHolder in project midpoint by Evolveum.
the class ObjectTypeUtil method createXPathHolder.
public static XPathHolder createXPathHolder(QName property) {
XPathSegment xpathSegment = new XPathSegment(property);
List<XPathSegment> segmentlist = new ArrayList<XPathSegment>(1);
segmentlist.add(xpathSegment);
XPathHolder xpath = new XPathHolder(segmentlist);
return xpath;
}
use of com.evolveum.midpoint.prism.marshaller.XPathHolder in project midpoint by Evolveum.
the class XPathTest method xPathFromDomNode2.
@Test
public void xPathFromDomNode2() throws ParserConfigurationException, SAXException, IOException {
// Given
Element el1 = parseDataGetEl1();
String xpathString = "declare namespace x='http://xx.com/'; /root/x:el1";
// When
XPathHolder xpath = new XPathHolder(xpathString, el1);
// Then
Map<String, String> namespaceMap = xpath.getNamespaceMap();
//AssertJUnit.assertEquals("http://default.com/", namespaceMap.get(XPathHolder.DEFAULT_PREFIX));
}
use of com.evolveum.midpoint.prism.marshaller.XPathHolder in project midpoint by Evolveum.
the class XPathTest method testXPathSerializationToDom.
@Test
public void testXPathSerializationToDom() {
// GIVEN
QName qname1 = new QName(NS_C, "extension");
QName qname2 = new QName(NS_FOO, "foo");
XPathHolder xPathHolder1 = new XPathHolder(qname1, qname2);
QName elementQName = new QName(NS_BAR, "bar");
// WHEN
Element element = xPathHolder1.toElement(elementQName, DOMUtil.getDocument());
XPathHolder xPathHolder2 = new XPathHolder(element);
// THEN
System.out.println("XPath from QNames:");
System.out.println(DOMUtil.serializeDOMToString(element));
ItemPath xpath1 = xPathHolder1.toItemPath();
ItemPath xpath2 = xPathHolder2.toItemPath();
assertTrue("Paths are not equivalent", xpath1.equivalent(xpath2));
}
Aggregations