Search in sources :

Example 1 with XPathHolder

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

the class ItemPathDeserializer method deserialize.

@Override
public ItemPath deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
    // TODO : implement..this is only for test
    if (jp.getCurrentToken() != JsonToken.VALUE_STRING) {
        throw new JsonParseException("Cannot parse path value. Expected that the value will be string but it is: " + jp.getCurrentTokenId(), jp.getCurrentLocation());
    }
    String path = jp.getText();
    if (StringUtils.isBlank(path)) {
        throw new IllegalStateException("Error while deserializing path. No path specified.");
    }
    //System.out.println("path: " + path);
    //		if (path.startsWith("declare.*")){
    XPathHolder holder = new XPathHolder(path);
    return holder.toItemPath();
//		}
//		else {
//			String[] segments = path.split("/");
//			if (segments.length == 1){
//				String[] pathItems = segments[0].split(":");
//				if (pathItems.length == 1){
//					return new ItemPath(QNameUtil.nullNamespace(pathItems[0]));
//				}
//				
//			} else
//		}
//		return new ItemPath(new QName("http://midpoint.evolveum.com/xml/ns/test/foo-1.xsd", "name"));
}
Also used : XPathHolder(com.evolveum.midpoint.prism.marshaller.XPathHolder) JsonParseException(com.fasterxml.jackson.core.JsonParseException)

Example 2 with XPathHolder

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

the class ItemPathTypeDeserializer method deserialize.

@Override
public ItemPathType deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
    if (jp.getCurrentToken() != JsonToken.VALUE_STRING) {
        throw new JsonParseException("Cannot parse path value. Expected that the value will be string but it is: " + jp.getCurrentTokenId(), jp.getCurrentLocation());
    }
    String path = jp.getText();
    if (StringUtils.isBlank(path)) {
        throw new IllegalStateException("Error while deserializing path. No path specified.");
    }
    //System.out.println("path: " + path);
    //		if (path.startsWith("declare.*")){
    XPathHolder holder = new XPathHolder(path);
    ItemPath itemPath = holder.toItemPath();
    ItemPathType itemPathType = new ItemPathType(itemPath);
    return itemPathType;
//		ItemPathType itemPathType = new ItemPathType();
//		itemPathType.getContent().add(jp.getText());
//		
//		return itemPathType;
}
Also used : XPathHolder(com.evolveum.midpoint.prism.marshaller.XPathHolder) ItemPathType(com.evolveum.prism.xml.ns._public.types_3.ItemPathType) JsonParseException(com.fasterxml.jackson.core.JsonParseException) ItemPath(com.evolveum.midpoint.prism.path.ItemPath)

Example 3 with XPathHolder

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

the class ItemPathTypeSerializer method serialize.

@Override
public void serialize(@NotNull ItemPathType value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
    XPathHolder xpath = new XPathHolder(value.getItemPath());
    String path = xpath.getXPathWithDeclarations(true);
    jgen.writeObject(path);
}
Also used : XPathHolder(com.evolveum.midpoint.prism.marshaller.XPathHolder)

Example 4 with XPathHolder

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

the class XPathTest method explicitNsRoundTripTest.

@Test
public void explicitNsRoundTripTest() {
    String xpathStr = "declare namespace foo='http://ff.com/';\ndeclare default namespace 'http://default.com/';\n declare  namespace bar = 'http://www.b.com' ;declare namespace x= \"http://xxx.com/\";\nfoo:foofoo/x:bar";
    XPathHolder xpath = new XPathHolder(xpathStr);
    System.out.println("Pure XPath: " + xpath.getXPathWithoutDeclarations());
    AssertJUnit.assertEquals("foo:foofoo/x:bar", xpath.getXPathWithoutDeclarations());
    System.out.println("ROUND TRIP: " + xpath.getXPathWithDeclarations());
    AssertJUnit.assertTrue("Unexpected path with declarations: " + xpath.getXPathWithDeclarations(), // java7
    "declare default namespace 'http://default.com/'; declare namespace foo='http://ff.com/'; declare namespace bar='http://www.b.com'; declare namespace x='http://xxx.com/'; foo:foofoo/x:bar".equals(xpath.getXPathWithDeclarations()) || // java8
    "declare default namespace 'http://default.com/'; declare namespace bar='http://www.b.com'; declare namespace foo='http://ff.com/'; declare namespace x='http://xxx.com/'; foo:foofoo/x:bar".equals(xpath.getXPathWithDeclarations()));
}
Also used : XPathHolder(com.evolveum.midpoint.prism.marshaller.XPathHolder) Test(org.testng.annotations.Test)

Example 5 with XPathHolder

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

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