Search in sources :

Example 11 with XPathHolder

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));
    }
}
Also used : DOMException(org.w3c.dom.DOMException) XPathHolder(com.evolveum.midpoint.prism.marshaller.XPathHolder) QName(javax.xml.namespace.QName) ItemPathType(com.evolveum.prism.xml.ns._public.types_3.ItemPathType) Element(org.w3c.dom.Element)

Example 12 with XPathHolder

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

Example 13 with XPathHolder

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;
}
Also used : XPathSegment(com.evolveum.midpoint.prism.marshaller.XPathSegment) XPathHolder(com.evolveum.midpoint.prism.marshaller.XPathHolder) ArrayList(java.util.ArrayList)

Example 14 with XPathHolder

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));
}
Also used : XPathHolder(com.evolveum.midpoint.prism.marshaller.XPathHolder) Element(org.w3c.dom.Element) Test(org.testng.annotations.Test)

Example 15 with XPathHolder

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));
}
Also used : XPathHolder(com.evolveum.midpoint.prism.marshaller.XPathHolder) QName(javax.xml.namespace.QName) Element(org.w3c.dom.Element) CanonicalItemPath(com.evolveum.midpoint.prism.path.CanonicalItemPath) ItemPath(com.evolveum.midpoint.prism.path.ItemPath) Test(org.testng.annotations.Test)

Aggregations

XPathHolder (com.evolveum.midpoint.prism.marshaller.XPathHolder)18 Test (org.testng.annotations.Test)11 Element (org.w3c.dom.Element)7 ItemPath (com.evolveum.midpoint.prism.path.ItemPath)4 QName (javax.xml.namespace.QName)4 XPathSegment (com.evolveum.midpoint.prism.marshaller.XPathSegment)3 ItemPathType (com.evolveum.prism.xml.ns._public.types_3.ItemPathType)3 CanonicalItemPath (com.evolveum.midpoint.prism.path.CanonicalItemPath)2 JsonParseException (com.fasterxml.jackson.core.JsonParseException)2 File (java.io.File)2 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)1 ObjectModificationType (com.evolveum.midpoint.xml.ns._public.common.api_types_3.ObjectModificationType)1 ItemDeltaType (com.evolveum.prism.xml.ns._public.types_3.ItemDeltaType)1 FileInputStream (java.io.FileInputStream)1 MappedByteBuffer (java.nio.MappedByteBuffer)1 FileChannel (java.nio.channels.FileChannel)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1