Search in sources :

Example 1 with XPathSegment

use of com.evolveum.midpoint.prism.marshaller.XPathSegment in project midpoint by Evolveum.

the class XPathTest method xpathTest.

/**
     * This is now a proper test yet.
     * It does some operations with XPath. If it does not die, then the
     * code some somehow consistent.
     *
     * It should be improved later.
     */
@Test
public void xpathTest() throws JAXBException, FileNotFoundException, IOException, ParserConfigurationException, SchemaException {
    ObjectModificationType objectModification = PrismTestUtil.parseAtomicValue(new File(FILENAME_CHANGETYPE), ObjectModificationType.COMPLEX_TYPE);
    for (ItemDeltaType change : objectModification.getItemDelta()) {
        ItemPathType pathType = change.getPath();
        System.out.println("  path=" + pathType + " (" + pathType.getClass().getName() + ") " + pathType.toString());
        //            NamedNodeMap attributes = path.getAttributes();
        //            for (int i = 0; i < attributes.getLength(); i++) {
        //                Node n = attributes.item(i);
        //                System.out.println("   A: " + n.getClass().getName() + " " + n.getNodeName() + "(" + n.getPrefix() + " : " + n.getLocalName() + ") = " + n.getNodeValue());
        //            }
        //            List<Object> any = change.getValue().getAny();
        //            for (Object e : any) {
        //                if (e instanceof Element) {
        //                    System.out.println("  E: " + ((Element) e).getLocalName());
        //                }
        //            }
        ItemPath path = pathType.getItemPath();
        XPathHolder xpath = new XPathHolder(path);
        AssertJUnit.assertEquals("c:extension/piracy:ship[2]/c:name", xpath.getXPathWithoutDeclarations());
        System.out.println("XPATH: " + xpath);
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        DocumentBuilder loader = factory.newDocumentBuilder();
        Document doc = loader.newDocument();
        Element xpathElement = xpath.toElement("http://elelel/", "path", doc);
        Attr nsC = xpathElement.getAttributeNodeNS("http://www.w3.org/2000/xmlns/", "c");
        Attr nsPiracy = xpathElement.getAttributeNodeNS("http://www.w3.org/2000/xmlns/", "piracy");
        System.out.println("c: " + nsC);
        System.out.println("piracy: " + nsPiracy);
        //            AssertJUnit.assertEquals("http://midpoint.evolveum.com/xml/ns/public/common/common-3", nsC.getValue());
        //            AssertJUnit.assertEquals("http://midpoint.evolveum.com/xml/ns/samples/piracy", nsPiracy.getValue());
        System.out.println("XPATH Element: " + xpathElement);
        XPathHolder xpathFromElement = new XPathHolder(xpathElement);
        AssertJUnit.assertEquals(xpath, xpathFromElement);
        //            attributes = xpathElement.getAttributes();
        //            for (int i = 0; i < attributes.getLength(); i++) {
        //                Node n = attributes.item(i);
        //                System.out.println(" A: " + n.getNodeName() + "(" + n.getPrefix() + " : " + n.getLocalName() + ") = " + n.getNodeValue());
        //            }
        List<XPathSegment> segments = xpath.toSegments();
        System.out.println("XPATH segments: " + segments);
        XPathHolder xpathFromSegments = new XPathHolder(segments);
        System.out.println("XPath from segments: " + xpathFromSegments);
        AssertJUnit.assertEquals("c:extension/piracy:ship[2]/c:name", xpathFromSegments.getXPathWithoutDeclarations());
    }
}
Also used : XPathSegment(com.evolveum.midpoint.prism.marshaller.XPathSegment) ObjectModificationType(com.evolveum.midpoint.xml.ns._public.common.api_types_3.ObjectModificationType) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) ItemPathType(com.evolveum.prism.xml.ns._public.types_3.ItemPathType) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) ItemDeltaType(com.evolveum.prism.xml.ns._public.types_3.ItemDeltaType) Attr(org.w3c.dom.Attr) XPathHolder(com.evolveum.midpoint.prism.marshaller.XPathHolder) DocumentBuilder(javax.xml.parsers.DocumentBuilder) File(java.io.File) CanonicalItemPath(com.evolveum.midpoint.prism.path.CanonicalItemPath) ItemPath(com.evolveum.midpoint.prism.path.ItemPath) Test(org.testng.annotations.Test)

Example 2 with XPathSegment

use of com.evolveum.midpoint.prism.marshaller.XPathSegment in project midpoint by Evolveum.

the class XPathTest method xPathFromDomNode1.

@Test
public void xPathFromDomNode1() throws ParserConfigurationException, SAXException, IOException {
    // Given
    Element el1 = parseDataGetEl1();
    String xpathString = "/root/x:el1[100]";
    el1.setTextContent(xpathString);
    // When
    XPathHolder xpath = new XPathHolder(el1);
    // Then
    Map<String, String> namespaceMap = xpath.getNamespaceMap();
    //AssertJUnit.assertEquals("http://default.com/", namespaceMap.get("c"));
    List<XPathSegment> segments = xpath.toSegments();
    AssertJUnit.assertNotNull(segments);
    AssertJUnit.assertEquals(3, segments.size());
    AssertJUnit.assertEquals(new QName("", "root"), segments.get(0).getQName());
    AssertJUnit.assertFalse(segments.get(0).isVariable());
    AssertJUnit.assertFalse(segments.get(0).isIdValueFilter());
    AssertJUnit.assertEquals(new QName("http://xx.com/", "el1"), segments.get(1).getQName());
    AssertJUnit.assertFalse(segments.get(1).isVariable());
    AssertJUnit.assertFalse(segments.get(1).isIdValueFilter());
    AssertJUnit.assertNull(segments.get(2).getQName());
    AssertJUnit.assertFalse(segments.get(2).isVariable());
    AssertJUnit.assertTrue(segments.get(2).isIdValueFilter());
    AssertJUnit.assertEquals("100", segments.get(2).getValue());
}
Also used : XPathSegment(com.evolveum.midpoint.prism.marshaller.XPathSegment) XPathHolder(com.evolveum.midpoint.prism.marshaller.XPathHolder) QName(javax.xml.namespace.QName) Element(org.w3c.dom.Element) Test(org.testng.annotations.Test)

Example 3 with XPathSegment

use of com.evolveum.midpoint.prism.marshaller.XPathSegment 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)

Aggregations

XPathHolder (com.evolveum.midpoint.prism.marshaller.XPathHolder)3 XPathSegment (com.evolveum.midpoint.prism.marshaller.XPathSegment)3 Test (org.testng.annotations.Test)2 Element (org.w3c.dom.Element)2 CanonicalItemPath (com.evolveum.midpoint.prism.path.CanonicalItemPath)1 ItemPath (com.evolveum.midpoint.prism.path.ItemPath)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 ItemPathType (com.evolveum.prism.xml.ns._public.types_3.ItemPathType)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 QName (javax.xml.namespace.QName)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 Attr (org.w3c.dom.Attr)1 Document (org.w3c.dom.Document)1