Package com.volantis.mcs.xml.xpath

Examples of com.volantis.mcs.xml.xpath.XPath


                                 Attributes attributes) throws SAXException {
            try {
                // feed the startElement to the tracker
                tracker.startElement(namespaceURI, localName, qName);
                // check that the trackers xpath is correct
                XPath path = tracker.currentXPath();
                // the expected path is provided via an "expectPath" attribute.
                String expectedPath = attributes.getValue("expectedPath");
                // get the actual path from the tracker.
                String actualPath = (path != null) ?
                        path.getExternalForm() : "";

                // check the actual path against the expected
                assertEquals("Tracker has incorrect path " + expectedPath + " " + actualPath,
                             expectedPath,
                             actualPath);
View Full Code Here


     * Tests the {@link ODOMXPath#getParent} method
     *
     * @throws Exception if an error occurs
     */
    public void testGetParent() throws Exception {
        XPath path = new XPath("/lpdm:a/lpdm:b",
                               new Namespace[]{MCSNamespace.LPDM});
        XPath parent =  path.getParent();
        assertEquals("parent not as ",
                     "/lpdm:a",
                     parent.getExternalForm());

        Map namespaces = getNamespacePrivateField(parent);
        assertEquals("namespace not as ",
                     MCSNamespace.LPDM.getURI(),
                     (String) namespaces.get(
View Full Code Here

     * Test findProblemMarkers.
     */
    public void testFindProblemMarkers() throws Exception {
        IResource resource = new MockFile(POLICY_FILE);

        XPath root = new XPath("/");
        XPath aPath1 = new XPath("/aone");
        XPath aPath2 = new XPath("/aone/atwo");

        XPath bPath1 = new XPath("/bone");
        XPath bPath2 = new XPath("/bone/btwo");

        IMarker marker1 = resource.createMarker(IMarker.PROBLEM);
        marker1.setAttribute(XPath.class.getName(), aPath2.getExternalForm());
        IMarker marker2 = resource.createMarker(IMarker.PROBLEM);
        marker2.setAttribute(XPath.class.getName(), aPath1.getExternalForm());
        IMarker marker3 = resource.createMarker(IMarker.PROBLEM);
        marker3.setAttribute(XPath.class.getName(), bPath2.getExternalForm());
        IMarker marker4 = resource.createMarker(IMarker.MESSAGE);
        marker4.setAttribute(XPath.class.getName(), bPath1.getExternalForm());

        IMarker markers [];

View Full Code Here

                // Create the root element and add content
                Element rootNode = factory.element("root");
                rootNode.addContent(parent1);
                rootNode.addContent(parent2);

                XPath rootPath = new XPath(rootNode);
                XPath parent1Path = new XPath(parent1);
                XPath child1aPath = new XPath(child1a);
                XPath child1bPath = new XPath(child1b);

                XPath parent2Path = new XPath(parent2);
                XPath child2aPath = new XPath(child2a);
                XPath child2bPath = new XPath(child2b);


                IMarker marker0 = resource.createMarker(IMarker.PROBLEM);
                marker0.setAttribute(XPath.class.getName(),
                        rootPath.getExternalForm());
                marker0.setAttribute(Element.class.getName(), rootNode.getName());

                IMarker marker1 = resource.createMarker(IMarker.PROBLEM);
                marker1.setAttribute(XPath.class.getName(),
                        parent1Path.getExternalForm());
                marker1.setAttribute(Element.class.getName(), rootNode.getName());

                IMarker marker2 = resource.createMarker(IMarker.PROBLEM);
                marker2.setAttribute(XPath.class.getName(),
                        child1aPath.getExternalForm());
                marker2.setAttribute(Element.class.getName(), child1a.getName());

                IMarker marker3 = resource.createMarker(IMarker.PROBLEM);
                marker3.setAttribute(XPath.class.getName(),
                        child1bPath.getExternalForm());
                marker3.setAttribute(Element.class.getName(), child1b.getName());

                IMarker marker4 = resource.createMarker(IMarker.PROBLEM);
                marker4.setAttribute(XPath.class.getName(),
                        parent2Path.getExternalForm());
                marker4.setAttribute(Element.class.getName(), parent2.getName());

                IMarker marker5 = resource.createMarker(IMarker.PROBLEM);
                marker5.setAttribute(XPath.class.getName(),
                        child2aPath.getExternalForm());
                marker5.setAttribute(Element.class.getName(), parent2.getName());

                IMarker marker6 = resource.createMarker(IMarker.PROBLEM);
                marker6.setAttribute(XPath.class.getName(),
                        child2bPath.getExternalForm());
                marker6.setAttribute(Element.class.getName(), parent2.getName());

                // Find markers associated with parent2
                IMarker[] markers = PolicyUtils.findProblemMarkers(resource,
                        parent2.getName(), parent2Path);
View Full Code Here

        // ------ -----  -----
        // a      b      b
        // a/b    c      c
        // a/b/c  d      d
        Element parent = context;
        XPath xpath;
        String xPathToken;

        while (tokenizer.hasMoreTokens()) {
            xPathToken = tokenizer.nextToken();
            // @todo pass the namespaces on in a nicer way (e.g. have a protected constructor that takes the map and copies it)
            xpath = new XPath(xPathToken, this.getNamespacesString());

            // Check to see if the node or nodes already exist in the document.
            List nodes = xpath.selectNodes(parent);
            ODOMObservable node = null;
            if ((nodes == null) || (nodes.size() == 0)) {
                // Node wasn't found, so create one.
                ODOMObservable result = null;
                int predicateStart = xPathToken.indexOf(PREDICATE_START);

                // Handle a predicate on the current path step if needed
                if (predicateStart != -1) {
                    String predicate =
                            xPathToken.substring(predicateStart + 1,
                                    xPathToken.
                            indexOf(PREDICATE_END));
                    if (!isPredicateValid(predicate)) {
                        throw new IllegalStateException(
                                "Unsupported predicate format: " + predicate);
                    }

                    String elementName = xPathToken.substring(0,
                            predicateStart);
                    while (node == null) {
                        // Create the correct number of nodes according to the
                        // number in the predicate field. This involves
                        // creating the node and checking to see if the
                        // original value can be found, if not create another
                        // and so on...
                        result = createNode((ODOMElement) parent,
                                factory,
                                elementName);
                        node = (ODOMObservable) xpath.selectSingleNode(parent);
                    }
                } else {
                    // No predicate required on this step
                    result = createNode((ODOMElement) parent,
                            factory,
                            xPathToken);
                }

                // Since a new node has been created, we need to track this
                // latest node creation (for the return value) and track
                // the parent for the next path step (if the new node is an
                // element and there are further steps)
                if (result != null) {
                    odomObservable = result;

                    if (result instanceof Element) {
                        parent = (Element) result;
                    } else if (tokenizer.hasMoreElements()) {
                        throw new XPathException(
                                "XPath creation for \"" + xpath +
                                "\" terminated early because the new node \"" +
                                new ODOMXPath(result).getExternalForm() + "\" is " +
                                "not an element but should have sub-nodes " +
                                "created");
                    }
                } else {
                    throw new XPathException("Unexpected null result while " +
                            "creating path step");
                }
            } else if (nodes.size() == 1) {
                // Node was found so store it as the new parent but only if it
                // is an Element).
                node = (ODOMObservable) nodes.get(0);

                if (node instanceof Element) {
                    parent = (Element) node;
                } else if (tokenizer.hasMoreTokens()) {
                    throw new XPathException(
                            "XPath creation for \"" + xpath +
                            "\" terminated early because the existing node \"" +
                            new ODOMXPath(node).getExternalForm() + "\" is not " +
                            "an element");
                }
            } else {
                throw new IllegalStateException(
                        "Creation of more than one node for this xpath is not " +
                        "supported: " + xpath.getExternalForm());
            }
        }

        return odomObservable;
    }
View Full Code Here

     * Test the constructor.
     */
    public void testODOMSelectionFilter() throws Exception {
        String filterNames[] = {"name1", "name2"};
        ODOMSelectionFilter filter;
        filter = new ODOMSelectionFilter(new XPath("."), null);
        assertNull(filter.getFilteredNames());

        filter = new ODOMSelectionFilter(new XPath("."), filterNames);
        assertTrue(filterNames != filter.getFilteredNames());

        filterNames[0] = null;
        try {
            filter = new ODOMSelectionFilter(new XPath("."), filterNames);
            fail("Filter array may not contain a null value");
        } catch (IllegalArgumentException e) {
        }
    }
View Full Code Here

     */
    public void testInclude() throws Exception {
        String filterNames[] = {"name1", "name2"};

        ODOMSelectionFilter filter;
        final XPath resolver = new XPath(".");
        filter = new ODOMSelectionFilter(resolver, filterNames);
        boolean result = filter.include((ODOMElement)factory.element("name1"));
        assertFalse(result);

        result = filter.include((ODOMElement)factory.element("name2"));
View Full Code Here

    /**
     * Test that this method returns a clone of the filter names.
     */
    public void testGetFilteredNames() throws Exception {
        final XPath resolver = new XPath(".");
        String filterNames[] = null;
        ODOMSelectionFilter filter = new ODOMSelectionFilter(resolver, filterNames);
        assertNull(filter.getFilteredNames());

        filterNames = new String[] { "test" };
View Full Code Here

    /**
     * Test the resolve method.
     */
    public void testResolve() throws Exception {
        XPath resolver = new XPath(".");
        String filterNames[] = null;
        ODOMSelectionFilter filter = new ODOMSelectionFilter(resolver, filterNames);
        ODOMElement result = filter.resolve((ODOMElement)factory.element("myName"));
        assertNotNull(result);
        assertEquals("Element name should match", "myName", result.getName());

        resolver = new XPath("cd");
        filter = new ODOMSelectionFilter(resolver, filterNames);
        result = filter.resolve((ODOMElement)factory.element("catalog"));
        assertNull("Result should be null", result);

        resolver = new XPath("cd");
        filter = new ODOMSelectionFilter(resolver, filterNames);
        ODOMElement catalog = (ODOMElement)factory.element("catalog");
        ODOMElement cd = (ODOMElement)factory.element("cd");
        cd.setAttribute("title", "titleName");
        catalog.addContent(cd);
        result = filter.resolve(catalog);

        assertNotNull("Result shouldn't be null.", result);
        assertEquals("Result should match", "cd", result.getName());

        catalog.addContent((ODOMElement)factory.element("cd"));
        try {
            result = filter.resolve(catalog);
            fail("Expected an XPathException.");
        } catch (XPathException e) {
        }

        resolver = new XPath("cd/@title");
        filter = new ODOMSelectionFilter(resolver, filterNames);
        try {
            result = filter.resolve(catalog);
            fail("Expected an XPathException.");
        } catch (XPathException e) {
View Full Code Here

        String filterNames[] = {"name1", "name2"};
        ODOMSelectionFilter filter1;
        ODOMSelectionFilter filter3;
        ODOMSelectionFilter filter4;
        ODOMSelectionFilter filter2;
        filter1 = new ODOMSelectionFilter(new XPath("."), filterNames);
        filter2 = new ODOMSelectionFilter(new XPath("."), filterNames);
        filter3 = new ODOMSelectionFilter(new XPath("."), filterNames);
        filter4 = new ODOMSelectionFilter(new XPath("catalog"), filterNames);

        // Reflexive.
        assertTrue(filter1.equals(filter1));

        // Symmetric
View Full Code Here

TOP

Related Classes of com.volantis.mcs.xml.xpath.XPath

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.